use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.
the class CostDataStatistics method getInERatio.
/**
* Compute inE scale ratio with start vertex label list and edge list
*
* @param input The given start vertex label list
* @param edgeList The given edge label list
* @return The result ratio
*/
public NodeStatistics getInERatio(NodeStatistics input, Set<String> edgeList) {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
NodeStatistics nodeStatistics = new NodeStatistics(schema);
Map<String, Double> inputVertexCountList = input.getVertexCountList();
for (String edgeLabel : edgeList) {
try {
GraphEdge edge = (GraphEdge) schema.getElement(edgeLabel);
List<EdgeRelation> relationList = edge.getRelationList();
if (relationList.size() > 0) {
double avgRelationRatio = 1.0 / edge.getRelationList().size();
relationList.stream().filter(v -> inputVertexCountList.containsKey(v.getTarget().getLabel())).forEach(v -> {
double sourceVertexCount = inputVertexCountList.getOrDefault(v.getTarget().getLabel(), INIT_VERTEX_COUNT);
double currEdgeCount = (this.edgeCountList.getOrDefault(edge.getLabel(), INIT_EDGE_COUNT) * avgRelationRatio / this.vertexCountList.getOrDefault(v.getTarget().getLabel(), INIT_VERTEX_COUNT)) * sourceVertexCount;
nodeStatistics.addEdgeCount(edgeLabel, currEdgeCount);
});
}
} catch (Exception ignored) {
}
}
return nodeStatistics;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.
the class CostDataStatistics method edgeQueryList.
public Map<String, String> edgeQueryList() {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
Map<String, String> queryList = Maps.newHashMap();
schema.getEdgeList().forEach(v -> {
queryList.put(v.getLabel(), String.format(EDGE_QUERY, v.getLabel()));
});
return queryList;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.
the class CostDataStatistics method getDirectionRatio.
private NodeStatistics getDirectionRatio(NodeStatistics input, Set<String> edgeList, boolean outDirection) {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
if (schema.getEdgeList().isEmpty()) {
return new NodeStatistics(schema);
}
Set<String> currEdgeList;
if (edgeList == null || edgeList.isEmpty()) {
currEdgeList = schema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toSet());
} else {
currEdgeList = Sets.newHashSet(edgeList);
}
NodeStatistics nodeStatistics = new NodeStatistics(schema);
Map<String, Double> vertexCountList = input.getVertexCountList();
currEdgeList.stream().map(v -> {
try {
return schema.getElement(v);
} catch (Exception ignored) {
return null;
}
}).filter(v -> v != null && v instanceof GraphEdge).map(v -> (GraphEdge) v).filter(v -> v.getRelationList().size() > 0).forEach(v -> {
double avgRelationRatio = 1.0 / v.getRelationList().size();
v.getRelationList().stream().filter(vv -> (outDirection && vertexCountList.containsKey(vv.getSource().getLabel())) || ((!outDirection) && vertexCountList.containsKey(vv.getTarget().getLabel()))).forEach(vv -> {
if (outDirection) {
Double edgeCount = this.edgeCountList.getOrDefault(v.getLabel(), INIT_EDGE_COUNT);
Double sourceVertexCount = this.vertexCountList.getOrDefault(vv.getSource().getLabel(), INIT_VERTEX_COUNT);
Double currSourceVertexCount = vertexCountList.getOrDefault(vv.getSource().getLabel(), INIT_VERTEX_COUNT);
nodeStatistics.addVertexCount(vv.getTarget().getLabel(), (edgeCount * avgRelationRatio / sourceVertexCount) * currSourceVertexCount);
} else {
nodeStatistics.addVertexCount(vv.getSource().getLabel(), (this.edgeCountList.getOrDefault(v.getLabel(), INIT_EDGE_COUNT) * avgRelationRatio / this.vertexCountList.getOrDefault(vv.getTarget().getLabel(), INIT_VERTEX_COUNT)) * vertexCountList.getOrDefault(vv.getTarget().getLabel(), INIT_VERTEX_COUNT));
}
});
});
return nodeStatistics;
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.
the class CostDataStatistics method initIfNeed.
private void initIfNeed() {
if (!this.initFlag.get()) {
synchronized (this) {
if (!this.initFlag.get()) {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
schema.getVertexList().forEach(v -> INSTANCE.vertexCountList.put(v.getLabel(), INIT_VERTEX_COUNT));
schema.getEdgeList().forEach(v -> INSTANCE.edgeCountList.put(v.getLabel(), INIT_EDGE_COUNT));
}
this.initFlag.set(true);
}
}
}
use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.
the class NodeLabelList method buildNodeLabel.
/**
* Build label list of current node from parent node, tree node and schema
*
* @param parentNodeLabel The given parent node
* @param treeNode The given tree node
* @param graphSchema The given graph schema
* @return The result node label list
*/
public static NodeLabelList buildNodeLabel(NodeLabelList parentNodeLabel, TreeNode treeNode, GraphSchema graphSchema) {
NodeLabelList nodeLabelList = new NodeLabelList();
if (null == parentNodeLabel) {
BaseTreeNode baseTreeNode = (BaseTreeNode) treeNode;
final Set<String> labelList = Sets.newHashSet();
baseTreeNode.getHasContainerList().forEach(v -> {
if (StringUtils.equals(v.getKey(), T.label.getAccessor())) {
if (v.getBiPredicate() instanceof Compare && v.getBiPredicate() == Compare.eq) {
labelList.add(v.getValue().toString());
} else if (v.getBiPredicate() instanceof Contains && v.getBiPredicate() == Contains.within) {
List<String> labelValueList = (List<String>) v.getValue();
labelList.addAll(labelValueList);
} else {
throw new IllegalArgumentException("Not support label compare " + v.toString());
}
}
});
if (treeNode instanceof SourceVertexTreeNode) {
List<String> vertexLabelList = graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
Set<String> resultLabelList;
if (labelList.isEmpty()) {
resultLabelList = Sets.newHashSet(vertexLabelList);
} else {
resultLabelList = labelList.stream().filter(vertexLabelList::contains).collect(Collectors.toSet());
}
nodeLabelList.addVertexLabel(resultLabelList);
} else if (treeNode instanceof SourceEdgeTreeNode) {
List<String> edgeLabelList = graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
Set<String> resultLabelList;
if (labelList.isEmpty()) {
resultLabelList = Sets.newHashSet(edgeLabelList);
} else {
resultLabelList = labelList.stream().filter(edgeLabelList::contains).collect(Collectors.toSet());
}
nodeLabelList.addEdgeLabel(resultLabelList);
} else {
nodeLabelList.enableUnknown();
}
} else {
Set<String> parentVertexLabelList = Sets.newHashSet();
Set<String> parentEdgeLabelList = Sets.newHashSet();
if (parentNodeLabel.isUnknownFlag()) {
parentVertexLabelList.addAll(graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
parentEdgeLabelList.addAll(graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
} else {
parentVertexLabelList.addAll(parentNodeLabel.getVertexLabelList());
parentEdgeLabelList.addAll(parentNodeLabel.getEdgeLabelList());
}
if (treeNode instanceof VertexTreeNode) {
VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
Direction direction = vertexTreeNode.getDirection();
nodeLabelList.addVertexLabel(computeVertexLabelList(parentVertexLabelList, direction, vertexTreeNode.getEdgeLabels(), graphSchema));
} else if (treeNode instanceof EdgeTreeNode) {
EdgeTreeNode edgeTreeNode = (EdgeTreeNode) treeNode;
Direction direction = edgeTreeNode.getDirection();
nodeLabelList.addEdgeLabel(computeEdgeLabelList(parentVertexLabelList, direction, edgeTreeNode.getEdgeLabels(), graphSchema));
} else if (treeNode instanceof EdgeVertexTreeNode) {
EdgeVertexTreeNode edgeVertexTreeNode = (EdgeVertexTreeNode) treeNode;
Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
Direction direction = edgeVertexTreeNode.getDirection();
edgeSourceTargetPairList.forEach((key, value) -> {
switch(direction) {
case OUT:
{
nodeLabelList.addVertexLabel(value.getLeft());
break;
}
case IN:
{
nodeLabelList.addVertexLabel(value.getRight());
break;
}
case BOTH:
{
nodeLabelList.addVertexLabel(value.getLeft());
nodeLabelList.addVertexLabel(value.getRight());
break;
}
}
});
} else if (treeNode instanceof EdgeOtherVertexTreeNode) {
Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
edgeSourceTargetPairList.forEach((key, value) -> {
nodeLabelList.addVertexLabel(value.getLeft());
nodeLabelList.addVertexLabel(value.getRight());
});
} else {
nodeLabelList.enableUnknown();
}
}
return nodeLabelList;
}
Aggregations