Search in sources :

Example 1 with GraphSchema

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;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SchemaFetcher(com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) Sets(com.google.common.collect.Sets) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Lists(com.google.common.collect.Lists) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 2 with GraphSchema

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;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 3 with GraphSchema

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;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SchemaFetcher(com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) Sets(com.google.common.collect.Sets) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Lists(com.google.common.collect.Lists) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 4 with GraphSchema

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);
        }
    }
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 5 with GraphSchema

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;
}
Also used : BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) Map(java.util.Map) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) T(org.apache.tinkerpop.gremlin.structure.T) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) Set(java.util.Set) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Direction(org.apache.tinkerpop.gremlin.structure.Direction) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) List(java.util.List) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) Map(java.util.Map) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)26 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)12 List (java.util.List)9 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)7 Sets (com.google.common.collect.Sets)7 Set (java.util.Set)7 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)6 Maps (com.google.common.collect.Maps)6 Collectors (java.util.stream.Collectors)6 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)5 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)5 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)5 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)5 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)5 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)5 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JSONObject (com.alibaba.fastjson.JSONObject)4 SchemaFetcher (com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher)4