Search in sources :

Example 1 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class AtomicQueryTest method testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly.

@Test
public void testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String childString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
    String parentString = "match ($x, $y) isa is-located-in; get;";
    QueryBuilder qb = graph.graql().infer(false);
    GetQuery childQuery = qb.parse(childString);
    GetQuery parentQuery = qb.parse(parentString);
    Set<Answer> answers = childQuery.stream().collect(toSet());
    Set<Answer> fullAnswers = parentQuery.stream().collect(toSet());
    Atom childAtom = ReasonerQueries.atomic(conjunction(childQuery.match().admin().getPattern()), graph).getAtom();
    Atom parentAtom = ReasonerQueries.atomic(conjunction(parentQuery.match().admin().getPattern()), graph).getAtom();
    MultiUnifier multiUnifier = childAtom.getMultiUnifier(childAtom, UnifierType.RULE);
    Set<Answer> permutedAnswers = answers.stream().flatMap(a -> multiUnifier.stream().map(a::unify)).collect(Collectors.toSet());
    MultiUnifier multiUnifier2 = childAtom.getMultiUnifier(parentAtom, UnifierType.RULE);
    Set<Answer> permutedAnswers2 = answers.stream().flatMap(a -> multiUnifier2.stream().map(a::unify)).collect(Collectors.toSet());
    assertEquals(fullAnswers, permutedAnswers2);
    assertEquals(answers, permutedAnswers);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) ArrayList(java.util.ArrayList) GraqlTestUtil.assertExists(ai.grakn.util.GraqlTestUtil.assertExists) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ClassRule(org.junit.ClassRule) ExpectedException(org.junit.rules.ExpectedException) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Equivalence(com.google.common.base.Equivalence) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) List(java.util.List) Rule(org.junit.Rule) GeoKB(ai.grakn.test.kbs.GeoKB) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) GraqlTestUtil.assertNotExists(ai.grakn.util.GraqlTestUtil.assertNotExists) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Schema(ai.grakn.util.Schema) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Test(org.junit.Test)

Example 2 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class AtomicTest method testUnification_ParentHasFewerRelationPlayers.

@Test
public void testUnification_ParentHasFewerRelationPlayers() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String childString = "{(subRole1: $y, subRole2: $x) isa binary;}";
    String parentString = "{(subRole1: $x) isa binary;}";
    String parentString2 = "{(subRole2: $y) isa binary;}";
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction(childString, graph), graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction(parentString, graph), graph);
    ReasonerAtomicQuery parentQuery2 = ReasonerQueries.atomic(conjunction(parentString2, graph), graph);
    Atom childAtom = childQuery.getAtom();
    Atom parentAtom = parentQuery.getAtom();
    Atom parentAtom2 = parentQuery2.getAtom();
    List<Answer> childAnswers = childQuery.getQuery().execute();
    List<Answer> parentAnswers = parentQuery.getQuery().execute();
    List<Answer> parentAnswers2 = parentQuery2.getQuery().execute();
    Unifier unifier = childAtom.getUnifier(parentAtom);
    Unifier unifier2 = childAtom.getUnifier(parentAtom2);
    assertCollectionsEqual(parentAnswers, childAnswers.stream().map(a -> a.unify(unifier)).map(a -> a.project(parentQuery.getVarNames())).distinct().collect(Collectors.toList()));
    assertCollectionsEqual(parentAnswers2, childAnswers.stream().map(a -> a.unify(unifier2)).map(a -> a.project(parentQuery2.getVarNames())).distinct().collect(Collectors.toList()));
}
Also used : Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Assert.assertThat(org.junit.Assert.assertThat) HashMultimap(com.google.common.collect.HashMultimap) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ClassRule(org.junit.ClassRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Assert.assertEquals(org.junit.Assert.assertEquals) 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)

Example 3 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class PatternImplTest method testCNFToDNF.

@Test
public void testCNFToDNF() {
    Conjunction cnf = conjunction(disjunction(x, y, z), disjunction(a, b, c));
    Set<Conjunction<VarPatternAdmin>> dnf = set(conjunction(x, a), conjunction(x, b), conjunction(x, c), conjunction(y, a), conjunction(y, b), conjunction(y, c), conjunction(z, a), conjunction(z, b), conjunction(z, c));
    assertHasDNF(dnf, cnf);
}
Also used : Conjunction(ai.grakn.graql.admin.Conjunction) Test(org.junit.Test)

Example 4 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class TypeInferenceQueryTest method typedAnswers.

private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
    List<Answer> answers = new ArrayList<>();
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
    for (Type type : possibleTypes) {
        GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
        typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
    }
    return answers;
}
Also used : Iterables(com.google.common.collect.Iterables) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Answer(ai.grakn.graql.admin.Answer) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) CollectionUtils(org.apache.commons.collections.CollectionUtils) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GraqlTestUtil(ai.grakn.util.GraqlTestUtil) ExpectedException(org.junit.rules.ExpectedException) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) GetQuery(ai.grakn.graql.GetQuery) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 5 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class MatchInfer method stream.

@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
    // If the tx is not embedded, treat it like there is no transaction
    // TODO: this is dodgy - when queries don't contain transactions this can be fixed
    EmbeddedGraknTx<?> tx = optionalOr(optionalGraph, inner.tx().filter(t -> t instanceof EmbeddedGraknTx).map(t -> (EmbeddedGraknTx<?>) t)).orElseThrow(GraqlQueryException::noTx);
    if (!RuleUtils.hasRules(tx))
        return inner.stream(optionalGraph);
    validatePattern(tx);
    try {
        Iterator<Conjunction<VarPatternAdmin>> conjIt = getPattern().getDisjunctiveNormalForm().getPatterns().iterator();
        Conjunction<VarPatternAdmin> conj = conjIt.next();
        ReasonerQuery conjQuery = ReasonerQueries.create(conj, tx);
        conjQuery.checkValid();
        Stream<Answer> answerStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
        while (conjIt.hasNext()) {
            conj = conjIt.next();
            conjQuery = ReasonerQueries.create(conj, tx);
            Stream<Answer> localStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
            answerStream = Stream.concat(answerStream, localStream);
        }
        return answerStream.map(result -> result.project(getSelectedNames()));
    } catch (GraqlQueryException e) {
        System.err.println(e.getMessage());
        return Stream.empty();
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) Answer(ai.grakn.graql.admin.Answer) CommonUtil.optionalOr(ai.grakn.util.CommonUtil.optionalOr) Match(ai.grakn.graql.Match) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) Answer(ai.grakn.graql.admin.Answer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Conjunction(ai.grakn.graql.admin.Conjunction) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery)

Aggregations

Conjunction (ai.grakn.graql.admin.Conjunction)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)6 Answer (ai.grakn.graql.admin.Answer)5 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)5 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)5 Schema (ai.grakn.util.Schema)5 Iterables (com.google.common.collect.Iterables)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Concept (ai.grakn.concept.Concept)4 GetQuery (ai.grakn.graql.GetQuery)4 Atomic (ai.grakn.graql.admin.Atomic)4 Patterns (ai.grakn.graql.internal.pattern.Patterns)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Test (org.junit.Test)4 Graql.var (ai.grakn.graql.Graql.var)3 Var (ai.grakn.graql.Var)3 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)3 Unifier (ai.grakn.graql.admin.Unifier)3 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)3