Skip to content

Commit

Permalink
#36 trying to do something with stochastic regular expressions
Browse files Browse the repository at this point in the history
doesn't seem right though. this expression library ain't made for this
either (gotta parse probabilities out of edge labels -> can't even parse
to number and calculate within the expression library; could just
extract the probability as a string or calculate with numbers -> no way
to extract a string and parse it into a number without changing the
expression library).

on the other hand the paper doesn't describe quite right, how the
probabilities are calculated, and it's somehow open to interpretation.
as far as i understand you can always look at everything except star as
an unary or binary concatenation and put a constant rate as probability
to it. and with star you can just take the old value. so it then would
be possible (but) pointless, to do it with just string operations. i was
trying to do this. but the expression library keeps throwing some
errors. i don't know if it's worth the hastle
  • Loading branch information
hoechp committed May 28, 2017
1 parent fbddb70 commit 6a83076
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
public class TTCStateCaseGraphLoader {
public static Graph load(String path) {
return load(path, false);
}
public static Graph load(String path, boolean loadProbabilities) {
Graph graph = new Graph();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
Expand Down Expand Up @@ -75,7 +78,7 @@ public static Graph load(String path) {
String label = "";
long sourceId = -1;
long targetId = -1;
// double probability = -1;
double probability = -1;
while (attributes.hasNext()) {
Attribute currentAttribute = attributes.next();
String attributeName = currentAttribute.getName().toString();
Expand All @@ -91,10 +94,16 @@ public static Graph load(String path) {
targetId = Long.parseLong(attributeValue.substring(attributeValue.lastIndexOf('.') + 1));
} else if (attributeName.equalsIgnoreCase("probability")) {
// the probability (not used for now)
// probability = Double.parseDouble(attributeValue); // TODO: also use the probability
if (loadProbabilities) {
probability = Double.parseDouble(attributeValue);
}
}
}
((Node)idManager.getObject(sourceId)).addEdge(label, ((Node)idManager.getObject(targetId)));
if (loadProbabilities) {
((Node)idManager.getObject(sourceId)).addEdge(label + "[" + probability + "]", ((Node)idManager.getObject(targetId)));
} else {
((Node)idManager.getObject(sourceId)).addEdge(label, ((Node)idManager.getObject(targetId)));
}
}
break;
case XMLStreamConstants.CHARACTERS:
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/org/fujaba/graphengine/unitTests/Evalution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import java.io.FileReader;
import java.util.regex.Pattern;

import org.fujaba.graphengine.algorithm.Algorithm;
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.pattern.PatternAttribute;
import org.fujaba.graphengine.pattern.PatternGraph;
import org.fujaba.graphengine.pattern.PatternNode;
import org.fujaba.graphengine.stateelimination.TTCStateCaseGraphLoader;
import org.junit.Test;

public class Evalution {

Expand Down Expand Up @@ -78,4 +84,47 @@ private String testWords(String regex, String acceptedWordsFile, boolean accept)
return passed + "/" + totalWords;
}

@Test
public void testStuff() {
// TODO: remove debug
PatternGraph pattern = getStochasticPrepareStateWithPqPkKkKqPattern();
Graph graph = new Graph();
Node p = new Node().setAttribute("current", true);
Node k = new Node().setAttribute("eliminate", true);
Node q = new Node();
graph.addNode(p, k, q);
p.addEdge("a[0.2]", q);
p.addEdge("b[0.3]", k);
k.addEdge("c[0.5]", k);
k.addEdge("d[0.7]", q);

Algorithm alg = new Algorithm("test");
alg.addAlgorithmStep(pattern, false);

graph = alg.process(graph).getOutput();

System.out.println(graph);

}

public static PatternGraph getStochasticPrepareStateWithPqPkKkKqPattern() { // #2.3.1 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with pq, pk, kk, kq)");
PatternNode p = new PatternNode("#{current}");
PatternNode k = new PatternNode("#{eliminate}");
PatternNode q = new PatternNode("!(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
gtr.addPatternNode(p, k, q);
/* CASE 1: there's pq, pk, kk and kq */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kq}", q);
k.addPatternEdge("==", "#{kk}", k);
p.addPatternEdge("-", "#{pq}", q);

String extractedEdge = "substring(#{pq}, 0, indexOf(#{pq}, '[', length(#{pq}) - 5))";
String extractedProb = "substring(#{pq}, indexOf(#{pq}, '[', length(#{pq}) - 5) + 1, length(#{pq}) - 1)";

p.addPatternEdge("+", "'(' + substring(#{pq}, 0, indexOf(#{pq}, '[', length(#{pq}) - 5)) + '[500])+((' + substring(#{pk}, 0, indexOf(#{pk}, '[', length(#{pk}) - 5)) + ')(' + substring(#{kk}, 0, indexOf(#{kk}, '[', length(#{kk}) - 5)) + ')*[' + substring(#{kk}, indexOf(#{kk}, '[', length(#{kk}) - 5) + 1, length(#{kk}) - 1) + '](' + substring(#{kq}, 0, indexOf(#{kq}, '[', length(#{kq}) - 5)) + ')[500])'", q);
return gtr;
}

}
168 changes: 164 additions & 4 deletions src/test/java/org/fujaba/graphengine/unitTests/TestTTCStateCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testSolvingTTC2017StateEliminationCaseWithAlgorithm() {
for (String fileName: fileNamesTaskMain) {
System.out.println("TTC2017 State Elimination: " + fileName + "...");
long beginTime = System.nanoTime();
Graph g = TTCStateCaseGraphLoader.load(taskMainPath + fileName); // get data
Graph g = TTCStateCaseGraphLoader.load(taskMainPath + fileName, false); // get data
// System.err.println(g);
Graph result = algorithmStateCaseTTC2017.process(g).getOutput();
String resultStringRaw = "";
Expand Down Expand Up @@ -149,6 +149,43 @@ public static Algorithm getStateCaseAlgorithmTTC2017() {
return stateCaseTTC2017;
}

public static Algorithm getStochasticStateCaseAlgorithmTTC2017() {

Algorithm stateCaseTTC2017 = new Algorithm("TTC 2017 State Case");

Algorithm prepareData = new Algorithm("prepare data");
Algorithm eliminateState = new Algorithm("eliminate state");
Algorithm handleSourceNode = new Algorithm("handle source node");
Algorithm redirectRoute = new Algorithm("redirect route");

stateCaseTTC2017.addAlgorithmStep(prepareData);
prepareData.addAlgorithmStep(getNewInitialPattern());
prepareData.addAlgorithmStep(getAddToInitialPattern(), true);
prepareData.addAlgorithmStep(getNewFinalPattern());
prepareData.addAlgorithmStep(getAddToFinalPattern(), true);
prepareData.addAlgorithmStep(getMergeEdgesPattern(), true);
stateCaseTTC2017.addAlgorithmStep(eliminateState, true);
eliminateState.addAlgorithmStep(getMarkStateForEliminationPattern());
eliminateState.addAlgorithmStep(handleSourceNode, true);
handleSourceNode.addAlgorithmStep(getMarkWithCurrentPattern());
handleSourceNode.addAlgorithmStep(getMarkFallbackWithCurrentPattern());
handleSourceNode.addAlgorithmStep(redirectRoute, true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPqPkKkKqPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPkKkKqPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPqPkKqPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPkKqPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPpPkKkKpPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPpPkKpPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPkKkKpPattern(), true);
redirectRoute.addAlgorithmStep(getStochasticPrepareStateWithPkKpPattern(), true);
handleSourceNode.addAlgorithmStep(getUnmarkCurrentPattern());
handleSourceNode.addAlgorithmStep(getRemoveMarksPattern(), true);
eliminateState.addAlgorithmStep(getEliminateMarkedStatePattern());
eliminateState.addAlgorithmStep(getUnmarkPastPattern(), true);

return stateCaseTTC2017;
}

public static Graph solveGraph(Graph g) {
PatternGraph gtr_1_1 = getNewInitialPattern();
PatternGraph gtr_1_1_b = getAddToInitialPattern();
Expand Down Expand Up @@ -592,7 +629,7 @@ public static PatternGraph getMarkFallbackWithCurrentPattern() { // #2.2.1
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 5: there's pp, pk, kk and kp */
/* CASE 6: there's just pp, pk and kp */
p.addPatternEdge("-", "#{pp}", p);
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kp}", p);
Expand All @@ -606,7 +643,7 @@ public static PatternGraph getMarkFallbackWithCurrentPattern() { // #2.2.1
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 5: there's pp, pk, kk and kp */
/* CASE 7: there's just pk, kk and kp */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kk}", k);
k.addPatternEdge("==", "#{kp}", p);
Expand All @@ -620,7 +657,7 @@ public static PatternGraph getMarkFallbackWithCurrentPattern() { // #2.2.1
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 5: there's pp, pk, kk and kp */
/* CASE 8: there's just pk and kp */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kp}", p);
p.addPatternEdge("+", "'((' + #{pk} + ')(' + #{kp} + '))*'", p);
Expand Down Expand Up @@ -659,5 +696,128 @@ public static PatternGraph getUnmarkPastPattern() { // #2.7
gtr.addPatternNode(n);
return gtr;
}

private static String extrEdge(String name) {
return "substring(#{" + name + "}, 0, indexOf(#{" + name + "}, '[', length(#{" + name + "}) - 5))";
}
private static String extrProb(String name) {
return "substring(#{" + name + "}, indexOf(#{" + name + "}, '[', length(#{" + name + "}) - 5) + 1, length(#{" + name + "}) - 1)";
}

public static PatternGraph getStochasticPrepareStateWithPqPkKkKqPattern() { // #2.3.1 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with pq, pk, kk, kq)");
PatternNode p = new PatternNode("#{current}");
PatternNode k = new PatternNode("#{eliminate}");
PatternNode q = new PatternNode("!(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
gtr.addPatternNode(p, k, q);
/* CASE 1: there's pq, pk, kk and kq */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kq}", q);
k.addPatternEdge("==", "#{kk}", k);
p.addPatternEdge("-", "#{pq}", q);
p.addPatternEdge("+", "'(' + " + extrEdge("pq") + " + ')[500]+((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kk") + " + ')*[" + extrProb("kk") + "](' + " + extrEdge("kq") + " + '))[500]'", q);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPkKkKqPattern() { // #2.3.2 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pk, kk, kq)");
PatternNode p = new PatternNode("#{current}");
PatternNode k = new PatternNode("#{eliminate}");
PatternNode q = new PatternNode("!(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
gtr.addPatternNode(p, k, q);
/* CASE 2: there's just pk, kk and kq */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kk}", k);
k.addPatternEdge("==", "#{kq}", q);
p.addPatternEdge("+", "'(' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kk") + " + ')*[" + extrEdge("kk") + "](' + " + extrEdge("kq") + " + ')[500]'", q);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPqPkKqPattern() { // #2.3.3 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pq, pk, kq)");
PatternNode p = new PatternNode("#{current}");
PatternNode k = new PatternNode("#{eliminate}");
PatternNode q = new PatternNode("!(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
gtr.addPatternNode(p, k, q);
/* CASE 3: there's just pq, pk and kq */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kq}", q);
p.addPatternEdge("-", "#{pq}", q);
p.addPatternEdge("+", "'(' + " + extrEdge("pq") + " + ')[500]+((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kq") + " + '))[500]'", q);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPkKqPattern() { // #2.3.4 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pk, kq)");
PatternNode p = new PatternNode("#{current}");
PatternNode k = new PatternNode("#{eliminate}");
PatternNode q = new PatternNode("!(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
gtr.addPatternNode(p, k, q);
/* CASE 4: there's just pk and kq */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kq}", q);
p.addPatternEdge("+", "'(' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kq") + " + ')[500]'", q);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPpPkKkKpPattern() { // #2.3.5 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with pp, pk, kk, kp)");
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 5: there's pp, pk, kk and kp */
p.addPatternEdge("-", "#{pp}", p);
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kk}", k);
k.addPatternEdge("==", "#{kp}", p);
p.addPatternEdge("+", "'((' + " + extrEdge("pp") + " + ')*[" + extrProb("pp") + "]((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kk") + " + ')*[" + extrProb("kk") + "](' + " + extrEdge("kp") + " + '))*[1.0])*[1.0]'", p);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPpPkKpPattern() { // #2.3.6 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pp, pk, kp)");
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 6: there's just pp, pk and kp */
p.addPatternEdge("-", "#{pp}", p);
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kp}", p);
p.addPatternEdge("+", "'((' + " + extrEdge("pp") + " + ')*[" + extrProb("pp") + "]((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kp") + " + '))*[1.0])*[1.0]'", p);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPkKkKpPattern() { // #2.3.7 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pk, kk, kp)");
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 7: there's just pk, kk and kp */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kk}", k);
k.addPatternEdge("==", "#{kp}", p);
p.addPatternEdge("+", "'((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kk") + " + ')*[" + extrProb("kk") + "](' + " + extrEdge("kp") + " + '))*[1.0]'", p);
return gtr;
}

public static PatternGraph getStochasticPrepareStateWithPkKpPattern() { // #2.3.8 (all matches; don't repeat) - could also be repeated
// gtr for adding new calculated labels
PatternGraph gtr = new PatternGraph("prepare elimination of state (with just pk, kp)");
PatternNode p = new PatternNode("#{current} && !(#{used})").addPatternAttribute(new PatternAttribute().setAction("+").setName("used").setValue(true));
PatternNode k = new PatternNode("#{eliminate}");
gtr.addPatternNode(p, k);
/* CASE 8: there's just pk and kp */
p.addPatternEdge("==", "#{pk}", k);
k.addPatternEdge("==", "#{kp}", p);
p.addPatternEdge("+", "'((' + " + extrEdge("pk") + " + ')(' + " + extrEdge("kp") + " + '))*[1.0]'", p);
return gtr;
}

}

0 comments on commit 6a83076

Please sign in to comment.