Search in sources :

Example 41 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project open-kilda by telstra.

the class FermaMirrorGroupRepository method exists.

@Override
public boolean exists(SwitchId switchId, GroupId groupId) {
    String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
    Long groupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(groupId);
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(MirrorGroupFrame.GROUP_ID_PROPERTY, groupIdAsLong).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr)).getRawTraversal()) {
        return traversal.hasNext();
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
}
Also used : GroupIdConverter(org.openkilda.persistence.ferma.frames.converters.GroupIdConverter) Collection(java.util.Collection) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) FermaPersistentImplementation(org.openkilda.persistence.ferma.FermaPersistentImplementation) SwitchIdConverter(org.openkilda.persistence.ferma.frames.converters.SwitchIdConverter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) MirrorGroupData(org.openkilda.model.MirrorGroup.MirrorGroupData) MirrorGroupRepository(org.openkilda.persistence.repositories.MirrorGroupRepository) List(java.util.List) SwitchId(org.openkilda.model.SwitchId) MirrorGroup(org.openkilda.model.MirrorGroup) MirrorGroupFrame(org.openkilda.persistence.ferma.frames.MirrorGroupFrame) Optional(java.util.Optional) FlowMeterFrame(org.openkilda.persistence.ferma.frames.FlowMeterFrame) KildaBaseVertexFrame(org.openkilda.persistence.ferma.frames.KildaBaseVertexFrame) GroupId(org.openkilda.model.GroupId) PathIdConverter(org.openkilda.persistence.ferma.frames.converters.PathIdConverter) PathId(org.openkilda.model.PathId) P(org.apache.tinkerpop.gremlin.process.traversal.P) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 42 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project open-kilda by telstra.

the class FermaFlowMeterRepository method findFirstUnassignedMeter.

@Override
public Optional<MeterId> findFirstUnassignedMeter(SwitchId switchId, MeterId lowestMeterId, MeterId highestMeterId) {
    String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
    Long lowestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(lowestMeterId);
    Long highestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(highestMeterId);
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, P.gte(lowestMeterIdAsLong)).has(FlowMeterFrame.METER_ID_PROPERTY, P.lt(highestMeterIdAsLong)).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr).values(FlowMeterFrame.METER_ID_PROPERTY).order().math("_ + 1").as("a").where(__.not(__.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr).values(FlowMeterFrame.METER_ID_PROPERTY).where(P.eq("a")))).select("a").limit(1)).getRawTraversal()) {
        if (traversal.hasNext()) {
            return traversal.tryNext().map(l -> ((Double) l).longValue()).map(MeterIdConverter.INSTANCE::toEntityAttribute);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, lowestMeterIdAsLong).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr)).getRawTraversal()) {
        if (!traversal.hasNext()) {
            return Optional.of(lowestMeterId);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
    return Optional.empty();
}
Also used : FlowMeter(org.openkilda.model.FlowMeter) Collection(java.util.Collection) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) FlowMeterData(org.openkilda.model.FlowMeter.FlowMeterData) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) FermaPersistentImplementation(org.openkilda.persistence.ferma.FermaPersistentImplementation) SwitchIdConverter(org.openkilda.persistence.ferma.frames.converters.SwitchIdConverter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) MeterIdConverter(org.openkilda.persistence.ferma.frames.converters.MeterIdConverter) MeterId(org.openkilda.model.MeterId) List(java.util.List) SwitchId(org.openkilda.model.SwitchId) FlowMeterRepository(org.openkilda.persistence.repositories.FlowMeterRepository) Optional(java.util.Optional) FlowMeterFrame(org.openkilda.persistence.ferma.frames.FlowMeterFrame) KildaBaseVertexFrame(org.openkilda.persistence.ferma.frames.KildaBaseVertexFrame) PathIdConverter(org.openkilda.persistence.ferma.frames.converters.PathIdConverter) PathId(org.openkilda.model.PathId) P(org.apache.tinkerpop.gremlin.process.traversal.P) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 43 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project open-kilda by telstra.

the class FermaFlowMeterRepository method exists.

