use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public ComputeJob<Long> run(CountQuery query) {
return runCompute(query, tinkerComputeQuery -> {
if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
LOG.debug("Count = 0");
return 0L;
}
Set<LabelId> typeLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
Map<Integer, Long> count;
Set<LabelId> rolePlayerLabelIds = tinkerComputeQuery.getRolePlayerLabelIds();
rolePlayerLabelIds.addAll(typeLabelIds);
ComputerResult result = tinkerComputeQuery.compute(new CountVertexProgram(), new CountMapReduceWithAttribute(), rolePlayerLabelIds, false);
count = result.memory().get(CountMapReduceWithAttribute.class.getName());
long finalCount = count.keySet().stream().filter(id -> typeLabelIds.contains(LabelId.of(id))).mapToLong(count::get).sum();
if (count.containsKey(GraknMapReduce.RESERVED_TYPE_LABEL_KEY)) {
finalCount += count.get(GraknMapReduce.RESERVED_TYPE_LABEL_KEY);
}
LOG.debug("Count = " + finalCount);
return finalCount;
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public ComputeJob<Optional<Number>> run(MedianQuery query) {
return runStatistics(query, tinkerComputeQuery -> {
AttributeType.DataType<?> dataType = tinkerComputeQuery.getDataTypeOfSelectedResourceTypes();
if (!tinkerComputeQuery.selectedResourceTypesHaveInstance()) {
return Optional.empty();
}
Set<LabelId> allSubLabelIds = convertLabelsToIds(tinkerComputeQuery.getCombinedSubTypes());
Set<LabelId> statisticsResourceLabelIds = convertLabelsToIds(tinkerComputeQuery.statisticsResourceLabels());
ComputerResult result = tinkerComputeQuery.compute(new MedianVertexProgram(statisticsResourceLabelIds, dataType), null, allSubLabelIds);
Number finalResult = result.memory().get(MedianVertexProgram.MEDIAN);
LOG.debug("Median = " + finalResult);
return Optional.of(finalResult);
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public ComputeJob<Map<Long, Set<String>>> run(DegreeQuery query) {
return runCompute(query, tinkerComputeQuery -> {
Set<Label> ofLabels;
// Check if ofType is valid before returning emptyMap
if (query.targetLabels().isEmpty()) {
ofLabels = tinkerComputeQuery.subLabels();
} else {
ofLabels = query.targetLabels().stream().flatMap(typeLabel -> {
Type type = tx.getSchemaConcept(typeLabel);
if (type == null)
throw GraqlQueryException.labelNotFound(typeLabel);
return type.subs();
}).map(SchemaConcept::getLabel).collect(Collectors.toSet());
}
Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
return Collections.emptyMap();
}
Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
ComputerResult result = tinkerComputeQuery.compute(new DegreeVertexProgram(ofLabelIds), new DegreeDistributionMapReduce(ofLabelIds, DegreeVertexProgram.DEGREE), subLabelIds);
return result.memory().get(DegreeDistributionMapReduce.class.getName());
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class DegreeStatisticsVertexProgram method degreeStatisticsStepResourceOwner.
static void degreeStatisticsStepResourceOwner(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) {
LabelId labelId = Utility.getVertexTypeId(vertex);
if (labelId.isValid() && !ofLabelIds.contains(labelId)) {
messenger.sendMessage(messageScopeShortcutIn, 1L);
messenger.sendMessage(messageScopeResourceOut, 1L);
}
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public <T> ComputeJob<T> run(ConnectedComponentQuery<T> query) {
return runCompute(query, tinkerComputeQuery -> {
if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
LOG.info("Selected types don't have instances");
return (T) Collections.emptyMap();
}
Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
GraknVertexProgram<?> vertexProgram;
if (query.sourceId().isPresent()) {
ConceptId conceptId = query.sourceId().get();
if (!tinkerComputeQuery.verticesExistInSubgraph(conceptId)) {
throw GraqlQueryException.instanceDoesNotExist();
}
vertexProgram = new ConnectedComponentVertexProgram(conceptId);
} else {
vertexProgram = new ConnectedComponentsVertexProgram();
}
Long clusterSize = query.clusterSize();
GraknMapReduce<?> mapReduce;
if (query.isMembersSet()) {
mapReduce = new ClusterMemberMapReduce(ConnectedComponentsVertexProgram.CLUSTER_LABEL, clusterSize);
} else {
mapReduce = new ClusterSizeMapReduce(ConnectedComponentsVertexProgram.CLUSTER_LABEL, clusterSize);
}
Memory memory = tinkerComputeQuery.compute(vertexProgram, mapReduce, subLabelIds).memory();
return memory.get(mapReduce.getClass().getName());
});
}
Aggregations