com.tinkerpop.blueprints.impls.tg.TinkerGraph Java Examples

The following examples show how to use com.tinkerpop.blueprints.impls.tg.TinkerGraph. 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: GraphApi.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
public Graph getEdges(RelationshipType type, boolean entail, long skip, long limit) {
  String query = "MATCH path = (start)-[r:" + type.name() + (entail ? "!" : "") + "]->(end) "
      + " RETURN path "
      // TODO: This slows down the query dramatically.
      // + " ORDER BY ID(r) "
      + " SKIP " + skip + " LIMIT " + limit;
  Graph graph = new TinkerGraph();
  TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
  Result result;
  try {
    result = cypherUtil.execute(query);
    while (result.hasNext()) {
      Map<String, Object> map = result.next();
      Path path = (Path) map.get("path");
      tgu.addPath(path);
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    // Return and empty graph if the limit is too high...
  }
  return graph;
}
 
Example #2
Source File: FunctionExportPlugin.java    From bjoern with GNU General Public License v3.0 6 votes vote down vote up
private void exportFunction(Vertex vertex) throws IOException
{

	executor.execute(() -> {
		Graph graph = orientConnector.getNoTxGraphInstance();
		try
		{
			Vertex functionRoot = graph.getVertex(vertex);
			Graph subgraph = new TinkerGraph();
			copyFunctionNodes(subgraph, functionRoot);
			copyFunctionEdges(subgraph, functionRoot);
			subgraph.shutdown();
			Path out = Paths.get(outputDirectory.toString(), "func" +
					functionRoot.getId().toString().split(":")[1] +
					"." + format);
			writeGraph(subgraph, out);
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			graph.shutdown();
		}
	});

}
 
Example #3
Source File: VersionedGraphTestSuite.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Expect failure when instantiating a graph instance without
 * initialization.
 */
@Test(expected = IllegalStateException.class)
public void createGraphInstanceWithoutInit() {
    ActiveVersionedGraph<?, V> g =
            new ActiveVersionedGraph.ActiveVersionedNonTransactionalGraphBuilder(new TinkerGraph(),
                    new LongGraphIdentifierBehavior()).build();
}
 
Example #4
Source File: NonTransactionalLongTypeVersionedGraphTest.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
protected ActiveVersionedGraph<?, Long> generateGraph(String graphDirectoryName, Configuration conf) {
    // For natural IDs tests
    // Configuration conf1 = new
    // Configuration.ConfBuilder().useNaturalIds(true).build();
    return new ActiveVersionedGraph.ActiveVersionedNonTransactionalGraphBuilder<TinkerGraph, Long>(
            new TinkerGraph(), new LongGraphIdentifierBehavior()).init(true).conf(conf).build();
}
 
Example #5
Source File: GraphInitTest.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotCreateDuplicatedRootVertices() {
    TinkerGraph graph = new TinkerGraph();
    Configuration conf = new Configuration.ConfBuilder().build();
    new ActiveVersionedGraph.ActiveVersionedNonTransactionalGraphBuilder<TinkerGraph, Long>(graph, new LongGraphIdentifierBehavior()).init(true).conf(conf).build();
    assertThat(Lists.newArrayList(graph.getVertices()).size(), is(2));
    assertThat(Lists.newArrayList(graph.getEdges()).size(), is(0));

    new ActiveVersionedGraph.ActiveVersionedNonTransactionalGraphBuilder<TinkerGraph, Long>(graph, new LongGraphIdentifierBehavior()).init(true).conf(conf).build();
    assertThat(Lists.newArrayList(graph.getVertices()).size(), is(2));
    assertThat(Lists.newArrayList(graph.getEdges()).size(), is(0));
}
 
Example #6
Source File: GraphInitTest.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void causeExceptionIfGraphNotInitialized() {
    TinkerGraph graph = new TinkerGraph();
    Configuration conf = new Configuration.ConfBuilder().build();
    ActiveVersionedGraph<TinkerGraph, Long> vg = new ActiveVersionedGraph.ActiveVersionedNonTransactionalGraphBuilder<TinkerGraph, Long>(
            graph, new LongGraphIdentifierBehavior()).init(false).conf(conf).build();

    graph.getVertices();
}
 
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: GraphServiceTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  when(api.getAllPropertyKeys()).thenReturn(newArrayList("foo", "bar"));
  when(api.getAllRelationshipTypes()).thenReturn(
      newArrayList(RelationshipType.withName("foo"), RelationshipType.withName("bar")));
  when(graphDb.beginTx()).thenReturn(tx);
  when(api.getEdges(any(RelationshipType.class), anyBoolean(), anyLong(), anyLong()))
      .thenReturn(new TinkerGraph());
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: TinkerGraphUtilTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  node = mockNode(0L);
  otherNode = mockNode(1L);
  graph = new TinkerGraph();
  relationship = mockRealtionship(node, otherNode);
  Map<String,String> iri2curie = new HashMap<>();
  iri2curie.put("B", "http://x.org/B_");
  curieUtil = new CurieUtil(iri2curie);
}
 
Example #15
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 #16
Source File: GraphWriterTestBase.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Test
public void assignable_fromGraph() {
  assertThat(writer.isWriteable(TinkerGraph.class, null, null, null), is(true));
  assertThat(writer.isWriteable(String.class, null, null, null), is(false));
}
 
Example #17
Source File: CypherInflectorTest.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Test
public void inflectorAppliesCorrectly() {
  path.setVendorExtension("x-query", "MATCH (n) RETURN n");
  TinkerGraph graph = (TinkerGraph) inflector.apply(context).getEntity();
  assertThat(graph.getVertices(), IsIterableWithSize.<Vertex>iterableWithSize(6));
}
 
Example #18
Source File: CypherInflectorTest.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Test
public void inflectorAppliesCorrectly_withRelationshipEntailment() {
  path.setVendorExtension("x-query", "MATCH (n)-[r:X:foo!]-(m) RETURN n, r, m");
  TinkerGraph graph = (TinkerGraph) inflector.apply(context).getEntity();
  assertThat(getOnlyElement(graph.getEdges()).getLabel(), is("http://x.org/#fizz"));
}
 
Example #19
Source File: CypherInflectorTest.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Test
public void inflectorAppliesCorrectly_withVariableRelationship() {
  path.setVendorExtension("x-query","MATCH (n)-[r:${rel_id}]-(m) RETURN n, r, m");
  TinkerGraph graph = (TinkerGraph) inflector.apply(context).getEntity();
  assertThat(getOnlyElement(graph.getEdges()).getLabel(), is("http://x.org/#fizz"));
}
 
Example #20
Source File: CypherInflectorTest.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Test
public void pathsAreReturnedCorrectly() {
  path.setVendorExtension("x-query","MATCH (n {iri:'http://x.org/#foo'})-[path:subPropertyOf*]-(m) RETURN n, path, m");
  TinkerGraph graph = (TinkerGraph) inflector.apply(context).getEntity();
  assertThat(graph.getEdges(), IsIterableWithSize.<Edge>iterableWithSize(1));
}
 
Example #21
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;
}
 
Example #22
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Inject
public TinkerGraphUtil(CurieUtil curieUtil) {
  this.curieUtil = curieUtil;
  this.graph = new TinkerGraph();
}