Search in sources :

Example 46 with Match

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

the class MatchTest method testResourceMatch.

@Test
public void testResourceMatch() throws ParseException {
    Match query = qb.match(x.has("release-date", LocalDate.of(1986, 3, 3).atStartOfDay()));
    assertThat(query, variable(x, contains(spy)));
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 47 with Match

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

the class MatchTest method testMovieQuery.

@Test
public void testMovieQuery() {
    Match query = qb.match(x.isa("movie"));
    assertThat(query, variable(x, containsAllMovies));
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 48 with Match

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

the class MatchTest method testMatchAllPairs.

@Test
public void testMatchAllPairs() {
    int numConcepts = (int) qb.match(x).stream().count();
    Match pairs = qb.match(x, y);
    // We expect there to be a result for every pair of concepts
    assertThat(pairs, iterableWithSize(numConcepts * numConcepts));
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 49 with Match

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

the class QueryToStringTest method whenCallingToStringOnAQueryWithAContainsPredicate_ResultIsCorrect.

@Test
public void whenCallingToStringOnAQueryWithAContainsPredicate_ResultIsCorrect() {
    Match match = match(var("x").val(contains(var("y"))));
    assertEquals("match $x val contains $y;", match.toString());
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 50 with Match

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

Match (ai.grakn.graql.Match)103 Test (org.junit.Test)98 Var (ai.grakn.graql.Var)8 ConceptId (ai.grakn.concept.ConceptId)5 Concept (ai.grakn.concept.Concept)4 SchemaConcept (ai.grakn.concept.SchemaConcept)4 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 AttributeType (ai.grakn.concept.AttributeType)3 Label (ai.grakn.concept.Label)3 Relationship (ai.grakn.concept.Relationship)3 Printer (ai.grakn.graql.Printer)3 QueryBuilder (ai.grakn.graql.QueryBuilder)3 Answer (ai.grakn.graql.admin.Answer)3 MatchableConcept (ai.grakn.matcher.MatchableConcept)3 Matcher (org.hamcrest.Matcher)3 RelationshipType (ai.grakn.concept.RelationshipType)2 Role (ai.grakn.concept.Role)2 Thing (ai.grakn.concept.Thing)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2