Search in sources :

Example 21 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class DirtyManagerGraph method testLoopOfNew.

@Test
public void testLoopOfNew() {
    OrientGraph graph = new OrientGraph("memory:" + DirtyManagerGraph.class.getSimpleName());
    try {
        graph.createEdgeType("next");
        OrientVertex vertex = graph.addVertex(null);
        OrientVertex vertex1 = graph.addVertex(null);
        OrientVertex vertex2 = graph.addVertex(null);
        OrientVertex vertex3 = graph.addVertex(null);
        OrientEdge edge1 = (OrientEdge) vertex.addEdge("next", vertex1);
        OrientEdge edge2 = (OrientEdge) vertex1.addEdge("next", vertex2);
        OrientEdge edge3 = (OrientEdge) vertex2.addEdge("next", vertex3);
        OrientEdge edge4 = (OrientEdge) vertex3.addEdge("next", vertex);
        ODocument rec = vertex.getRecord();
        ODirtyManager manager = ORecordInternal.getDirtyManager(rec);
        List<OIdentifiable> pointed = manager.getPointed(vertex.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge1.getRecord()));
        assertTrue(pointed.contains(edge4.getRecord()));
        pointed = manager.getPointed(vertex1.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge1.getRecord()));
        assertTrue(pointed.contains(edge2.getRecord()));
        pointed = manager.getPointed(vertex2.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge2.getRecord()));
        assertTrue(pointed.contains(edge3.getRecord()));
        pointed = manager.getPointed(vertex3.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(edge3.getRecord()));
        assertTrue(pointed.contains(edge4.getRecord()));
        pointed = manager.getPointed(edge1.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex.getRecord()));
        assertTrue(pointed.contains(vertex1.getRecord()));
        pointed = manager.getPointed(edge2.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex1.getRecord()));
        assertTrue(pointed.contains(vertex2.getRecord()));
        pointed = manager.getPointed(edge3.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex2.getRecord()));
        assertTrue(pointed.contains(vertex3.getRecord()));
        pointed = manager.getPointed(edge4.getRecord());
        assertEquals(2, pointed.size());
        assertTrue(pointed.contains(vertex3.getRecord()));
        assertTrue(pointed.contains(vertex.getRecord()));
    } finally {
        graph.drop();
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ODirtyManager(com.orientechnologies.orient.core.record.impl.ODirtyManager) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 22 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OServerCommandGetGephi method generateGraphDbOutput.

protected void generateGraphDbOutput(final Iterable<OrientElement> iVertices, final OJSONWriter json) throws IOException {
    if (iVertices == null)
        return;
    // CREATE A SET TO SPEED UP SEARCHES ON VERTICES
    final Set<OrientVertex> vertexes = new HashSet<OrientVertex>();
    final Set<OrientEdge> edges = new HashSet<OrientEdge>();
    for (OrientElement id : iVertices) if (id instanceof OrientVertex)
        vertexes.add((OrientVertex) id);
    else
        edges.add((OrientEdge) id);
    for (OrientVertex vertex : vertexes) {
        json.resetAttributes();
        json.beginObject(0, false, null);
        json.beginObject(1, false, "an");
        json.beginObject(2, false, vertex.getIdentity());
        // ADD ALL THE EDGES
        for (Edge e : vertex.getEdges(Direction.BOTH)) edges.add((OrientEdge) e);
        // ADD ALL THE PROPERTIES
        for (String field : vertex.getPropertyKeys()) {
            final Object v = vertex.getProperty(field);
            if (v != null)
                json.writeAttribute(3, false, field, v);
        }
        json.endObject(2, false);
        json.endObject(1, false);
        json.endObject(0, false);
        json.newline();
    }
    for (OrientEdge edge : edges) {
        final ORID sourceVertex = (ORID) edge.getVertex(Direction.OUT).getId();
        final ORID targetVertex = (ORID) edge.getVertex(Direction.IN).getId();
        if (!vertexes.contains(sourceVertex) || !vertexes.contains(targetVertex))
            continue;
        json.resetAttributes();
        json.beginObject();
        json.beginObject(1, false, "ae");
        json.beginObject(2, false, edge.getId());
        json.writeAttribute(3, false, "directed", false);
        json.writeAttribute(3, false, "source", sourceVertex);
        json.writeAttribute(3, false, "target", targetVertex);
        for (String field : edge.getPropertyKeys()) {
            final Object v = edge.getProperty(field);
            if (v != null)
                json.writeAttribute(3, false, field, v);
        }
        json.endObject(2, false);
        json.endObject(1, false);
        json.endObject(0, false);
        json.newline();
    }
}
Also used : OrientElement(com.tinkerpop.blueprints.impls.orient.OrientElement) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) HashSet(java.util.HashSet)

