Search in sources :

Example 1 with GraphEdge

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;
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) DefaultGraphSchema(com.alibaba.graphscope.groot.schema.mapper.DefaultGraphSchema) VertexTypeMapper(com.alibaba.graphscope.groot.schema.mapper.VertexTypeMapper) EdgeTypeMapper(com.alibaba.graphscope.groot.schema.mapper.EdgeTypeMapper) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) SchemaElementMapper(com.alibaba.graphscope.groot.schema.mapper.SchemaElementMapper)

Example 2 with GraphEdge

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

the class GraphSchemaMapper method parseFromSchema.

public static GraphSchemaMapper parseFromSchema(GraphSchema schema) {
    GraphSchemaMapper schemaMapper = new GraphSchemaMapper();
    schemaMapper.version = schema.getVersion();
    schemaMapper.types = Lists.newArrayList();
    for (GraphVertex graphVertex : schema.getVertexList()) {
        schemaMapper.types.add(VertexTypeMapper.parseFromVertexType(graphVertex));
    }
    for (GraphEdge graphEdge : schema.getEdgeList()) {
        schemaMapper.types.add(EdgeTypeMapper.parseFromEdgeType(graphEdge));
    }
    return schemaMapper;
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Example 3 with GraphEdge

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

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

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

the class PageRankVertexProgramTreeNode method createOperatorArgument.

private Message.Value.Builder createOperatorArgument() {
    Message.Value.Builder valueBuilder = Message.Value.newBuilder();
    Message.ProgramPageRankArg.Builder pageRankArgBuilder = Message.ProgramPageRankArg.newBuilder();
    String pageRankProperty = ReflectionUtils.getFieldValue(PageRankVertexProgramStep.class, pageRankVertexProgramStep, "pageRankProperty");
    double alpha = ReflectionUtils.getFieldValue(PageRankVertexProgramStep.class, pageRankVertexProgramStep, "alpha");
    int times = ReflectionUtils.getFieldValue(PageRankVertexProgramStep.class, pageRankVertexProgramStep, "times");
    Traversal.Admin<Vertex, Edge> edgeTraversal = pageRankVertexProgramStep.getLocalChildren().get(0);
    if (pageRankProperty.equals(PageRankVertexProgram.PAGE_RANK)) {
        pageRankProperty = PAGERANK;
    }
    pageRankArgBuilder.setPropertyPrId(SchemaUtils.getPropId(pageRankProperty, schema));
    checkArgument(alpha > 0 && alpha < 1, "alpha must > 0 and < 1 for PageRank");
    pageRankArgBuilder.setAlpha(alpha);
    checkArgument(times > 0, "iteration must > 0 for PageRank");
    pageRankArgBuilder.setLoopLimit(times);
    List<Step> steps = edgeTraversal.getSteps();
    // supports with(PageRank.edges,outE('knows'))
    VertexStep vstep = (VertexStep) steps.get(0);
    String[] labels = vstep.getEdgeLabels();
    for (String edgeLabel : labels) {
        GraphEdge edgeType = (GraphEdge) schema.getElement(edgeLabel);
        pageRankArgBuilder.addEdgeLabels(edgeType.getLabelId());
    }
    if (Direction.BOTH.equals(vstep.getDirection())) {
        pageRankArgBuilder.setDirection(Message.EdgeDirection.DIR_NONE);
    } else if (Direction.IN.equals(vstep.getDirection())) {
        pageRankArgBuilder.setDirection(Message.EdgeDirection.DIR_IN);
    } else if (Direction.OUT.equals(vstep.getDirection())) {
        pageRankArgBuilder.setDirection(Message.EdgeDirection.DIR_OUT);
    } else {
        checkArgument(false, "direction must be in/out/both for pagerank");
    }
    for (int i = 1; i < steps.size(); ++i) {
        Step step = steps.get(i);
        if (step instanceof HasStep) {
            HasContainer hasContainer = (HasContainer) ((HasStep) step).getHasContainers().get(0);
            Object value = hasContainer.getPredicate().getValue();
            GraphEdge edgeType = (GraphEdge) schema.getElement(value.toString());
            pageRankArgBuilder.addEdgeLabels(edgeType.getLabelId());
        }
    }
    valueBuilder.setPayload(pageRankArgBuilder.build().toByteString());
    return valueBuilder;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) PageRankVertexProgramStep(org.apache.tinkerpop.gremlin.process.computer.traversal.step.map.PageRankVertexProgramStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Aggregations

GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)17 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)6 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)6 JSONObject (com.alibaba.fastjson.JSONObject)5 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)5 GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)5 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)5 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)5 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)5 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)5 SchemaFetcher (com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher)4 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)4 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)4 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)4 NodeType (com.alibaba.maxgraph.compiler.tree.NodeType)4 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)4 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)4 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)4 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4