/** * 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); } } }
public GraphQuery query() { return new WrappedGraphQuery(this.baseGraph.query()) { @Override public Iterable<Edge> edges() { return new WrappedEdgeIterable(this.query.edges()); } @Override public Iterable<Vertex> vertices() { return new WrappedVertexIterable(this.query.vertices()); } }; }
private void init() { try { this.graph.getVertices("class", ""); } catch (IllegalArgumentException e) { Vertex v = this.graph.addVertex(null); v.setProperty("class", ""); } }
Graph graph = new Graph(); graph.getNodeTable().addColumn("duration", double.class); Node n1 = graph.addNode(); n1.setDouble("duration", 20.0); Node n2 = graph.addNode(); Edge e = graph.addEdge(n1, n2);
@ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, produces = MediaType.APPLICATION_JSON, method = HttpMethod.GET, path = EXTENSION_METHOD_COUNT) @ExtensionDescriptor(description = "get true count of vertices to calculate true split size for faunus") public ExtensionResponse getVertexCount(@RexsterContext final Graph graph) { logger.info("Faunus is configured to get the true count of vertices in the graph."); int counter = 0; final Iterable<Vertex> vertices = graph.getVertices(); for (Vertex v : vertices) { counter++; if (logger.isDebugEnabled() && counter % WRITE_STATUS_EVERY == 0) { logger.debug(String.format("True count at: %s", counter)); } } final Map<String, Integer> m = new HashMap<String, Integer>(); m.put(EXTENSION_METHOD_COUNT, counter); return ExtensionResponse.ok(m); }
@Override public void write(OutputStream out) throws IOException { long counter = 0; long vertexCount = 0; final DataOutputStream dos = new DataOutputStream(out); final Iterable<Vertex> vertices = graph.getVertices(); for (Vertex vertex : vertices) { if (counter >= start && counter < end) { vertexToFaunusBinary.writeVertex(vertex, dos); if (logger.isDebugEnabled() && counter % WRITE_STATUS_EVERY == 0) { logger.debug(String.format("Request [%s] at [%s] on the way to [%s].", requestIdentifier, vertexCount, verticesInSplit)); } vertexCount++; } else if (counter >= end) { logger.debug(String.format("Request [%s] completed.", requestIdentifier)); break; } counter++; } } }).build());
@Override public void cleanup(final Mapper<NullWritable, FaunusVertex, LongWritable, Holder<FaunusVertex>>.Context context) throws IOException, InterruptedException { if (this.graph instanceof TransactionalGraph) { try { ((TransactionalGraph) this.graph).commit(); context.getCounter(Counters.SUCCESSFUL_TRANSACTIONS).increment(1l); } catch (Exception e) { LOGGER.error("Could not commit transaction during VertexMap.cleanup():", e); ((TransactionalGraph) this.graph).rollback(); context.getCounter(Counters.FAILED_TRANSACTIONS).increment(1l); throw new IOException(e.getMessage(), e); } } this.graph.shutdown(); }
@Override public void stop() { // need to shutdown all the graphs that were started with the web server for (RexsterApplicationGraph rag : this.graphs.values()) { final Graph graph = rag.getGraph(); logger.info(String.format("Shutting down [%s] - [%s]", rag.getGraphName(), graph)); // graph may not have been initialized properly if an exception gets tossed in // on graph creation if (graph != null) { // call shutdown on the unwrapped graph as some wrappers don't allow shutdown() to be called. final Graph shutdownGraph = rag.getUnwrappedGraph(); shutdownGraph.shutdown(); } } }
if (graph.getFeatures().supportsVertexIndex && graph instanceof KeyIndexableGraph) { ((KeyIndexableGraph) graph).createKeyIndex("name", Vertex.class); GraphQuery result = graph.query().has(nodeId); logger.info(graphmlFile.getAbsolutePath() +" "+ "does not exist"); return null; logger.info(versionDir.getAbsolutePath() + " " + "does not exist");
Vertex blueprintsVertex = null; if (null != blueprintsId) blueprintsVertex = this.graph.getVertex(blueprintsId); final Object otherId = faunusBlueprintsIdMap.get(faunusEdge.getVertex(IN).getId()); Vertex otherVertex = null; if (null != otherId) otherVertex = this.graph.getVertex(otherId); if (null != otherVertex) { this.getOrCreateEdge((FaunusEdge) faunusEdge, blueprintsVertex, otherVertex, context); } else { LOGGER.warn("No target vertex: faunusVertex[" + faunusEdge.getVertex(IN).getId() + "] blueprintsVertex[" + otherId + "]"); context.getCounter(Counters.NULL_VERTEX_EDGES_IGNORED).increment(1l); LOGGER.warn("No source vertex: faunusVertex[" + NullWritable.get() + "] blueprintsVertex[" + blueprintsId + "]"); context.getCounter(Counters.NULL_VERTICES_IGNORED).increment(1l);
private void writeGraphEdges(OutputStream os) throws IOException { for(Edge edge : graph.getEdges()) { String label = edge.getLabel(); String source = ""+edge.getVertex(Direction.IN).getId().hashCode(); String target = ""+edge.getVertex(Direction.OUT).getId().hashCode(); writeGraphEdge(label, source, target, os); } }
Map<String, String> edgeKeyTypes = new HashMap<String, String>(); for (Vertex vertex : graph.getVertices()) { for (String key : vertex.getPropertyKeys()) { if (!vertexKeyTypes.containsKey(key)) { vertexKeyTypes.put(key, GraphMLWriter.getStringType(vertex.getProperty(key))); if (normalize) { vertices = new ArrayList<Vertex>(); for (Vertex v : graph.getVertices()) { ((Collection<Vertex>) vertices).add(v); vertices = graph.getVertices(); for (Vertex vertex : graph.getVertices()) { for (Edge edge : vertex.getEdges(Direction.OUT)) { edges.add(edge); writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString()); writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString()); writer.writeAttribute(GraphMLTokens.TARGET, edge.getVertex(Direction.IN).getId().toString()); for (Vertex vertex : graph.getVertices()) { for (Edge edge : vertex.getEdges(Direction.OUT)) { writer.writeStartElement(GraphMLTokens.EDGE);
private void mergeEdge(Graph graph1, Edge edge2) { Edge edge1 = graph1.getEdge(edge2.getId()); if (edge1 == null) { Vertex outVertex1 = graph1.getVertex(edge2.getVertex(Direction.OUT).getId()); Vertex outVertex2 = graph1.getVertex(edge2.getVertex(Direction.IN).getId()); edge1 = graph1.addEdge(edge2.getId(), outVertex1, outVertex2, edge2.getLabel()); for (String key2 : edge2.getPropertyKeys()) { edge1.setProperty(key2,edge2.getProperty(key2)); } } else { Set<String> keys1 = edge1.getPropertyKeys(); Set<String> keys2 = edge2.getPropertyKeys(); for (String key2 : keys2) { if (keys1.contains(key2)) { MergeConflictResolver conflictResolver = getEdgeConflictResolver(key2); Object merge = conflictResolver.resolveConflict(edge1.getProperty(key2), edge2.getProperty(key2)); edge1.setProperty(key2, merge); } else { edge1.setProperty(key2, edge2.getProperty(key2)); } } } }
for (Vertex vertex : graph.getVertices()) { BbopNode bbopNode = new BbopNode(); bbopNode.setId(getCurieOrIri(vertex)); getFirst(TinkerGraphUtil.getProperties(vertex, NodeProperties.LABEL, String.class), null); bbopNode.setLbl(label); for (String key : vertex.getPropertyKeys()) { if (IGNORED_PROPERTY_KEYS.contains(key)) { continue; for (Edge edge : graph.getEdges()) { BbopEdge bbopEdge = new BbopEdge(); Vertex subject = edge.getVertex(Direction.OUT); Vertex object = edge.getVertex(Direction.IN); bbopEdge.setSub(getCurieOrIri(subject)); bbopEdge.setObj(getCurieOrIri(object)); bbopEdge.setPred(edge.getLabel()); for (String key : edge.getPropertyKeys()) { if (IGNORED_PROPERTY_KEYS_FOR_EDGES.contains(key)) {
Edge addEdge(Edge edge) { Edge newEdge = graph.getEdge(edge.getId()); if (null == newEdge) { Vertex outVertex = addNode(edge.getVertex(Direction.OUT)); Vertex inVertex = addNode(edge.getVertex(Direction.IN)); String label = edge.getLabel(); newEdge = graph.addEdge(edge.getId(), outVertex, inVertex, label); copyProperties(edge, edge); } return newEdge; }
private void writeGraphNodes(OutputStream os) throws IOException { //iterate the nodes. for(Vertex vertex : graph.getVertices()) { String id = ""+vertex.getId().hashCode(); String label = vertex.getProperty(vertexLabelProperty); if(StringUtils.isBlank(label)) { label = vertex.toString(); } writeGraphNode(id, label, os); } }
void addCuries(Graph graph) { for (Vertex vertex: graph.getVertices()) { String iri = (String)vertex.getProperty(CommonProperties.IRI); Optional<String> curie = curieUtil.getCurie(iri); if (curie.isPresent()) { vertex.setProperty(CommonProperties.CURIE, curie.get()); } } }
private void populateLists(final List<Vertex> vertices, final List<Edge> edges) { for (Vertex v : graph.getVertices()) { vertices.add(v); } for (Edge e : graph.getEdges()) { edges.add(e); } }