Example 23 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionAstar method getNeighborEdges.

protected Set<OrientEdge> getNeighborEdges(final OrientVertex node) {
    context.incrementVariable("getNeighbors");
    final Set<OrientEdge> neighbors = new HashSet<OrientEdge>();
    if (node != null) {
        for (Edge v : node.getEdges(paramDirection, paramEdgeTypeNames)) {
            final OrientEdge ov = (OrientEdge) v;
            if (ov != null)
                neighbors.add(ov);
        }
    }
    return neighbors;
}
Also used : Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge)

Example 24 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionAstar method internalExecute.

private LinkedList<OrientVertex> internalExecute(final OCommandContext iContext, OrientBaseGraph graph) {
    OrientVertex start = paramSourceVertex;
    OrientVertex goal = paramDestinationVertex;
    open.add(start);
    // The cost of going from start to start is zero.
    gScore.put(start, 0.0);
    // For the first node, that value is completely heuristic.
    fScore.put(start, getHeuristicCost(start, null, goal));
    while (!open.isEmpty()) {
        OrientVertex current = open.poll();
        // we discussed about this feature in https://github.com/orientechnologies/orientdb/pull/6002#issuecomment-212492687
        if (paramEmptyIfMaxDepth == true && currentDepth >= paramMaxDepth) {
            // to ensure our result is empty
            route.clear();
            return getPath();
        }
        // if start and goal vertex is equal so return current path from  cameFrom hash map
        if (current.getIdentity().equals(goal.getIdentity()) || currentDepth >= paramMaxDepth) {
            while (current != null) {
                route.add(0, current);
                current = cameFrom.get(current);
            }
            return getPath();
        }
        closedSet.add(current);
        for (OrientEdge neighborEdge : getNeighborEdges(current)) {
            OrientVertex neighbor = getNeighbor(current, neighborEdge, graph);
            // Ignore the neighbor which is already evaluated.
            if (closedSet.contains(neighbor)) {
                continue;
            }
            // The distance from start to a neighbor
            double tentative_gScore = gScore.get(current) + getDistance(neighborEdge);
            boolean contains = open.contains(neighbor);
            if (!contains || tentative_gScore < gScore.get(neighbor)) {
                gScore.put(neighbor, tentative_gScore);
                fScore.put(neighbor, tentative_gScore + getHeuristicCost(neighbor, current, goal));
                if (contains) {
                    open.remove(neighbor);
                }
                open.offer(neighbor);
                cameFrom.put(neighbor, current);
            }
        }
        // Increment Depth Level
        currentDepth++;
    }
    return getPath();
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge)

Example 25 with OrientEdge

use of com.tinkerpop.blueprints.impls.orient.OrientEdge in project orientdb by orientechnologies.

the class OSQLFunctionOut method fetchFromIndex.

private Object fetchFromIndex(OrientBaseGraph graph, OIdentifiable iFrom, Iterable<OIdentifiable> iTo, String[] iEdgeTypes) {
    String edgeClassName = null;
    if (iEdgeTypes == null) {
        edgeClassName = "E";
    } else if (iEdgeTypes.length == 1) {
        edgeClassName = iEdgeTypes[0];
    } else {
        return null;
    }
    OClass edgeClass = graph.getRawGraph().getMetadata().getSchema().getClass(edgeClassName);
    if (edgeClass == null) {
        return null;
    }
    Set<OIndex<?>> indexes = edgeClass.getInvolvedIndexes("out", "in");
    if (indexes == null || indexes.size() == 0) {
        return null;
    }
    OIndex index = indexes.iterator().next();
    OMultiCollectionIterator<OrientVertex> result = new OMultiCollectionIterator<OrientVertex>();
    for (OIdentifiable to : iTo) {
        OCompositeKey key = new OCompositeKey(iFrom, to);
        Object indexResult = index.get(key);
        if (indexResult instanceof OIdentifiable) {
            indexResult = Collections.singleton(indexResult);
        }
        Set<OIdentifiable> identities = new HashSet<OIdentifiable>();
        for (OIdentifiable edge : ((Iterable<OrientEdge>) indexResult)) {
            identities.add((OIdentifiable) ((ODocument) edge.getRecord()).rawField("in"));
        }
        result.add(identities);
    }
    return result;
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)32 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 Test (org.junit.Test)17 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)6 Edge (com.tinkerpop.blueprints.Edge)5 HashSet (java.util.HashSet)5 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)4 HashMap (java.util.HashMap)4 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)3 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)2 OIndex (com.orientechnologies.orient.core.index.OIndex)2 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)2 ODirtyManager (com.orientechnologies.orient.core.record.impl.ODirtyManager)2