Here's the code directly in the mail, since filters makes it difficult to send attachments sometimes:
package com.windh.eqn.util; import org.neo4j.api.core.*; public class RailNetApp { public enum MyRelationshipTypes implements RelationshipType { CONNECTION, } public static void main( String[] args ) { NeoService neo = new EmbeddedNeo( "var/examples/railnet" ); Transaction tx = neo.beginTx(); try { // Stations Node stationAbc = createStation( neo, "Abc" ); Node stationBcd = createStation( neo, "Bcd" ); Node stationCde = createStation( neo, "Cde" ); Node stationDef = createStation( neo, "Def" ); Node stationEfg = createStation( neo, "Efg" ); // Connections Relationship abcBcdT1 = connectStations( stationAbc, stationBcd, "T1" ); abcBcdT1.setProperty( "departure", "11:00" ); abcBcdT1.setProperty( "arrival", "11:45" ); Relationship bcdCdeT1 = connectStations( stationBcd, stationCde, "T1" ); bcdCdeT1.setProperty( "departure", "11:50" ); bcdCdeT1.setProperty( "arrival", "12:30" ); Relationship cdeDefT1 = connectStations( stationCde, stationDef, "T1" ); cdeDefT1.setProperty( "departure", "12:35" ); cdeDefT1.setProperty( "arrival", "13:00" ); Relationship cdeDefT2 = connectStations( stationCde, stationDef, "T2" ); cdeDefT2.setProperty( "departure", "14:35" ); cdeDefT2.setProperty( "arrival", "15:00" ); Relationship abcEfgT2 = connectStations( stationAbc, stationEfg, "T2" ); abcEfgT2.setProperty( "departure", "13:15" ); abcEfgT2.setProperty( "arrival", "13:45" ); Relationship efgCdeT2 = connectStations( stationEfg, stationCde, "T2" ); efgCdeT2.setProperty( "departure", "13:50" ); efgCdeT2.setProperty( "arrival", "14:30" ); for ( Relationship connection : stationAbc .getRelationships( MyRelationshipTypes.CONNECTION ) ) { System.out.println( "Train " + connection.getProperty( "path-name" ) + ": departure " + connection.getProperty( "departure" ) ); } // If you'd the transaction to commit then enable this line // tx.success(); } finally { tx.finish(); neo.shutdown(); } } private static Node createStation( NeoService neo, String stationName ) { Node station = neo.createNode(); station.setProperty( "station-name", stationName ); return station; } private static Relationship connectStations( Node start, Node end, String pathName ) { Relationship connection = start.createRelationshipTo( end, MyRelationshipTypes.CONNECTION ); connection.setProperty( "path-name", pathName ); return connection; } } 2009/6/30 Mattias Persson <matt...@neotechnology.com>: > Yep, those errors are easily fixed. Your private methods needs to be > static as well. I fixed those errors (see attachment). > > 2009/6/30 Bert Fitié <b...@analytag.com>: >> Hi, Mattias >> >> I started off with the following partial code but got three types of >> errors: >> >> >> >> import org.neo4j.api.core.*; >> >> public class RailNetApp >> { >> >> public enum MyRelationshipTypes implements RelationshipType >> { >> CONNECTION, >> } >> >> public static void main(String[] args) >> { >> NeoService neo = new EmbeddedNeo("var/examples/railnet"); >> >> Transaction tx = neo.beginTx(); >> try >> { >> // Stations >> Node stationAbc = createStation("Abc"); >> Node stationBcd = createStation("Bcd"); >> Node stationCde = createStation("Cde"); >> Node stationDef = createStation("Def"); >> Node stationEfg = createStation("Efg"); >> >> // Connections >> Relationship abcBcdT1 = connectStations(stationAbc, stationBcd, >> "T1"); >> abcBcdT1.setProperty("departure", "11:00"); >> abcBcdT1.setProperty("arrival", "11:45"); >> >> Relationship bcdCdeT1 = connectStations(stationBcd, stationCde, >> "T1"); >> bcdCdeT1.setProperty("departure", "11:50"); >> bcdCdeT1.setProperty("arrival", "12:30"); >> >> Relationship cdeDefT1 = connectStations(stationCde, stationDef, >> "T1"); >> cdeDefT1.setProperty("departure", "12:35"); >> cdeDefT1.setProperty("arrival", "13:00"); >> >> Relationship cdeDefT2 = connectStations(stationCde, stationDef, >> "T2"); >> cdeDefT2.setProperty("departure", "14:35"); >> cdeDefT2.setProperty("arrival", "15:00"); >> >> Relationship abcEfgT2 = connectStations(stationAbc, stationEfg, >> "T2"); >> abcEfgT2.setProperty("departure", "13:15"); >> abcEfgT2.setProperty("arrival", "13:45"); >> >> Relationship efgCdeT2 = connectStations(stationEfg, stationCde, >> "T2"); >> efgCdeT2.setProperty("departure", "13:50"); >> efgCdeT2.setProperty("arrival", "14:30"); >> >> for (Relationship connection : >> stationAbc.getRelationships(MyRelationshipTypes.CONNECTION)) >> { >> System.out.println("Train " + connection.getProperty("path- >> name") + >> ": departure " + connection.getProperty("departure")); >> } >> } >> finally >> { >> tx.finish(); >> neo.shutdown(); >> } >> } >> >> private Node createStation(String stationName) >> { >> Node station = neo.createNode(); >> station.setProperty("station-name", stationName); >> return station; >> } >> >> private Relationship connectStations(Node start, Node end, String >> pathName) >> { >> Relationship connection = start.createRelationshipTo(end, >> MyRelationshipTypes.CONNECTION); >> connection.setProperty( "path-name", pathName ); >> return connection; >> } >> } >> >> >> >> I got the following three types of errors >> >> (1) src/examples/RailNetApp.java:19: non-static method >> createStation(java.lang.String) cannot be referenced from a static >> context >> Node stationAbc = createStation("Abc"); >> >> (2) src/examples/RailNetApp.java:26: non-static method >> connectStations >> (org.neo4j.api.core.Node,org.neo4j.api.core.Node,java.lang.String) >> cannot be referenced from a static context >> Relationship abcBcdT1 = connectStations(stationAbc, stationBcd, >> "T1"); >> >> (3) src/examples/RailNetApp.java:65: cannot find symbol >> symbol : variable neo >> location: class RailNetApp >> Node station = neo.createNode(); >> >> >> >> Could you get me on the way again? (As I said earlier, my Java is >> rusty. And to make things worse, I'm more an enduser than a >> professional developer. If this doesn't discourage you, it certainly >> doesn't discourage me!) >> >> -- Bert >> >> >> On 29 jun 2009, at 10:22, Mattias Persson wrote: >> >>> 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 >>> >> >> _______________________________________________ >> Neo mailing list >> User@lists.neo4j.org >> https://lists.neo4j.org/mailman/listinfo/user >> > > > > -- > Mattias Persson, [matt...@neotechnology.com] > Neo Technology, www.neotechnology.com > -- Mattias Persson, [matt...@neotechnology.com] Neo Technology, www.neotechnology.com _______________________________________________ Neo mailing list User@lists.neo4j.org https://lists.neo4j.org/mailman/listinfo/user