Search in sources :

Example 1 with Adjacency

use of com.tinkerpop.frames.Adjacency in project org.openntf.domino by OpenNTF.

the class AbstractIncidenceHandler method countEdges.

@SuppressWarnings({ "rawtypes", "unused" })
private int countEdges(final Annotation annotation, final Vertex vertex) {
    int result = 0;
    Direction dir = Direction.BOTH;
    String label = "";
    if (annotation instanceof Adjacency) {
        dir = ((Adjacency) annotation).direction();
        label = ((Adjacency) annotation).label();
    } else if (annotation instanceof AdjacencyUnique) {
        dir = ((AdjacencyUnique) annotation).direction();
        label = ((AdjacencyUnique) annotation).label();
    } else if (annotation instanceof Incidence) {
        dir = ((Incidence) annotation).direction();
        label = ((Incidence) annotation).label();
    } else if (annotation instanceof IncidenceUnique) {
        dir = ((IncidenceUnique) annotation).direction();
        label = ((IncidenceUnique) annotation).label();
    }
    switch(dir) {
        case OUT:
            if (vertex instanceof DVertex) {
                result = ((DVertex) vertex).getOutEdgeCount(label);
            } else {
                Iterable<Edge> it = vertex.getEdges(Direction.OUT, label);
                if (it instanceof Collection) {
                    result = ((Collection) it).size();
                } else {
                    for (Edge e : it) {
                        result++;
                    }
                }
            }
            break;
        case IN:
            if (vertex instanceof DVertex) {
                result = ((DVertex) vertex).getInEdgeCount(label);
            } else {
                Iterable<Edge> it = vertex.getEdges(Direction.IN, label);
                if (it instanceof Collection) {
                    result = ((Collection) it).size();
                } else {
                    for (Edge e : it) {
                        result++;
                    }
                }
            }
            break;
        case BOTH:
            throw new UnsupportedOperationException("Direction.BOTH it not supported on 'add' or 'set' methods");
    }
    return result;
}
Also used : DVertex(org.openntf.domino.graph2.DVertex) Collection(java.util.Collection) Adjacency(com.tinkerpop.frames.Adjacency) Direction(com.tinkerpop.blueprints.Direction) Edge(com.tinkerpop.blueprints.Edge) Incidence(com.tinkerpop.frames.Incidence)

Example 2 with Adjacency

use of com.tinkerpop.frames.Adjacency in project org.openntf.domino by OpenNTF.

the class AbstractIncidenceHandler method processVertexAdjacency.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Object processVertexAdjacency(final Annotation annotation, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex vertex) {
    Edge resultEdge = null;
    Class<?> returnType = method.getReturnType();
    Direction dir = Direction.BOTH;
    String label = "";
    boolean unique = false;
    if (annotation instanceof Adjacency) {
        dir = ((Adjacency) annotation).direction();
        label = ((Adjacency) annotation).label();
    } else if (annotation instanceof AdjacencyUnique) {
        dir = ((AdjacencyUnique) annotation).direction();
        label = ((AdjacencyUnique) annotation).label();
        unique = true;
    } else if (annotation instanceof Incidence) {
        dir = ((Incidence) annotation).direction();
        label = ((Incidence) annotation).label();
    } else if (annotation instanceof IncidenceUnique) {
        dir = ((IncidenceUnique) annotation).direction();
        label = ((IncidenceUnique) annotation).label();
        unique = true;
    }
    if (ClassUtilities.isGetMethod(method)) {
        final FramedVertexList r = new FramedVertexList(framedGraph, vertex, vertex.getVertices(dir, label), ClassUtilities.getGenericClass(method));
        if (ClassUtilities.returnsIterable(method)) {
            return r;
        } else {
            return r.iterator().hasNext() ? r.iterator().next() : null;
        }
    } else if (ClassUtilities.isAddMethod(method)) {
        Vertex newVertex;
        Object returnValue = null;
        if (arguments == null) {
            // Use this method to get the vertex so that the vertex
            // initializer is called.
            returnValue = framedGraph.addVertex(null, returnType);
            newVertex = ((VertexFrame) returnValue).asVertex();
        } else {
            newVertex = ((VertexFrame) arguments[0]).asVertex();
        }
        if (unique) {
            resultEdge = findEdge(annotation, framedGraph, vertex, newVertex);
        }
        if (resultEdge == null) {
            String replicaid = null;
            if (framedGraph instanceof DFramedTransactionalGraph) {
                DElementStore store = ((DFramedTransactionalGraph) framedGraph).getElementStore(returnType);
                long rawkey = store.getStoreKey();
                replicaid = NoteCoordinate.Utils.getReplidFromLong(rawkey);
            }
            resultEdge = addEdge(annotation, framedGraph, vertex, newVertex, replicaid);
        }
        if (returnType.isPrimitive()) {
            return null;
        } else if (Edge.class.isAssignableFrom(returnType)) {
            return resultEdge;
        } else if (EdgeFrame.class.isAssignableFrom(returnType)) {
            // System.out.println("TEMP DEBUG about to wrap edge with id " + resultEdge.getId());
            Object result = framedGraph.frame(resultEdge, returnType);
            return result;
        } else {
            return returnValue;
        }
    } else if (ClassUtilities.isRemoveMethod(method)) {
        removeEdges(dir, label, vertex, ((VertexFrame) arguments[0]).asVertex(), framedGraph);
        return null;
    } else if (AnnotationUtilities.isCountMethod(method)) {
        return countEdges(annotation, vertex);
    } else if (ClassUtilities.isSetMethod(method)) {
        removeEdges(dir, label, vertex, null, framedGraph);
        if (ClassUtilities.acceptsIterable(method)) {
            for (Object o : (Iterable) arguments[0]) {
                Vertex v = ((VertexFrame) o).asVertex();
                addEdge(annotation, framedGraph, vertex, v, null);
            }
            return null;
        } else {
            if (null != arguments[0]) {
                Vertex newVertex = ((VertexFrame) arguments[0]).asVertex();
                addEdge(annotation, framedGraph, vertex, newVertex, null);
            }
            return null;
        }
    }
    return null;
}
Also used : DVertex(org.openntf.domino.graph2.DVertex) Vertex(com.tinkerpop.blueprints.Vertex) EdgeFrame(com.tinkerpop.frames.EdgeFrame) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) DElementStore(org.openntf.domino.graph2.DElementStore) Direction(com.tinkerpop.blueprints.Direction) Incidence(com.tinkerpop.frames.Incidence) VertexFrame(com.tinkerpop.frames.VertexFrame) Adjacency(com.tinkerpop.frames.Adjacency) Edge(com.tinkerpop.blueprints.Edge)

