Skip to content

Commit

Permalink
Added support for translating additional attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
delas committed Feb 8, 2022
1 parent 858738a commit 4cea2e4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 33 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>

<build>
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/dtu/qpms/MotifsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;
import org.deckfour.xes.extension.std.XConceptExtension;
import org.deckfour.xes.in.XParser;
import org.deckfour.xes.in.XesXmlParser;
import org.deckfour.xes.model.XAttributeMap;
import org.deckfour.xes.model.XEvent;
import org.deckfour.xes.model.XLog;
import org.deckfour.xes.model.XTrace;
Expand Down Expand Up @@ -47,9 +49,8 @@ public static void main(String[] args) throws Exception {
System.out.println("motifs max distance: " + maxDistance);
System.out.println("");

Set<Sequence<String>> strings = new HashSet<Sequence<String>>();
Set<Sequence<String>> motifs = new HashSet<Sequence<String>>();
MotifsFilterExecutor<String> mf = new MotifsFilterExecutor<String>(c);
MotifsFilterExecutor<String, XAttributeMap> mf = new MotifsFilterExecutor<String, XAttributeMap>(c);

XParser parser = new XesXmlParser();

Expand All @@ -59,11 +60,12 @@ public static void main(String[] args) throws Exception {

for (XTrace t : logStrings) {
Sequence<String> s = new Sequence<String>();
Sequence<XAttributeMap> attributes = new Sequence<XAttributeMap>();
for (XEvent e : t) {
s.add(XConceptExtension.instance().extractName(e));
attributes.add(e.getAttributes());
}
strings.add(s);
mf.addString(s);
mf.addString(s, attributes);
}
System.out.println("Done! - " + (System.currentTimeMillis() - time) + "ms");

Expand All @@ -84,17 +86,20 @@ public static void main(String[] args) throws Exception {

time = System.currentTimeMillis();
System.out.print("3. Filtering motifs... ");
Set<Sequence<String>> replaced = mf.filter(maxDistance, ACTIVITY_NAME_ABSTRACTED_ACTIVITY);
Set<Pair<Sequence<String>, Sequence<XAttributeMap>>> replaced = mf.filter(maxDistance, ACTIVITY_NAME_ABSTRACTED_ACTIVITY);
System.out.println("Done! - " + (System.currentTimeMillis() - time) + "ms");

time = System.currentTimeMillis();
System.out.print("4. Saving motifs... ");
int motifCounter = 1;
XLog logFiltered = XLogHelper.generateNewXLog("filtered");
for(Sequence<String> seq : replaced) {
for(Pair<Sequence<String>, Sequence<XAttributeMap>> seq : replaced) {
XTrace t = XLogHelper.createTrace("case_" + motifCounter);
for (String s : seq) {
XLogHelper.insertEvent(t, s);
for (int i = 0; i < seq.getLeft().size(); i++) {
XEvent e = XLogHelper.xesFactory.createEvent();
e.setAttributes(seq.getRight().get(i));
XConceptExtension.instance().assignName(e, seq.getLeft().get(i));
t.add(e);
}
logFiltered.add(t);
motifCounter++;
Expand Down
63 changes: 42 additions & 21 deletions src/main/java/dtu/qpms/model/MotifsFilterExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@
import java.util.Map;
import java.util.Set;

public class MotifsFilterExecutor <T> {
import org.apache.commons.lang3.tuple.Pair;

public class MotifsFilterExecutor <T, K> {

private static final char TO_REPLACE = '!';
private Set<String> strings;
private Set<String> motifs;
private Set<Pair<String, Sequence<K>>> strings;
private Set<Pair<String, Sequence<K>>> motifs;
private CostMapping<T> costs;
private Map<Character, T> charsToValues;
private Map<T, Character> valuesToChars;

public MotifsFilterExecutor(CostMapping<T> costs) {
this.strings = new HashSet<String>();
this.motifs = new HashSet<String>();
this.strings = new HashSet<Pair<String, Sequence<K>>>();
this.motifs = new HashSet<Pair<String, Sequence<K>>>();
this.charsToValues = new HashMap<Character, T>();
this.valuesToChars = new HashMap<T, Character>();
this.costs = costs;
}

public boolean addString(Sequence<T> string) {
return add(string, strings);
public boolean addString(Sequence<T> string, Sequence<K> attributes) {
return add(string, attributes, strings);
}

public boolean addMotif(Sequence<T> motif) {
return add(motif, motifs);
return add(motif, null, motifs);
}

private boolean add(Sequence<T> string, Set<String> set) {
private boolean add(Sequence<T> string, Sequence<K> attributes, Set<Pair<String, Sequence<K>>> set) {
String s = "";
for (int i = 0; i < string.size(); i++) {
T t = string.get(i);
Expand All @@ -44,36 +46,42 @@ private boolean add(Sequence<T> string, Set<String> set) {
}
s += valuesToChars.get(t);
}
return set.add(s);
return set.add(Pair.of(s, attributes));
}

public Set<Sequence<T>> filter(double maxDistance, T replace) {
Set<Sequence<T>> toReturn = new HashSet<Sequence<T>>();
for (String s : filterStrings(maxDistance)) {
public Set<Pair<Sequence<T>, Sequence<K>>> filter(double maxDistance, T replace) {
Set<Pair<Sequence<T>, Sequence<K>>> toReturn = new HashSet<Pair<Sequence<T>, Sequence<K>>>();
for (Pair<String, Sequence<K>> pair : filterStrings(maxDistance)) {
String s = pair.getLeft();
Sequence<T> seq = new Sequence<T>();
Sequence<K> attSeq = new Sequence<K>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (c.equals(TO_REPLACE)) {
seq.add(replace);
attSeq.add(pair.getRight().get(i));
} else {
seq.add(charsToValues.get(c));
attSeq.add(pair.getRight().get(i));
}
}
toReturn.add(seq);
toReturn.add(Pair.of(seq, attSeq));
}
return toReturn;
}

private Set<String> filterStrings(double maxDistance) {
private Set<Pair<String, Sequence<K>>> filterStrings(double maxDistance) {
if (motifs.isEmpty()) {
return strings;
}
HammingDistance<T> hamming = new HammingDistance<T>(costs, charsToValues);
Set<String> toReturn = new HashSet<String>();
int motifLength = motifs.iterator().next().length();
for (String s : strings) {
Set<Pair<String, Sequence<K>>> toReturn = new HashSet<Pair<String, Sequence<K>>>();
int motifLength = motifs.iterator().next().getLeft().length();
for (Pair<String, Sequence<K>> pair : strings) {
String s = pair.getLeft();
List<Integer> indexesWithMotifs = new ArrayList<Integer>();
for (String m : motifs) {
for (Pair<String, Sequence<K>> motifPair : motifs) {
String m = motifPair.getLeft();
int stringLength = s.length();
if (stringLength >= motifLength) {
for (int i = 0; i <= stringLength - motifLength; i++) {
Expand All @@ -85,7 +93,7 @@ private Set<String> filterStrings(double maxDistance) {
}
}
if (indexesWithMotifs.size() == 0) {
toReturn.add(s);
toReturn.add(Pair.of(s, pair.getRight()));
} else {
Collections.sort(indexesWithMotifs);

Expand All @@ -97,18 +105,31 @@ private Set<String> filterStrings(double maxDistance) {
}

String replacement = "";
Sequence<K> attributes = new Sequence<K>();
for (int i = 0; i < indexesWithMotifsToReplace.size(); i++) {
if (i==0) {
replacement += s.substring(0, indexesWithMotifsToReplace.get(i));
for (int j = 0; j < indexesWithMotifsToReplace.get(i); j++) {
attributes.add(pair.getRight().get(j));
}
} else {
replacement += s.substring(indexesWithMotifsToReplace.get(i - 1) + motifLength, indexesWithMotifsToReplace.get(i));
for (int j = indexesWithMotifsToReplace.get(i - 1) + motifLength; j < indexesWithMotifsToReplace.get(i); j++) {
attributes.add(pair.getRight().get(j));
}
}

replacement += TO_REPLACE;
attributes.add(pair.getRight().get(indexesWithMotifsToReplace.get(i)));

if (i + 1 == indexesWithMotifsToReplace.size()) {
replacement += s.substring(indexesWithMotifsToReplace.get(i) + motifLength);
for (int j = indexesWithMotifsToReplace.get(i) + motifLength; j < s.length(); j++) {
attributes.add(pair.getRight().get(j));
}
}
}
toReturn.add(replacement);
toReturn.add(Pair.of(replacement, attributes));
}
}
return toReturn;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dtu/qpms/utils/XLogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
*/
public class XLogHelper {

private static XFactory xesFactory = new XFactoryNaiveImpl();
public static XFactory xesFactory = new XFactoryNaiveImpl();
private static XExtensionManager xesExtensionManager = XExtensionManager.instance();

/**
Expand Down
15 changes: 12 additions & 3 deletions test-files/toy-input.xes
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
<extension name="Concept" prefix="concept" uri="http://www.xes-standard.org/concept.xesext"/>
<trace>
<string key="concept:name" value="1"/>
<event><string key="concept:name" value="A"/><string key="test" value="v1.1" /><string key="test2" value="v21.1" /></event>
<event><string key="concept:name" value="A"/></event>
<event><string key="concept:name" value="B"/></event>
<event><string key="concept:name" value="C"/><string key="test" value="v1.2" /><string key="test2" value="v21.2" /></event>
<event><string key="concept:name" value="A"/></event>
<event><string key="concept:name" value="B"/></event>
<event><string key="concept:name" value="C"/></event>
<event><string key="concept:name" value="D"/></event>
</trace>
<trace>
<string key="concept:name" value="2"/>
<event><string key="concept:name" value="A"/></event>
<event><string key="concept:name" value="B"/></event>
<event><string key="concept:name" value="A"/><string key="test" value="v2.1" /><string key="test2" value="v22.1" /></event>
<event><string key="concept:name" value="B"/><string key="test" value="v2.2" /><string key="test2" value="v22.2" /></event>
<event><string key="concept:name" value="E"/></event>
<event><string key="concept:name" value="F"/><string key="test" value="v2.3" /><string key="test2" value="v22.2" /></event>
</trace>
<trace>
<string key="concept:name" value="2"/>
<event><string key="concept:name" value="C"/></event>
<event><string key="concept:name" value="D"/></event>
<event><string key="concept:name" value="E"/><string key="test" value="v3" /><string key="test2" value="v23" /></event>
<event><string key="concept:name" value="F"/></event>
</trace>
</log>

0 comments on commit 4cea2e4

Please sign in to comment.