@Override
public boolean exists(SwitchId switchId, MeterId meterId) {
    String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
    Long meterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(meterId);
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(FlowMeterFrame.FRAME_LABEL).has(FlowMeterFrame.METER_ID_PROPERTY, meterIdAsLong).has(FlowMeterFrame.SWITCH_PROPERTY, switchIdAsStr)).getRawTraversal()) {
        return traversal.hasNext();
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
}
Also used : FlowMeter(org.openkilda.model.FlowMeter) Collection(java.util.Collection) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) FlowMeterData(org.openkilda.model.FlowMeter.FlowMeterData) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) FermaPersistentImplementation(org.openkilda.persistence.ferma.FermaPersistentImplementation) SwitchIdConverter(org.openkilda.persistence.ferma.frames.converters.SwitchIdConverter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) MeterIdConverter(org.openkilda.persistence.ferma.frames.converters.MeterIdConverter) MeterId(org.openkilda.model.MeterId) List(java.util.List) SwitchId(org.openkilda.model.SwitchId) FlowMeterRepository(org.openkilda.persistence.repositories.FlowMeterRepository) Optional(java.util.Optional) FlowMeterFrame(org.openkilda.persistence.ferma.frames.FlowMeterFrame) KildaBaseVertexFrame(org.openkilda.persistence.ferma.frames.KildaBaseVertexFrame) PathIdConverter(org.openkilda.persistence.ferma.frames.converters.PathIdConverter) PathId(org.openkilda.model.PathId) P(org.apache.tinkerpop.gremlin.process.traversal.P) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 44 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project grakn by graknlabs.

the class ComparatorPredicate method applyPredicate.

@Override
public final <S, E> GraphTraversal<S, E> applyPredicate(GraphTraversal<S, E> traversal) {
    var.ifPresent(theVar -> {
        // Compare to another variable
        String thisVar = UUID.randomUUID().toString();
        Var otherVar = theVar.var();
        String otherValue = UUID.randomUUID().toString();
        Traversal[] traversals = Stream.of(VALUE_PROPERTIES).map(prop -> __.values(prop).as(otherValue).select(thisVar).values(prop).where(gremlinPredicate(otherValue))).toArray(Traversal[]::new);
        traversal.as(thisVar).select(otherVar.name()).or(traversals).select(thisVar);
    });
    persistedValue().ifPresent(theValue -> {
        // Compare to a given value
        DataType<?> dataType = DataType.SUPPORTED_TYPES.get(value().get().getClass().getTypeName());
        Schema.VertexProperty property = dataType.getVertexProperty();
        traversal.has(property.name(), gremlinPredicate(theValue));
    });
    return traversal;
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) VarPattern(ai.grakn.graql.VarPattern) StringUtil(ai.grakn.util.StringUtil) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) UUID(java.util.UUID) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ValuePredicate(ai.grakn.graql.ValuePredicate) Stream(java.util.stream.Stream) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) DataType(ai.grakn.concept.AttributeType.DataType) Schema(ai.grakn.util.Schema) P(org.apache.tinkerpop.gremlin.process.traversal.P) Var(ai.grakn.graql.Var) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal)

Example 45 with GraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project grakn by graknlabs.

the class JanusPreviousPropertyStepTest method whenFilteringAPropertyToBeEqualToAPreviousProperty_UseJanusGraphStep.

@Test
public void whenFilteringAPropertyToBeEqualToAPreviousProperty_UseJanusGraphStep() {
    GraphTraversal traversal = optimisableTraversal(janus);
    GraphTraversal expected = optimisedTraversal(janus);
    traversal.asAdmin().applyStrategies();
    assertEquals(expected, traversal);
}
Also used : GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Test(org.junit.Test)

Aggregations

GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)93 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)67 Test (org.junit.Test)57 List (java.util.List)51 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)50 VertexMatcher.likeVertex (nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex)44 Before (org.junit.Before)42 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)40 Matchers.contains (org.hamcrest.Matchers.contains)39 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)35 Lists (com.google.common.collect.Lists)34 FacetValue (nl.knaw.huygens.timbuctoo.search.FacetValue)31 Optional (java.util.Optional)24 P (org.apache.tinkerpop.gremlin.process.traversal.P)21 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)20 ListFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue)19 Matchers.is (org.hamcrest.Matchers.is)17 Mockito.mock (org.mockito.Mockito.mock)17 Matchers.empty (org.hamcrest.Matchers.empty)15 Edge (org.apache.tinkerpop.gremlin.structure.Edge)14