/** * The compute statistics base algorithm that is called in every compute statistics query * * @param <S> The return type of {@link StatisticsMapReduce} * @return result of compute statistics algorithm, which will be of type S */ @Nullable private <S> S runComputeStatistics() { AttributeType.DataType<?> targetDataType = validateAndGetTargetDataType(); if (!targetContainsInstance()) return null; Set<LabelId> extendedScopeTypes = convertLabelsToIds(extendedScopeTypeLabels()); Set<LabelId> targetTypes = convertLabelsToIds(targetTypeLabels()); VertexProgram program = initStatisticsVertexProgram(query, targetTypes, targetDataType); StatisticsMapReduce<?> mapReduce = initStatisticsMapReduce(targetTypes, targetDataType); ComputerResult computerResult = compute(program, mapReduce, extendedScopeTypes); if (query.method().equals(MEDIAN)) { Number result = computerResult.memory().get(MedianVertexProgram.MEDIAN); LOG.debug("Median = " + result); return (S) result; } Map<Serializable, S> resultMap = computerResult.memory().get(mapReduce.getClass().getName()); LOG.debug("Result = " + resultMap.get(MapReduce.NullObject.instance())); return resultMap.get(MapReduce.NullObject.instance()); }
public static Method<?> of(String name) { for (Method<?> m : Method.values()) { if (m.name.equals(name)) { return m; } } return null; }
GraqlParser.ComputeConditionsContext conditions = context.computeConditions(); ComputeQuery query = queryBuilder.compute(Method.of(method.getText())); if (conditions == null) return query;
/** * Helper method to initialise the MapReduce algorithm for compute statistics queries * * @param targetTypes representing the attribute types in which the statistics computation is targeted for * @param targetDataType representing the data type of the target attribute types * @return an object which is a subclass of StatisticsMapReduce */ private StatisticsMapReduce<?> initStatisticsMapReduce(Set<LabelId> targetTypes, AttributeType.DataType<?> targetDataType) { GraqlSyntax.Compute.Method<?> method = query.method(); if (method.equals(MIN)) { return new MinMapReduce(targetTypes, targetDataType, DegreeVertexProgram.DEGREE); } else if (method.equals(MAX)) { return new MaxMapReduce(targetTypes, targetDataType, DegreeVertexProgram.DEGREE); } else if (method.equals(MEAN)) { return new MeanMapReduce(targetTypes, targetDataType, DegreeVertexProgram.DEGREE); }else if (method.equals(STD)) { return new StdMapReduce(targetTypes, targetDataType, DegreeVertexProgram.DEGREE); } else if (method.equals(SUM)) { return new SumMapReduce(targetTypes, targetDataType, DegreeVertexProgram.DEGREE); } return null; }
@Override public Stream<T> stream() { GraqlSyntax.Compute.Method<?> method = query.method(); if (method.equals(MIN) || method.equals(MAX) || method.equals(MEDIAN) || method.equals(SUM)) { return (Stream<T>) runComputeMinMaxMedianOrSum(); } else if (method.equals(MEAN)) { return (Stream<T>) runComputeMean(); } else if (method.equals(STD)) { return (Stream<T>) runComputeStd(); } else if (method.equals(COUNT)) { return (Stream<T>) runComputeCount(); } else if (method.equals(PATH)) { return (Stream<T>) runComputePath(); } else if (method.equals(CENTRALITY)) { return (Stream<T>) runComputeCentrality(); } else if (method.equals(CLUSTER)) { return (Stream<T>) runComputeCluster(); } throw GraqlQueryException.invalidComputeQuery_invalidMethod(); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ComputeQuery<?> that = (ComputeQuery<?>) o; return (Objects.equals(this.tx(), that.tx()) && this.method().equals(that.method()) && this.from().equals(that.from()) && this.to().equals(that.to()) && this.of().equals(that.of()) && this.in().equals(that.in()) && this.using().equals(that.using()) && this.where().equals(that.where()) && this.includesAttributes() == that.includesAttributes()); }
/** * Helper method to intialise the vertex program for compute statistics queries * * @param query representing the compute query * @param targetTypes representing the attribute types in which the statistics computation is targeted for * @param targetDataType representing the data type of the target attribute types * @return an object which is a subclass of VertexProgram */ private VertexProgram initStatisticsVertexProgram(ComputeQuery query, Set<LabelId> targetTypes, AttributeType.DataType<?> targetDataType) { if (query.method().equals(MEDIAN)) return new MedianVertexProgram(targetTypes, targetDataType); else return new DegreeStatisticsVertexProgram(targetTypes); }