@Override public Edge addEdge( final Object id, final Vertex outVertex, final Vertex inVertex, final String label) { final Resource inV = model.createResource(inVertex.getId().toString()); final Resource outV = model.createResource(outVertex.getId().toString()); final Property edge = model.createProperty(label); final Statement statement = model.createStatement(outV, edge, inV); model.add(statement); return new JenaEdge(model, edge, inV, outV); }
@Override public void removeVertex(final Vertex vertex) { // find all statements that have the Vertex id (URI) as either // the subject or the object if (vertex.getId() == null) { return; } final StmtIterator statements = model.listStatements( new SimpleSelector() { @Override public boolean selects(final Statement s) { final Resource subject = s.getSubject(); final RDFNode object = s.getObject(); if (subject.getURI().equals(vertex.getId().toString())) { return true; } else if (object.isResource() && object.asResource().getURI().equals(vertex.getId().toString())) { return true; } return false; } }); // anything that matched should be removed final List<Statement> statementsToRemove = new ArrayList<Statement>(); while (statements.hasNext()) { final Statement statement = statements.next(); statementsToRemove.add(statement); } for (final Statement statement : statementsToRemove) { model.remove(statement); } }
/** * The object id must be a Map<String,Object> or null. The id is the properties written when * the vertex is created. While it is possible to Edge.setProperty(), this method is faster. * * @param id a id of properties which can be null * @return the newly created vertex */ public Edge addEdge( final Object id, final Vertex outVertex, final Vertex inVertex, final String label) { final Map<String, Object> finalProperties; if (id == null || !(id instanceof Map)) finalProperties = new HashMap<String, Object>(); else finalProperties = makePropertyMap((Map<String, Object>) id); final Long finalId = this.rawGraph.createRelationship( (Long) outVertex.getId(), (Long) inVertex.getId(), DynamicRelationshipType.withName(label), finalProperties); final Neo4jBatchEdge edge = new Neo4jBatchEdge(this, finalId, label); if (finalProperties.size() > 0) { for (final Neo4jBatchAutomaticIndex<Neo4jBatchEdge> index : this.automaticEdgeIndices.values()) { index.autoUpdate(edge, finalProperties); } } return edge; }
@Test public void inputGraphWithTypesFullCycle() throws IOException { TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); GraphSONWriter writer = new GraphSONWriter(graph); writer.outputGraph(stream, null, null, true); stream.flush(); stream.close(); String jsonString = new String(stream.toByteArray()); byte[] bytes = jsonString.getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); TinkerGraph emptyGraph = new TinkerGraph(); GraphSONReader.inputGraph(emptyGraph, inputStream); Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices())); Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges())); for (Vertex v : graph.getVertices()) { Vertex found = emptyGraph.getVertex(v.getId()); Assert.assertNotNull(v); for (String key : found.getPropertyKeys()) { Assert.assertEquals(v.getProperty(key), found.getProperty(key)); } } for (Edge e : graph.getEdges()) { Edge found = emptyGraph.getEdge(e.getId()); Assert.assertNotNull(e); for (String key : found.getPropertyKeys()) { Assert.assertEquals(e.getProperty(key), found.getProperty(key)); } } }