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