Search in sources :

Example 76 with QueryBuilder

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

the class MatchTest method testGraqlPlaysSemanticsMatchGraphAPI.

@Test
public void testGraqlPlaysSemanticsMatchGraphAPI() {
    GraknTx tx = emptyKB.tx();
    QueryBuilder qb = tx.graql();
    Label a = Label.of("a");
    Label b = Label.of("b");
    Label c = Label.of("c");
    Label d = Label.of("d");
    Label e = Label.of("e");
    Label f = Label.of("f");
    qb.define(Graql.label(c).sub(Graql.label(b).sub(Graql.label(a).sub("entity"))), Graql.label(f).sub(Graql.label(e).sub(Graql.label(d).sub("role"))), Graql.label(b).plays(Graql.label(e))).execute();
    Stream.of(a, b, c, d, e, f).forEach(type -> {
        Set<Concept> graqlPlays = qb.match(Graql.label(type).plays(x)).get(x).collect(Collectors.toSet());
        Collection<Role> graphAPIPlays;
        SchemaConcept schemaConcept = tx.getSchemaConcept(type);
        if (schemaConcept.isType()) {
            graphAPIPlays = schemaConcept.asType().plays().collect(toSet());
        } else {
            graphAPIPlays = Collections.EMPTY_SET;
        }
        assertEquals(graqlPlays, graphAPIPlays);
    });
    Stream.of(d, e, f).forEach(type -> {
        Set<Concept> graqlPlayedBy = qb.match(x.plays(Graql.label(type))).get(x).collect(toSet());
        Collection<Type> graphAPIPlayedBy = tx.<Role>getSchemaConcept(type).playedByTypes().collect(toSet());
        assertEquals(graqlPlayedBy, graphAPIPlayedBy);
    });
}
Also used : Concept(ai.grakn.concept.Concept) MatchableConcept(ai.grakn.matcher.MatchableConcept) SchemaConcept(ai.grakn.concept.SchemaConcept) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) AttributeType(ai.grakn.concept.AttributeType) GraknMatchers.hasType(ai.grakn.matcher.GraknMatchers.hasType) Type(ai.grakn.concept.Type) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 77 with QueryBuilder

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

the class ExplanationTest method testExplanationConsistency.

@Test
public void testExplanationConsistency() {
    GraknTx genealogyGraph = genealogyKB.tx();
    final long limit = 3;
    QueryBuilder iqb = genealogyGraph.graql().infer(true);
    String queryString = "match " + "($x, $y) isa cousins;" + "limit " + limit + ";" + "get;";
    List<Answer> answers = iqb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), limit);
    answers.forEach(answer -> {
        testExplanation(answer);
        String specificQuery = "match " + "$x id '" + answer.get(var("x")).getId().getValue() + "';" + "$y id '" + answer.get(var("y")).getId().getValue() + "';" + "(cousin: $x, cousin: $y) isa cousins;" + "limit 1; get;";
        Answer specificAnswer = Iterables.getOnlyElement(iqb.<GetQuery>parse(specificQuery).execute());
        assertEquals(answer, specificAnswer);
        testExplanation(specificAnswer);
    });
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 78 with QueryBuilder

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

the class QueryValidityTest method whenQueryingForMismatchedRelationTypeLabel_Throws.

@Test
public void whenQueryingForMismatchedRelationTypeLabel_Throws() throws GraqlQueryException {
    QueryBuilder qb = testContext.tx().graql().infer(true);
    String queryString = "match ($x, $y) isa name; get;";
    expectedException.expect(GraqlQueryException.class);
    qb.<GetQuery>parse(queryString).execute();
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 79 with QueryBuilder

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

the class QueryValidityTest method whenQueryingForInexistentRelationTypeId_emptyResultReturned.

@Test
public void whenQueryingForInexistentRelationTypeId_emptyResultReturned() {
    QueryBuilder qb = testContext.tx().graql().infer(true);
    String queryString = "match ($x, $y) isa $type; $type id 'V123'; get;";
    String queryString2 = "match $r ($x, $y) isa $type; $r id 'V123'; get;";
    assertThat(qb.<GetQuery>parse(queryString).execute(), empty());
    assertThat(qb.<GetQuery>parse(queryString2).execute(), empty());
}
Also used : GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 80 with QueryBuilder

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

the class QueryValidityTest method whenQueryingForIllegalRolePlayer_emptyResultReturned.

@Test
public void whenQueryingForIllegalRolePlayer_emptyResultReturned() {
    QueryBuilder qb = testContext.tx().graql().infer(true);
    String queryString = "match ($x, $y) isa binary; $x isa anotherNoRoleEntity; get;";
    assertThat(qb.<GetQuery>parse(queryString).execute(), empty());
}
Also used : 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