[READ-ONLY] Mirror of https://github.com/Schniz/Neo4jGraphAvailabilityBlockerPlugin. Neo4j Graph Availability Blocker Plugin (Time-dependent graphs in Neo4j)
0

Configure Feed

Select the types of activity you want to include in your feed.

Pulled it down to 600ms for the heavy shit

Gal (Jun 23, 2014, 12:14 PM +0300) b2be1330 1b43eb77

+169 -122
+14 -14
src/main/java/org/neo4j/graphalgo/impl/shobydoby/GraphAvailabilityBlocker.java
··· 102 102 } 103 103 104 104 private double[] getAvailabilities(Relationship objRelation) { 105 - Object availabilities = objRelation.getProperty("availability"); 105 + Object availabilities = objRelation.getProperty("availability", new double[0]); 106 106 double[] result; 107 107 108 - if (availabilities instanceof int[]) 109 - { 110 - int[] arrToManipulate = (int[]) availabilities; 111 - double[] arr = new double[arrToManipulate.length]; 112 - for (int i = 0; i < arrToManipulate.length; i++) 113 - { 114 - arr[i] = (double) arrToManipulate[i]; 115 - } 116 - result = arr; 117 - } 118 - else 119 - { 108 + // if (availabilities instanceof int[]) 109 + // { 110 + // int[] arrToManipulate = (int[]) availabilities; 111 + // double[] arr = new double[arrToManipulate.length]; 112 + // for (int i = 0; i < arrToManipulate.length; i++) 113 + // { 114 + // arr[i] = (double) arrToManipulate[i]; 115 + // } 116 + // result = arr; 117 + // } 118 + // else 119 + // { 120 120 result = (double[]) availabilities; 121 - } 121 + // } 122 122 123 123 return result; 124 124 }
+46 -44
src/main/java/org/neo4j/graphalgo/impl/shobydoby/StatefulEdgeAvailabilityPathExpander.java
··· 20 20 21 21 package org.neo4j.graphalgo.impl.shobydoby; 22 22 23 + import org.neo4j.cypher.ExecutionResult; 24 + import org.neo4j.graphalgo.WeightedPath; 23 25 import org.neo4j.graphdb.Path; 24 26 import org.neo4j.graphdb.PathExpander; 25 27 import org.neo4j.graphdb.Relationship; 26 28 import org.neo4j.graphdb.RelationshipType; 27 29 import org.neo4j.graphdb.traversal.BranchState; 28 30 31 + import java.util.ArrayList; 29 32 import java.util.LinkedList; 30 33 import java.util.List; 31 34 ··· 47 50 @Override 48 51 public Iterable<Relationship> expand(Path path, BranchState<Double> state) { 49 52 50 - double currCost = 0; 53 + double currCost = state.getState(); 51 54 52 - if ( path.length() == 0 ) 55 + if ( path.length() != 0 ) 53 56 { 54 - currCost = state.getState(); 55 - } 56 - else 57 - { 58 - currCost = (state.getState()) + ((Number)(path.lastRelationship().getProperty( "distance" ))).doubleValue(); 59 - 57 + currCost += ((Number) path.lastRelationship().getProperty( "distance" )).doubleValue(); 60 58 state.setState( currCost ); 61 59 } 62 60 63 - List<Relationship> relationships = new LinkedList<Relationship>(); 61 + List<Relationship> relationships = new ArrayList<>(); 62 + Iterable<Relationship> relationshipIterable = path.endNode().getRelationships(Relations.NODE, Relations.NEXT); 64 63 65 - for (Relationship objRelation : path.endNode().getRelationships(Relations.NODE, Relations.NEXT)) { 66 - double[] availabilities = getAvailabilities(objRelation); 64 + for (Relationship relationship : relationshipIterable) { 65 + double[] availabilities = getAvailabilities(relationship); 66 + EdgeAvailability e = EdgeAvailability.fromDoubleArray(availabilities); 67 67 68 - int startIndex = 0; 69 - int endIndex = availabilities.length - 1; 70 - int greatestSmallerIndex = 0; 71 - 72 - if (availabilities.length > 0) { 73 - greatestSmallerIndex = binarySearch(startIndex, endIndex, availabilities, currCost); 74 - } 75 - 76 - if ((availabilities.length == 0) || 77 - (isLastElementInCouple(greatestSmallerIndex) && 78 - (isLastElementInArray(availabilities, greatestSmallerIndex) || 79 - isNextElementNotInterferingThisOne(currCost, availabilities[greatestSmallerIndex + 1])))) 68 + // int startIndex = 0; 69 + // int endIndex = availabilities.length - 1; 70 + // int greatestSmallerIndex = 0; 71 + // 72 + // if (availabilities.length > 0) { 73 + // greatestSmallerIndex = binarySearch(startIndex, endIndex, availabilities, currCost); 74 + // } 75 + // 76 + // if ((availabilities.length == 0) || 77 + // (isLastElementInCouple(greatestSmallerIndex) && 78 + // (isLastElementInArray(availabilities, greatestSmallerIndex) || 79 + // isNextElementNotInterferingThisOne(currCost, availabilities[greatestSmallerIndex + 1])))) 80 + if (e.isFree(new TimeSpan().setFrom(currCost).setTo(currCost + length))) 80 81 { 81 - relationships.add(objRelation); 82 + relationships.add(relationship); 82 83 } 83 84 } 84 85 ··· 87 88 88 89 private double[] getAvailabilities(Relationship objRelation) { 89 90 90 - if (!objRelation.hasProperty("availability")){ 91 - objRelation.setProperty("availability", new double[0]); 92 - } 91 + Object availabilities = objRelation.getProperty("availability", new double[0]); 92 + // 93 + // if (objRelation.hasProperty("availability")){ 94 + // availabilities = objRelation.getProperty("availability"); 95 + // } 93 96 94 - Object availabilities = objRelation.getProperty("availability"); 95 - double[] result; 97 + // double[] result; 96 98 97 - if (availabilities instanceof int[]) 98 - { 99 - int[] arrToManipulate = (int[]) availabilities; 100 - double[] arr = new double[arrToManipulate.length]; 101 - for (int i = 0; i < arrToManipulate.length; i++) 102 - { 103 - arr[i] = (double) arrToManipulate[i]; 104 - } 105 - result = arr; 106 - } 107 - else 108 - { 109 - result = (double[]) availabilities; 110 - } 99 + // if (availabilities instanceof int[]) 100 + // { 101 + // int[] arrToManipulate = (int[]) availabilities; 102 + // double[] arr = new double[arrToManipulate.length]; 103 + // for (int i = 0; i < arrToManipulate.length; i++) 104 + // { 105 + // arr[i] = (double) arrToManipulate[i]; 106 + // } 107 + // result = arr; 108 + // } 109 + // else 110 + // { 111 + // result = (double[]) availabilities; 112 + // } 111 113 112 - return result; 114 + return (double[]) availabilities; 113 115 } 114 116 115 117 private boolean isNextElementNotInterferingThisOne(double currCost, double i) {
+71 -25
src/test/java/org/neo4j/graphalgo/impl/shobydoby/EmbeddedTest.java
··· 3 3 import junit.framework.TestCase; 4 4 import org.apache.commons.lang.time.StopWatch; 5 5 import org.junit.Before; 6 + import org.neo4j.graphalgo.CommonEvaluators; 7 + import org.neo4j.graphalgo.GraphAlgoFactory; 6 8 import org.neo4j.graphalgo.WeightedPath; 9 + import org.neo4j.graphalgo.impl.path.Dijkstra; 7 10 import org.neo4j.graphdb.*; 11 + import org.neo4j.graphdb.factory.GraphDatabaseBuilder; 8 12 import org.neo4j.graphdb.factory.GraphDatabaseFactory; 9 - import org.neo4j.kernel.impl.core.TestNeo4j; 13 + import org.neo4j.graphdb.factory.GraphDatabaseSettings; 14 + import org.neo4j.graphdb.traversal.InitialBranchState; 15 + import org.neo4j.tooling.GlobalGraphOperations; 10 16 11 - import javax.management.relation.Relation; 12 17 import java.util.ArrayList; 13 18 import java.util.List; 14 19 ··· 24 29 @Before 25 30 public void setUp() throws Exception { 26 31 super.setUp(); 27 - db = new GraphDatabaseFactory().newEmbeddedDatabase("/tmp/neo4j-db"); 32 + db = new GraphDatabaseFactory() 33 + .newEmbeddedDatabaseBuilder("/tmp/neo4j-db") 34 + // .setConfig( GraphDatabaseSettings.relationshipstore_mapped_memory_size, "300M" ) 35 + // .setConfig( GraphDatabaseSettings.nodestore_mapped_memory_size, "300M" ) 36 + // .setConfig( GraphDatabaseSettings.nodestore_propertystore_mapped_memory_size, "300M") 37 + // .setConfig(GraphDatabaseSettings.cache_type, "strong") 38 + .newGraphDatabase(); 28 39 40 + try (Transaction tx = db.beginTx()) { 41 + Iterable<Node> allNodes = GlobalGraphOperations.at(db).getAllNodes(); 42 + for (Node node : allNodes) { 43 + for (Relationship r : node.getRelationships()) { 44 + Iterable<String> propertyKeys = r.getPropertyKeys(); 45 + for (String propertyKey : propertyKeys) { 46 + r.getProperty(propertyKey); 47 + } 48 + } 49 + 50 + for (String propertyKey : node.getPropertyKeys()) { 51 + node.getProperty(propertyKey); 52 + } 53 + } 54 + } 29 55 } 30 56 31 57 void printPath(Node startNode, Node endNode, WeightedPath path) ··· 85 111 86 112 public void testPerformance() throws Exception 87 113 { 88 - long min = Long.MAX_VALUE; 89 - long max = Long.MIN_VALUE; 114 + // Thread.sleep(10000); 90 115 91 116 try (Transaction tx = db.beginTx()) 92 117 { 118 + new Dijkstra(new StatefulEdgeAvailabilityPathExpander(2), new InitialBranchState.State<>(0.0, 0.0), CommonEvaluators.doubleCostEvaluator("distance")).findSinglePath(db.getNodeById(3681), db.getNodeById(13130)); 119 + System.out.println("YES"); 120 + 93 121 Node nodeA; 94 122 Node nodeB; 95 123 96 - nodeA = db.getNodeById(startNodeId); 97 - nodeB = db.getNodeById(endNodeId); 98 - 99 - for(int i = 0; i < 5; i++) 100 - { 101 - StopWatch stopWatch = new StopWatch(); 102 - stopWatch.start(); 103 - WeightedPath availablePath = new GraphAvailabilityBlocker(2).tryBlock(0.0, nodeA, nodeB); 104 - stopWatch.stop(); 105 - 106 - System.out.println(i + ": " + stopWatch.getTime() + "ms"); 107 - max = Math.max(max, stopWatch.getTime()); 108 - min = Math.min(min, stopWatch.getTime()); 109 - printPath(nodeA, nodeB, availablePath); 124 + for (double i = 0.0; i < 3.0; i += 1.0) { 125 + System.out.println(i); 126 + // findAndBlockAvailablePaths(7491, 89888, i); 127 + findAndBlockAvailablePaths(3681, 455030, i); 128 + findAndBlockAvailablePaths(3681, 13130, i); 129 + // findAndBlockAvailablePaths(startNodeId, endNodeId, i); 110 130 } 111 - 112 - 113 - System.out.println("Minimum: " + min + "ms, max: " + max + "ms"); 114 - 115 - assertTrue("minimum time suppose to be below 1000ms. got " + min + "ms", min < 1000); 116 - 117 131 tx.failure(); 118 132 } 133 + 134 + // System.out.println("click"); 135 + // Thread.sleep(10000); 136 + } 137 + 138 + private void findAndBlockAvailablePaths(long start, long end, double startRoadHour) { 139 + long min = Long.MAX_VALUE; 140 + long max = Long.MIN_VALUE; 141 + 142 + Node nodeA; 143 + Node nodeB; 144 + nodeA = db.getNodeById(start); 145 + nodeB = db.getNodeById(end); 146 + 147 + for(int i = 0; i < 5; i++) 148 + { 149 + StopWatch stopWatch = new StopWatch(); 150 + stopWatch.start(); 151 + // Dijkstra dijkstra = new Dijkstra(PathExpanders, InitialBranchState.NO_STATE, CommonEvaluators.doubleCostEvaluator("distance")); 152 + // dijkstra.findSinglePath(nodeA, nodeB); 153 + WeightedPath availablePath = new GraphAvailabilityBlocker(2).tryBlock(startRoadHour, nodeA, nodeB); 154 + stopWatch.stop(); 155 + 156 + min = Math.min(min, stopWatch.getTime()); 157 + max = Math.max(max, stopWatch.getTime()); 158 + 159 + System.out.println(i + ": " + stopWatch.getTime() + "ms. road exists? " + (null != availablePath)); 160 + // printPath(nodeA, nodeB, availablePath); 161 + } 162 + 163 + System.out.println("Minimum: " + min + "ms, max: " + max + "ms"); 164 + 119 165 } 120 166 }
+38 -39
src/test/java/org/neo4j/graphalgo/impl/shobydoby/StatefulEdgeAvailabilityPathExpanderTest.java
··· 21 21 package org.neo4j.graphalgo.impl.shobydoby; 22 22 23 23 import common.Neo4jAlgoTestCase; 24 - import common.SimpleGraphBuilder; 25 24 import org.junit.Assert; 26 25 import org.junit.Test; 27 26 import org.neo4j.graphalgo.CommonEvaluators; ··· 29 28 import org.neo4j.graphalgo.impl.path.Dijkstra; 30 29 import org.neo4j.graphdb.Node; 31 30 import org.neo4j.graphdb.PathExpander; 32 - import org.neo4j.graphdb.Relationship; 33 31 import org.neo4j.graphdb.traversal.InitialBranchState; 34 32 35 33 public class StatefulEdgeAvailabilityPathExpanderTest extends Neo4jAlgoTestCase { ··· 42 40 Node nodeD = graph.makeNode( "D" ); 43 41 Node nodeE = graph.makeNode( "E" ); 44 42 45 - graph.makeEdge("A", "B", "length", 1, "availability", new double[0]); 46 - graph.makeEdge("B", "C", "length", 3, "availability", new double[0]); 47 - graph.makeEdge("D", "C", "length", 1, "availability", new double[] { 1, 2 }); 48 - graph.makeEdge("E", "D", "length", 1, "availability", new double[0]); 49 - graph.makeEdge("A", "E", "length", 1, "availability", new double[0]); 50 - graph.makeEdge("E", "C", "length", 1, "availability", new double[]{2, 4}); 43 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[0]); 44 + graph.makeEdge("B", "C", "distance", 3, "availability", new double[0]); 45 + graph.makeEdge("D", "C", "distance", 1, "availability", new double[] { 1, 2 }); 46 + graph.makeEdge("E", "D", "distance", 1, "availability", new double[0]); 47 + graph.makeEdge("A", "E", "distance", 1, "availability", new double[0]); 48 + graph.makeEdge("E", "C", "distance", 1, "availability", new double[]{2, 4}); 51 49 52 50 PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(2); 53 51 54 52 Dijkstra algo = new Dijkstra( myPathExpander, 55 53 new InitialBranchState.State<>( 0d, 0d ), 56 - CommonEvaluators.doubleCostEvaluator("length") ); 54 + CommonEvaluators.doubleCostEvaluator("distance") ); 57 55 58 56 assertPath( algo.findSinglePath( nodeA, nodeC ), nodeA, nodeE, nodeD, nodeC ); 59 57 } ··· 67 65 Node nodeD = graph.makeNode( "D" ); 68 66 Node nodeE = graph.makeNode( "E" ); 69 67 70 - graph.makeEdge("A", "B", "length", 1, "availability", new double[0]); 71 - graph.makeEdge("B", "C", "length", 3, "availability", new double[0]); 72 - graph.makeEdge("D", "C", "length", 1, "availability", new double[] { 1, 3 }); 73 - graph.makeEdge("E", "D", "length", 1, "availability", new double[0]); 74 - graph.makeEdge("A", "E", "length", 1, "availability", new double[0]); 75 - graph.makeEdge("E", "C", "length", 1, "availability", new double[] { 2, 4 }); 68 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[0]); 69 + graph.makeEdge("B", "C", "distance", 3, "availability", new double[0]); 70 + graph.makeEdge("D", "C", "distance", 1, "availability", new double[] { 1, 3 }); 71 + graph.makeEdge("E", "D", "distance", 1, "availability", new double[0]); 72 + graph.makeEdge("A", "E", "distance", 1, "availability", new double[0]); 73 + graph.makeEdge("E", "C", "distance", 1, "availability", new double[] { 2, 4 }); 76 74 77 75 PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(2); 78 76 79 77 Dijkstra algo = new Dijkstra( myPathExpander, 80 78 new InitialBranchState.State<Double>( 0.0, 0.0 ), 81 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 79 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 82 80 83 81 assertPath( algo.findSinglePath( nodeA, nodeC ), nodeA, nodeB, nodeC ); 84 82 } ··· 91 89 Node nodeC = graph.makeNode( "C" ); 92 90 Node nodeD = graph.makeNode( "D" ); 93 91 94 - graph.makeEdge("A", "B", "length", 1, "availability", new double[0]); 95 - graph.makeEdge("D", "B", "length", 1, "availability", new double[] { 0, 2 }); 96 - graph.makeEdge("A", "C", "length", 1, "availability", new double[0]); 97 - graph.makeEdge("C", "D", "length", 2, "availability", new double[0]); 92 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[0]); 93 + graph.makeEdge("D", "B", "distance", 1, "availability", new double[] { 0, 2 }); 94 + graph.makeEdge("A", "C", "distance", 1, "availability", new double[0]); 95 + graph.makeEdge("C", "D", "distance", 2, "availability", new double[0]); 98 96 99 97 PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(2); 100 98 101 99 Dijkstra algo = new Dijkstra( myPathExpander, 102 100 new InitialBranchState.State<Double>( 0.0, 0.0 ), 103 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 101 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 104 102 105 103 assertPath( algo.findSinglePath( nodeA, nodeD ), nodeA, nodeC, nodeD ); 106 104 } ··· 113 111 Node nodeC = graph.makeNode( "C" ); 114 112 Node nodeD = graph.makeNode( "D" ); 115 113 116 - graph.makeEdge("A", "B", "length", 1, "availability", new double[0]); 117 - graph.makeEdge("D", "B", "length", 1, "availability", new double[] { 4, 5 }); 118 - graph.makeEdge("A", "C", "length", 1, "availability", new double[0]); 119 - graph.makeEdge("C", "D", "length", 2, "availability", new double[0]); 114 + 115 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[0]); 116 + graph.makeEdge("D", "B", "distance", 1, "availability", new double[] { 4, 5 }); 117 + graph.makeEdge("A", "C", "distance", 1, "availability", new double[0]); 118 + graph.makeEdge("C", "D", "distance", 2, "availability", new double[0]); 120 119 121 120 PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(2); 122 121 123 122 Dijkstra algo = new Dijkstra( myPathExpander, 124 123 new InitialBranchState.State<Double>( 0.0, 0.0 ), 125 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 124 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 126 125 127 126 assertPath( algo.findSinglePath( nodeA, nodeD ), nodeA, nodeB, nodeD ); 128 127 } ··· 135 134 Node nodeC = graph.makeNode( "C" ); 136 135 Node nodeD = graph.makeNode( "D" ); 137 136 138 - graph.makeEdge("A", "B", "length", 1, "availability", new double[0]); 139 - graph.makeEdge("D", "B", "length", 1, "availability", new double[] { 3, 5 }); 140 - graph.makeEdge("A", "C", "length", 1, "availability", new double[0]); 141 - graph.makeEdge("C", "D", "length", 2, "availability", new double[0]); 137 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[0]); 138 + graph.makeEdge("D", "B", "distance", 1, "availability", new double[] { 3, 5 }); 139 + graph.makeEdge("A", "C", "distance", 1, "availability", new double[0]); 140 + graph.makeEdge("C", "D", "distance", 2, "availability", new double[0]); 142 141 143 142 PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(2); 144 143 145 144 Dijkstra algo = new Dijkstra( myPathExpander, 146 145 new InitialBranchState.State<Double>( 0.0, 0.0 ), 147 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 146 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 148 147 149 148 assertPath( algo.findSinglePath( nodeA, nodeD ), nodeA, nodeC, nodeD ); 150 149 } ··· 156 155 Node nodeC = graph.makeNode( "C" ); 157 156 Node nodeD = graph.makeNode( "D" ); 158 157 159 - graph.makeEdge("A", "B", "length", 1, "availability", new double[] { 0, 1 }); 160 - graph.makeEdge("D", "B", "length", 1, "availability", new double[] { 0, 1, 5, 7 }); 161 - graph.makeEdge("A", "C", "length", 1, "availability", new double[] { 0, 1 }); 162 - graph.makeEdge("C", "D", "length", 2, "availability", new double[] { 0, 1 }); 158 + graph.makeEdge("A", "B", "distance", 1, "availability", new double[] { 0, 1 }); 159 + graph.makeEdge("D", "B", "distance", 1, "availability", new double[] { 0, 1, 5, 7 }); 160 + graph.makeEdge("A", "C", "distance", 1, "availability", new double[] { 0, 1 }); 161 + graph.makeEdge("C", "D", "distance", 2, "availability", new double[] { 0, 1 }); 163 162 164 - int trainLength = 2; 165 - PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(trainLength); 163 + int traindistance = 2; 164 + PathExpander<Double> myPathExpander = new StatefulEdgeAvailabilityPathExpander(traindistance); 166 165 167 166 Dijkstra algo = new Dijkstra( myPathExpander, 168 167 new InitialBranchState.State<Double>( 0.0, 0.0 ), 169 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 168 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 170 169 171 170 Assert.assertNull( "There should be no path", algo.findSinglePath( nodeA, nodeD ) ); 172 171 173 172 algo = new Dijkstra( myPathExpander, 174 173 new InitialBranchState.State<Double>( 2.0, 0.0 ), 175 - CommonEvaluators.doubleCostEvaluator( "length" ) ); 174 + CommonEvaluators.doubleCostEvaluator( "distance" ) ); 176 175 177 176 WeightedPath singlePath = algo.findSinglePath(nodeA, nodeD); 178 177 assertPath(singlePath, nodeA, nodeC, nodeD);