Java Code Examples for com.tinkerpop.blueprints.Graph#addEdge()

The following examples show how to use com.tinkerpop.blueprints.Graph#addEdge() . 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: InputFormatsTest.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testEdgeInputMap() throws Exception {
  final String INSTANCE_NAME = "_mapreduce_instance";
  final String TEST_TABLE_NAME = "_mapreduce_table_edgeInputMap";

  if (!System.getProperty("os.name").startsWith("Windows")) {
    Graph g = GraphFactory.open(new AccumuloGraphConfiguration().setInstanceName(INSTANCE_NAME)
        .setUser("root").setPassword("".getBytes())
        .setGraphName(TEST_TABLE_NAME).setInstanceType(InstanceType.Mock)
        .setAutoFlush(true).setCreate(true).getConfiguration());

    for (int i = 0; i < 100; i++) {
      Vertex v1 = g.addVertex(i+"");
      Vertex v2 = g.addVertex(i+"a");
      g.addEdge(null, v1, v2, "knows");
    }

    assertEquals(0, MRTester.main(new String[]{"root", "",
        TEST_TABLE_NAME, INSTANCE_NAME, "true"}));
    assertNull(e1);
    assertNull(e2);

    g.shutdown();
  }
}
 
Example 2
Source File: ElementCacheTest.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testElementCacheSize() throws Exception {
  AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils
      .generateGraphConfig("elementCacheSize");
  Graph graph = GraphFactory.open(cfg.getConfiguration());

  Vertex[] verts = new Vertex[10];
  for (int i = 0; i < verts.length; i++) {
    verts[i] = graph.addVertex(i);
  }

  Edge[] edges = new Edge[9];
  for (int i = 0; i < edges.length; i++) {
    edges[i] = graph.addEdge(null,
        verts[0], verts[i+1], "edge");
  }

  sizeTests(verts);
  sizeTests(edges);

  graph.shutdown();
}
 
Example 3
Source File: GraphOperations.java    From bjoern with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add an edge from the node src to the node dst if it does
 * not already exist.
 *
 * @param src the source of the edge
 * @param dst the destination of the edge
 */
public static void addEdge(Graph graph, OctopusNode src, OctopusNode dst, String edgeType)
{
	for (Edge edge : src.getBaseVertex().getEdges(Direction.OUT,
			edgeType))
	{
		if (edge.getVertex(Direction.IN).equals(dst.getBaseVertex()))
		{
			return;
		}
	}
	graph.addEdge(0, src.getBaseVertex(), dst.getBaseVertex(), edgeType);
}
 
Example 4
Source File: EdgeProcessor.java    From bjoern with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void processRow(String[] row)
{
	if (row.length < 3)
		return;

	String srcId = row[0];
	String dstId = row[1];
	String label = row[2];

	Graph graph = importer.getGraph();

	Vertex outVertex = lookupVertex(srcId, graph);
	Vertex inVertex = lookupVertex(dstId, graph);

	if (outVertex == null)
	{
		logger.info("Cannot resolve source node {} for {} -> {}", srcId,
				srcId, dstId);
		return;
	}

	if (inVertex == null)
	{
		logger.info("Cannot resolve destination node {} for {} -> {}",
				dstId, srcId, dstId);
		return;
	}

	Edge edge = graph.addEdge(0, outVertex, inVertex, label);

	for (int i = 3; i < row.length; i++)
	{
		edge.setProperty(importer.getEdgeKeys()[i], row[i]);
	}
}
 
Example 5
Source File: NodeProcessor.java    From bjoern with GNU General Public License v3.0 5 votes vote down vote up
private void linkToPreviousNode(String baseId, int num)
{
	String previousId = createCompleteId(baseId, num - 1);
	String thisId = createCompleteId(baseId, num);

	Graph graph = importer.getGraph();

	Vertex fromNode = graph.getVertex(previousId);
	Vertex toNode = graph.getVertex(thisId);

	graph.addEdge(0, fromNode, toNode, "foo");
}
 
Example 6
Source File: AccumuloElementTest.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStringIds() throws Exception {
  Graph graph = AccumuloGraphTestUtils.makeGraph("nonStringIds");

  Object[] ids = new Object[] {
      10, 20, 30L, 40L,
      50.0f, 60.0f, 70.0d, 80.0d,
      (byte) 'a', (byte) 'b', 'c', 'd',
      "str1", "str2",
      new GenericObject("str3"), new GenericObject("str4"),
  };

  Object[] edgeIds = new Object[] {
      100, 200, 300L, 400L,
      500.0f, 600.0f, 700.0d, 800.0d,
      (byte) 'e', (byte) 'f', 'g', 'h',
      "str5", "str6",
      new GenericObject("str7"), new GenericObject("str8"),
  };

  for (int i = 0; i < ids.length; i++) {
    assertNull(graph.getVertex(ids[i]));
    Vertex v = graph.addVertex(ids[i]);
    assertNotNull(v);
    assertNotNull(graph.getVertex(ids[i]));
  }
  assertEquals(ids.length, count(graph.getVertices()));

  for (int i = 1; i < edgeIds.length; i++) {
    assertNull(graph.getEdge(edgeIds[i-1]));
    Edge e = graph.addEdge(edgeIds[i-1],
        graph.getVertex(ids[i-1]),
        graph.getVertex(ids[i]), "label");
    assertNotNull(e);
    assertNotNull(graph.getEdge(edgeIds[i-1]));
  }
  assertEquals(edgeIds.length-1, count(graph.getEdges()));

  graph.shutdown();
}
 
