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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations