-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/cz/cvut/kbss/analysis/model/util/Anonymizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cz.cvut.kbss.analysis.model.util; | ||
|
||
import cz.cvut.kbss.jopa.model.EntityManager; | ||
import org.apache.commons.io.IOUtils; | ||
import org.eclipse.rdf4j.repository.Repository; | ||
import org.eclipse.rdf4j.repository.RepositoryConnection; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Anonymizer { | ||
|
||
protected final String VALUES = "\\$\\$VALUES\\$\\$"; | ||
protected String updatePartNumbersTemplate; | ||
protected String updateStock; | ||
protected String updateEventTypeLabels; | ||
protected String updateFailureRates; | ||
|
||
|
||
|
||
public Anonymizer() { | ||
loadTemplateUpdates(); | ||
} | ||
|
||
protected String load(String path){ | ||
try { | ||
return IOUtils.toString(this.getClass().getResource(path), Charset.defaultCharset()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
protected void loadTemplateUpdates(){ | ||
updatePartNumbersTemplate = load("/queries/anonymize/replace-part-numbers.sparql"); | ||
updateStock = load("/queries/anonymize/replace-stock-codes.sparql"); | ||
updateEventTypeLabels = load("/queries/anonymize/replace-fault-event-and-failure-mode-names.sparql"); | ||
updateFailureRates = load("/queries/anonymize/replace-failure-rates.sparql"); | ||
} | ||
|
||
public void anonymize(EntityManager em, SensitiveProperties sensitiveProperties){ | ||
Repository r = em.unwrap(Repository.class); | ||
RepositoryConnection c = r.getConnection(); | ||
|
||
for(String update : prepareQueries(sensitiveProperties)){ | ||
c.prepareUpdate(update).execute(); | ||
} | ||
} | ||
|
||
|
||
|
||
protected List<String> prepareQueries(SensitiveProperties sensitiveProperties){ | ||
return Arrays.asList( | ||
preparePartNumberUpdate(sensitiveProperties.getPartNumberSet()), | ||
prepareStockUpdate(sensitiveProperties.getStockSet()), | ||
prepareNamesUpdate(sensitiveProperties.getFaultEventNamesSet()), | ||
prepareFailureRateUpdate(sensitiveProperties.getFailureRateSet()) | ||
); | ||
} | ||
|
||
protected String preparePartNumberUpdate(Set<String> partNumbers){ | ||
List<String> l = new ArrayList<>(partNumbers); | ||
return updatePartNumbersTemplate.replaceFirst(VALUES, IntStream.range(0, l.size()) | ||
.mapToObj(i -> String.format("(\"%s\" \"pn-%d\")", l.get(i), i)) | ||
.collect(Collectors.joining("\n"))); | ||
} | ||
|
||
protected String prepareStockUpdate(Set<String> stock){ | ||
List<String> l = new ArrayList<>(stock); | ||
return updatePartNumbersTemplate.replaceFirst(VALUES, IntStream.range(0, l.size()) | ||
.mapToObj(i -> String.format("(\"%s\" \"stock-%d\")", l.get(i), i)) | ||
.collect(Collectors.joining("\n"))); | ||
} | ||
|
||
protected String prepareNamesUpdate(Set<String> faultEventNames){ | ||
return updateEventTypeLabels; | ||
} | ||
|
||
protected String prepareFailureRateUpdate(Set<String> failureRates){ | ||
return updateFailureRates; | ||
} | ||
|
||
protected void executeUpdates(){ | ||
|
||
} | ||
|
||
|
||
protected void loadQueries(){ | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/cz/cvut/kbss/analysis/model/util/Exporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cz.cvut.kbss.analysis.model.util; | ||
|
||
import cz.cvut.kbss.jopa.model.EntityManager; | ||
import org.eclipse.rdf4j.repository.Repository; | ||
import org.eclipse.rdf4j.repository.RepositoryConnection; | ||
import org.eclipse.rdf4j.rio.RDFFormat; | ||
import org.eclipse.rdf4j.rio.Rio; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
public class Exporter { | ||
public static void exportAsTrig(EntityManager em, String file){ | ||
Repository r = em.unwrap(Repository.class); | ||
RepositoryConnection c = r.getConnection(); | ||
try(Writer w = new FileWriter(file)) { | ||
c.export(Rio.createWriter(RDFFormat.TRIG, w)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/cz/cvut/kbss/analysis/model/util/SensitiveProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cz.cvut.kbss.analysis.model.util; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class SensitiveProperties { | ||
protected Set<String> partNumberSet = new HashSet<String>(); | ||
protected Set<String> stockSet = new HashSet<>(); | ||
protected Set<String> failureRateSet = new HashSet<>(); | ||
protected Set<String> faultEventNamesSet = new HashSet<>(); | ||
|
||
public SensitiveProperties add(String partNumber, String stock, String failureRate, String faultEventName) { | ||
addPartNumber(partNumber); | ||
addStock(stock); | ||
addFailureRate(failureRate); | ||
addFaultEventName(faultEventName); | ||
return this; | ||
|
||
} | ||
|
||
public SensitiveProperties addPartNumber(String partNumber) { | ||
if (partNumber != null) | ||
partNumberSet.add(partNumber); | ||
return this; | ||
} | ||
|
||
|
||
public SensitiveProperties addStock(String stock) { | ||
if(stock != null) | ||
stockSet.add(stock); | ||
return this; | ||
} | ||
|
||
public SensitiveProperties addFailureRate(String failureRate) { | ||
if(failureRate != null) | ||
failureRateSet.add(failureRate); | ||
return this; | ||
} | ||
public SensitiveProperties addFaultEventName(String faultEventName) { | ||
if(faultEventName != null) | ||
faultEventNamesSet.add(faultEventName); | ||
return this; | ||
} | ||
|
||
|
||
public Set<String> getPartNumberSet() { | ||
return partNumberSet; | ||
} | ||
|
||
public Set<String> getStockSet() { | ||
return stockSet; | ||
} | ||
|
||
public Set<String> getFailureRateSet() { | ||
return failureRateSet; | ||
} | ||
|
||
public Set<String> getFaultEventNamesSet() { | ||
return faultEventNamesSet; | ||
} | ||
} |