use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class GreedyTraversalPlan method getLabelVarTypeMap.
// find all vars representing types
private static Map<Var, Type> getLabelVarTypeMap(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
Map<Var, Type> labelVarTypeMap = new HashMap<>();
allFragments.stream().filter(LabelFragment.class::isInstance).forEach(fragment -> {
// TODO: labels() should return ONE label instead of a set
SchemaConcept schemaConcept = tx.getSchemaConcept(Iterators.getOnlyElement(((LabelFragment) fragment).labels().iterator()));
if (schemaConcept != null && !schemaConcept.isRole() && !schemaConcept.isRule()) {
labelVarTypeMap.put(fragment.start(), schemaConcept.asType());
}
});
return labelVarTypeMap;
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class GraqlCompleter method create.
public static GraqlCompleter create(GraknSession session) {
ImmutableSet<Label> labels;
try (GraknAdmin tx = session.open(GraknTxType.READ).admin()) {
Stream<SchemaConcept> metaConcepts = Stream.of(tx.getMetaConcept(), tx.getMetaRole(), tx.getMetaRule()).flatMap(SchemaConcept::subs);
labels = metaConcepts.map(SchemaConcept::getLabel).collect(toImmutableSet());
}
return new GraqlCompleter(labels);
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class GraqlPrinter method build.
@Override
public Function<StringBuilder, StringBuilder> build(Concept concept) {
return sb -> {
// Display values for resources and ids for everything else
if (concept.isAttribute()) {
sb.append(colorKeyword("val ")).append(StringUtil.valueToString(concept.asAttribute().getValue()));
} else if (concept.isSchemaConcept()) {
SchemaConcept ontoConcept = concept.asSchemaConcept();
sb.append(colorKeyword("label ")).append(colorType(ontoConcept));
SchemaConcept superConcept = ontoConcept.sup();
if (superConcept != null) {
sb.append(colorKeyword(" sub ")).append(colorType(superConcept));
}
} else {
sb.append(colorKeyword("id ")).append(idToString(concept.getId()));
}
if (concept.isRelationship()) {
String relationString = concept.asRelationship().allRolePlayers().entrySet().stream().flatMap(entry -> {
Role role = entry.getKey();
Set<Thing> things = entry.getValue();
return things.stream().map(instance -> Optional.of(colorType(role) + ": id " + idToString(instance.getId())));
}).flatMap(CommonUtil::optionalToStream).collect(Collectors.joining(", "));
sb.append(" (").append(relationString).append(")");
}
// Display type of each instance
if (concept.isThing()) {
Type type = concept.asThing().type();
sb.append(colorKeyword(" isa ")).append(colorType(type));
}
// Display when and then for rules
if (concept.isRule()) {
sb.append(colorKeyword(" when ")).append("{ ").append(concept.asRule().getWhen()).append(" }");
sb.append(colorKeyword(" then ")).append("{ ").append(concept.asRule().getThen()).append(" }");
}
// Display any requested resources
if (concept.isThing() && attributeTypes.length > 0) {
concept.asThing().attributes(attributeTypes).forEach(resource -> {
String resourceType = colorType(resource.type());
String value = StringUtil.valueToString(resource.getValue());
sb.append(colorKeyword(" has ")).append(resourceType).append(" ").append(value);
});
}
return sb;
};
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class HasAttributeTypeProperty method mapToAtom.
@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
// NB: HasResourceType is a special case and it doesn't allow variables as resource types
Var varName = var.var().asUserDefined();
Label label = this.resourceType().getTypeLabel().orElse(null);
Var predicateVar = var().asUserDefined();
SchemaConcept schemaConcept = parent.tx().getSchemaConcept(label);
ConceptId predicateId = schemaConcept != null ? schemaConcept.getId() : null;
// isa part
VarPatternAdmin resVar = varName.has(Graql.label(label)).admin();
return HasAtom.create(resVar, predicateVar, predicateId, parent);
}
use of ai.grakn.concept.SchemaConcept 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());
});
}
Aggregations