Java Code Examples for com.tinkerpop.blueprints.impls.tg.TinkerGraph#addVertex()

The following examples show how to use com.tinkerpop.blueprints.impls.tg.TinkerGraph#addVertex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
public Graph resultToGraph(Result result) {
  graph = new TinkerGraph();
  while (result.hasNext()) {
    Map<String, Object> map = result.next();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      Object value = entry.getValue();
      String key = entry.getKey();
      if (null == value) {
        continue;
      } else if (value instanceof PropertyContainer) {
        addElement((PropertyContainer) value);
      } else if (value instanceof Path) {
        for (PropertyContainer container : (Path) value) {
          addElement(container);
        }
      } else if (value instanceof ArrayList) {
        for (Object thing : (ArrayList<?>) value) {
          if (thing instanceof PropertyContainer) {
            addElement((PropertyContainer) thing);
          }
        }
      } else if (value instanceof Boolean) {
        // generates a lonely node which contains the result
        Vertex vertex = graph.addVertex(key);
        vertex.setProperty(key, value);
        vertex.setProperty(NodeProperties.LABEL, "Boolean result");
        vertex.setProperty(CommonProperties.IRI, key);
      } else {
        logger.warning("Not converting " + value.getClass() + " to tinker graph");
      }
    }
  }
  return graph;
}
 
Example 2
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void graphsAreMerged() {
  TinkerGraph graph1 = new TinkerGraph();
  Vertex g1v1 = graph1.addVertex(0);
  Vertex g1v2 = graph1.addVertex(1);
  Edge g1e1 = graph1.addEdge(0, g1v1, g1v2, "test");
  TinkerGraph graph2 = new TinkerGraph();
  Vertex g2v1 = graph2.addVertex(1);
  Vertex g2v2 = graph2.addVertex(2);
  Edge g2e1 = graph1.addEdge(1, g2v1, g2v2, "test2");
  TinkerGraphUtil tgu = new TinkerGraphUtil(graph1, curieUtil);
  Graph graph = tgu.combineGraphs(graph2);
  assertThat(graph.getVertices(), containsInAnyOrder(g1v1, g1v2, g2v2));
  assertThat(graph.getEdges(), containsInAnyOrder(g1e1, g2e1));
}
 
Example 3
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void primitivePropertiesAreReturned() {
  TinkerGraph graph = new TinkerGraph();
  Vertex v = graph.addVertex(1);
  assertThat(TinkerGraphUtil.getProperty(v, "foo", String.class), is(Optional.<String>empty()));
  v.setProperty("foo", "bar");
  assertThat(TinkerGraphUtil.getProperty(v, "foo", String.class), is(Optional.of("bar")));
}
 
Example 4
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void collectionsAreReturned() {
  TinkerGraph graph = new TinkerGraph();
  Vertex v = graph.addVertex(1);
  assertThat(TinkerGraphUtil.getProperties(v, "foo", String.class), is(empty()));
  v.setProperty("foo", "bar");
  assertThat(TinkerGraphUtil.getProperties(v, "foo", String.class), contains("bar"));
  v.setProperty("foo", newHashSet("bar", "baz"));
  assertThat(TinkerGraphUtil.getProperties(v, "foo", String.class), containsInAnyOrder("bar", "baz"));
  v.setProperty("foo", new String[] {"bar", "baz"});
  assertThat(TinkerGraphUtil.getProperties(v, "foo", String.class), containsInAnyOrder("bar", "baz"));
}
 
Example 5
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void propertiesProject() {
  TinkerGraph graph = new TinkerGraph();
  Vertex v = graph.addVertex(1);
  v.setProperty(CommonProperties.IRI, "http://x.org/a");
  v.setProperty("foo", "fizz");
  v.setProperty("bar", "baz");
  TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
  tgu.project(newHashSet("foo"));
  assertThat(v.getPropertyKeys(), containsInAnyOrder("foo", CommonProperties.IRI));
}
 
Example 6
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void allPropertiesProject() {
  TinkerGraph graph = new TinkerGraph();
  Vertex v = graph.addVertex(1);
  v.setProperty(CommonProperties.IRI, "http://x.org/a");
  v.setProperty("foo", "fizz");
  v.setProperty("bar", "baz");
  TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
  tgu.project(newHashSet("*"));
  assertThat(v.getPropertyKeys(), containsInAnyOrder("foo", "bar", CommonProperties.IRI));
}
 
Example 7
Source File: ArrayPropertyTransformerTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  TinkerGraph graph = new TinkerGraph();
  v1 = graph.addVertex(0);
  v2 = graph.addVertex(1);
  v1.setProperty(CommonProperties.IRI, "foo");
  v1.setProperty("foo", "bar");
  e = graph.addEdge(0, v1, v2, "test");
  e.setProperty("foo", 1);
  ArrayPropertyTransformer.transform(graph);
}
 
Example 8
Source File: CFGScanDroid.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public static Graph buildGraph() {
	TinkerGraph graph = new TinkerGraph("/tmp/cfggraph", TinkerGraph.FileType.GRAPHML); 
	graph.createIndex("signatureName", Vertex.class);
	graph.createIndex("sha256", Vertex.class);
	graph.createIndex("md5", Vertex.class);
	graph.createIndex("type", Vertex.class);

	Map sigLookup = new HashMap<String, Vertex>();
	Map fileLookup = new HashMap<String, Vertex>();
	
	for(Match match : matches) {
		// check map for sig
		CFGSig matchSig = match.getSignature();
		String sigString = matchSig.getStringSignature();
		Vertex sigVertex = (Vertex)sigLookup.get(sigString);

		if(sigVertex == null) {
			// create vertex
			sigVertex = graph.addVertex(null);
			sigVertex.setProperty("type", "signature");
			sigVertex.setProperty("signature", sigString);
			sigVertex.setProperty("signatureName", matchSig.getName());
			// add sig to map
			sigLookup.put(sigString, sigVertex);
		}

		// check map for file
		String fileSHA256 = match.getFileSHA256();
		Vertex fileVertex = (Vertex)fileLookup.get(fileSHA256);

		if(fileVertex == null) {
			// create vertex
			fileVertex = graph.addVertex(null);
			sigVertex.setProperty("type", "file");
			fileVertex.setProperty("sha256", fileSHA256);
			fileVertex.setProperty("md5", match.getFileMD5());
			fileVertex.setProperty("fileNameList", new ArrayList<String>());
			// add file to map
			fileLookup.put(fileSHA256, fileVertex);
		}

		// what idiot would scan the same file multiple times with different names?
		List<String> fileNames = fileVertex.getProperty("fileNameList");
		if(!fileNames.contains(match.getFileName())) {
			fileNames.add(match.getFileName());
		}

		// TODO: comment this out and see if it still works
		fileVertex.setProperty("fileNameList", fileNames);

		// create edge(sig, file)
		Edge matchEdge = graph.addEdge(null, sigVertex, fileVertex, "matches");

		ControlFlowGraph cfg = match.getControlFlowGraph();
		matchEdge.setProperty("method", cfg.getIdentifier(false));
		// matchEdge.setProperty("fileBytes", cfg.getMethodBytesAsHexString());
	}

	return graph;
}