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;
}
Aggregations