Hi, interesting application! I'll supply some example code of how it could look like.
Assume the neo4j is set up as follows: public enum MyRelationshipTypes implements RelationshipType { CONNECTION, } NeoService neo = new EmbeddedNeo( "neo" ); Some code: (1) Stations private Node createStation( String name ) { Node station = neo.createNode(); station.setProperty( "name", name ); return station; } Node stationA = createStation( "A" ); Node stationB = createStation( "B" ); Node stationC = createStation( "C" ); Node stationD = createStation( "D" ); Node stationE = createStation( "E" ); (2)-(4) Connections private Relationship connectStations( Node start, Node end, String pathName ) { Relationship connection = start.createRelationshipTo( end, MyRelationshipTypes.CONNECTION ); connection.setProperty( "path", pathName ); return connection; } // The departure/arrival properties should be set on the relationship itself // (how to handle multiple dep/arr of a connection?). They are here for // demonstration only. Relationship connectionAB = connectStations( stationA, stationB, "T1" ); connectionAB.setProperty( "departure", "11:00" ); connectionAB.setProperty( "arrival", "11:45" ); Relationship connectionBC = connectStations( stationB, stationC, "T1" ); connectionBC.setProperty( "departure", "11:50" ); connectionBC.setProperty( "arrival", "12:30" ); Relationship connectionCD1 = connectStations( stationC, stationD, "T1" ); connectionCD1.setProperty( "departure", "12:35" ); connectionCD1.setProperty( "arrival", "13:00" ); Relationship connectionCD2 = connectStations( stationC, stationD, "T2" ); connectionCD2.setProperty( "departure", "14:35" ); connectionCD2.setProperty( "arrival", "15:00" ); Relationship connectionAE = connectStations( stationA, stationE, "T2" ); connectionAE.setProperty( "departure", "13:15" ); connectionAE.setProperty( "arrival", "13:45" ); Relationship connectionEC = connectStations( stationE, stationC, "T2" ); connectionEC.setProperty( "departure", "13:50" ); connectionEC.setProperty( "arrival", "14:30" ); (5) for ( Relationship connection : stationA.getRelationships( MyRelationshipTypes.CONNECTION ) ) { System.out.println( "Train " + connection.getProperty( "path" ) + ": departure " + connection.getProperty( "departure" ) ); } (6) You'll have to do this with a traverser and custom evaluators (See f.ex. http://wiki.neo4j.org/content/Design_Guide#Searching_using_traversing) public class PathEvaluator implements StopEvaluator { private final String pathName; public PathEvaluator( String pathName ) { this.pathName = pathName; } public boolean isStopNode( TraversalPosition position ) { // Do we have an outgoing connection for the given path? for ( Relationship connection : position.currentNode().getRelationships( MyRelationshipTypes.CONNECTION, Direction.OUTGOING ) ) { if ( connection.getProperty( "path" ).equals( pathName ) ) { // We do, then this is NOT a stop node, i.e. go on return false; } } return true; } } for ( Node station : stationA.traverse( Traverser.Order.DEPTH_FIRST, new PathEvaluator( "T1" ), ReturnableEvaluator.ALL, MyRelationshipTypes.CONNECTION, Direction.OUTGOING ) ) { // Print information. You don't get the relationships from the traverser, but // you can either write a custom ReturnableEvaluator or find the wanted // relationship from the station node and get the one with the property "path"="T1". } In addition to this you'd probably want to index your station nodes (see http://components.neo4j.org/index-util/) and it's generally a good idea to connect all your station nodes to a "station reference node" so that you can iterate through all of them if you'd like to (http://wiki.neo4j.org/content/Design_Guide#Organizing_your_Nodespace). Then you'll have to wrap your operations in a transaction (http://wiki.neo4j.org/content/Design_Guide#Transaction_handling) and call neo.shutdown() before the JVM halts. This was just a very basic example of how your application could look like. The one thing that doesn't really work in a bigger example is the departure/arrival times which sits on the connections, it can be solved in many other ways. Don't hesitate to ask for help if you get stuck trying to come up with a good solution to that problem. Have fun with this application! / Mattias 2009/6/28, Bert Fitié <b...@analytag.com>: > > Hi, > > I'm new to the list and Neo4j (and my Java is rather rusty). I want to > investigate the easy-of-use of Neo4j for a (large) railway network > application. > > (1) Nodes. The nodes of this network are railway stations. > (2) Relationships. There is only one (directed) RelationshipType (ARC) > between stations. > (3) Paths. There are named paths (e.g. trains). A path P consists of a > sequence of arcs that all have a property named "P". The value of this > property could be a distance between two stations or a departure and > arrival time and will (usually) be different for each arc. > (4) Usage. The application will be used for the following navigation. > (4.1) The user selects a start station. > (4.2) A list will be generated of all outgoing paths from this > station. > (4.3) The user selects a path (e.g. a train) > (4.4) A sequence will be generated of all stations on this path > (4.5) The user selects a station from the path > (4.6) If this station is the final destination, the user is done, > otherwise, the user returns to (4.2) > > > Could you provide me with (guidelines for) Neo4j code for the > following example network: > > (1) The example network has 5 nodes (stations): A, B, C, D, E > (2) The example network has 5 arcs: AB, BC, CD, AE, EC > (3) The example network has 2 paths (trains): T1, T2 > (4) The example network has 6 arc (path) properties: > (4.1) AB: "T1" => "departure=11:00 arrival=11:45" > (4.2) BC: "T1" => "departure=11:50 arrival=12:30" > (4.3) CD: "T1" => "departure=12:35 arrival=13:00" > (4.4) CD: "T2" => "departure=14:35 arrival=15:00" > (4.5) AE: "T2" => "departure=13:15 arrival=13:45" > (4.6) EC: "T2" => "departure=13:50 arrival=14:30" > (5) Traversal 1: given station A, give a list of all outgoing paths > from this station. > Result: > * Station A > - Train T1: departure 11:00 > - Train T2: departure 13:15 > (6) Traversal 2: given an outgoing path from a station, give the > sequence of stations on this path. > Result in case of selecting T1: > * Train T1 > - Station A, departure 11:00 > - Station B: arrival 11:45, departure 11:50 > - Station C: arrival 12:30, departure 12:35 > - Station D: arrival 13:00 > Result in case of selecting T2: > * Train T2 > - Station A, departure 13:15 > - Station E: arrival 13:45, departure 13:50 > - Station C: arrival 14:30, departure 14:35 > - Station D: arrival 15:00 > > > Your help is very much appreciated! > > -- Bert Fitié > > _______________________________________________ > Neo mailing list > User@lists.neo4j.org > https://lists.neo4j.org/mailman/listinfo/user > -- Mattias Persson, [matt...@neotechnology.com] Neo Technology, www.neotechnology.com _______________________________________________ Neo mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user