public static List<Node> getChildrenConnectedBy(Node node, String edgeType) { List<Node> retval = new LinkedList<Node>(); long nodeId = node.getId(); Iterable<Relationship> rels = node.getRelationships(); for (Relationship rel : rels) { if (!rel.getType().name().equals(edgeType)) continue; Node childNode = rel.getEndNode(); if (childNode.getId() == nodeId) continue; retval.add(childNode); } return retval; }
public static List<Node> getParentsConnectedBy(Node node, String edgeType) { List<Node> retval = new LinkedList<Node>(); long nodeId = node.getId(); Iterable<Relationship> rels = node.getRelationships(); for (Relationship rel : rels) { if (!rel.getType().name().equals(edgeType)) continue; Node parentNode = rel.getStartNode(); if (parentNode.getId() == nodeId) continue; retval.add(parentNode); } return retval; }
public static DDG getDDGForFunction(Node funcNode) { DDG retval = new DDG(); for (Node statement : Traversals.getStatementsForFunction(funcNode)) { Iterable<Relationship> rels = statement .getRelationships(Direction.OUTGOING); long srcId = statement.getId(); for (Relationship rel : rels) { if (!rel.getType().toString().equals(EdgeTypes.REACHES)) continue; long dstId = rel.getEndNode().getId(); String symbol = rel.getProperty("var").toString(); retval.add(srcId, dstId, symbol); } } return retval; }
static ArrayList<Node> alternatingOrder (Node order) { ArrayList<Node> products = new ArrayList<>(); Relationship prevRel = order.getSingleRelationship(RelationshipTypes.PREV, Direction.OUTGOING); if (prevRel != null) { Node prevOrder = prevRel.getEndNode(); prevRel = prevOrder.getSingleRelationship(RelationshipTypes.PREV, Direction.OUTGOING); if (prevRel != null) { prevOrder = prevRel.getEndNode(); for (Relationship r1 : prevOrder.getRelationships(Direction.OUTGOING, RelationshipTypes.HAS)) { Node product = r1.getEndNode(); products.add(product); } } } return products; }
@Procedure(value = "graph.versioner.diff.from.current", mode = DEFAULT) @Description("graph.versioner.diff.from.current(state) - Get a list of differences that must be applied to the given state in order to become the current entity state") public Stream<DiffOutput> diffFromCurrent( @Name("state") Node state) { Optional<Node> currentState = Optional.ofNullable(state.getSingleRelationship(RelationshipType.withName(Utility.HAS_STATE_TYPE), Direction.INCOMING)) .map(Relationship::getStartNode).map(entity -> entity.getSingleRelationship(RelationshipType.withName(Utility.CURRENT_TYPE), Direction.OUTGOING)) .map(Relationship::getEndNode); Stream<DiffOutput> result = Stream.empty(); if(currentState.isPresent() && !currentState.equals(Optional.of(state))){ result = diffBetweenStates(Optional.of(state), currentState); } return result; }
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed // TODO add your handling code here: try (Transaction tx = db.beginTx()) { Node vols = db.createNode(Main.Tables.Vol); Node avions = db.createNode(Main.Tables.Avion); Node departs = db.createNode(Main.Tables.Depart); Node passagers = db.createNode(Main.Tables.Passager); Node personnels = db.createNode(Main.Tables.Personnel); Node naviguants = db.createNode(Main.Tables.Naviguant); Node nonNaviguants = db.createNode(Main.Tables.NonNaviguant); Node pilotes = db.createNode(Main.Tables.Pilote); Node quantiteAvion = db.createNode(Main.Tables.QuantiteAvion); Relationship constituer = vols.createRelationshipTo(departs, Relations.Constituer); Relationship enregistrer = passagers.createRelationshipTo(departs, Relations.Enregistrer); Relationship affecterPersonnel = personnels.createRelationshipTo(departs, Relations.AffecterPersonnel); Relationship affecterAvion = avions.createRelationshipTo(departs, Relations.AffecterAvion); } }
@Procedure(name = "com.maxdemarzi.match.user", mode = Mode.READ) @Description("CALL com.maxdemarzi.match.user(username) - find matching rules") public Stream<NodeResult> matchUser(@Name("username") String username) throws IOException { // We start by finding the user Node user = db.findNode(Labels.User, "username", username); if (user != null) { // Gather all of their attributes in to a Set Set<Node> userAttributes = new HashSet<>(); Collection<String> attributes = new HashSet<>(); for (Relationship r : user.getRelationships(Direction.OUTGOING, RelationshipTypes.HAS)) { userAttributes.add(r.getEndNode()); attributes.add((String)r.getEndNode().getProperty("id")); } // Find the rules Set<Node> rules = findRules(attributes, userAttributes); return rules.stream().map(NodeResult::new); } return null; }
@Test public void validates_supported_simple_types() { assertThat( visitor().visit( typeMirrorTestUtils().typeOf( String.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Number.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Long.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( TypeKind.LONG ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Double.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( TypeKind.DOUBLE ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Boolean.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( TypeKind.BOOLEAN ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Path.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Node.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Relationship.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Object.class ) ) ).isTrue(); }
@Test public void validates_supported_generic_types() { assertThat( visitor().visit( typeMirrorTestUtils().typeOf( Map.class, String.class, Object.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( HashMap.class, String.class, Object.class ) ) ) .isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( LinkedHashMap.class, String.class, Object.class ) ) ) .isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, String.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( LinkedList.class, Number.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( ArrayList.class, Long.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Double.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Boolean.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Path.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Node.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Relationship.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils().typeOf( List.class, Object.class ) ) ).isTrue(); assertThat( visitor().visit( typeMirrorTestUtils() .typeOf( List.class, typeMirrorTestUtils().typeOf( Map.class, String.class, Object.class ) ) ) ) .isTrue(); assertThat( visitor().visit( typeMirrorTestUtils() .typeOf( List.class, typeMirrorTestUtils().typeOf( LinkedList.class, Long.class ) ) ) ).isTrue(); }
@Test public void supported_simple_type_is_valid() { assertThat( validator.test( typeMirrorTestUtils.typeOf( BOOLEAN ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( LONG ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( DOUBLE ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Boolean.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Long.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Double.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( String.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Number.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Object.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Node.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Relationship.class ) ) ).isTrue(); assertThat( validator.test( typeMirrorTestUtils.typeOf( Path.class ) ) ).isTrue(); }
@Override public <R, E extends Throwable> R accept(Visitor<R, E> visitor) throws E { //filternodes: for (Node node : nodes) { visitor.visitNode(node); for (Relationship relationship : node.getRelationships(Direction.OUTGOING)) { if (nodes.contains(relationship.getOtherNode(node))) { if (!relationshipLabels.contains(relationship.getType().name())) { continue; } visitor.visitRelationship(relationship); } } } return visitor.done(); }
@Override public <R, E extends Throwable> R accept(Visitor<R, E> visitor) throws E { for (Node node : dbServices.getAllNodes()) { if (node.hasLabel(Label.label("CompilationUnit"))) { continue; } if (node.hasLabel(Label.label("SourceSpan"))) { continue; } if (node.hasLabel(Label.label("SourceLocation"))) { continue; } visitor.visitNode(node); for (Relationship edge : node.getRelationships(Direction.OUTGOING)) { if (edge.isType(RelationshipType.withName("location"))) { continue; } visitor.visitRelationship(edge); } } return visitor.done(); }
@Override public Set<Node> deleteRelations(DocumentRelationContext context) { Set<Node> orphans = new HashSet<>(); String documentKey = context.getDocumentKey(); if(StringUtils.isBlank(documentKey)) { return orphans; } Result result = db.execute("MATCH (p)-[r:"+documentKey+"]->(c) RETURN r"); result.forEachRemaining((res)->{ Relationship rel = (Relationship) res.get("r"); Node parent = rel.getStartNode(); Node child = rel.getEndNode(); rel.delete(); if(log.isDebugEnabled()) { log.debug("Delete relation "+rel); } updateOrphans(orphans, parent); updateOrphans(orphans, child); }); return orphans; }
@Test public void shuldCreateRelation() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(parent.getId(), rel.getStartNode().getId()); Assert.assertEquals(child.getId(), rel.getEndNode().getId()); Assert.assertEquals("key", rel.getType().name()); }
@Test public void shuldAddRelation() { context.setDocumentKey("key1"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel1 = this.docrel.buildRelation(parent, child, context); context.setDocumentKey("key2"); Relationship rel2 = this.docrel.buildRelation(parent, child, context); Assert.assertNotEquals(rel1.getId(), rel2.getId()); Assert.assertEquals("key1", rel1.getType().name()); Assert.assertEquals("key2", rel2.getType().name()); }
@Test public void shuldDoNothing() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel1 = this.docrel.buildRelation(parent, child, context); Relationship rel2 = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(rel1.getId(), rel2.getId()); Assert.assertEquals("key", rel2.getType().name()); }
@Test public void shuldCreateRelation() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(parent.getId(), rel.getStartNode().getId()); Assert.assertEquals(child.getId(), rel.getEndNode().getId()); Assert.assertEquals("HAS_ARTIST", rel.getType().name()); String[] keys = (String[]) rel.getProperty("docKeys"); Assert.assertEquals(1, keys.length); Assert.assertEquals("key", keys[0]); }
@Test public void shuldCreateRelation() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(parent.getId(), rel.getStartNode().getId()); Assert.assertEquals(child.getId(), rel.getEndNode().getId()); Assert.assertEquals("album_artist", rel.getType().name()); String[] keys = (String[]) rel.getProperty("docKeys"); Assert.assertEquals(1, keys.length); Assert.assertEquals("key", keys[0]); }
@Test public void shuldUpdateRelation() { context.setDocumentKey("key1"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel1 = this.docrel.buildRelation(parent, child, context); context.setDocumentKey("key2"); Relationship rel2 = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(rel1.getId(), rel2.getId()); String[] keys = (String[]) rel2.getProperty("docKeys"); Assert.assertEquals(2, keys.length); Assert.assertEquals("key1", keys[0]); Assert.assertEquals("key2", keys[1]); }
@Test public void shuldDoNothing() { context.setDocumentKey("key"); Node parent = db.createNode(); parent.setProperty("type", "album"); Node child = db.createNode(); child.setProperty("type", "artist"); Relationship rel1 = this.docrel.buildRelation(parent, child, context); Relationship rel2 = this.docrel.buildRelation(parent, child, context); Assert.assertEquals(rel1.getId(), rel2.getId()); String[] keys = (String[]) rel2.getProperty("docKeys"); Assert.assertEquals(1, keys.length); Assert.assertEquals("key", keys[0]); }
@Override public Edge getEdge(long id1, long id2) { Edge edge = null; try { beginTransaction(); Relationship rel = getInnerRelationship(id1, id2); if (rel != null) { Vertex v1 = getVertex(id1); Vertex v2 = getVertex(id2); int weigth = 0; if (rel.hasProperty(NetworkNeo4jProperties.WEIGHT)) { weigth = (int) rel.getProperty(NetworkNeo4jProperties.WEIGHT); } long id = (long) rel.getProperty(NetworkNeo4jProperties.INDEX); edge = new Edge(id, weigth, v1, v2); } } finally { endTransaction(); } return edge; }
@Override public Edge getEdge(long id) { Edge edge = null; try { beginTransaction(); Relationship rel = getInnerRelationship(id); if (rel != null) { Vertex v1 = getVertex((long) rel.getStartNode().getProperty(NetworkNeo4jProperties.INDEX)); Vertex v2 = getVertex((long) rel.getEndNode().getProperty(NetworkNeo4jProperties.INDEX)); int weigth = 0; if (rel.hasProperty(NetworkNeo4jProperties.WEIGHT)) { weigth = (int) rel.getProperty(NetworkNeo4jProperties.WEIGHT); } edge = new Edge(id, weigth, v1, v2); } } finally { endTransaction(); } return edge; }
@Override public Iterable<Edge> getAllEdges() { List<Edge> list = new ArrayList<>(); try { beginTransaction(); Result result = graphDb.execute("MATCH (a:NODE)-[r:LINK]->(b:NODE) RETURN r ORDER BY r.INDEX"); while (result.hasNext()) { Map<String, Object> row = result.next(); Relationship rel = (Relationship) row.get("r"); Vertex v1 = getVertex((long) rel.getStartNode().getProperty(NetworkNeo4jProperties.INDEX)); Vertex v2 = getVertex((long) rel.getEndNode().getProperty(NetworkNeo4jProperties.INDEX)); Edge edge = new Edge((long) rel.getProperty(NetworkNeo4jProperties.INDEX), (int) rel.getProperty(NetworkNeo4jProperties.WEIGHT), v1, v2); list.add(edge); } } finally { endTransaction(); } return new EdgeIterable<Edge>(list.iterator(), new Edge()); }
@Override public Iterable<Edge> getAllEdgesSortedByWight() { List<Edge> list = new ArrayList<>(); try { beginTransaction(); Result result = graphDb.execute("MATCH (a:NODE)-[r:LINK]->(b:NODE) RETURN r ORDER BY r.WIGHT ASC"); while (result.hasNext()) { Map<String, Object> row = result.next(); Relationship rel = (Relationship) row.get("r"); Vertex v1 = getVertex((long) rel.getStartNode().getProperty(NetworkNeo4jProperties.INDEX)); Vertex v2 = getVertex((long) rel.getEndNode().getProperty(NetworkNeo4jProperties.INDEX)); Edge edge = new Edge((long) rel.getProperty(NetworkNeo4jProperties.INDEX), (int) rel.getProperty(NetworkNeo4jProperties.WEIGHT), v1, v2); list.add(edge); } } finally { endTransaction(); } return new EdgeIterable<Edge>(list.iterator(), new Edge()); }
@Override public Iterable<Edge> getAllEdgesSortedByWightDesc() { List<Edge> list = new ArrayList<>(); try { beginTransaction(); Result result = graphDb.execute("MATCH (a:NODE)-[r:LINK]->(b:NODE) RETURN r ORDER BY r.WIGHT DESC"); while (result.hasNext()) { Map<String, Object> row = result.next(); Relationship rel = (Relationship) row.get("r"); Vertex v1 = getVertex((long) rel.getStartNode().getProperty(NetworkNeo4jProperties.INDEX)); Vertex v2 = getVertex((long) rel.getEndNode().getProperty(NetworkNeo4jProperties.INDEX)); Edge edge = new Edge((long) rel.getProperty(NetworkNeo4jProperties.INDEX), (int) rel.getProperty(NetworkNeo4jProperties.WEIGHT), v1, v2); list.add(edge); } } finally { endTransaction(); } return new EdgeIterable<Edge>(list.iterator(), new Edge()); }
@Override public boolean edgeExist(long id1, long id2) { boolean res = false; try { beginTransaction(); Result result = graphDb.execute("MATCH (a:NODE{INDEX:" + String.valueOf(id1) + "})-[r:LINK]-(b:NODE{INDEX:" + String.valueOf(id2) + "}) RETURN r LIMIT 1"); Relationship rel_result = null; Iterator<Relationship> n_column = result.columnAs("r"); for (Relationship rel : IteratorUtil.asIterable(n_column)) { rel_result = rel; } if (rel_result != null) { res = false; } } finally { endTransaction(); } return res; }
@Override public boolean edgeExist(long id1, long id2, int level) { if (level == 0) { return edgeExist(id1, id2); } boolean res = false; try { Result result = graphDb.execute("MATCH (a:COARSEN_NODE{INDEX:" + String.valueOf(id1) + ", LEVEL:" + level + "})-[r:LINK]-(b:COARSEN_NODE{INDEX:" + String.valueOf(id2) + ", LEVEL:" + level + "}) RETURN r LIMIT 1"); Relationship rel_result = null; Iterator<Relationship> n_column = result.columnAs("r"); for (Relationship rel : IteratorUtil.asIterable(n_column)) { rel_result = rel; } } finally { endTransaction(); } return res; }
public void traverse() { if (null != graph && null != root) { try (Transaction tx = graph.beginTx()) { PathFinder<Path> finder = GraphAlgoFactory.allPaths( PathExpanders.forTypesAndDirections(RelTypes.CHILD_HI, Direction.OUTGOING, RelTypes.CHILD_LO, Direction.OUTGOING), 5); // 5 is the number of original relationships + 1 Iterable<Path> paths = finder.findAllPaths(root, one); double total = 0.0; for (Path path : paths) { double current = 1.0; for (Relationship relationship : path.relationships()) { current *= (double) relationship.getProperty(PROB, 1.0); } total += current; } System.out.format("The probability is %.2f.\n", total); tx.success(); } } }
public void stamp() { if (null != graph && null != root) { try (Transaction tx = graph.beginTx()) { PathFinder<Path> finder = GraphAlgoFactory.allPaths( PathExpanders.forTypesAndDirections(RelTypes.CHILD_HI, Direction.OUTGOING, RelTypes.CHILD_LO, Direction.OUTGOING), 5); // 5 is the number of original relationships + 1 Iterable<Path> paths = finder.findAllPaths(root, one); for (Path path : paths) { boolean first = true; for (Relationship relationship : path.relationships()) { if (first) { System.out.format("(%s)", relationship.getStartNode().getProperty(ID)); first = false; } System.out.format(" --[%s,%.2f]-> ", relationship.getType().name(), relationship.getProperty(PROB, 1.0)); System.out.format("(%s)", relationship.getEndNode().getProperty(ID)); } System.out.println(); } tx.success(); } } }
private void analyseDb() { Transaction tx = graphDb.beginTx(); try { PathFinder<Path> finder = GraphAlgoFactory.allPaths(// PathExpanders.forTypeAndDirection(RelTypes.KNOWS, Direction.BOTH), -1); Iterable<Path> paths = finder.findAllPaths(firstNode, secondNode); for (Path path : paths) { for (Node node : path.nodes()) { for (String prop : node.getPropertyKeys()) System.out.print(prop); System.out.print(node.toString()); } for (Relationship relationship : path.relationships()) System.out.print(relationship.toString()); } tx.success(); } finally { tx.finish(); } }
public final Relationship add(RelationshipType type, Node tail, Node head, double prob) { if (null == type) throw new IllegalArgumentException("Illegal 'type' argument in Problem.add(RelationshipType, Node, Node, double): " + type); if (null == tail) throw new IllegalArgumentException("Illegal 'tail' argument in Problem.add(RelationshipType, Node, Node, double): " + tail); if (null == head) throw new IllegalArgumentException("Illegal 'head' argument in Problem.add(RelationshipType, Node, Node, double): " + head); if (prob < 0.0 || prob > 1.0) throw new IllegalArgumentException("Illegal 'prob' argument in Problem.add(RelationshipType, Node, Node, double): " + prob); Relationship result = null; try (Transaction tx = graph.beginTx()) { result = tail.createRelationshipTo(head, type); result.setProperty("prob", prob); count += 1; tx.success(); } return result; }
public double traverse() { if (null != root && value < 0.0) { try (Transaction ignore = graph.beginTx()) { value = 0.0; PathFinder<Path> finder = GraphAlgoFactory.allPaths( PathExpanders.forTypesAndDirections(RelType.LO_CHILD, Direction.OUTGOING, RelType.HI_CHILD, Direction.OUTGOING), max); Iterable<Path> paths = finder.findAllPaths(root, one); for (Path path : paths) { double current = 1.0; for (Relationship relationship : path.relationships()) { current *= (double) relationship.getProperty(PROB, 1.0); } value += current; } } } return value; }
@Override public Graph execute(Map<String, List<String>> parameters, Integer limit) { String vertexQuery = prepareGetVertexQuery(parameters, limit); Result result = (Result) currentStorage.executeQuery(vertexQuery); Iterator<Node> nodeSet = result.columnAs(VERTEX_ALIAS); Node node; if(nodeSet.hasNext()) { // starting point can only be one vertex node = nodeSet.next(); } else return null; Iterable<Relationship> relationships = node.getRelationships(Direction.INCOMING); Graph children = new Graph(); for(Relationship relationship: relationships) { children.putVertex(convertNodeToVertex(relationship.getEndNode())); } return children; }
protected Set<AbstractEdge> prepareEdgeSetFromNeo4jResult(String query) { Set<AbstractEdge> edgeSet = new HashSet<>(); try { Result result = (Result) currentStorage.executeQuery(query); Iterator<Relationship> relationshipSet = result.columnAs(EDGE_ALIAS); AbstractEdge edge; while(relationshipSet.hasNext()) { edge = convertRelationshipToEdge(relationshipSet.next()); if(!edge.isEmpty()) edgeSet.add(edge); } } catch (Exception ex) { Logger.getLogger(Neo4j.class.getName()).log(Level.SEVERE, "Edge set querying unsuccessful!", ex); } return edgeSet; }
@Override public Graph execute(Map<String, List<String>> parameters, Integer limit) { String vertexQuery = prepareGetVertexQuery(parameters, limit); Result result = (Result) currentStorage.executeQuery(vertexQuery); Iterator<Node> nodeSet = result.columnAs(VERTEX_ALIAS); Node node; if(nodeSet.hasNext()) { // starting point can only be one vertex node = nodeSet.next(); } else return null; Iterable<Relationship> relationships = node.getRelationships(Direction.OUTGOING); Graph parents = new Graph(); for(Relationship relationship: relationships) { parents.putVertex(convertNodeToVertex(relationship.getEndNode())); } return parents; }
/** * This function inserts the given edge into the underlying storage(s) and * updates the cache(s) accordingly. * * @param incomingEdge edge to insert into the storage * @return returns true if the insertion is successful. Insertion is considered * not successful if the edge is already present in the storage. */ @Override public boolean putEdge(AbstractEdge incomingEdge) { String hashCode = incomingEdge.bigHashCode(); if (Cache.isPresent(hashCode)) return true; globalTxCheckin(); Node srcNode = graphDb.findNode(NodeTypes.VERTEX, PRIMARY_KEY, incomingEdge.getChildVertex().bigHashCode()); Node dstNode = graphDb.findNode(NodeTypes.VERTEX, PRIMARY_KEY, incomingEdge.getParentVertex().bigHashCode()); Relationship newEdge = srcNode.createRelationshipTo(dstNode, RelationshipTypes.EDGE); newEdge.setProperty(PRIMARY_KEY, hashCode); for (Map.Entry<String, String> currentEntry : incomingEdge.getAnnotations().entrySet()) { String key = currentEntry.getKey(); String value = currentEntry.getValue(); newEdge.setProperty(key, value); } return true; }
/** * This function finds the children of a given vertex. * A child is defined as a vertex which is the source of a * direct edge between itself and the given vertex. * * @param parentHash hash of the given vertex * @return returns graph object containing children of the given vertex OR NULL. */ @Deprecated @Override public Graph getChildren(String parentHash) { Graph children = null; try (Transaction tx = graphDb.beginTx()) { children = new Graph(); Node parentNode = graphDb.findNode(NodeTypes.VERTEX, PRIMARY_KEY, parentHash); for(Relationship relationship: parentNode.getRelationships(Direction.OUTGOING)) { children.putVertex(convertNodeToVertex(relationship.getEndNode())); } tx.success(); } return children; }
/** * This function finds the parents of a given vertex. * A parent is defined as a vertex which is the destination of a * direct edge between itself and the given vertex. * * @param childVertexHash hash of the given vertex * @return returns graph object containing parents of the given vertex OR NULL. */ @Deprecated @Override public Graph getParents(String childVertexHash) { Graph parents = null; try (Transaction tx = graphDb.beginTx()) { parents = new Graph(); Node childNode = graphDb.findNode(NodeTypes.VERTEX, PRIMARY_KEY, childVertexHash); for(Relationship relationship: childNode.getRelationships(Direction.INCOMING)) { parents.putVertex(convertNodeToVertex(relationship.getStartNode())); } tx.success(); } return parents; }
public void putSequences(final Catalog catalog) { try (Transaction tx = getDbService().beginTx()) { for (final Schema schema : catalog.getSchemas()) { for (final Sequence sequence : catalog.getSequences(schema)) { Node seqNode = dbService.createNode(DatabaseNodeType.SEQUENCE); seqNode.setProperty("fullName", sequence.getFullName()); seqNode.setProperty("increment", sequence.getIncrement()); seqNode.setProperty("lookupKey", sequence.getLookupKey()); seqNode.setProperty("maximumValue", sequence.getMaximumValue() + ""); seqNode.setProperty("minimumValue", sequence.getMinimumValue() + ""); seqNode.setProperty("name", sequence.getName()); if (sequence.getRemarks() != null) { seqNode.setProperty("remarks", sequence.getRemarks()); } seqNode.setProperty("isCycle", sequence.isCycle()); // Attach sequence to schema //dbService.findNode(DatabaseNodeType.SCHEMA, "fullName", schema.getFullName()); Relationship belongsToSchema = seqNode.createRelationshipTo(dbService.findNode(DatabaseNodeType.SCHEMA, "fullName", schema.getFullName()), SchemaRelationShips.BELONGS_TO_SCHEMA); // attach the sequence to a column (if applicable) } } tx.success(); } }
/** * Rank the destination nodes of the outgoing edges of the given node. * @param n the source node of the destination nodes to be ranked. */ private void rankDest(Node n) { int baseSource = (int) n.getProperty(BASE_DIST.name()) + (int) n.getProperty(Scores.SEQ_LENGTH.name()); int rankSource = (int) n.getProperty(RANK.name()) + 1; for (Relationship r : n.getRelationships(RelTypes.NEXT, Direction.OUTGOING)) { Node dest = r.getEndNode(); if ((int) dest.getProperty(BASE_DIST.name()) < baseSource) { dest.setProperty(BASE_DIST.name(), baseSource); } if ((int) dest.getProperty(RANK.name()) < rankSource) { dest.setProperty(RANK.name(), rankSource); } } }