Search in sources :

Example 1 with TypeAtom

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);
}
Also used : QueryAnswerStream.entityTypeFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.entityTypeFilter) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Concept(ai.grakn.concept.Concept) LoggerFactory(org.slf4j.LoggerFactory) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) ReasonerQueryIterator(ai.grakn.graql.internal.reasoner.iterator.ReasonerQueryIterator) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) QueryAnswerStream.knownFilterWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.knownFilterWithInverse) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Preconditions(com.google.common.base.Preconditions) AtomicStateProducer(ai.grakn.graql.internal.reasoner.state.AtomicStateProducer) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Example 2 with TypeAtom

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);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 3 with TypeAtom

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;
}
Also used : Var(ai.grakn.graql.Var) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 4 with TypeAtom

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;
}
Also used : ValuePredicate.createValueVar(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate.createValueVar) Var(ai.grakn.graql.Var) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Unifier(ai.grakn.graql.admin.Unifier)

Aggregations

Var (ai.grakn.graql.Var)4 TypeAtom (ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom)4 Unifier (ai.grakn.graql.admin.Unifier)3 SchemaConcept (ai.grakn.concept.SchemaConcept)2 Answer (ai.grakn.graql.admin.Answer)2 Atomic (ai.grakn.graql.admin.Atomic)2 Conjunction (ai.grakn.graql.admin.Conjunction)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)2 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)2 QueryStateBase (ai.grakn.graql.internal.reasoner.state.QueryStateBase)2 ResolutionState (ai.grakn.graql.internal.reasoner.state.ResolutionState)2 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)2 Sets (com.google.common.collect.Sets)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Concept (ai.grakn.concept.Concept)1 ConceptId (ai.grakn.concept.ConceptId)1 Rule (ai.grakn.concept.Rule)1