Created
March 14, 2018 08:59
-
-
Save bshambaugh/846d5533b9e5b1193d990d596c0c8a6c to your computer and use it in GitHub Desktop.
this is the latest version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import org.apache.jena.query.Dataset; | |
import org.apache.jena.query.DatasetFactory; | |
import org.apache.jena.rdf.model.Model; | |
import org.apache.jena.rdf.model.Property; | |
import org.apache.jena.rdf.model.RDFNode; | |
import org.apache.jena.rdf.model.Resource; | |
import org.apache.jena.rdf.model.Statement; | |
import org.apache.jena.rdf.model.StmtIterator; | |
import org.apache.jena.riot.RDFDataMgr; | |
import org.apache.jena.riot.RDFFormat; | |
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; | |
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; | |
import org.apache.tinkerpop.gremlin.structure.Edge; | |
import org.apache.tinkerpop.gremlin.structure.Graph; | |
import org.apache.tinkerpop.gremlin.structure.T; | |
import org.apache.tinkerpop.gremlin.structure.Vertex; | |
import org.apache.tinkerpop.gremlin.structure.io.IoCore; | |
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; | |
public class GremlinJenaTest { | |
private static Dataset loadDataset(String path) throws IOException { | |
InputStream is = null; | |
Dataset dataset = null; | |
try { | |
is = GremlinJenaTest.class.getResourceAsStream(path); | |
dataset = DatasetFactory.create(); | |
RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang()); | |
} finally { | |
if (is != null) { | |
is.close(); | |
} | |
} | |
return dataset; | |
} | |
public static void main(String[] args) throws IOException { | |
Dataset input = loadDataset("/conversations/twoProposalsTwoAgreementsBothCancelled.trig"); | |
//RDFDataMgr.write(System.out, input, Lang.TRIG); | |
Graph graph = TinkerGraph.open(); | |
Iterator<String> modelNames = input.listNames(); | |
while (modelNames.hasNext()) { | |
String modelName = modelNames.next(); | |
// System.out.println("The model name is:"+modelName); | |
Model model = input.getNamedModel(modelName); | |
StmtIterator stmtIt = model.listStatements(); | |
while(stmtIt.hasNext()) { | |
Statement stmt = stmtIt.next(); | |
Resource subject = stmt.getSubject(); | |
Vertex subjectVertex = getVertexForResource(graph, subject); | |
RDFNode object = stmt.getObject(); | |
Property predicate = stmt.getPredicate(); | |
if (object.isResource()) { | |
Vertex objectVertex = getVertexForResource(graph, object.asResource()); | |
subjectVertex.addEdge(predicate.getURI(), objectVertex); | |
} else if (object.isLiteral()){ | |
subjectVertex.property(predicate.getURI(), object.asLiteral().getValue()); | |
} | |
} | |
} | |
System.out.println(graph); | |
// Brents custom code.... | |
GraphTraversalSource g = graph.traversal(); | |
// | |
List<Vertex> vertices = g.V().toList(); | |
// | |
// System.out.println(vertices.size()); | |
// | |
// for(Vertex hello : vertices) { | |
// System.out.println(hello); | |
// } | |
// | |
// List edges = g.E().toList(); | |
// System.out.println(edges.size()); | |
// | |
// for(Object hello : edges) { | |
// // System.out.println(hello); | |
// } | |
String vertexlabel = "https://localhost:8443/won/resource/event/r5hu2rq515sq6wzgdjov"; | |
String vertexlabel2 = "https://localhost:8443/won/resource/event/d0muf1vbp6zai6jtnp7w"; | |
// GraphTraversal<Vertex,Vertex> traversal = g.V(vertexlabel).out("http://purl.org/webofneeds/message#hasReceiver"); | |
// //traversal.fold() | |
// while(traversal.hasNext()) { | |
// System.out.println(traversal.next()); | |
// | |
// } | |
// for(Object foo : g.V(vertexlabel).outE().label().toList()) | |
// System.out.println(foo); | |
// | |
// System.out.println("string");; | |
// | |
// for(Object foo : g.V(vertexlabel2).outE().label().toList()) | |
// System.out.println(foo); | |
// | |
// for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) { | |
// System.out.println("The edges are:"+bar); | |
// } | |
// System.out.println("\n"); | |
// } | |
// http://purl.org/webofneeds/agreement#proposes | |
System.out.println("Here are the proposes"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/agreement#proposes")) { | |
System.out.println("We have a winner!"+foo); | |
System.out.println("\n"); | |
} | |
} | |
} | |
System.out.println("\n"); | |
// http://purl.org/webofneeds/agreement#accepts | |
System.out.println("Here are the accepts"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/agreement#accepts")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
// http://purl.org/webofneeds/agreement#accepts | |
System.out.println("Here are the proposesToCancel"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/agreement#proposesToCancel")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
System.out.println("Here are the retracts"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/modification#retracts")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
System.out.println("Here are the hasPreviousMessage"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/message#hasPreviousMessage")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
System.out.println("Here are the hasPreviousMessage"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/message#hasCorrespondingRemoteMessage")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
System.out.println("Here are the isResponseTo"+"\n"); | |
for(Vertex foo : vertices ) { | |
// System.out.println("The vertex is:"+foo.id()); | |
// for(Object bar : g.V(foo.id()).outE().label().toList()) | |
// System.out.println("The edges are:"+bar); | |
// I need to iterate through this list and see if the predicate is matched... | |
List<String> astring = g.V(foo.id()).outE().label().toList(); | |
Iterator<String> iterator = astring.iterator(); | |
while(iterator.hasNext()) { | |
// System.out.println(iterator.next()); | |
String next = iterator.next(); | |
if(next.equals("http://purl.org/webofneeds/message#isResponseTo")) { | |
System.out.println("We have a winner!"+foo); | |
} | |
} | |
} | |
System.out.println("\n"); | |
System.out.println(g.V().outE("http://purl.org/webofneeds/message#isResponseTo").label().toList()); | |
g.V("https://localhost:8443/won/resource/event/cbcccoqqqbec6bxkl3y3").outE("http://purl.org/webofneeds/agreement#proposes").inV().toStream().forEach(x -> System.out.println(x)); | |
g.V().outE("http://purl.org/webofneeds/message#isResponseTo").outV().toStream().forEach(x -> System.out.println(x)); | |
// | |
// System.out.println(g.V(vertexlabel).aggregate("foobar")); | |
// Iterator<Edge> joe = g.getGraph().edges(); | |
// | |
// while(joe.hasNext()) { | |
// System.out.println(joe.next()); | |
// } | |
// | |
// Iterator<Vertex> tom = g.getGraph().vertices(); | |
// | |
// while(tom.hasNext()) { | |
// System.out.println(tom.next()); | |
// } | |
// | |
// System.out.println(g.V("Kevin")); | |
//for(receiver : )) { | |
// System.out.println(receiver); | |
//}; | |
// System.out.println(g.V().values(vertexlabel)); | |
// System.out.println(g.V().values("bunny")); | |
List<Vertex> first = new ArrayList<Vertex>(); | |
List<Vertex> second = new ArrayList<Vertex>(); | |
g.V() | |
.inE("http://purl.org/webofneeds/message#isResponseTo") | |
.outV().sideEffect(consumer -> first.add(consumer.get())) | |
.outE("http://purl.org/webofneeds/message#hasCorrespondingRemoteMessage").outV().sideEffect(consumer -> second.add(consumer.get())).toStream().forEach(x -> System.out.println(x)); | |
System.out.println("first: " + first); | |
System.out.println("second: " + second); | |
// more for the next ....n | |
/// what methods can you add to foreach with the mapping??? | |
g.V() | |
.inE("http://purl.org/webofneeds/message#isResponseTo") | |
.outV().sideEffect(consumer -> first.add(consumer.get())) | |
.outE("http://purl.org/webofneeds/message#hasCorrespondingRemoteMessage").outV().sideEffect(consumer -> second.add(consumer.get())).toStream().collect(Collectors.toList()); | |
} | |
private static Vertex getVertexForResource(Graph graph, Resource subject) { | |
String vertexId = subject.getURI(); | |
System.out.println("making vertex for node " + subject.getURI()); | |
if (vertexId == null) { | |
vertexId = subject.asNode().getBlankNodeLabel(); | |
} | |
Iterator<Vertex> it = graph.vertices(vertexId); | |
Vertex subjectVertex = null; | |
if (it.hasNext()) { | |
subjectVertex = it.next(); | |
} else { | |
subjectVertex = graph.addVertex(T.id, vertexId); | |
} | |
return subjectVertex; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment