Search in sources :

Example 1 with Neo4jVertex

use of org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex in project timbuctoo by HuygensING.

the class VertexBuilder method build.

@Override
public Tuple<Vertex, String> build(Graph graph, Consumer<RelationData> relationRequestor) {
    Vertex vertex;
    if (labels.size() == 1) {
        // If there is exactly one label, it is still a valid tinkerpop vertex and needs to be passed to the
        // addVertex method. Otherwise the vertex also gets the label "vertex" and hasLabel will stop working
        vertex = graph.addVertex(labels.get(0));
    } else {
        vertex = graph.addVertex();
    }
    try {
        if (vres == null) {
            vres = Lists.newArrayList("");
        }
        if (type == null) {
            type = "<type>";
        }
        vertex.property("types", objectMapper.writeValueAsString(Lists.newArrayList(vres.stream().map(vre -> vre + type).iterator())));
        if (timId != null) {
            vertex.property("tim_id", timId);
        }
        vertex.property("isLatest", isLatest);
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            vertex.property(entry.getKey(), entry.getValue());
        }
        if (labels.size() > 1) {
            for (String label : labels.subList(1, labels.size())) {
                ((Neo4jVertex) vertex).addLabel(label);
            }
        }
        relationList.forEach(relationDataBuilder -> {
            // can't be done earlier because vres might not be complete
            RelationData relationData = relationDataBuilder.build(vres);
            relationRequestor.accept(relationData);
        });
        return tuple(vertex, id);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) RelationDataBuilder.makeRelationData(nl.knaw.huygens.timbuctoo.util.RelationData.RelationDataBuilder.makeRelationData) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with Neo4jVertex

use of org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex in project timbuctoo by HuygensING.

the class LoadSaveVreTest method saveAddsVreCollectionsToTheVre.

@Test
public void saveAddsVreCollectionsToTheVre() {
    Vre vre = vre("VreName", "prefix").withCollection("prefixpersons").withCollection("prefixdocuments").build();
    Optional<Vertex> vreVertex = save(vre, initGraph()).stream().filter(x -> ((Neo4jVertex) x).labels().contains(Vre.DATABASE_LABEL)).findAny();
    if (!vreVertex.isPresent()) {
        throw new RuntimeException("No Vre vertex found!");
    } else {
        List<Vertex> edges = Lists.newArrayList(vreVertex.get().vertices(Direction.OUT, HAS_COLLECTION_RELATION_NAME));
        assertThat(edges, containsInAnyOrder(likeVertex().withLabel(Collection.DATABASE_LABEL).withProperty(Collection.COLLECTION_NAME_PROPERTY_NAME, "prefixpersons"), likeVertex().withLabel(Collection.DATABASE_LABEL).withProperty(Collection.COLLECTION_NAME_PROPERTY_NAME, "prefixdocuments")));
    }
}
Also used : TestGraphBuilder(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder) HAS_ARCHETYPE_RELATION_NAME(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection.HAS_ARCHETYPE_RELATION_NAME) Graph(org.apache.tinkerpop.gremlin.structure.Graph) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) HashMap(java.util.HashMap) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) VRE_NAME_PROPERTY_NAME(nl.knaw.huygens.timbuctoo.model.vre.Vre.VRE_NAME_PROPERTY_NAME) Lists(com.google.common.collect.Lists) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DataStoreOperations(nl.knaw.huygens.timbuctoo.core.DataStoreOperations) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Maps(com.google.common.collect.Maps) KEYWORD_TYPES_PROPERTY_NAME(nl.knaw.huygens.timbuctoo.model.vre.Vre.KEYWORD_TYPES_PROPERTY_NAME) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) HAS_COLLECTION_RELATION_NAME(nl.knaw.huygens.timbuctoo.model.vre.Vre.HAS_COLLECTION_RELATION_NAME) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) Optional(java.util.Optional) VreBuilder.vre(nl.knaw.huygens.timbuctoo.model.vre.VreBuilder.vre) Tuple.tuple(nl.knaw.huygens.timbuctoo.util.Tuple.tuple) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) Test(org.junit.Test)

Example 3 with Neo4jVertex

use of org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex in project timbuctoo by HuygensING.

the class VertexDuplicator method duplicateVertex.

public static Vertex duplicateVertex(GraphTraversalSource traversal, Vertex vertex, IndexHandler indexHandler) {
    Vertex duplicate = traversal.addV().next();
    for (Iterator<VertexProperty<Object>> properties = vertex.properties(); properties.hasNext(); ) {
        VertexProperty<Object> property = properties.next();
        duplicate.property(property.key(), property.value());
    }
    for (String label : ((Neo4jVertex) vertex).labels()) {
        ((Neo4jVertex) duplicate).addLabel(label);
    }
    moveIncomingEdges(vertex, duplicate, indexHandler);
    moveOutgoingEdges(vertex, duplicate, indexHandler);
    vertex.property(IS_LATEST, false);
    duplicate.property(IS_LATEST, true);
    vertex.addEdge(VERSION_OF, duplicate);
    return duplicate;
}
Also used : Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Aggregations

Neo4jVertex (org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 List (java.util.List)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 DataStoreOperations (nl.knaw.huygens.timbuctoo.core.DataStoreOperations)1 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)1 HAS_ARCHETYPE_RELATION_NAME (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection.HAS_ARCHETYPE_RELATION_NAME)1 Vre (nl.knaw.huygens.timbuctoo.model.vre.Vre)1 HAS_COLLECTION_RELATION_NAME (nl.knaw.huygens.timbuctoo.model.vre.Vre.HAS_COLLECTION_RELATION_NAME)1 KEYWORD_TYPES_PROPERTY_NAME (nl.knaw.huygens.timbuctoo.model.vre.Vre.KEYWORD_TYPES_PROPERTY_NAME)1 VRE_NAME_PROPERTY_NAME (nl.knaw.huygens.timbuctoo.model.vre.Vre.VRE_NAME_PROPERTY_NAME)1 VreBuilder.vre (nl.knaw.huygens.timbuctoo.model.vre.VreBuilder.vre)1 TinkerPopGraphManager (nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager)1