Example 3 with Adjacency

use of com.tinkerpop.frames.Adjacency in project org.openntf.domino by OpenNTF.

the class AbstractIncidenceHandler method addEdge.

@SuppressWarnings("rawtypes")
private Edge addEdge(final Annotation annotation, final FramedGraph framedGraph, final Vertex vertex, final Vertex newVertex, final String replicaid) {
    Edge result = null;
    Direction dir = Direction.BOTH;
    String label = "";
    boolean unique = false;
    if (annotation instanceof Adjacency) {
        dir = ((Adjacency) annotation).direction();
        label = ((Adjacency) annotation).label();
    } else if (annotation instanceof AdjacencyUnique) {
        dir = ((AdjacencyUnique) annotation).direction();
        label = ((AdjacencyUnique) annotation).label();
        unique = true;
    } else if (annotation instanceof Incidence) {
        dir = ((Incidence) annotation).direction();
        label = ((Incidence) annotation).label();
    } else if (annotation instanceof IncidenceUnique) {
        dir = ((IncidenceUnique) annotation).direction();
        label = ((IncidenceUnique) annotation).label();
        unique = true;
    }
    NoteCoordinate id = null;
    switch(dir) {
        case OUT:
            if (unique) {
                id = getForcedId(vertex, newVertex, label, replicaid);
            }
            result = framedGraph.addEdge(id, vertex, newVertex, label);
            break;
        case IN:
            if (unique) {
                id = getForcedId(newVertex, vertex, label, replicaid);
            }
            result = framedGraph.addEdge(id, newVertex, vertex, label);
            break;
        case BOTH:
            throw new UnsupportedOperationException("Direction.BOTH it not supported on 'add' or 'set' methods");
    }
    return result;
}
Also used : NoteCoordinate(org.openntf.domino.big.NoteCoordinate) Adjacency(com.tinkerpop.frames.Adjacency) Edge(com.tinkerpop.blueprints.Edge) Direction(com.tinkerpop.blueprints.Direction) Incidence(com.tinkerpop.frames.Incidence)

Example 4 with Adjacency

use of com.tinkerpop.frames.Adjacency in project org.openntf.domino by OpenNTF.

the class AbstractIncidenceHandler method findEdge.

@SuppressWarnings("rawtypes")
private Edge findEdge(final Annotation annotation, final FramedGraph framedGraph, final Vertex vertex, final Vertex newVertex) {
    Edge result = null;
    Direction dir = Direction.BOTH;
    String label = "";
    boolean unique = false;
    if (annotation instanceof Adjacency) {
        dir = ((Adjacency) annotation).direction();
        label = ((Adjacency) annotation).label();
    } else if (annotation instanceof AdjacencyUnique) {
        dir = ((AdjacencyUnique) annotation).direction();
        label = ((AdjacencyUnique) annotation).label();
        unique = true;
    } else if (annotation instanceof Incidence) {
        dir = ((Incidence) annotation).direction();
        label = ((Incidence) annotation).label();
    } else if (annotation instanceof IncidenceUnique) {
        dir = ((IncidenceUnique) annotation).direction();
        label = ((IncidenceUnique) annotation).label();
        unique = true;
    }
    switch(dir) {
        case OUT:
            result = ((DVertex) vertex).findOutEdge(newVertex, label, unique);
            break;
        case IN:
            result = ((DVertex) vertex).findInEdge(newVertex, label, unique);
            break;
        default:
            break;
    }
    return result;
}
Also used : Adjacency(com.tinkerpop.frames.Adjacency) Edge(com.tinkerpop.blueprints.Edge) Direction(com.tinkerpop.blueprints.Direction) Incidence(com.tinkerpop.frames.Incidence)

