private Object toJsonCompatible(Object value) { if (value instanceof Node) { final Node node = (Node) value; final Map<String, Object> result = SubGraph.toMap(node); result.put("_id", node.getId()); return result; } if (value instanceof Relationship) { final Relationship relationship = (Relationship) value; final Map<String, Object> result = SubGraph.toMap(relationship); result.put("_id", relationship.getId()); result.put("_start", relationship.getStartNode().getId()); result.put("_end", relationship.getEndNode().getId()); result.put("_type", relationship.getType().name()); return result; } if (value instanceof Iterable) { final List<Object> result = new ArrayList<Object>(); for (Object inner : (Iterable) value) { result.add(toJsonCompatible(inner)); } return result; } return value; }
private void appendRelationships(PrintWriter out) { for (Node node : graph.getNodes()) { for (Relationship rel : node.getRelationships(Direction.OUTGOING)) { appendRelationship(out, rel); } } }
private Node getReferenceNode() { try { return graph.getReferenceNode(); } catch (NotFoundException nfe) { return null; } }
@Test public void testGraphToYuml() throws Exception { gdb.beginTx(); final Node n1 = gdb.createNode(); gdb.getReferenceNode().createRelationshipTo(n1, DynamicRelationshipType.withName("REL")); final String res = new YumlExport().toYuml(SubGraph.from(gdb)); assertEquals("[0],[1],[0]REL->[1],", res); }
@Test public void testIdPropsToYuml() throws Exception { gdb.beginTx(); gdb.getReferenceNode().setProperty("name", "root"); final String res = new YumlExport().toYuml(SubGraph.from(gdb), "name"); System.out.println("res = " + res); assertEquals("[root],", res); }
// генерируем подграфы на множестве вершин v (закодировано) private void generateSubgraphs(int v) { List<Integer> edges = new ArrayList<Integer>(); // для любого ребря графа, проверяем, можно ли его добавить в подграф for (Integer e : matrix) { if (isEdgeBasedOnVertexs(e, v)) edges.add(e); } // создаем подграфы из множества вершин v и всех возможных комбинаций // ребер из edges // то есть тех, которые можно добавить в этот подграф for (int e = 0; e < Math.pow(2d, edges.size()); e++) { SubGraph sub = new SubGraph(this.v, v); for (int k = e, i = 0; k > 0; k /= 2, i++) { if (k % 2 == 1) sub.addEdge(edges.get(i)); } subgraphs.add(sub); } }
private void appendNodes(PrintWriter out) { for (Node node : graph.getNodes()) { if (isReferenceNode(node)) { continue; } appendNode(out, node); } }
public Collection<String> exportIndexes() { Collection<String> result = new ArrayList<String>(); for (IndexDefinition index : graph.indexes()) { StringBuilder keys = new StringBuilder(); for (String key : index.getPropertyKeys()) { if (keys.length() > 0) keys.append(", "); keys.append(quote(key)); } result.add("create index on :" + quote(index.getLabel().name()) + "(" + keys + ")"); } return result; }
private Object toJsonCompatible(Object value) { if (value instanceof Node) { final Node node = (Node) value; final Map<String, Object> result = SubGraph.toMap((PropertyContainer) node); result.put("_id", node.getId()); final List<String> labelNames = SubGraph.getLabelNames(node); if (!labelNames.isEmpty()) result.put("_labels", labelNames); return result; } if (value instanceof Relationship) { final Relationship relationship = (Relationship) value; final Map<String, Object> result = SubGraph.toMap((PropertyContainer) relationship); result.put("_id", relationship.getId()); result.put("_start", relationship.getStartNode().getId()); result.put("_end", relationship.getEndNode().getId()); result.put("_type", relationship.getType().name()); return result; } if (value instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) value; final Map<String, Object> result = new LinkedHashMap<>(map.size()); for (Map.Entry<String, Object> entry : map.entrySet()) { result.put(entry.getKey(), toJsonCompatible(entry.getValue())); } return result; } if (value instanceof Iterable) { final List<Object> result = new ArrayList<>(); for (Object inner : (Iterable) value) { result.add(toJsonCompatible(inner)); } return result; } return value; }
@Test public void testNamedGraphToYuml() throws Exception { gdb.beginTx(); gdb.getReferenceNode().setProperty("name", "root"); final Node n1 = gdb.createNode(); n1.setProperty("name", "Peter"); gdb.getReferenceNode().createRelationshipTo(n1, DynamicRelationshipType.withName("PERSON")); final Node n2 = gdb.createNode(); n2.setProperty("name", "Andreas"); gdb.getReferenceNode().createRelationshipTo(n2, DynamicRelationshipType.withName("PERSON")); final Node n3 = gdb.createNode(); n3.setProperty("name", "Michael"); gdb.getReferenceNode().createRelationshipTo(n3, DynamicRelationshipType.withName("PERSON")); n1.createRelationshipTo(n2, DynamicRelationshipType.withName("FRIEND")); n3.createRelationshipTo(n1, DynamicRelationshipType.withName("FRIEND")); final String res = new YumlExport().toYuml(SubGraph.from(gdb), "name"); System.out.println("res = " + res); assertEquals(true, res.contains("[Peter]FRIEND->[Andreas],")); assertEquals(true, res.contains("[root],[Peter],[Andreas],[Michael],")); }
@Test public void testSimpleToYuml() throws Exception { final String res = new YumlExport().toYuml(SubGraph.from(gdb)); assertEquals("[0],", res); }