use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public ComputeJob<Map<String, Set<String>>> run(KCoreQuery query) {
return runCompute(query, tinkerComputeQuery -> {
long k = query.kValue();
if (k < 2L)
throw GraqlQueryException.kValueSmallerThanTwo();
if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
return Collections.emptyMap();
}
ComputerResult result;
Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
try {
result = tinkerComputeQuery.compute(new KCoreVertexProgram(k), new ClusterMemberMapReduce(KCoreVertexProgram.K_CORE_LABEL), subLabelIds);
} catch (NoResultException e) {
return Collections.emptyMap();
}
return result.memory().get(ClusterMemberMapReduce.class.getName());
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public ComputeJob<Map<Long, Set<String>>> run(CorenessQuery query) {
return runCompute(query, tinkerComputeQuery -> {
long k = query.minK();
if (k < 2L)
throw GraqlQueryException.kValueSmallerThanTwo();
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);
if (type.isRelationshipType())
throw GraqlQueryException.kCoreOnRelationshipType(typeLabel);
return type.subs();
}).map(SchemaConcept::getLabel).collect(Collectors.toSet());
}
Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
return Collections.emptyMap();
}
ComputerResult result;
Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
try {
result = tinkerComputeQuery.compute(new CorenessVertexProgram(k), new DegreeDistributionMapReduce(ofLabelIds, CorenessVertexProgram.CORENESS), subLabelIds);
} catch (NoResultException e) {
return Collections.emptyMap();
}
return result.memory().get(DegreeDistributionMapReduce.class.getName());
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method execWithMapReduce.
private <T, S, Q extends StatisticsQuery<?>> TinkerComputeJob<Optional<S>> execWithMapReduce(Q query, MapReduceFactory<T> mapReduceFactory, Function<T, S> operator) {
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());
GraknMapReduce<T> mapReduce = mapReduceFactory.get(statisticsResourceLabelIds, dataType, DegreeVertexProgram.DEGREE);
ComputerResult result = tinkerComputeQuery.compute(new DegreeStatisticsVertexProgram(statisticsResourceLabelIds), mapReduce, allSubLabelIds);
Map<Serializable, T> map = result.memory().get(mapReduce.getClass().getName());
LOG.debug("Result = " + map.get(MapReduce.NullObject.instance()));
return Optional.of(operator.apply(map.get(MapReduce.NullObject.instance())));
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class TinkerComputeQueryRunner method run.
public TinkerComputeJob<List<List<Concept>>> run(PathsQuery query) {
return runCompute(query, tinkerComputeQuery -> {
ConceptId sourceId = query.from();
ConceptId destinationId = query.to();
if (!tinkerComputeQuery.verticesExistInSubgraph(sourceId, destinationId)) {
throw GraqlQueryException.instanceDoesNotExist();
}
if (sourceId.equals(destinationId)) {
return Collections.singletonList(Collections.singletonList(tx.getConcept(sourceId)));
}
ComputerResult result;
Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
try {
result = tinkerComputeQuery.compute(new ShortestPathVertexProgram(sourceId, destinationId), null, subLabelIds);
} catch (NoResultException e) {
return Collections.emptyList();
}
Multimap<Concept, Concept> predecessorMapFromSource = tinkerComputeQuery.getPredecessorMap(result);
List<List<Concept>> allPaths = tinkerComputeQuery.getAllPaths(predecessorMapFromSource, sourceId);
if (tinkerComputeQuery.isAttributeIncluded()) {
// this can be slow
return tinkerComputeQuery.getExtendedPaths(allPaths);
}
LOG.info("Number of paths: " + allPaths.size());
return allPaths;
});
}
use of ai.grakn.concept.LabelId in project grakn by graknlabs.
the class GlobalCache method populateSchemaTxCache.
void populateSchemaTxCache(TxCache txCache) {
try {
lock.writeLock().lock();
Map<Label, SchemaConcept> cachedSchemaSnapshot = getCachedTypes();
Map<Label, LabelId> cachedLabelsSnapshot = getCachedLabels();
// Read central cache into txCache cloning only base concepts. Sets clones later
for (SchemaConcept type : cachedSchemaSnapshot.values()) {
txCache.cacheConcept(type);
}
// Load Labels Separately. We do this because the TypeCache may have expired.
cachedLabelsSnapshot.forEach(txCache::cacheLabel);
} finally {
lock.writeLock().unlock();
}
}
Aggregations