Search in sources :

Example 1 with ResourceAtom

use of ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom 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 2 with ResourceAtom

use of ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom in project grakn by graknlabs.

the class AtomicTest method testUnification_ResourceWithIndirectValuePredicate.

@Test
public void testUnification_ResourceWithIndirectValuePredicate() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String resource = "{$x has resource $r;$r val 'f';}";
    String resource2 = "{$r has resource $x;$x val 'f';}";
    String resource3 = "{$r has resource 'f';}";
    ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction(resource, graph), graph);
    ReasonerAtomicQuery resourceQuery2 = ReasonerQueries.atomic(conjunction(resource2, graph), graph);
    ReasonerAtomicQuery resourceQuery3 = ReasonerQueries.atomic(conjunction(resource3, graph), graph);
    String type = "{$x isa resource;$x id '" + resourceQuery.getQuery().execute().iterator().next().get("r").getId().getValue() + "';}";
    ReasonerAtomicQuery typeQuery = ReasonerQueries.atomic(conjunction(type, graph), graph);
    Atom typeAtom = typeQuery.getAtom();
    Atom resourceAtom = resourceQuery.getAtom();
    Atom resourceAtom2 = resourceQuery2.getAtom();
    Atom resourceAtom3 = resourceQuery3.getAtom();
    Unifier unifier = resourceAtom.getUnifier(typeAtom);
    Unifier unifier2 = resourceAtom2.getUnifier(typeAtom);
    Unifier unifier3 = resourceAtom3.getUnifier(typeAtom);
    Answer typeAnswer = typeQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer = resourceQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer2 = resourceQuery2.getQuery().execute().iterator().next();
    Answer resourceAnswer3 = resourceQuery3.getQuery().execute().iterator().next();
    assertEquals(typeAnswer.get(var("x")), resourceAnswer.unify(unifier).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer2.unify(unifier2).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer3.unify(unifier3).get(var("x")));
}
Also used : Answer(ai.grakn.graql.admin.Answer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

Answer (ai.grakn.graql.admin.Answer)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 Unifier (ai.grakn.graql.admin.Unifier)2 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)2 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)2 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)2 ConceptId (ai.grakn.concept.ConceptId)1 Rule (ai.grakn.concept.Rule)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Var (ai.grakn.graql.Var)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1 Patterns (ai.grakn.graql.internal.pattern.Patterns)1 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)1 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)1 TypeAtom (ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom)1 ValuePredicate (ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)1 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)1