Search in sources :

Example 1 with ConstantStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep in project GraphScope by alibaba.

the class ShortestPathVertexProgramTreeNode method createOperatorArgument.

private Message.Value.Builder createOperatorArgument() {
    Message.Value.Builder valueBuilder = Message.Value.newBuilder();
    Message.VertexProgramShortestPathArg.Builder shortestPathArgBuilder = Message.VertexProgramShortestPathArg.newBuilder();
    List<Traversal.Admin<?, ?>> traversalSteps = step.getLocalChildren();
    Traversal.Admin<?, ?> targetVertexFilter = traversalSteps.get(0);
    Traversal.Admin<?, ?> edgeTraversal = traversalSteps.get(1);
    Traversal.Admin<?, ?> distanceTraversal = traversalSteps.get(2);
    Number maxDistance = ReflectionUtils.getFieldValue(ShortestPathVertexProgramStep.class, step, "maxDistance");
    boolean includeEdges = ReflectionUtils.getFieldValue(ShortestPathVertexProgramStep.class, step, "includeEdges");
    // targetVertexFilter
    // only support .with(ShortestPath.target,__.has('name','peter')) or default case
    Step targetVertexStep = targetVertexFilter.getSteps().get(0);
    if (targetVertexStep instanceof HasStep) {
        HasStep hasStep = (HasStep) targetVertexStep;
        List<HasContainer> hasContainerList = hasStep.getHasContainers();
        for (HasContainer hasContainer : hasContainerList) {
            String key = hasContainer.getKey();
            Object value = hasContainer.getValue();
            Message.Value.Builder propertyValueBuilder = Message.Value.newBuilder();
            Message.VariantType variantType = MaxGraphUtils.parsePropertyDataType(key, schema);
            propertyValueBuilder.setIndex(schema.getPropertyId(key));
            propertyValueBuilder.setValueType(variantType);
            switch(variantType) {
                case VT_INT:
                    propertyValueBuilder.setIntValue(Integer.parseInt(value.toString()));
                    break;
                case VT_LONG:
                    propertyValueBuilder.setLongValue(Long.parseLong(value.toString()));
                    break;
                case VT_DOUBLE:
                    propertyValueBuilder.setDoubleValue(Double.parseDouble(value.toString()));
                    break;
                case VT_STRING:
                    propertyValueBuilder.setStrValue(value.toString());
                    break;
                default:
                    throw new IllegalArgumentException("value in with-step is not supported yet => " + hasStep.toString());
            }
            shortestPathArgBuilder.setTarget(propertyValueBuilder);
            shortestPathArgBuilder.setHasTarget(true);
        }
    } else if (targetVertexStep instanceof IdentityStep) {
        shortestPathArgBuilder.setHasTarget(false);
    } else {
        throw new IllegalArgumentException("step for targetVertexFilter in shortest path is not supported yet => " + step);
    }
    // distanceTraversal supports ".with(ShortestPath.distance, 'weight')" and default case
    Step distanceStep = distanceTraversal.getSteps().get(0);
    if (distanceStep instanceof PropertiesStep) {
        String weight = ((PropertiesStep) distanceStep).getPropertyKeys()[0];
        shortestPathArgBuilder.setPropertyEdgeWeightId(SchemaUtils.getPropId(weight, schema));
        shortestPathArgBuilder.setWeightFlag(true);
    } else if (distanceStep instanceof ConstantStep) {
        shortestPathArgBuilder.setPropertyEdgeWeightId(-1);
        shortestPathArgBuilder.setWeightFlag(false);
    } else {
        throw new IllegalArgumentException("step for distanceTraversal in shortest path is not supported yet => " + step);
    }
    if (maxDistance == null) {
        maxDistance = 10;
    }
    checkArgument((int) maxDistance > 0, "iteration must > 0 for shortest path");
    shortestPathArgBuilder.setLoopLimit((int) maxDistance);
    // edgeTraversal supports ".with(ShortestPath.edges, Direction.IN)",
    // ".with(ShortestPath.edges, outE('edge'))
    // ", default as OUT
    List<Step> edgeTraversalSteps = edgeTraversal.getSteps();
    shortestPathArgBuilder.setWeightLb(0);
    VertexStep vstep = (VertexStep) edgeTraversalSteps.get(0);
    for (String edgeLabel : vstep.getEdgeLabels()) {
        GraphEdge edgeType = (GraphEdge) schema.getElement(edgeLabel);
        shortestPathArgBuilder.addEdgeLabels(edgeType.getLabelId());
    }
    if (Direction.BOTH.equals(vstep.getDirection())) {
        shortestPathArgBuilder.setDirection(Message.EdgeDirection.DIR_NONE);
    } else if (Direction.IN.equals(vstep.getDirection())) {
        shortestPathArgBuilder.setDirection(Message.EdgeDirection.DIR_IN);
    } else if (Direction.OUT.equals(vstep.getDirection())) {
        shortestPathArgBuilder.setDirection(Message.EdgeDirection.DIR_OUT);
    } else {
        checkArgument(false, "direction must be in/out/both for shortest path");
    }
    for (int i = 1; i < edgeTraversalSteps.size(); ++i) {
        Step hasStep = edgeTraversalSteps.get(i);
        if (hasStep instanceof HasStep) {
            HasContainer hasContainer = (HasContainer) ((HasStep) hasStep).getHasContainers().get(0);
            String key = hasContainer.getKey();
            Object value = hasContainer.getPredicate().getValue();
            if (hasContainer.getPredicate().getBiPredicate().toString().equals("gt") && (value instanceof Number)) {
                shortestPathArgBuilder.setWeightLb(((Number) value).doubleValue());
                shortestPathArgBuilder.setPropertyEdgeWeightId(SchemaUtils.getPropId(key, schema));
            } else {
                GraphEdge edgeType = (GraphEdge) schema.getElement(value.toString());
                shortestPathArgBuilder.addEdgeLabels(edgeType.getLabelId());
            }
        }
    }
    valueBuilder.setPayload(shortestPathArgBuilder.build().toByteString());
    return valueBuilder;
}
Also used : PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Message(com.alibaba.maxgraph.Message) ConstantStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) ShortestPathVertexProgramStep(org.apache.tinkerpop.gremlin.process.computer.traversal.step.map.ShortestPathVertexProgramStep) ConstantStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep) PropertiesStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Aggregations

Message (com.alibaba.maxgraph.Message)1 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)1 ShortestPathVertexProgramStep (org.apache.tinkerpop.gremlin.process.computer.traversal.step.map.ShortestPathVertexProgramStep)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)1 ConstantStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.ConstantStep)1 PropertiesStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep)1 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)1 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1