Example 5 with Adjacency

use of com.tinkerpop.frames.Adjacency in project org.openntf.domino by OpenNTF.

the class AbstractIncidenceHandler method processVertexIncidence.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Object processVertexIncidence(final Annotation annotation, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex vertex) {
    Direction dir = Direction.BOTH;
    String label = "";
    boolean unique = false;
    if (annotation instanceof Adjacency) {
        dir = ((Adjacency) annotation).direction();
        label = ((Adjacency) annotation).label();
    } else if (annotation instanceof AdjacencyUnique) {
        dir = ((AdjacencyUnique) annotation).direction();
        label = ((AdjacencyUnique) annotation).label();
        unique = true;
    } else if (annotation instanceof Incidence) {
        dir = ((Incidence) annotation).direction();
        label = ((Incidence) annotation).label();
    } else if (annotation instanceof IncidenceUnique) {
        dir = ((IncidenceUnique) annotation).direction();
        label = ((IncidenceUnique) annotation).label();
        unique = true;
    }
    if (ClassUtilities.isGetMethod(method)) {
        Class<?> returnType = method.getReturnType();
        if (Iterable.class.isAssignableFrom(returnType)) {
            DEdgeList edgeList = (DEdgeList) vertex.getEdges(dir, label);
            edgeList.setUnique(unique);
            return new FramedEdgeList(framedGraph, vertex, edgeList, ClassUtilities.getGenericClass(method));
        } else if (Edge.class.isAssignableFrom(returnType)) {
            Iterator<Edge> it = vertex.getEdges(dir, label).iterator();
            if (it.hasNext()) {
                return it.next();
            } else {
                return null;
            }
        } else {
            Iterator<Edge> it = vertex.getEdges(dir, label).iterator();
            if (it.hasNext()) {
                Edge e = it.next();
                return framedGraph.frame(e, returnType);
            } else {
                return null;
            }
        }
    } else if (AnnotationUtilities.isFindMethod(method)) {
        Vertex newVertex;
        Edge resultEdge = null;
        newVertex = ((VertexFrame) arguments[0]).asVertex();
        resultEdge = findEdge(annotation, framedGraph, vertex, newVertex);
        if (resultEdge != null) {
            return framedGraph.frame(resultEdge, method.getReturnType());
        }
    } else if (AnnotationUtilities.isCountMethod(method)) {
        return countEdges(annotation, vertex);
    } else if (ClassUtilities.isAddMethod(method)) {
        Vertex newVertex;
        Edge resultEdge = null;
        newVertex = ((VertexFrame) arguments[0]).asVertex();
        if (unique) {
            resultEdge = findEdge(annotation, framedGraph, vertex, newVertex);
        }
        if (resultEdge == null) {
            String replicaid = null;
            if (framedGraph instanceof DFramedTransactionalGraph) {
                DElementStore store = ((DFramedTransactionalGraph) framedGraph).getElementStore(method.getReturnType());
                long rawkey = store.getStoreKey();
                replicaid = NoteCoordinate.Utils.getReplidFromLong(rawkey);
            }
            resultEdge = addEdge(annotation, framedGraph, vertex, newVertex, replicaid);
        }
        return framedGraph.frame(resultEdge, method.getReturnType());
    } else if (ClassUtilities.isRemoveMethod(method)) {
        framedGraph.removeEdge(((EdgeFrame) arguments[0]).asEdge());
        return null;
    }
    return null;
}
Also used : DVertex(org.openntf.domino.graph2.DVertex) Vertex(com.tinkerpop.blueprints.Vertex) DFramedTransactionalGraph(org.openntf.domino.graph2.impl.DFramedTransactionalGraph) DElementStore(org.openntf.domino.graph2.DElementStore) Direction(com.tinkerpop.blueprints.Direction) Incidence(com.tinkerpop.frames.Incidence) VertexFrame(com.tinkerpop.frames.VertexFrame) Iterator(java.util.Iterator) Adjacency(com.tinkerpop.frames.Adjacency) DEdgeList(org.openntf.domino.graph2.DEdgeList) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

Direction (com.tinkerpop.blueprints.Direction)5 Edge (com.tinkerpop.blueprints.Edge)5 Adjacency (com.tinkerpop.frames.Adjacency)5 Incidence (com.tinkerpop.frames.Incidence)5 DVertex (org.openntf.domino.graph2.DVertex)3 Vertex (com.tinkerpop.blueprints.Vertex)2 VertexFrame (com.tinkerpop.frames.VertexFrame)2 DElementStore (org.openntf.domino.graph2.DElementStore)2 DFramedTransactionalGraph (org.openntf.domino.graph2.impl.DFramedTransactionalGraph)2 EdgeFrame (com.tinkerpop.frames.EdgeFrame)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 NoteCoordinate (org.openntf.domino.big.NoteCoordinate)1 DEdgeList (org.openntf.domino.graph2.DEdgeList)1