use of com.alibaba.maxgraph.compiler.api.schema.GraphEdge in project GraphScope by alibaba.
the class CostDataStatistics method computeStepCount.
/**
* Compute step count with given node and node label
*
* @param inputStatistics The given input statistics
* @param treeNode The given tree node
* @param currNodeLabel The given current node label
* @return The result step count
*/
private NodeStatistics computeStepCount(NodeStatistics inputStatistics, TreeNode treeNode, NodeLabelList currNodeLabel) {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
NodeStatistics nodeStatistics = new NodeStatistics(schema);
if (null == inputStatistics) {
if (currNodeLabel.isUnknownFlag()) {
for (Map.Entry<String, Double> entry : vertexCountList.entrySet()) {
nodeStatistics.addVertexCount(entry.getKey(), entry.getValue());
}
} else {
SourceTreeNode sourceTreeNode = (SourceTreeNode) treeNode;
Object[] ids = sourceTreeNode.getIds();
Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
for (String vertexLabel : vertexLabelList) {
double vertexCount = null == ids ? vertexCountList.getOrDefault(vertexLabel, INIT_VERTEX_COUNT) : (ids.length * 1.0 / vertexLabelList.size());
nodeStatistics.addVertexCount(vertexLabel, vertexCount);
}
Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
for (String edgeLabel : currNodeLabel.getEdgeLabelList()) {
double edgeCount = null == ids ? edgeCountList.getOrDefault(edgeLabel, INIT_EDGE_COUNT) : (ids.length * 1.0 / edgeLabelList.size());
nodeStatistics.addEdgeCount(edgeLabel, edgeCount);
}
}
} else {
if (treeNode instanceof VertexTreeNode) {
VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
Direction direction = vertexTreeNode.getDirection();
Set<String> edgeLabelList;
if (null == vertexTreeNode.getEdgeLabels()) {
edgeLabelList = Sets.newHashSet();
} else {
edgeLabelList = Sets.newHashSet(vertexTreeNode.getEdgeLabels());
}
switch(direction) {
case OUT:
{
nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
break;
}
case IN:
{
nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
break;
}
case BOTH:
{
nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
break;
}
}
} else if (treeNode instanceof EdgeTreeNode) {
Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
Direction direction = ((EdgeTreeNode) treeNode).getDirection();
switch(direction) {
case OUT:
{
nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
break;
}
case IN:
{
nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
break;
}
case BOTH:
{
nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
break;
}
}
} else if (treeNode instanceof EdgeVertexTreeNode) {
Map<String, Double> edgeCountList = inputStatistics.getEdgeCountList();
Direction direction = ((EdgeVertexTreeNode) treeNode).getDirection();
edgeCountList.keySet().stream().map(v -> {
try {
return schema.getElement(v);
} catch (Exception ignored) {
return null;
}
}).filter(v -> null != v && v instanceof GraphEdge).map(v -> (GraphEdge) v).forEach(v -> {
v.getRelationList().forEach(vv -> {
double currEdgeCount = edgeCountList.get(v.getLabel());
if (direction == Direction.OUT || direction == Direction.BOTH) {
List<String> resultVertexLabelList = Lists.newArrayList();
if (currNodeLabel.getVertexLabelList().contains(vv.getSource().getLabel())) {
resultVertexLabelList.add(vv.getSource().getLabel());
}
double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
for (String vertexLabel : resultVertexLabelList) {
nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
}
}
if (direction == Direction.IN || direction == Direction.BOTH) {
List<String> resultVertexLabelList = Lists.newArrayList();
if (currNodeLabel.getVertexLabelList().contains(vv.getTarget().getLabel())) {
resultVertexLabelList.add(vv.getTarget().getLabel());
}
double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
for (String vertexLabel : resultVertexLabelList) {
nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
}
}
});
});
} else if (treeNode instanceof EdgeOtherVertexTreeNode) {
Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
double avgVertexCount = inputStatistics.totalCount() / vertexLabelList.size();
vertexLabelList.forEach(v -> nodeStatistics.addVertexCount(v, avgVertexCount));
} else {
NodeType nodeType = treeNode.getNodeType();
if (NodeType.MAP == nodeType) {
nodeStatistics.addElementCount(inputStatistics.totalCount());
} else if (NodeType.FILTER == nodeType) {
nodeStatistics.merge(inputStatistics, FILTER_RATIO);
} else if (NodeType.FLATMAP == nodeType) {
nodeStatistics.addElementCount(inputStatistics.totalCount() * FLATMAP_RATIO);
} else if (NodeType.AGGREGATE == nodeType) {
nodeStatistics.addElementCount(1);
} else {
nodeStatistics.merge(inputStatistics);
}
}
}
return nodeStatistics;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphEdge 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;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphEdge in project GraphScope by alibaba.
the class PeerPressureVertexProgramTreeNode method createOperatorArgument.
private Message.Value.Builder createOperatorArgument() {
Message.Value.Builder valueBuilder = Message.Value.newBuilder();
Message.ProgramPeerPressureArg.Builder peerPressureArgBuilder = Message.ProgramPeerPressureArg.newBuilder();
String clusterProperty = ReflectionUtils.getFieldValue(PeerPressureVertexProgramStep.class, step, "clusterProperty");
int times = ReflectionUtils.getFieldValue(PeerPressureVertexProgramStep.class, step, "times");
PureTraversal<Vertex, Edge> edgeTraversal = ReflectionUtils.getFieldValue(PeerPressureVertexProgramStep.class, step, "edgeTraversal");
if (clusterProperty.equals(PeerPressureVertexProgram.CLUSTER)) {
clusterProperty = PEER_PRESSURE;
}
peerPressureArgBuilder.setPropertyPpId(SchemaUtils.getPropId(clusterProperty, schema));
checkArgument(times > 0, "iteration must > 0 for PeerPressure");
peerPressureArgBuilder.setLoopLimit(times);
List<Step> edgeTraversalSteps = edgeTraversal.getPure().getSteps();
VertexStep vstep = (VertexStep) edgeTraversalSteps.get(0);
for (String edgeLabel : vstep.getEdgeLabels()) {
GraphEdge edgeType = (GraphEdge) schema.getElement(edgeLabel);
peerPressureArgBuilder.addEdgeLabels(edgeType.getLabelId());
}
if (Direction.BOTH.equals(vstep.getDirection())) {
peerPressureArgBuilder.setDirection(Message.EdgeDirection.DIR_NONE);
} else if (Direction.IN.equals(vstep.getDirection())) {
peerPressureArgBuilder.setDirection(Message.EdgeDirection.DIR_IN);
} else if (Direction.OUT.equals(vstep.getDirection())) {
peerPressureArgBuilder.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);
Object value = hasContainer.getPredicate().getValue();
GraphEdge edgeType = (GraphEdge) schema.getElement(value.toString());
peerPressureArgBuilder.addEdgeLabels(edgeType.getLabelId());
}
}
valueBuilder.setPayload(peerPressureArgBuilder.build().toByteString());
return valueBuilder;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphEdge in project GraphScope by alibaba.
the class DefaultGraphSchema method buildSchemaFromJson.
public static GraphSchema buildSchemaFromJson(String schemaJson) {
JSONObject jsonObject = JSONObject.parseObject(schemaJson);
Map<String, GraphVertex> vertexList = Maps.newHashMap();
Map<String, GraphEdge> edgeList = Maps.newHashMap();
Map<String, Integer> propNameToIdList = Maps.newHashMap();
JSONArray typeList = jsonObject.getJSONArray("types");
if (null != typeList) {
int propId = 1;
for (int i = 0; i < typeList.size(); i++) {
JSONObject typeObject = typeList.getJSONObject(i);
int labelId = typeObject.getInteger("id");
String label = typeObject.getString("label");
String type = typeObject.getString("type");
Map<String, GraphProperty> namePropertyList = Maps.newHashMap();
List<GraphProperty> propertyList = Lists.newArrayList();
JSONArray propArray = typeObject.getJSONArray("propertyDefList");
if (null != propArray) {
for (int j = 0; j < propArray.size(); j++) {
JSONObject propObject = propArray.getJSONObject(j);
String propName = propObject.getString("name");
Integer currPropId = propObject.getInteger("id");
if (null == currPropId) {
currPropId = propId++;
}
String propDataTypeString = propObject.getString("data_type");
com.alibaba.maxgraph.sdkcommon.meta.DataType dataType;
if (StringUtils.startsWith(propDataTypeString, "LIST")) {
dataType = new com.alibaba.maxgraph.sdkcommon.meta.DataType(InternalDataType.LIST);
try {
dataType.setExpression(StringUtils.removeEnd(StringUtils.removeStart(propDataTypeString, "LIST<"), ">"));
} catch (MaxGraphException e) {
throw new RuntimeException(e);
}
} else {
dataType = com.alibaba.maxgraph.sdkcommon.meta.DataType.valueOf(propDataTypeString);
}
GraphProperty property = new DefaultGraphProperty(currPropId, propName, DataType.parseFromDataType(dataType));
propertyList.add(property);
namePropertyList.put(propName, property);
propNameToIdList.put(propName, currPropId);
}
} else {
logger.warn("There's no property def list in " + label);
}
if (StringUtils.equals(type, "VERTEX")) {
List<GraphProperty> primaryPropertyList = Lists.newArrayList();
JSONArray indexArray = typeObject.getJSONArray("indexes");
if (indexArray != null) {
for (int k = 0; k < indexArray.size(); k++) {
JSONObject indexObject = indexArray.getJSONObject(k);
JSONArray priNameList = indexObject.getJSONArray("propertyNames");
for (int j = 0; j < priNameList.size(); j++) {
primaryPropertyList.add(namePropertyList.get(priNameList.getString(j)));
}
}
}
DefaultGraphVertex graphVertex = new DefaultGraphVertex(labelId, label, propertyList, primaryPropertyList);
vertexList.put(label, graphVertex);
} else {
List<EdgeRelation> relationList = Lists.newArrayList();
JSONArray relationArray = typeObject.getJSONArray("rawRelationShips");
if (null != relationArray) {
for (int k = 0; k < relationArray.size(); k++) {
JSONObject relationObject = relationArray.getJSONObject(k);
String sourceLabel = relationObject.getString("srcVertexLabel");
String targetLabel = relationObject.getString("dstVertexLabel");
relationList.add(new DefaultEdgeRelation(vertexList.get(sourceLabel), vertexList.get(targetLabel)));
}
} else {
logger.warn("There's no relation def in edge " + label);
}
DefaultGraphEdge graphEdge = new DefaultGraphEdge(labelId, label, propertyList, relationList);
edgeList.put(label, graphEdge);
}
}
} else {
logger.error("Cant get types field in json[" + schemaJson + "]");
}
return new DefaultGraphSchema(vertexList, edgeList, propNameToIdList);
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphEdge in project GraphScope by alibaba.
the class GraphSchemaMapper method toGraphSchema.
public GraphSchema toGraphSchema() {
DefaultGraphSchema graphSchema = new DefaultGraphSchema();
Map<String, GraphVertex> vertexTypeMap = Maps.newHashMap();
for (SchemaElementMapper elementMapper : this.types) {
if (elementMapper instanceof VertexTypeMapper) {
GraphVertex graphVertex = ((VertexTypeMapper) elementMapper).toVertexType();
graphSchema.createVertexType(graphVertex);
vertexTypeMap.put(graphVertex.getLabel(), graphVertex);
}
}
for (SchemaElementMapper elementMapper : this.types) {
if (elementMapper instanceof EdgeTypeMapper) {
GraphEdge graphEdge = ((EdgeTypeMapper) elementMapper).toEdgeType(vertexTypeMap);
graphSchema.createEdgeType(graphEdge);
}
}
return graphSchema;
}
Aggregations