From 2590a73e786e95e52ab072b5daeb2f8d8d92fd12 Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com> Date: Sun, 2 Nov 2014 07:11:33 -0500 Subject: [PATCH] Added TypeResolver example. --- .../example/titangods/LocationExtended.java | 8 ++++ .../example/titangods/TitanGods.java | 2 +- .../example/titangods/FramesTest.java | 39 ++++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/syncleus/titangraph/example/titangods/LocationExtended.java diff --git a/src/main/java/com/syncleus/titangraph/example/titangods/LocationExtended.java b/src/main/java/com/syncleus/titangraph/example/titangods/LocationExtended.java new file mode 100644 index 0000000..cbc6e63 --- /dev/null +++ b/src/main/java/com/syncleus/titangraph/example/titangods/LocationExtended.java @@ -0,0 +1,8 @@ +package com.syncleus.titangraph.example.titangods; + +import com.tinkerpop.frames.Property; + +public interface LocationExtended extends Location { + @Property("other") + public String getOther(); +} diff --git a/src/main/java/com/syncleus/titangraph/example/titangods/TitanGods.java b/src/main/java/com/syncleus/titangraph/example/titangods/TitanGods.java index 4e13c1c..c8d7ff5 100644 --- a/src/main/java/com/syncleus/titangraph/example/titangods/TitanGods.java +++ b/src/main/java/com/syncleus/titangraph/example/titangods/TitanGods.java @@ -72,7 +72,7 @@ public class TitanGods { saturn.setProperty("type", "titan"); Vertex sky = graph.addVertex(null); - ElementHelper.setProperties(sky, "name", "sky", "type", "location"); + ElementHelper.setProperties(sky, "name", "sky", "type", "location", "other", "more useless info"); Vertex sea = graph.addVertex(null); ElementHelper.setProperties(sea, "name", "sea", "type", "location"); diff --git a/src/test/java/com/syncleus/titangraph/example/titangods/FramesTest.java b/src/test/java/com/syncleus/titangraph/example/titangods/FramesTest.java index 57c443b..412d0f3 100644 --- a/src/test/java/com/syncleus/titangraph/example/titangods/FramesTest.java +++ b/src/test/java/com/syncleus/titangraph/example/titangods/FramesTest.java @@ -1,9 +1,9 @@ package com.syncleus.titangraph.example.titangods; -import com.syncleus.titangraph.example.titangods.TitanGods; -import com.syncleus.titangraph.example.titangods.TitanGods.God; import com.thinkaurelius.titan.core.TitanGraph; +import com.tinkerpop.blueprints.*; import com.tinkerpop.frames.*; +import com.tinkerpop.frames.modules.*; import com.tinkerpop.frames.modules.gremlingroovy.GremlinGroovyModule; import com.tinkerpop.frames.modules.javahandler.JavaHandlerModule; import org.junit.*; @@ -33,4 +33,39 @@ public class FramesTest { Assert.assertEquals(god.getName(), "saturn"); Assert.assertTrue(god.isAgeEven()); } + + @Test + public void testFramesTypeResolver() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + + final TypeResolver resolver = new TypeResolver() { + + @Override + public Class<?>[] resolveTypes(final Vertex v, final Class<?> defaultType) { + if( v.getPropertyKeys().contains("other") ) { + return new Class<?>[]{LocationExtended.class}; + } + return new Class<?>[0]; + } + + @Override + public Class<?>[] resolveTypes(final Edge e, final Class<?> defaultType) { + return new Class<?>[0]; + } + }; + + final Module resolverModule = new AbstractModule() { + public void doConfigure(FramedGraphConfiguration config) { + config.addTypeResolver(resolver); + } + }; + + final FramedGraphFactory factory = new FramedGraphFactory(new JavaHandlerModule(), new GremlinGroovyModule(), resolverModule); + + final FramedGraph framedGraph = factory.create(godGraph); + + final Iterable<Location> skys = (Iterable<Location>) framedGraph.getVertices("name", "sky", Location.class); + final Location sky = skys.iterator().next(); + Assert.assertTrue(sky instanceof LocationExtended); + } } -- GitLab