Skip to content

Commit

Permalink
#36 Like almost done, I hope. Something is missing, though.
Browse files Browse the repository at this point in the history
Awesome commit message, right?
  • Loading branch information
hoechp committed May 21, 2017
1 parent 729dc0e commit d716366
Show file tree
Hide file tree
Showing 4 changed files with 757 additions and 297 deletions.
27 changes: 17 additions & 10 deletions src/main/java/org/fujaba/graphengine/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public class Match {
*/
private HashMap<PatternNode, Node> nodeMatch;

private HashMap<String, String> edgeMatch;

/**
* the evaluator that's used for wildcard labels within the GTR
*/
private Evaluator labelEvaluator;
private Evaluator edgeEvaluator;

public static Evaluator buildLabelEvaluator(HashMap<String, String> labelMatches) {
public static Evaluator buildEdgeEvaluator(HashMap<String, String> labelMatches) {
Evaluator evaluator = new Evaluator();
for (String key: labelMatches.keySet()) {
evaluator.putVariable(key, "" + labelMatches.get(key));
Expand All @@ -50,11 +52,12 @@ public static Evaluator buildLabelEvaluator(HashMap<String, String> labelMatches
* @param pattern the pattern that was matched
* @param nodeMatch the mapping from pattern nodes to graph nodes
*/
public Match(Graph graph, PatternGraph pattern, HashMap<PatternNode, Node> nodeMatch, HashMap<String, String> labelMatches) {
public Match(Graph graph, PatternGraph pattern, HashMap<PatternNode, Node> nodeMatch, HashMap<String, String> edgeMatch) {
this.graph = graph;
this.pattern = pattern;
this.nodeMatch = nodeMatch;
this.labelEvaluator = buildLabelEvaluator(labelMatches);
this.edgeEvaluator = buildEdgeEvaluator(edgeMatch);
this.edgeMatch = edgeMatch;
}

public Graph getGraph() {
Expand All @@ -75,13 +78,17 @@ public HashMap<PatternNode, Node> getNodeMatch() {
public void setNodeMatch(HashMap<PatternNode, Node> nodeMatch) {
this.nodeMatch = nodeMatch;
}

public Evaluator getLabelEvaluator() {
return labelEvaluator;
public Evaluator getEdgeEvaluator() {
return edgeEvaluator;
}

public void setLabelEvaluator(Evaluator labelEvaluator) {
this.labelEvaluator = labelEvaluator;
public void setEdgeEvaluator(Evaluator labelEvaluator) {
this.edgeEvaluator = labelEvaluator;
}
public HashMap<String, String> getEdgeMatch() {
return edgeMatch;
}
public void setEdgeMatch(HashMap<String, String> edgeMatch) {
this.edgeMatch = edgeMatch;
}

}
80 changes: 67 additions & 13 deletions src/main/java/org/fujaba/graphengine/PatternEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public static boolean doesntMatchNegativeNodes(HashMap<PatternNode, Node> map, G
}

public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, boolean single, ArrayList<ArrayList<PatternNode>> nodeMatchLists, ArrayList<ArrayList<ArrayList<Node>>> couldMatch) {
HashMap<String, String> labelMatches = new HashMap<String, String>();
HashMap<String, String> edgeMatch = new HashMap<String, String>();

ArrayList<Match> matches = new ArrayList<Match>();

Expand All @@ -757,12 +757,13 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
}
/*
* only check this index against previous ones,
* if ok, increment and check only that one, and so on
* if okay, increment and check only that one, and so on
*/
ArrayList<HashMap<PatternNode, Node>> mappings = new ArrayList<HashMap<PatternNode, Node>>();
int checkIndex = 1;
loop: while (checkIndex > -1) {
for (int i = checkIndex; i < nodeMatchLists.get(0).size(); ++i) {

/*
* check nodeMatchLists.get(0).get(i) only against all previous nodes,
* if it is duplicate, or any edge (outgoing or incoming) is missing.
Expand All @@ -774,7 +775,44 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
*/
PatternNode currentSubNode = nodeMatchLists.get(0).get(i);
boolean fail = false;

/* ##### also check for edges to self */
for (PatternEdge p: currentSubNode.getPatternEdges()) {
if (p.getTarget() != currentSubNode) {
continue;
}
boolean exists = false;
if (mapping.get(currentSubNode).getEdges(p.getName()) != null) {
exists = mapping.get(currentSubNode).getEdges(p.getName()).contains(mapping.get(currentSubNode));
}
//##### NEW TTC2017 FEATURE:
ArrayList<String> extractedVariableNames = extractVariableNames(p.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(currentSubNode))) {
edgeMatch.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
break;
}
}
}
}
//#####
if (("!=".equals(p.getAction()) && exists) || (!"!=".equals(p.getAction()) && !exists)) {
fail = true; // failure at incoming edge
break;
}
}
/* ##### */

match: for (int j = 0; j < i; ++j) {
if (fail == true) {
break match;
}
PatternNode otherSubNode = nodeMatchLists.get(0).get(j);
if (mapping.get(currentSubNode) == mapping.get(otherSubNode)) {
fail = true; // found duplicate!
Expand All @@ -799,7 +837,7 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(currentSubNode).getEdges().keySet()) {
if (mapping.get(currentSubNode).getEdges(ttcTestString).contains(mapping.get(otherSubNode))) {
labelMatches.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
edgeMatch.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
Expand Down Expand Up @@ -835,7 +873,7 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
int numberOfEdgesFound = 0;
for (String ttcTestString: mapping.get(otherSubNode).getEdges().keySet()) {
if (mapping.get(otherSubNode).getEdges(ttcTestString).contains(mapping.get(currentSubNode))) {
labelMatches.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
edgeMatch.put(extractedVariableNames.get(numberOfEdgesFound), "'" + ttcTestString + "'");
++numberOfEdgesFound;
if (numberOfEdgesFound >= extractedVariableNames.size()) {
exists = true;
Expand Down Expand Up @@ -897,7 +935,7 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
}
// nothing left to check => return results
for (HashMap<PatternNode, Node> successfulMapping: mappings) {
matches.add(new Match(graph, pattern, successfulMapping, labelMatches));
matches.add(new Match(graph, pattern, successfulMapping, edgeMatch));
}
// if (matches.size() > 0 && pattern.getName().startsWith("signalTo")) { // TODO: remove debug
// System.out.println("found " + matches.size() + " matches for '" + pattern.getName() + "'"); // TODO: remove debug
Expand Down Expand Up @@ -1248,18 +1286,32 @@ public static ArrayList<Match> matchPattern(Graph graph, PatternGraph pattern, b
// return matches;
// }


public static Graph applyMatch(Match match) {
return applyMatch(match, false);
}

/**
* applies a pattern to a match, applying all 'create'- and 'delete'-actions specified in the pattern to the graph.
*
* @param match the match that was previously found
* @return the resulting graph
*/
public static Graph applyMatch(Match match) {
public static Graph applyMatch(Match match, boolean keepGraph) {
// System.out.println("applying match for '" + match.getPattern().getName() + "'"); // TODO: remove debug
Graph clonedGraph = match.getGraph().clone();
HashMap<PatternNode, Node> clonedNodeMatch = new HashMap<PatternNode, Node>();
for (PatternNode patternNode: match.getNodeMatch().keySet()) {
clonedNodeMatch.put(patternNode, clonedGraph.getNodes().get(match.getGraph().getNodes().indexOf(match.getNodeMatch().get(patternNode))));

Graph clonedGraph;
HashMap<PatternNode, Node> clonedNodeMatch;

if (!keepGraph) {
clonedGraph = match.getGraph().clone();
clonedNodeMatch = new HashMap<PatternNode, Node>();
for (PatternNode patternNode: match.getNodeMatch().keySet()) {
clonedNodeMatch.put(patternNode, clonedGraph.getNodes().get(match.getGraph().getNodes().indexOf(match.getNodeMatch().get(patternNode))));
}
} else {
clonedGraph = match.getGraph();
clonedNodeMatch = match.getNodeMatch();
}

// first create new nodes, so it can be used for targets of new edges from other nodes and so on:
Expand Down Expand Up @@ -1298,7 +1350,9 @@ public static Graph applyMatch(Match match) {
//##### NEW TTC2017 FEATURE:

for (String s: extractedVariableNames) {
matchedNode.removeEdge(s, clonedNodeMatch.get(patternEdge.getTarget()));
String edgeName = match.getEdgeMatch().get(s);
edgeName = edgeName.substring(1, edgeName.length() - 1);
matchedNode.removeEdge(edgeName, clonedNodeMatch.get(patternEdge.getTarget()));
}

//#####
Expand All @@ -1311,7 +1365,7 @@ public static Graph applyMatch(Match match) {
String label = patternEdge.getName();
if (extractedVariableNames.size() > 0) {
try {
label = match.getLabelEvaluator().evaluate(("'' + (" + label + ")")); // convert to String to be sure
label = match.getEdgeEvaluator().evaluate(("'' + (" + label + ")")); // convert to String to be sure
label = label.substring(1, label.length() - 1); // now remove those single quote-characters ("'result'")
} catch (Throwable t) {
t.printStackTrace();
Expand Down Expand Up @@ -1341,7 +1395,7 @@ public static Graph applyMatch(Match match) {
}
}
return clonedGraph;
}
}

public static boolean evaluate(Node node, String expression) {
//return evaluate(buildNodeEvaluator(node), expression); // uncached
Expand Down
Loading

0 comments on commit d716366

Please sign in to comment.