Search in sources :

Example 1 with EdgeRelation

use of com.alibaba.maxgraph.compiler.api.schema.EdgeRelation 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 EdgeRelation

use of com.alibaba.maxgraph.compiler.api.schema.EdgeRelation in project GraphScope by alibaba.

the class SubgraphTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    TreeNodeLabelManager treeNodeLabelManager = contextManager.getTreeNodeLabelManager();
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
    LogicalVertex sourceDelegateVertex = getInputNode().getOutputVertex();
    logicalSubQueryPlan.addLogicalVertex(sourceDelegateVertex);
    List<Integer> edgeLabelList = Lists.newArrayList();
    if (sourceDelegateVertex.getProcessorFunction().getOperatorType() == QueryFlowOuterClass.OperatorType.E) {
        Message.Value.Builder argumentBuilder = sourceDelegateVertex.getProcessorFunction().getArgumentBuilder();
        edgeLabelList.addAll(argumentBuilder.getIntValueListList());
    }
    Set<Integer> sourceVertexList = Sets.newHashSet();
    Set<Integer> targetVertexList = Sets.newHashSet();
    Message.SubgraphVertexList.Builder subgraphBuilder = Message.SubgraphVertexList.newBuilder();
    for (Integer edgeLabel : edgeLabelList) {
        GraphEdge edgeType = (GraphEdge) schema.getElement(edgeLabel);
        for (EdgeRelation relationShip : edgeType.getRelationList()) {
            sourceVertexList.add(relationShip.getSource().getLabelId());
            targetVertexList.add(relationShip.getTarget().getLabelId());
        }
    }
    if (sourceVertexList.isEmpty()) {
        schema.getVertexList().forEach(v -> {
            sourceVertexList.add(v.getLabelId());
            targetVertexList.add(v.getLabelId());
        });
    }
    subgraphBuilder.addAllSourceVertexList(sourceVertexList).addAllTargetVertexList(targetVertexList);
    ProcessorFunction processorFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.SUBGRAPH, Message.Value.newBuilder().setBoolFlag(this.vertexFlag).setPayload(subgraphBuilder.build().toByteString()));
    LogicalVertex graphVertex = new LogicalUnaryVertex(vertexIdManager.getId(), processorFunction, false, sourceDelegateVertex);
    logicalSubQueryPlan.addLogicalVertex(graphVertex);
    logicalSubQueryPlan.addLogicalEdge(sourceDelegateVertex, graphVertex, new LogicalEdge());
    setFinishVertex(graphVertex, treeNodeLabelManager);
    addUsedLabelAndRequirement(graphVertex, treeNodeLabelManager);
    return logicalSubQueryPlan;
}
Also used : LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 3 with EdgeRelation

use of com.alibaba.maxgraph.compiler.api.schema.EdgeRelation in project GraphScope by alibaba.

the class CostDataStatistics method getOutERatio.

/**
 * Compute outE 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 getOutERatio(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.getSource().getLabel())).forEach(v -> {
                    double sourceVertexCount = inputVertexCountList.getOrDefault(v.getSource().getLabel(), INIT_VERTEX_COUNT);
                    double currEdgeCount = (this.edgeCountList.getOrDefault(edge.getLabel(), INIT_EDGE_COUNT) * avgRelationRatio / this.vertexCountList.getOrDefault(v.getSource().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 4 with EdgeRelation

use of com.alibaba.maxgraph.compiler.api.schema.EdgeRelation 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);
}
Also used : MaxGraphException(com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) JSONArray(com.alibaba.fastjson.JSONArray) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) JSONObject(com.alibaba.fastjson.JSONObject) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Example 5 with EdgeRelation

use of com.alibaba.maxgraph.compiler.api.schema.EdgeRelation in project GraphScope by alibaba.

the class EdgeTypeMapper method parseFromEdgeType.

public static SchemaElementMapper parseFromEdgeType(GraphEdge graphEdge) {
    EdgeTypeMapper edgeTypeMapper = new EdgeTypeMapper();
    edgeTypeMapper.setId(graphEdge.getLabelId());
    edgeTypeMapper.setLabel(graphEdge.getLabel());
    edgeTypeMapper.setType(TypeEnum.EDGE.toString());
    List<EdgeRelationMapper> relationMapperList = Lists.newArrayList();
    for (EdgeRelation edgeRelation : graphEdge.getRelationList()) {
        relationMapperList.add(EdgeRelationMapper.parseFromEdgeRelation(graphEdge.getLabel(), edgeRelation));
    }
    edgeTypeMapper.setRelationShips(relationMapperList);
    List<GraphPropertyMapper> propertyMapperList = Lists.newArrayList();
    for (GraphProperty graphProperty : graphEdge.getPropertyList()) {
        propertyMapperList.add(GraphPropertyMapper.parseFromGrapyProperty(graphProperty));
    }
    edgeTypeMapper.setPropertyDefList(propertyMapperList);
    return edgeTypeMapper;
}
Also used : GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)

Aggregations

EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)6 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)4 JSONObject (com.alibaba.fastjson.JSONObject)3 GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)3 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)2 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)2 SchemaFetcher (com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher)2 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)2 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)2 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)2 NodeType (com.alibaba.maxgraph.compiler.tree.NodeType)2 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)2 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)2 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2