use of ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom in project grakn by graknlabs.
the class ReasonerAtomicQuery method getMultiUnifier.
/**
* @throws IllegalArgumentException if passed a {@link ReasonerQuery} that is not a {@link ReasonerAtomicQuery}.
*/
@Override
public MultiUnifier getMultiUnifier(ReasonerQuery p, UnifierComparison unifierType) {
if (p == this)
return new MultiUnifierImpl();
Preconditions.checkArgument(p instanceof ReasonerAtomicQuery);
ReasonerAtomicQuery parent = (ReasonerAtomicQuery) p;
MultiUnifier multiUnifier = this.getAtom().getMultiUnifier(parent.getAtom(), unifierType);
Set<TypeAtom> childTypes = this.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (childTypes.isEmpty())
return multiUnifier;
// get corresponding type unifiers
Set<TypeAtom> parentTypes = parent.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (multiUnifier.isEmpty())
return new MultiUnifierImpl(typeUnifier(childTypes, parentTypes, new UnifierImpl()));
Set<Unifier> unifiers = multiUnifier.unifiers().stream().map(unifier -> typeUnifier(childTypes, parentTypes, unifier)).collect(Collectors.toSet());
return new MultiUnifierImpl(unifiers);
}
use of ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom 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.graql.internal.reasoner.atom.binary.TypeAtom in project grakn by graknlabs.
the class QueryAnswerStream method entityTypeFilter.
static boolean entityTypeFilter(Answer answer, Set<TypeAtom> types) {
if (types.isEmpty())
return true;
for (TypeAtom type : types) {
Var var = type.getVarName();
SchemaConcept t = type.getSchemaConcept();
if (t.subs().noneMatch(sub -> sub.equals(answer.get(var).asThing().type()))) {
return false;
}
}
return true;
}
use of ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom in project grakn by graknlabs.
the class ReasonerUtils method typeUnifier.
/**
* @param childTypes type atoms of child query
* @param parentTypes type atoms of parent query
* @param childParentUnifier unifier to unify child with parent
* @return combined unifier for type atoms
*/
public static Unifier typeUnifier(Set<TypeAtom> childTypes, Set<TypeAtom> parentTypes, Unifier childParentUnifier) {
Unifier unifier = childParentUnifier;
for (TypeAtom childType : childTypes) {
Var childVarName = childType.getVarName();
Var parentVarName = unifier.containsKey(childVarName) ? Iterables.getOnlyElement(childParentUnifier.get(childVarName)) : childVarName;
// types are unique so getting one is fine
TypeAtom parentType = parentTypes.stream().filter(pt -> pt.getVarName().equals(parentVarName)).findFirst().orElse(null);
if (parentType != null)
unifier = unifier.merge(childType.getUnifier(parentType));
}
return unifier;
}
Aggregations