Search in sources :

Example 6 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class ReasonerTest method testParsingQueryWithResourceVariable.

@Test
public void testParsingQueryWithResourceVariable() {
    String patternString = "{$x isa person, has firstname $y;}";
    String patternString2 = "{$x isa person;$x has firstname $y;}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, snbKB.tx()), snbKB.tx());
    ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, snbKB.tx()), snbKB.tx());
    assertEquals(query, query2);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 7 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class QueryTest method testAlphaEquivalence_indirectTypes.

// tests alpha-equivalence of queries with indirect types
@Test
public void testAlphaEquivalence_indirectTypes() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{(entity-location: $x2, geo-entity: $x1) isa is-located-in;" + "$x1 isa $t1; $t1 sub geoObject;}";
    String patternString2 = "{(geo-entity: $y1, entity-location: $y2) isa is-located-in;" + "$y1 isa $t2; $t2 sub geoObject;}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, graph), graph);
    queryEquivalence(query, query2, true);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 8 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class RelationshipAtom method getExplicitRoles.

private Stream<Role> getExplicitRoles() {
    ReasonerQueryImpl parent = (ReasonerQueryImpl) getParentQuery();
    GraknTx graph = parent.tx();
    return getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(graph::<Role>getSchemaConcept);
}
Also used : GraknTx(ai.grakn.GraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 9 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class ResourceAtom method isRuleApplicableViaAtom.

@Override
public boolean isRuleApplicableViaAtom(Atom ruleAtom) {
    // findbugs complains about cast without it
    if (!(ruleAtom instanceof ResourceAtom))
        return false;
    ResourceAtom childAtom = (ResourceAtom) ruleAtom;
    ReasonerQueryImpl childQuery = (ReasonerQueryImpl) childAtom.getParentQuery();
    // check type bindings compatiblity
    Type parentType = this.getParentQuery().getVarTypeMap().get(this.getVarName());
    Type childType = childQuery.getVarTypeMap().get(childAtom.getVarName());
    if (parentType != null && childType != null && areDisjointTypes(parentType, childType) || !childQuery.isTypeRoleCompatible(ruleAtom.getVarName(), parentType))
        return false;
    // check value predicate compatibility
    if (childAtom.getMultiPredicate().isEmpty() || getMultiPredicate().isEmpty())
        return true;
    for (ValuePredicate childPredicate : childAtom.getMultiPredicate()) {
        Iterator<ValuePredicate> parentIt = getMultiPredicate().iterator();
        boolean predicateCompatible = false;
        while (parentIt.hasNext() && !predicateCompatible) {
            ValuePredicate parentPredicate = parentIt.next();
            predicateCompatible = parentPredicate.isCompatibleWith(childPredicate);
        }
        if (!predicateCompatible)
            return false;
    }
    return true;
}
Also used : Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 10 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class RuleUtils method getDependentRules.

/**
 * @param query top query
 * @return all rules that are reachable from the entry types
 */
public static Set<InferenceRule> getDependentRules(ReasonerQueryImpl query) {
    final Equivalence<Atom> equivalence = new Equivalence<Atom>() {

        @Override
        protected boolean doEquivalent(Atom a1, Atom a2) {
            return a1.isAlphaEquivalent(a2);
        }

        @Override
        protected int doHash(Atom a) {
            return a.alphaEquivalenceHashCode();
        }
    };
    Set<InferenceRule> rules = new HashSet<>();
    Set<Equivalence.Wrapper<Atom>> visitedAtoms = new HashSet<>();
    Stack<Equivalence.Wrapper<Atom>> atoms = new Stack<>();
    query.selectAtoms().stream().map(equivalence::wrap).forEach(atoms::push);
    while (!atoms.isEmpty()) {
        Equivalence.Wrapper<Atom> wrappedAtom = atoms.pop();
        Atom atom = wrappedAtom.get();
        if (!visitedAtoms.contains(wrappedAtom) && atom != null) {
            atom.getApplicableRules().peek(rules::add).flatMap(rule -> rule.getBody().selectAtoms().stream()).map(equivalence::wrap).filter(at -> !visitedAtoms.contains(at)).filter(at -> !atoms.contains(at)).forEach(atoms::add);
            visitedAtoms.add(wrappedAtom);
        }
    }
    return rules;
}
Also used : HashSet(java.util.HashSet) Stream(java.util.stream.Stream) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Equivalence(com.google.common.base.Equivalence) Iterator(java.util.Iterator) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraknTx(ai.grakn.GraknTx) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Schema(ai.grakn.util.Schema) Rule(ai.grakn.concept.Rule) Stack(java.util.Stack) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Stack(java.util.Stack) Equivalence(com.google.common.base.Equivalence) HashSet(java.util.HashSet)

Aggregations

ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)28 Test (org.junit.Test)20 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)11 ResolutionPlan (ai.grakn.graql.internal.reasoner.plan.ResolutionPlan)7 Var (ai.grakn.graql.Var)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 GraknTx (ai.grakn.GraknTx)3 Rule (ai.grakn.concept.Rule)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 Answer (ai.grakn.graql.admin.Answer)3 Atomic (ai.grakn.graql.admin.Atomic)3 Unifier (ai.grakn.graql.admin.Unifier)3 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)3 ValuePredicate (ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)3 Sets (com.google.common.collect.Sets)3