use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class SchemaConceptMapper method formatBase.
/**
* Create a var with the information underlying all Types
* @param schemaConcept type to be mapped
* @return {@link VarPattern} containing basic information about the given type
*/
private static VarPattern formatBase(SchemaConcept schemaConcept) {
VarPattern var = var().label(schemaConcept.getLabel());
SchemaConcept superType = schemaConcept.sup();
if (schemaConcept.sup() != null) {
var = var.sub(Graql.label(superType.getLabel()));
}
if (schemaConcept.isType()) {
Type type = schemaConcept.asType();
var = plays(var, type);
var = isAbstract(var, type);
}
return var;
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class ConceptBuilder method putSchemaConcept.
private SchemaConcept putSchemaConcept() {
SchemaConcept superConcept = use(SUPER_CONCEPT);
Label label = use(LABEL);
SchemaConcept concept;
if (superConcept.isEntityType()) {
concept = executor.tx().putEntityType(label);
} else if (superConcept.isRelationshipType()) {
concept = executor.tx().putRelationshipType(label);
} else if (superConcept.isRole()) {
concept = executor.tx().putRole(label);
} else if (superConcept.isAttributeType()) {
AttributeType attributeType = superConcept.asAttributeType();
AttributeType.DataType<?> dataType = useOrDefault(DATA_TYPE, attributeType.getDataType());
concept = executor.tx().putAttributeType(label, dataType);
} else if (superConcept.isRule()) {
concept = executor.tx().putRule(label, use(WHEN), use(THEN));
} else {
throw GraqlQueryException.insertMetaType(label, superConcept);
}
setSuper(concept, superConcept);
return concept;
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class InferenceRule method propagateConstraints.
/**
* @param parentAtom atom containing constraints (parent)
* @param unifier unifier unifying parent with the rule
* @return rule with propagated constraints from parent
*/
public InferenceRule propagateConstraints(Atom parentAtom, Unifier unifier) {
if (!parentAtom.isRelation() && !parentAtom.isResource())
return this;
Atom headAtom = head.getAtom();
Set<Atomic> bodyAtoms = new HashSet<>(body.getAtoms());
// transfer value predicates
parentAtom.getPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).forEach(bodyAtoms::add);
// if head is a resource merge vps into head
if (headAtom.isResource() && ((ResourceAtom) headAtom).getMultiPredicate().isEmpty()) {
ResourceAtom resourceHead = (ResourceAtom) headAtom;
Set<ValuePredicate> innerVps = parentAtom.getInnerPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).peek(bodyAtoms::add).collect(toSet());
headAtom = ResourceAtom.create(headAtom.getPattern(), headAtom.getPredicateVariable(), resourceHead.getRelationVariable(), resourceHead.getTypeId(), innerVps, headAtom.getParentQuery());
}
Set<TypeAtom> unifiedTypes = parentAtom.getTypeConstraints().flatMap(type -> type.unify(unifier).stream()).collect(toSet());
// set rule body types to sub types of combined query+rule types
Set<TypeAtom> ruleTypes = body.getAtoms(TypeAtom.class).filter(t -> !t.isRelation()).collect(toSet());
Set<TypeAtom> allTypes = Sets.union(unifiedTypes, ruleTypes);
allTypes.stream().filter(ta -> {
SchemaConcept schemaConcept = ta.getSchemaConcept();
SchemaConcept subType = allTypes.stream().map(Atom::getSchemaConcept).filter(Objects::nonNull).filter(t -> ReasonerUtils.supers(t).contains(schemaConcept)).findFirst().orElse(null);
return schemaConcept == null || subType == null;
}).forEach(t -> bodyAtoms.add(t.copy(body)));
return new InferenceRule(ReasonerQueries.atomic(headAtom), ReasonerQueries.create(bodyAtoms, tx), ruleId, tx);
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class ReasonerUtils method supers.
/**
* @param schemaConcept input type
* @return set of all non-meta super types of the role
*/
public static Set<SchemaConcept> supers(SchemaConcept schemaConcept) {
Set<SchemaConcept> superTypes = new HashSet<>();
SchemaConcept superType = schemaConcept.sup();
while (superType != null && !Schema.MetaSchema.isMetaLabel(superType.getLabel())) {
superTypes.add(superType);
superType = superType.sup();
}
return superTypes;
}
use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.
the class ResourceAtom method toRelationshipAtom.
@Override
public RelationshipAtom toRelationshipAtom() {
SchemaConcept type = getSchemaConcept();
if (type == null)
throw GraqlQueryException.illegalAtomConversion(this);
GraknTx tx = getParentQuery().tx();
Label typeLabel = Schema.ImplicitType.HAS.getLabel(type.getLabel());
return RelationshipAtom.create(Graql.var().rel(Schema.ImplicitType.HAS_OWNER.getLabel(type.getLabel()).getValue(), getVarName()).rel(Schema.ImplicitType.HAS_VALUE.getLabel(type.getLabel()).getValue(), getPredicateVariable()).isa(typeLabel.getValue()).admin(), getPredicateVariable(), tx.getSchemaConcept(typeLabel).getId(), getParentQuery());
}
Aggregations