[Neo] Neo Template API
Mattias Persson
mattias at neotechnology.com
Wed Sep 24 11:34:13 CEST 2008
Hi Michael,
Great utilities. It'll be interesting to try and use them as soon as
possible in a project to see what they can do.
Over time I've collected some various utilities in neo-utils. They
aren't very organized :) but there's stuff in there which I see myself
writing over and over again, f.ex:
o If you'd like to have a "sub reference" node to the neo reference
node, f.ex. a node to connect all users to:
NeoUtil neoUtil = new NeoUtil( neo );
Node usersRefNode = neoUtil.getOrCreateSubReferenceNode( MyRelTypes.USERS );
o Simple methods which wraps a single node/relationship
has/get/set/removeProperty in a transaction.
public String methodWhichIsntInATransactionRightNow()
{
return ( String ) neoUtil.getProperty( "myKey" );
}
o Various handling of node/relationship array property values.
List<Object> values = neoUtil.neoPropertyAsList( node.getProperty(
"keyWithOrWithoutArrayValue" ) );
values.add( "Mattias" );
node.setProperty( "keyWithOrWithoutArrayValue", values );
o Expose a node as a Collection (1 to *) or a Link (1 to 0..1)
public class User extends NodeWrapperImpl
....
Collection<User> users = new NodeWrapperRelationshipSet<User>(
neo, usersRefNode, MyRelTypes.USERS, User.class );
for ( User user : users ) ...
users.add( ... );
users.remove( ... );
users.clear();
a.s.o.
Feel free to try out neo-utils as well and give feedback. I should
definately document that component more though.
--
Mattias Persson
Neo Technology
mattias at neotechnology.com
2008/9/24 Michael Hunger <neo at jexp.de>:
> Hi folks,
>
> after a cool geek cruise with Emil and other. I added my contribution to the svn.
> You can find it under neo-template-api.
>
> It's an IoC, ResourceManagement API (like Spring Templates) which executes the callback within a neo transaction.
>
> I also added wrapper for the NeoService for just having the Node stuff available and a Status interface for marking the
> transaction for rollback.
>
> Other stuff I did was creating a GraphDescription class which can be used programmatically and with property files to
> setup test data (dot syntax files are next).
>
> The Traversal class has a fluent API for specifiying what you want.
>
> some examples:
> final NeoTemplate template = new NeoTemplate(neo);
> template.execute(new NeoCallback()
> {
> public void neo(final Status status, final Graph graph) throws Exception
> {
> Node refNode = graph.getReferenceNode();
> Node node = graph.createNode(_("name", "Test"), _("size", 100));
> refNode.createRelationshipTo(node, HAS);
>
> final Relationship toTestNode = refNode.getSingleRelationship(HAS, Direction.OUTGOING);
> final Node nodeByRelationship = toTestNode.getEndNode();
> assertEquals("Test", nodeByRelationship.getProperty("name"));
> assertEquals(100, nodeByRelationship.getProperty("size"));
> }
> });
> template.execute(new NeoCallback()
> {
> public void neo(final Status status, final Graph graph) throws Exception
> {
> Node refNode = graph.getReferenceNode();
> final Relationship toTestNode = refNode.getSingleRelationship(HAS, Direction.OUTGOING);
> final Node nodeByRelationship = toTestNode.getEndNode();
> assertEquals("Test", nodeByRelationship.getProperty("name"));
> assertEquals(100, nodeByRelationship.getProperty("size"));
> }
> });
>
>
> some traversal fluent api possibilities:
> final Traversal traversal = Traversal.walk().breadthFirst().depthFirst()
> .stopOn(StopEvaluator.DEPTH_ONE).first().all()
> .incoming(HAS).outgoing(HAS).twoway(HAS);
>
> use just graph.traverse(traversal,[startNode],[Mapper])
> like in:
> assertEquals("all members", asList(names),
> graph.traverse(graph.getReferenceNode(), traversal,
> new Mapper<Node, String>()
> {
> public String map(final Node node)
> {
> return (String) node.getProperty("name", "");
> }
> }));
>
>
>
> template.execute(new NeoCallback()
> {
> public void neo(final Status status, final Graph graph) throws Exception
> {
> final GraphDescription heaven = new GraphDescription();
> heaven.add("adam", "age", 1);
> heaven.add("eve", "age", 0);
> heaven.relate("adam", HAS, "eve");
> graph.load(heaven);
>
> checkHeaven(graph);
> }
> });
>
>
> Have fun
>
> Michael
>
> --
> Michael Hunger
> Independent Consultant
>
> Web: http://www.jexp.de
> Email: michael.hunger at jexp.de
>
> Enthusiastic Evangelist for Better Software Development
>
> Don't stop where you are: http://creating.passionate-developers.org
> We support Software Engineering Radio (www.se-radio.net)
> _______________________________________________
> Neo mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>
More information about the User
mailing list