Search in sources :

Example 6 with Answer

use of ai.grakn.graql.admin.Answer 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 7 with Answer

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

the class ReasonerTest method testReasoningWithQueryContainingRelationVariableWithMaterialisation_requeryingDoesntCreateDuplicates.

@Test
public void testReasoningWithQueryContainingRelationVariableWithMaterialisation_requeryingDoesntCreateDuplicates() {
    String queryString = "match $x isa is-located-in; get;";
    String queryString2 = "match $x (geo-entity: $x1, entity-location: $x2) isa is-located-in; get $x;";
    String queryString3 = "match $x ($x1, $x2) isa is-located-in; get $x;";
    GetQuery query = geoKB.tx().graql().infer(true).parse(queryString);
    GetQuery query2 = geoKB2.tx().graql().infer(true).parse(queryString2);
    GetQuery query3 = geoKB3.tx().graql().infer(true).parse(queryString3);
    assertQueriesEqual(query, query2);
    assertQueriesEqual(query2, query3);
    List<Answer> requeriedAnswers = query.execute();
    List<Answer> requeriedAnswers2 = query2.execute();
    List<Answer> requeriedAnswers3 = query3.execute();
    assertCollectionsEqual(requeriedAnswers, requeriedAnswers2);
    assertCollectionsEqual(requeriedAnswers2, requeriedAnswers3);
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 8 with Answer

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

the class ReasonerTest method testReasoningWithQueryContainingContradiction.

@Test
public void testReasoningWithQueryContainingContradiction() {
    GraknTx graph = nonMaterialisedGeoKB.tx();
    // geoObject sub city always returns an empty set
    String queryString = "match ($x, $y) isa is-located-in;geoObject sub city; get;";
    QueryBuilder iqb = graph.graql().infer(true);
    List<Answer> answers = iqb.<GetQuery>parse(queryString).execute();
    assertThat(answers, empty());
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 9 with Answer

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

the class OntologicalQueryTest method allInstancesOfTypesThatCanHaveAGivenResourceType.

/**
 * HasAtom *
 */
@Test
public void allInstancesOfTypesThatCanHaveAGivenResourceType() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type has name; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    // 1 x noRoleEntity + 3 x 3 (hierarchy) anotherTwoRoleEntities
    assertEquals(answers.size(), 10);
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 10 with Answer

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

the class OntologicalQueryTest method allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults.

@Test
public void allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type sub relationship; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), tx.getRelationshipType("relationship").subs().flatMap(RelationshipType::instances).count());
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) RelationshipType(ai.grakn.concept.RelationshipType) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

Answer (ai.grakn.graql.admin.Answer)193 Test (org.junit.Test)164 QueryBuilder (ai.grakn.graql.QueryBuilder)104 GetQuery (ai.grakn.graql.GetQuery)69 GraknTx (ai.grakn.GraknTx)47 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)37 Concept (ai.grakn.concept.Concept)32 Var (ai.grakn.graql.Var)21 Set (java.util.Set)21 Unifier (ai.grakn.graql.admin.Unifier)19 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)18 Collectors (java.util.stream.Collectors)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 Sets (com.google.common.collect.Sets)16 List (java.util.List)16 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)15 Stream (java.util.stream.Stream)15 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)14 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)12