Search in sources :

Example 1 with TypeResolver

use of com.tinkerpop.frames.modules.TypeResolver in project frames by tinkerpop.

the class FramedGraph method frame.

/**
 * A helper method for framing an edge. Note that all framed edges implement
 * {@link EdgeFrame} to allow access to the underlying element
 *
 * @param edge
 *            the edge to frame
 * @param direction
 *            the direction of the edges
 * @param kind
 *            the default annotated interface to frame the edges as
 * @param <F>
 *            the default type of the annotated interface
 * @return a proxy objects backed by an edge and interpreted from the
 *         perspective of the annotate interface or null if the edge paramenter was null
 *
 * @deprecated Use {@link #frame(Edge, Class)}, in combination with {@link InVertex} and {@link OutVertex}.
 */
public <F> F frame(final Edge edge, final Direction direction, final Class<F> kind) {
    if (edge == null) {
        return null;
    }
    Collection<Class<?>> resolvedTypes = new HashSet<Class<?>>();
    resolvedTypes.add(EdgeFrame.class);
    resolvedTypes.add(kind);
    for (TypeResolver typeResolver : config.getTypeResolvers()) {
        resolvedTypes.addAll(Arrays.asList(typeResolver.resolveTypes(edge, kind)));
    }
    return (F) Proxy.newProxyInstance(config.getFrameClassLoaderResolver().resolveClassLoader(kind), resolvedTypes.toArray(new Class[resolvedTypes.size()]), new FramedElement(this, edge, direction));
}
Also used : TypeResolver(com.tinkerpop.frames.modules.TypeResolver) HashSet(java.util.HashSet)

Example 2 with TypeResolver

use of com.tinkerpop.frames.modules.TypeResolver in project frames by tinkerpop.

the class FramedGraph method frame.

/**
 * A helper method for framing a vertex. Note that all framed vertices
 * implement {@link VertexFrame} to allow access to the underlying element
 *
 * @param vertex
 *            the vertex to frame
 * @param kind
 *            the default annotated interface to frame the vertex as
 * @param <F>
 *            the default type of the annotated interface
 * @return a proxy objects backed by a vertex and interpreted from the
 *         perspective of the annotate interface or null if the vertex parameter was null
 */
public <F> F frame(final Vertex vertex, final Class<F> kind) {
    if (vertex == null) {
        return null;
    }
    Collection<Class<?>> resolvedTypes = new HashSet<Class<?>>();
    resolvedTypes.add(VertexFrame.class);
    resolvedTypes.add(kind);
    for (TypeResolver typeResolver : config.getTypeResolvers()) {
        resolvedTypes.addAll(Arrays.asList(typeResolver.resolveTypes(vertex, kind)));
    }
    return (F) Proxy.newProxyInstance(config.getFrameClassLoaderResolver().resolveClassLoader(kind), resolvedTypes.toArray(new Class[resolvedTypes.size()]), new FramedElement(this, vertex));
}
Also used : TypeResolver(com.tinkerpop.frames.modules.TypeResolver) HashSet(java.util.HashSet)

Example 3 with TypeResolver

use of com.tinkerpop.frames.modules.TypeResolver in project frames by tinkerpop.

the class TypeResolverTest method testExtendedTypes.

@Test
public void testExtendedTypes() {
    Graph graph = TinkerGraphFactory.createTinkerGraph();
    FramedGraphFactory factory = new FramedGraphFactory(new AbstractModule() {

        @Override
        public void doConfigure(FramedGraphConfiguration config) {
            config.addTypeResolver(new TypeResolver() {

                @Override
                public Class<?>[] resolveTypes(Edge e, Class<?> defaultType) {
                    return new Class[0];
                }

                @Override
                public Class<?>[] resolveTypes(Vertex v, Class<?> defaultType) {
                    return new Class[] { ExtendedPerson.class };
                }
            });
        }
    });
    framedGraph = factory.create(graph);
    Person marko = framedGraph.getVertex(1, Person.class);
    Assert.assertTrue(marko instanceof ExtendedPerson);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TransactionalGraph(com.tinkerpop.blueprints.TransactionalGraph) FramedGraph(com.tinkerpop.frames.FramedGraph) Graph(com.tinkerpop.blueprints.Graph) FramedGraphFactory(com.tinkerpop.frames.FramedGraphFactory) TypeResolver(com.tinkerpop.frames.modules.TypeResolver) Edge(com.tinkerpop.blueprints.Edge) Person(com.tinkerpop.frames.domain.classes.Person) FramedGraphConfiguration(com.tinkerpop.frames.FramedGraphConfiguration) AbstractModule(com.tinkerpop.frames.modules.AbstractModule) Test(org.junit.Test)

Example 4 with TypeResolver

use of com.tinkerpop.frames.modules.TypeResolver in project frames by tinkerpop.

the class TypeResolverTest method testAdditionalTypes.

@Test
public void testAdditionalTypes() {
    Graph graph = TinkerGraphFactory.createTinkerGraph();
    FramedGraphFactory factory = new FramedGraphFactory(new AbstractModule() {

        @Override
        public void doConfigure(FramedGraphConfiguration config) {
            config.addTypeResolver(new TypeResolver() {

                @Override
                public Class<?>[] resolveTypes(Edge e, Class<?> defaultType) {
                    return new Class[] { AdditionalEdge.class };
                }

                @Override
                public Class<?>[] resolveTypes(Vertex v, Class<?> defaultType) {
                    return new Class[] { AdditionalVertex.class };
                }
            });
        }
    });
    framedGraph = factory.create(graph);
    Person marko = framedGraph.getVertex(1, Person.class);
    Assert.assertTrue(marko instanceof AdditionalVertex);
    Assert.assertFalse(marko instanceof AdditionalEdge);
    Knows knows = marko.getKnows().iterator().next();
    Assert.assertTrue(knows instanceof AdditionalEdge);
    Assert.assertFalse(knows instanceof AdditionalVertex);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TypeResolver(com.tinkerpop.frames.modules.TypeResolver) AbstractModule(com.tinkerpop.frames.modules.AbstractModule) TransactionalGraph(com.tinkerpop.blueprints.TransactionalGraph) FramedGraph(com.tinkerpop.frames.FramedGraph) Graph(com.tinkerpop.blueprints.Graph) FramedGraphFactory(com.tinkerpop.frames.FramedGraphFactory) Knows(com.tinkerpop.frames.domain.incidences.Knows) Edge(com.tinkerpop.blueprints.Edge) Person(com.tinkerpop.frames.domain.classes.Person) FramedGraphConfiguration(com.tinkerpop.frames.FramedGraphConfiguration) Test(org.junit.Test)

Aggregations

TypeResolver (com.tinkerpop.frames.modules.TypeResolver)4 Edge (com.tinkerpop.blueprints.Edge)2 Graph (com.tinkerpop.blueprints.Graph)2 TransactionalGraph (com.tinkerpop.blueprints.TransactionalGraph)2 Vertex (com.tinkerpop.blueprints.Vertex)2 FramedGraph (com.tinkerpop.frames.FramedGraph)2 FramedGraphConfiguration (com.tinkerpop.frames.FramedGraphConfiguration)2 FramedGraphFactory (com.tinkerpop.frames.FramedGraphFactory)2 Person (com.tinkerpop.frames.domain.classes.Person)2 AbstractModule (com.tinkerpop.frames.modules.AbstractModule)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 Knows (com.tinkerpop.frames.domain.incidences.Knows)1