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 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; }
public void stamp() { if (null != graph && null != root) { try (Transaction tx = graph.beginTx()) { // Traverser traverser = // graph.traversalDescription().relationships(RelTypes.LINKS, // Direction.OUTGOING) // .evaluator(Evaluators.excludeStartPosition()).traverse(root); // for (Path path : traverser) { PathFinder<Path> finder = GraphAlgoFactory.allPaths(// PathExpanders.forTypeAndDirection(RelTypes.LINKS, Direction.OUTGOING), 4); // 4 // is // the // number // of // nodes // + // 1 Iterable<Path> paths = finder.findAllPaths(root, done); 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:%d,%.2f]-> ", RelTypes.LINKS, relationship.getProperty(ID), relationship.getProperty(PROB, 1.0)); System.out.format("(%s)", relationship.getEndNode().getProperty(ID)); } System.out.println(); // System.out.println(Paths.defaultPathToString(path)); } tx.success(); } } }
public double correlate(Node source, Node target, RelationshipType type, Direction dir, Object... more) { if (null == source) throw new IllegalArgumentException("Illegal 'source' argument in Problem.correlate(Node, Node, RelationshipType, Direction, Object...): " + source); if (null == target) throw new IllegalArgumentException("Illegal 'target' argument in Problem.correlate(Node, Node, RelationshipType, Direction, Object...): " + target); if (null == type) throw new IllegalArgumentException("Illegal 'type' argument in Problem.correlate(Node, Node, RelationshipType, Direction, Object...): " + type); if (null == dir) throw new IllegalArgumentException("Illegal 'dir' argument in Problem.correlate(Node, Node, RelationshipType, Direction, Object...): " + dir); if (source.equals(target)) return 1.0; try (Transaction ignore = graph.beginTx()) { PathExpander<Object> expander = null; if (null == more || 0 == more.length) expander = PathExpanders.forTypeAndDirection(type, dir); else { RelationshipType t = (RelationshipType) more[0]; Direction d = (Direction) more[1]; expander = PathExpanders.forTypesAndDirections(type, dir, t, d, Arrays.copyOfRange(more, 2, more.length)); } PathFinder<Path> finder = GraphAlgoFactory.allPaths(expander, 1 + count); Iterable<Path> paths = finder.findAllPaths(source, target); Set<Relationship> relationships = new HashSet<>(); Set<Set<Relationship>> expression = new HashSet<>(); for (Path path : paths) { Set<Relationship> item = new HashSet<>(); for (Relationship relationship : path.relationships()) { relationships.add(relationship); item.add(relationship); } expression.add(item); } BDD bdd = new BDD(expression, relationships); bdd.dump("bdd.gv"); return bdd.traverse(); } }
public Integer shortestDistance( Node source, Node target, Integer depth, String type, String direction ) { PathFinder<org.neo4j.graphdb.Path> finder; if ( type.equals("go") ) { // The relationships we will follow RelationshipType isa = DynamicRelationshipType.withName( "is_a" ); RelationshipType partof = DynamicRelationshipType.withName( "part_of" ); if ( direction.equals( "direction" ) ) { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypesAndDirections( isa, Direction.OUTGOING, partof, Direction.OUTGOING ), depth ); } else { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypesAndDirections( isa, Direction.BOTH, partof, Direction.BOTH ), depth ); } } else { // The relationships we will follow RelationshipType parent = DynamicRelationshipType.withName( "has_parent" ); if ( direction.equals( "direction" ) ) { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypeAndDirection( parent, Direction.OUTGOING ), depth ); } else { finder = GraphAlgoFactory.shortestPath( PathExpanders.forType( parent ), depth ); } } Iterable<org.neo4j.graphdb.Path> ListPaths = finder.findAllPaths( source, target ); Iterator<org.neo4j.graphdb.Path> itr = ListPaths.iterator(); while ( itr.hasNext() ) { Integer hoplength = itr.next().length(); if ( hoplength < depth ) { depth = hoplength; } } return depth; }
public ArrayList<Long> shortestPathNodes( Node source, Node target, Integer depth, String type, String direction ) { ArrayList<Long> pathNodes = new ArrayList<Long>(); PathFinder<org.neo4j.graphdb.Path> finder; BioRelationHelper helper = new BioRelationHelper(); if ( type.equals("go") ) { // The relationships we will follow RelationshipType isa = DynamicRelationshipType.withName( "is_a" ); RelationshipType partof = DynamicRelationshipType.withName( "part_of" ); if ( direction.equals( "direction" ) ) { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypesAndDirections( isa, Direction.OUTGOING, partof, Direction.OUTGOING ), depth ); } else { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypesAndDirections( isa, Direction.BOTH, partof, Direction.BOTH ), depth ); } } else { // The relationships we will follow RelationshipType parent = DynamicRelationshipType.withName( "has_parent" ); if ( direction.equals( "direction" ) ) { finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypeAndDirection( parent, Direction.OUTGOING ), depth ); } else { finder = GraphAlgoFactory.shortestPath( PathExpanders.forType( parent ), depth ); } } Iterable<org.neo4j.graphdb.Path> ListPaths = finder.findAllPaths( source, target ); Iterator<org.neo4j.graphdb.Path> itr = ListPaths.iterator(); while ( itr.hasNext() ) { org.neo4j.graphdb.Path nodePath = itr.next(); Integer hoplength = nodePath.length(); if ( hoplength < depth ) { depth = hoplength; pathNodes.clear(); // Clear arrayList Iterable<Node> ListNodes = nodePath.nodes(); pathNodes = helper.nodes2Array( ListNodes ); } } return pathNodes; }