Skip to content

Commit

Permalink
#36 added new feature to handle multiple 'wildcard' edges
Browse files Browse the repository at this point in the history
  • Loading branch information
hoechp committed May 20, 2017
1 parent fccac4e commit 729dc0e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 26 deletions.
105 changes: 79 additions & 26 deletions src/main/java/org/fujaba/graphengine/PatternEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ private static ArrayList<ArrayList<ArrayList<Node>>> findPossibleMatchesForPosit
boolean exists = !(node.getEdges(patternEdge.getName()) == null || node.getEdges(patternEdge.getName()).size() == 0);

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
exists = node.getEdges().keySet().size() > 0;
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = node.getEdges().keySet().size() >= extractedVariableNames.size();
}
//#####

Expand Down Expand Up @@ -572,12 +573,17 @@ public static boolean doesntMatchNegativeNodes(HashMap<PatternNode, Node> map, G
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(currentSubNode).getEdges().keySet()) {
if (mapping.get(currentSubNode).getEdges(ttcTestString).contains(mapping.get(otherSubNode))) {
exists = true;
break;
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand All @@ -603,12 +609,17 @@ public static boolean doesntMatchNegativeNodes(HashMap<PatternNode, Node> map, G
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(otherSubNode).getEdges().keySet()) {
if (mapping.get(otherSubNode).getEdges(ttcTestString).contains(mapping.get(currentSubNode))) {
exists = true;
break;
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand Down Expand Up @@ -643,12 +654,17 @@ public static boolean doesntMatchNegativeNodes(HashMap<PatternNode, Node> map, G
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(currentSubNode).getEdges().keySet()) {
if (mapping.get(currentSubNode).getEdges(ttcTestString).contains(mapping.get(nodeMatchLists.get(0).get(k)))) {
exists = true;
break;
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand Down Expand Up @@ -676,12 +692,17 @@ public static boolean doesntMatchNegativeNodes(HashMap<PatternNode, Node> map, G
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(nodeMatchLists.get(0).get(k)).getEdges().keySet()) {
if (mapping.get(nodeMatchLists.get(0).get(k)).getEdges(ttcTestString).contains(mapping.get(currentSubNode))) {
exists = true;
break;
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand Down Expand Up @@ -772,13 +793,18 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(currentSubNode).getEdges().keySet()) {
if (mapping.get(currentSubNode).getEdges(ttcTestString).contains(mapping.get(otherSubNode))) {
exists = true;
labelMatches.put(patternEdge.getName().substring(2, patternEdge.getName().length() - 1), "'" + ttcTestString + "'");
break;
labelMatches.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand All @@ -803,13 +829,18 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
}

//##### NEW TTC2017 FEATURE:
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
if (extractedVariableNames.size() > 0) {
exists = false;
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(otherSubNode).getEdges().keySet()) {
if (mapping.get(otherSubNode).getEdges(ttcTestString).contains(mapping.get(currentSubNode))) {
exists = true;
labelMatches.put(patternEdge.getName().substring(2, patternEdge.getName().length() - 1), "'" + ttcTestString + "'");
break;
labelMatches.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
Expand Down Expand Up @@ -873,6 +904,28 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
// } // TODO: remove debug
return matches;
}

public static ArrayList<String> extractVariableNames(String s) {
ArrayList<String> result = new ArrayList<String>();
if (s == null) {
return result;
}
String openingSequence = "#{";
String closingSequence = "}";
while (true) {
int beginIndex = s.indexOf(openingSequence);
if (beginIndex < 0) {
break;
}
s = s.substring(beginIndex + openingSequence.length());
int endIndex = s.indexOf(closingSequence);
if (endIndex < 0) {
break;
}
result.add(s.substring(0, endIndex));
}
return result;
}

/**
* finds matches for a pattern in a graph.
Expand Down Expand Up @@ -1237,14 +1290,14 @@ public static Graph applyMatch(Match match) {
}
}
for (PatternEdge patternEdge: patternNode.getPatternEdges()) {
ArrayList<String> extractedVariableNames = extractVariableNames(patternEdge.getName());
switch (patternEdge.getAction()) {
case "-": // remove
if (patternEdge.getName().startsWith("#{") && patternEdge.getName().endsWith("}")) {
if (extractedVariableNames.size() > 0) {

//##### NEW TTC2017 FEATURE:

Set<String> oldKeys = new HashSet<String>(matchedNode.getEdges().keySet());
for (String s: oldKeys) { // TODO: this is experimental - too many edges could be removed...
for (String s: extractedVariableNames) {
matchedNode.removeEdge(s, clonedNodeMatch.get(patternEdge.getTarget()));
}

Expand All @@ -1256,7 +1309,7 @@ public static Graph applyMatch(Match match) {
break;
case "+": // create
String label = patternEdge.getName();
if (label.contains("#{") && label.contains("}")) {
if (extractedVariableNames.size() > 0) {
try {
label = match.getLabelEvaluator().evaluate(("'' + (" + label + ")")); // convert to String to be sure
label = label.substring(1, label.length() - 1); // now remove those single quote-characters ("'result'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ public void testTransformingTTCStateCaseData() {
// p.addPatternEdge("-", "#{pq}", q);
// p.addPatternEdge("+", "'(' + #{pq} + ')+(' + #{pk} + ')(' + #{kk} + ')*(' + #{kq} + ')'", q);


/*
* just trying to build some experimental GTR:
*/
// gtr for merging multiple edges between the same two nodes:
PatternGraph gtrMerge = new PatternGraph("merge multiple labels between the same two nodes");
PatternNode a = new PatternNode();
PatternNode b = new PatternNode();
gtrMerge.addPatternNode(a, b);
a.addPatternEdge("-", "#{x}, #{y}", b);
a.addPatternEdge("+", "#{x} + '+' + #{y}", b);
/*
* done with the experimental GTR
*/


do {
matches = PatternEngine.matchPattern(g, gtrEliminate, true);
if (matches != null && matches.size() > 0) {
Expand Down

0 comments on commit 729dc0e

Please sign in to comment.