use of ai.grakn.graql.analytics.DegreeQuery 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.graql.analytics.DegreeQuery in project grakn by graknlabs.
the class GraqlTest method testDegrees.
@Test
public void testDegrees() throws Exception {
addSchemaAndEntities();
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
Map<Long, Set<String>> degrees = graph.graql().<DegreeQuery>parse("compute degrees;").execute();
Map<String, Long> correctDegrees = new HashMap<>();
correctDegrees.put(entityId1, 1L);
correctDegrees.put(entityId2, 2L);
correctDegrees.put(entityId3, 0L);
correctDegrees.put(entityId4, 1L);
correctDegrees.put(relationId12, 2L);
correctDegrees.put(relationId24, 2L);
assertTrue(!degrees.isEmpty());
degrees.forEach((key, value) -> value.forEach(id -> {
assertTrue(correctDegrees.containsKey(id));
assertEquals(correctDegrees.get(id), key);
}));
}
}
Aggregations