Search in sources :

Example 1 with QueryBuilder

use of ai.grakn.graql.QueryBuilder 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 QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class AtomicQueryTest method testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred.

@Test
public void testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
    EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
    QueryBuilder qb = graph.graql().infer(false);
    Concept firstEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity1; get;").execute()).get("x");
    Concept secondEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity2; get;").execute()).get("x");
    ReasonerAtomicQuery relationQuery = ReasonerQueries.atomic(conjunction("{" + "$r (role1: $x, role2: $y);" + "$x id " + firstEntity.getId().getValue() + ";" + "$y id " + secondEntity.getId().getValue() + ";" + "}", graph), graph);
    assertEquals(relationQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("r").asRelationship().isInferred(), true);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryBuilder(ai.grakn.graql.QueryBuilder) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 3 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class BenchmarkTests method testDiagonal.

/**
 * single-rule mimicking transitivity test rule defined by two-hop relations
 * Initial data arranged in N x N square grid.
 *
 * Rule:
 * (rel-from:$x, rel-to:$y) isa horizontal;
 * (rel-from:$y, rel-to:$z) isa horizontal;
 * (rel-from:$z, rel-to:$u) isa vertical;
 * (rel-from:$u, rel-to:$v) isa vertical;
 * ->
 * (rel-from:$x, rel-to:$v) isa diagonal;
 *
 * Initial data arranged as follows:
 *
 *  a_{i  , j} -  horizontal  - a_{i, j + 1}
 *       |                    |
 *    vertical             vertical
 *       |                    |
 *  a_{i+1, j} -  horizontal  - a_{i+1, j+1}
 *
 *  i e [0, N)
 *  j e [0, N)
 */
@Test
public void testDiagonal() {
    // 9604
    final int N = 10;
    final int limit = 10;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    // results @N = 40  1444  3.5s
    // results @N = 50  2304    8s    / 1s
    // results @N = 100 9604  loading takes ages
    SampleKBContext kb = DiagonalKB.context(N, N);
    QueryBuilder iqb = kb.tx().graql().infer(true).materialise(false);
    String queryString = "match (rel-from: $x, rel-to: $y) isa diagonal; get;";
    GetQuery query = iqb.parse(queryString);
    executeQuery(query, "full");
    executeQuery(query.match().limit(limit).get(), "limit " + limit);
}
Also used : SampleKBContext(ai.grakn.test.rule.SampleKBContext) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 4 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class OntologicalQueryTest method allRolesGivenRelationRelates.

@Test
public void allRolesGivenRelationRelates() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match reifying-relation relates $x; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.stream().map(ans -> ans.get("x")).collect(Collectors.toSet()), tx.getRelationshipType("reifying-relation").relates().collect(Collectors.toSet()));
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 5 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasonerTest method testReasoningWithQueryContainingSub.

@Test
public void testReasoningWithQueryContainingSub() {
    GraknTx graph = nonMaterialisedGeoKB.tx();
    String queryString = "match " + "$x isa $type;$type sub geoObject;" + "(geo-entity: $x, entity-location: $y) isa is-located-in; $y isa country;" + "$y has name 'Poland';" + "$x has name $name; get;";
    String queryString2 = "match " + "$x isa $type;{$type label 'region';} or {$type label 'city';} or {$type label 'geoObject';};" + "(geo-entity: $x, entity-location: $y) isa is-located-in;$y isa country;" + "$y has name 'Poland';" + "$x has name $name; get;";
    QueryBuilder iqb = graph.graql().infer(true);
    GetQuery query = iqb.parse(queryString);
    GetQuery query2 = iqb.parse(queryString2);
    assertQueriesEqual(query, query2);
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (ai.grakn.graql.QueryBuilder)208 Test (org.junit.Test)202 Answer (ai.grakn.graql.admin.Answer)101 GetQuery (ai.grakn.graql.GetQuery)60 GraknTx (ai.grakn.GraknTx)51 SampleKBContext (ai.grakn.test.rule.SampleKBContext)20 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)18 Concept (ai.grakn.concept.Concept)16 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)9 Ignore (org.junit.Ignore)8 Var (ai.grakn.graql.Var)5 VarPattern (ai.grakn.graql.VarPattern)5 List (java.util.List)5 Label (ai.grakn.concept.Label)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Sets (com.google.common.collect.Sets)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 RelationshipType (ai.grakn.concept.RelationshipType)3 Graql (ai.grakn.graql.Graql)3 Graql.var (ai.grakn.graql.Graql.var)3