Search in sources :

Example 56 with GetQuery

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

the class QueryParserTest method testParseBooleanType.

@Test
public void testParseBooleanType() {
    GetQuery query = parse("match $x datatype boolean; get;");
    VarPatternAdmin var = query.match().admin().getPattern().varPatterns().iterator().next();
    // noinspection OptionalGetWithoutIsPresent
    DataTypeProperty property = var.getProperty(DataTypeProperty.class).get();
    Assert.assertEquals(AttributeType.DataType.BOOLEAN, property.dataType());
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GetQuery(ai.grakn.graql.GetQuery) DataTypeProperty(ai.grakn.graql.internal.pattern.property.DataTypeProperty) Test(org.junit.Test)

Example 57 with GetQuery

use of ai.grakn.graql.GetQuery 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 58 with GetQuery

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

the class SNBInferenceTest method testRecommendation.

@Test
public void testRecommendation() {
    QueryBuilder qb = snbGraph.tx().graql().infer(false);
    QueryBuilder iqb = snbGraph.tx().graql().infer(true);
    String queryString = "match $x isa person;($x, $y) isa recommendation; get;";
    String limitedQueryString = "match $x isa person;($x, $y) isa recommendation; limit 1; get;";
    GetQuery query = iqb.parse(queryString);
    GetQuery limitedQuery = iqb.parse(limitedQueryString);
    String explicitQuery = "match $x isa person;" + "{$x has name 'Alice';$y has name 'War of the Worlds';} or" + "{$x has name 'Bob';{$y has name 'Ducatti 1299';} or {$y has name 'The Good the Bad the Ugly';};} or" + "{$x has name 'Charlie';{$y has name 'Blizzard of Ozz';} or {$y has name 'Stratocaster';};} or " + "{$x has name 'Denis';{$y has name 'Colour of Magic';} or {$y has name 'Dorian Gray';};} or" + "{$x has name 'Frank';$y has name 'Nocturnes';} or" + "{$x has name 'Karl Fischer';{$y has name 'Faust';} or {$y has name 'Nocturnes';};} or " + "{$x has name 'Gary';$y has name 'The Wall';} or" + "{$x has name 'Charlie';" + "{$y has name 'Yngwie Malmsteen';} or {$y has name 'Cacophony';} or {$y has name 'Steve Vai';} or {$y has name 'Black Sabbath';};} or " + "{$x has name 'Gary';$y has name 'Pink Floyd';}; get;";
    long startTime = System.nanoTime();
    List<Answer> limitedAnswers = limitedQuery.execute();
    System.out.println("limited time: " + (System.nanoTime() - startTime) / 1e6);
    startTime = System.nanoTime();
    List<Answer> answers = query.execute();
    System.out.println("full time: " + (System.nanoTime() - startTime) / 1e6);
    assertCollectionsEqual(answers, qb.<GetQuery>parse(explicitQuery).execute());
    assertTrue(answers.containsAll(limitedAnswers));
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 59 with GetQuery

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

the class ReasoningTests method relationTypesAreCorrectlyInferredInConjunction_TypeArePresent.

// Expected result: no answers (if types were incorrectly inferred the query would yield answers)
@Test
public void relationTypesAreCorrectlyInferredInConjunction_TypeArePresent() {
    QueryBuilder qb = testSet28.tx().graql().infer(true);
    String queryString = "match " + "(role1: $x, role2: $y) isa relation1;" + "(role1: $y, role2: $z) isa relation1;" + "(role3: $z, role4: $w) isa relation3; get;";
    assertThat(qb.<GetQuery>parse(queryString).execute(), empty());
}
Also used : GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 60 with GetQuery

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

the class GetQueryTest method whenGettingResultsString_StringHasExpectedContents.

@Test
public void whenGettingResultsString_StringHasExpectedContents() {
    GetQuery query = qb.match(x.isa("movie")).get();
    List<String> resultsString = query.resultsString(Printers.graql(false)).collect(toList());
    assertThat(resultsString, everyItem(allOf(containsString("$x"), containsString("movie"), containsString(";"))));
}
Also used : GetQuery(ai.grakn.graql.GetQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

GetQuery (ai.grakn.graql.GetQuery)163 Test (org.junit.Test)161 Answer (ai.grakn.graql.admin.Answer)65 QueryBuilder (ai.grakn.graql.QueryBuilder)61 GraknTx (ai.grakn.GraknTx)51 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)23 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)12 Concept (ai.grakn.concept.Concept)9 List (java.util.List)8 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Set (java.util.Set)6 Label (ai.grakn.concept.Label)5 RelationshipType (ai.grakn.concept.RelationshipType)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)5 Sets (com.google.common.collect.Sets)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5