Example 7
Source File: ElementCacheTest.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testElementCacheTimeout() throws Exception {
  AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils
      .generateGraphConfig("elementCacheTimeout");
  Graph graph = GraphFactory.open(cfg.getConfiguration());

  ElementCache<Element> cache =
      new ElementCache<Element>(10, 1000);

  Vertex v1 = graph.addVertex(1);
  Vertex v2 = graph.addVertex(2);
  assertNull(cache.retrieve(1));
  assertNull(cache.retrieve(2));

  cache.cache(v1);
  assertNotNull(cache.retrieve(v1.getId()));
  Thread.sleep(1500);
  assertNull(cache.retrieve(v1.getId()));

  Edge e = graph.addEdge(null, v1, v2, "label");
  assertNull(cache.retrieve(e.getId()));

  cache.cache(e);
  assertNotNull(cache.retrieve(e.getId()));
  Thread.sleep(1500);
  assertNull(cache.retrieve(e.getId()));

  graph.shutdown();
}
 
Example 8
Source File: EdgeHelper.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Edges are relabeled by creating new edges with the same properties, but new label.
 * Note that for each edge is deleted and an edge is added.
 *
 * @param graph    the graph to add the new edge to
 * @param oldEdges the existing edges to "relabel"
 * @param newLabel the label of the new edge
 */
public static void relabelEdges(final Graph graph, final Iterable<Edge> oldEdges, final String newLabel) {
    for (final Edge oldEdge : oldEdges) {
        final Vertex outVertex = oldEdge.getVertex(Direction.OUT);
        final Vertex inVertex = oldEdge.getVertex(Direction.IN);
        final Edge newEdge = graph.addEdge(null, outVertex, inVertex, newLabel);
        ElementHelper.copyProperties(oldEdge, newEdge);
        graph.removeEdge(oldEdge);
    }
}
 
Example 9
Source File: GraphHelper.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Copy the vertex/edges of one graph over to another graph.
 * The id of the elements in the from graph are attempted to be used in the to graph.
 * This method only works for graphs where the user can control the element ids.
 *
 * @param from the graph to copy from
 * @param to   the graph to copy to
 */
public static void copyGraph(final Graph from, final Graph to) {
    for (final Vertex fromVertex : from.getVertices()) {
        final Vertex toVertex = to.addVertex(fromVertex.getId());
        ElementHelper.copyProperties(fromVertex, toVertex);
    }
    for (final Edge fromEdge : from.getEdges()) {
        final Vertex outVertex = to.getVertex(fromEdge.getVertex(Direction.OUT).getId());
        final Vertex inVertex = to.getVertex(fromEdge.getVertex(Direction.IN).getId());
        final Edge toEdge = to.addEdge(fromEdge.getId(), outVertex, inVertex, fromEdge.getLabel());
        ElementHelper.copyProperties(fromEdge, toEdge);
    }
}
 
Example 10
Source File: EdgeHelper.java    From org.openntf.domino with Apache License 2.0 3 votes vote down vote up
/**
 * An edge is relabeled by creating a new edge with the same properties, but new label.
 * Note that an edge is deleted and an edge is added.
 *
 * @param graph    the graph to add the new edge to
 * @param oldEdge  the existing edge to "relabel"
 * @param newId    the id of the new edge
 * @param newLabel the label of the new edge
 * @return the newly created edge
 */
public static Edge relabelEdge(final Graph graph, final Edge oldEdge, final Object newId, final String newLabel) {
    final Vertex outVertex = oldEdge.getVertex(Direction.OUT);
    final Vertex inVertex = oldEdge.getVertex(Direction.IN);
    final Edge newEdge = graph.addEdge(newId, outVertex, inVertex, newLabel);
    ElementHelper.copyProperties(oldEdge, newEdge);
    graph.removeEdge(oldEdge);
    return newEdge;
}
 
Example 11
Source File: GraphHelper.java    From org.openntf.domino with Apache License 2.0 3 votes vote down vote up
/**
 * Add an edge to the graph with specified id and provided properties.
 *
 * @param graph      the graph to create the edge in
 * @param id         the id of the edge to create
 * @param outVertex  the outgoing/tail vertex of the edge
 * @param inVertex   the incoming/head vertex of the edge
 * @param label      the label of the edge
 * @param properties the properties of the edge to add (must be String,Object,String,Object,...)
 * @return the edge created in the graph with the provided properties set
 */
public static Edge addEdge(final Graph graph, final Object id, final Vertex outVertex, final Vertex inVertex, final String label, final Object... properties) {
    if ((properties.length % 2) != 0)
        throw new RuntimeException("There must be an equal number of keys and values");
    final Edge edge = graph.addEdge(id, outVertex, inVertex, label);
    for (int i = 0; i < properties.length; i = i + 2) {
        edge.setProperty((String) properties[i], properties[i + 1]);
    }
    return edge;
}