Skip to content

Commit

Permalink
#36 added gson-adapter for algorithm; now saving/loading algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
hoechp committed May 26, 2017
1 parent 5f115b9 commit e79a0d0
Show file tree
Hide file tree
Showing 6 changed files with 1,009 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/fujaba/graphengine/GraphEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.ArrayList;
import java.util.HashMap;

import org.fujaba.graphengine.algorithm.Algorithm;
import org.fujaba.graphengine.algorithm.adapter.AlgorithmAdapter;
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.graph.adapter.GraphAdapter;
Expand Down Expand Up @@ -102,7 +104,8 @@ public static Gson getGson() {
.registerTypeAdapter(PatternNode.class, new PatternNodeAdapter())
.registerTypeAdapter(PatternGraph.class, new PatternGraphAdapter())
.registerTypeAdapter(NodeSortTree.class, new NodeSortTreeAdapter())
// .setPrettyPrinting()
.registerTypeAdapter(Algorithm.class, new AlgorithmAdapter())
.setPrettyPrinting()
// .serializeNulls()
.create();
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/fujaba/graphengine/algorithm/Algorithm.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.fujaba.graphengine.algorithm;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.fujaba.graphengine.GraphEngine;
import org.fujaba.graphengine.Match;
import org.fujaba.graphengine.PatternEngine;
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.pattern.PatternGraph;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

public class Algorithm {

private String name;
Expand All @@ -21,9 +29,28 @@ public Algorithm(String name) {
this.repeating = false;
}

public static Algorithm loadFrom(String path) throws JsonSyntaxException, JsonIOException, FileNotFoundException {
return GraphEngine.getGson().fromJson(new FileReader(path), Algorithm.class);
}

public Algorithm saveTo(String path) throws JsonIOException, IOException {
FileWriter fw = new FileWriter(path);
GraphEngine.getGson().toJson(this, fw);
fw.flush();
fw.close();
return this;
}

public Application process(Graph input) {
Graph output = input;
if (atomicAlgorithm != null) {

// String test = GraphEngine.getGson().toJson(atomicAlgorithm);
// System.err.println(test);
// PatternGraph testPG = GraphEngine.getGson().fromJson(test, PatternGraph.class);
// test = GraphEngine.getGson().toJson(testPG);
// System.err.println(test + "\n");

while (true) {
ArrayList<Match> matches = PatternEngine.matchPattern(output, atomicAlgorithm, true);
if (matches.size() > 0) {
Expand Down Expand Up @@ -105,5 +132,9 @@ public Algorithm setRepeating(boolean repeating) {
this.repeating = repeating;
return this;
}

public String toString() {
return GraphEngine.getGson().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.fujaba.graphengine.algorithm.adapter;

import java.io.IOException;

import org.fujaba.graphengine.GraphEngine;
import org.fujaba.graphengine.algorithm.Algorithm;
import org.fujaba.graphengine.pattern.PatternGraph;
import org.fujaba.graphengine.pattern.adapter.PatternGraphAdapter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class AlgorithmAdapter extends TypeAdapter<Algorithm> {

@Override
public void write(JsonWriter out, Algorithm algorithm) throws IOException {
PatternGraphAdapter patternGraphAdapter = new PatternGraphAdapter();
out.beginObject();
out.name("name").value(algorithm.getName());
if (algorithm.isRepeating()) {
out.name("repeating").value(algorithm.isRepeating());
}
if (algorithm.getAtomicAlgorithm() != null) {
// out.name("atomic").value(GraphEngine.getGson().toJson(algorithm.getAtomicAlgorithm()));
out.name("atomic");
patternGraphAdapter.write(out, algorithm.getAtomicAlgorithm());
}
if (algorithm.getAlgorithmSteps().size() > 0) {
out.name("steps");
out.beginArray();
for (Algorithm subAlgorithm: algorithm.getAlgorithmSteps()) {
write(out, subAlgorithm);
}
out.endArray();
}
out.endObject();
}

@Override
public Algorithm read(JsonReader in) throws IOException {
PatternGraphAdapter patternGraphAdapter = new PatternGraphAdapter();
final Algorithm algorithm = new Algorithm("");
in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "name":
algorithm.setName(in.nextString());
break;
case "repeating":
algorithm.setRepeating(in.nextBoolean());
break;
case "atomic":
// algorithm.setAtomicAlgorithm(GraphEngine.getGson().fromJson(in.nextString(), PatternGraph.class));
algorithm.setAtomicAlgorithm(patternGraphAdapter.read(in));
break;
case "steps":
in.beginArray();
while (in.hasNext()) {
algorithm.addAlgorithmStep(read(in));
}
in.endArray();
break;
}
}
in.endObject();
return algorithm;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public PatternGraph readWithManager(JsonReader in, IdManager idManager) throws I
}
}
}
attribute.setValue(in.nextString());
// attribute.setValue(in.nextString());
break;
}
}
Expand All @@ -165,26 +165,28 @@ public PatternGraph readWithManager(JsonReader in, IdManager idManager) throws I
edgesToBuildAction.put(sourceId, new HashMap<String, ArrayList<String>>());
while (in.hasNext()) {
in.beginObject();
PatternEdge edge = new PatternEdge().setSource(node);
String action = "==";
String name = "";
Long target = (long)-1;
while (in.hasNext()) {
switch (in.nextName()) {
case "action":
edge.setAction(in.nextString());
action = in.nextString();
break;
case "name":
edge.setName(in.nextString());
name = in.nextString();
break;
case "target":
edge.setTarget((PatternNode)idManager.getObject(in.nextLong()));
target = in.nextLong();
break;
}
}
if (!edgesToBuild.get(sourceId).containsKey(edge.getName())) {
edgesToBuild.get(sourceId).put(edge.getName(), new ArrayList<Long>());
edgesToBuildAction.get(sourceId).put(edge.getName(), new ArrayList<String>());
if (!edgesToBuild.get(sourceId).containsKey(name)) {
edgesToBuild.get(sourceId).put(name, new ArrayList<Long>());
edgesToBuildAction.get(sourceId).put(name, new ArrayList<String>());
}
edgesToBuild.get(sourceId).get(edge.getName()).add(idManager.getId(edge.getTarget()));
edgesToBuildAction.get(sourceId).get(edge.getName()).add(edge.getAction());
edgesToBuild.get(sourceId).get(name).add(target);
edgesToBuildAction.get(sourceId).get(name).add(action);
in.endObject();
}
in.endArray();
Expand Down
Loading

0 comments on commit e79a0d0

Please sign in to comment.