Search in sources :

Example 1 with Graql

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

the class QueryVisitor method visitPropHas.

@Override
public UnaryOperator<VarPattern> visitPropHas(GraqlParser.PropHasContext ctx) {
    Label type = visitLabel(ctx.label());
    VarPattern relation = Optional.ofNullable(ctx.relation).map(this::getVariable).orElseGet(Graql::var);
    VarPattern resource = Optional.ofNullable(ctx.resource).map(this::getVariable).orElseGet(Graql::var);
    if (ctx.predicate() != null) {
        resource = resource.val(visitPredicate(ctx.predicate()));
    }
    VarPattern finalResource = resource;
    return var -> var.has(type, finalResource, relation);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Token(org.antlr.v4.runtime.Token) Graql(ai.grakn.graql.Graql) ConnectedComponentQuery(ai.grakn.graql.analytics.ConnectedComponentQuery) UnaryOperator(java.util.function.UnaryOperator) Label(ai.grakn.concept.Label) Map(java.util.Map) ComputeQuery(ai.grakn.graql.ComputeQuery) ConceptId(ai.grakn.concept.ConceptId) Aggregate(ai.grakn.graql.Aggregate) MinQuery(ai.grakn.graql.analytics.MinQuery) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) MaxQuery(ai.grakn.graql.analytics.MaxQuery) ImmutableSet(com.google.common.collect.ImmutableSet) AggregateQuery(ai.grakn.graql.AggregateQuery) ImmutableMap(com.google.common.collect.ImmutableMap) Graql.eq(ai.grakn.graql.Graql.eq) Collection(java.util.Collection) StringUtil(ai.grakn.util.StringUtil) Set(java.util.Set) ValuePredicate(ai.grakn.graql.ValuePredicate) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Match(ai.grakn.graql.Match) List(java.util.List) GraqlParser(ai.grakn.graql.internal.antlr.GraqlParser) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Optional(java.util.Optional) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DegreeQuery(ai.grakn.graql.analytics.DegreeQuery) DeleteQuery(ai.grakn.graql.DeleteQuery) GraqlBaseVisitor(ai.grakn.graql.internal.antlr.GraqlBaseVisitor) StdQuery(ai.grakn.graql.analytics.StdQuery) InsertQuery(ai.grakn.graql.InsertQuery) LocalDateTime(java.time.LocalDateTime) Function(java.util.function.Function) PathQuery(ai.grakn.graql.analytics.PathQuery) AttributeType(ai.grakn.concept.AttributeType) MeanQuery(ai.grakn.graql.analytics.MeanQuery) CommonUtil(ai.grakn.util.CommonUtil) SumQuery(ai.grakn.graql.analytics.SumQuery) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Query(ai.grakn.graql.Query) MedianQuery(ai.grakn.graql.analytics.MedianQuery) Graql.and(ai.grakn.graql.Graql.and) Collectors.toList(java.util.stream.Collectors.toList) Order(ai.grakn.graql.Order) NamedAggregate(ai.grakn.graql.NamedAggregate) DateTimeFormatter(java.time.format.DateTimeFormatter) CountQuery(ai.grakn.graql.analytics.CountQuery) PathsQuery(ai.grakn.graql.analytics.PathsQuery) Pattern(ai.grakn.graql.Pattern) Label(ai.grakn.concept.Label) VarPattern(ai.grakn.graql.VarPattern) Graql(ai.grakn.graql.Graql)

Example 2 with Graql

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

the class ShortestPathTest method testMultiplePathInSubGraph.

@Test
public void testMultiplePathInSubGraph() {
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    List<List<Concept>> allPaths;
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        if (GraknTestUtil.usingJanus()) {
            // If no path is found in the vertex program, NoResultException is thrown to skip map reduce.
            // Tinker doesn't handle this well, so the next graql query would find the graph is empty.
            allPaths = graph.graql().compute().paths().in(thing, anotherThing).to(entityId1).from(entityId4).execute();
            assertEquals(0, allPaths.size());
        }
        correctPaths.add(Lists.newArrayList(entityId1, relationId12, entityId2, relationId24, entityId4));
        correctPaths.add(Lists.newArrayList(entityId1, relationId13, entityId3, relationId34, entityId4));
        allPaths = graph.graql().compute().paths().from(entityId1).to(entityId4).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Graql

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

the class SNBInferenceTest method testQueryConsistency.

/**
 * Tests transitivity and Bug #7416
 */
@Test
public void testQueryConsistency() {
    QueryBuilder iqb = snbGraph.tx().graql().infer(true);
    String queryString = "match $x isa person; $y isa place; ($x, $y) isa resides;" + "$z isa person;$z has name 'Miguel Gonzalez'; ($x, $z) isa knows; get $x, $y;";
    String queryString2 = "match $x isa person; $y isa person;$y has name 'Miguel Gonzalez';" + "$z isa place; ($x, $y) isa knows; ($x, $z) isa resides; get $x, $z;";
    Unifier unifier = new UnifierImpl(ImmutableMap.of(Graql.var("z"), Graql.var("y")));
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute().stream().map(a -> a.unify(unifier)).collect(Collectors.toList());
    assertCollectionsEqual(answers, answers2);
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) ImmutableMap(com.google.common.collect.ImmutableMap) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Graql(ai.grakn.graql.Graql) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Answer(ai.grakn.graql.admin.Answer) GraqlTestUtil.assertQueriesEqual(ai.grakn.util.GraqlTestUtil.assertQueriesEqual) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) SNBKB(ai.grakn.test.kbs.SNBKB) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) Unifier(ai.grakn.graql.admin.Unifier) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) QueryBuilder(ai.grakn.graql.QueryBuilder) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 4 with Graql

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

the class ReasoningTests method inferrableRelationWithRolePlayersSharingResource.

// tests whether shared resources are recognised correctly
@Test
public void inferrableRelationWithRolePlayersSharingResource() {
    QueryBuilder qb = testSet29.tx().graql().infer(true);
    String queryString = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name $n;" + "$y has name $n;" + "get;";
    String queryString2 = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name $n;" + "$y has name $n;" + "$n val 'a';" + "get;";
    String queryString3 = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name 'a';" + "$y has name 'a';" + "get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    List<Answer> answers2 = qb.<GetQuery>parse(queryString2).execute();
    List<Answer> answers3 = qb.<GetQuery>parse(queryString3).execute();
    assertEquals(answers.size(), 3);
    answers.forEach(ans -> {
        assertEquals(ans.size(), 3);
        assertEquals(ans.get("x"), ans.get("y"));
    });
    assertEquals(answers2.size(), 1);
    assertEquals(answers3.size(), 1);
    answers2.stream().map(a -> a.project(Sets.newHashSet(var("x"), var("y")))).forEach(a -> assertTrue(answers3.contains(a)));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 5 with Graql

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

the class ReasoningTests method whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults.

@Test
public void whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults() {
    QueryBuilder withInference = testSet14.tx().graql().infer(true);
    QueryBuilder withoutInference = testSet14.tx().graql().infer(false);
    VarPattern owner = label(HAS_OWNER.getLabel("resource"));
    VarPattern value = label(HAS_VALUE.getLabel("resource"));
    VarPattern hasRes = label(HAS.getLabel("resource"));
    Function<QueryBuilder, GetQuery> query = qb -> qb.match(var().rel(owner, "x").rel(value, "y").isa(hasRes), // This pattern is added only to encourage reasoning to activate
    var("a").has("resource", var("b"))).get();
    Set<Answer> resultsWithoutInference = query.apply(withoutInference).stream().collect(toSet());
    Set<Answer> resultsWithInference = query.apply(withInference).stream().collect(toSet());
    assertThat(resultsWithoutInference, not(empty()));
    assertThat(Sets.difference(resultsWithoutInference, resultsWithInference), empty());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

Graql (ai.grakn.graql.Graql)5 List (java.util.List)5 Label (ai.grakn.concept.Label)4 GetQuery (ai.grakn.graql.GetQuery)4 QueryBuilder (ai.grakn.graql.QueryBuilder)4 Set (java.util.Set)4 GraknTx (ai.grakn.GraknTx)3 Graql.label (ai.grakn.graql.Graql.label)3 Var (ai.grakn.graql.Var)3 VarPattern (ai.grakn.graql.VarPattern)3 Answer (ai.grakn.graql.admin.Answer)3 SampleKBContext (ai.grakn.test.rule.SampleKBContext)3 GraknTestUtil (ai.grakn.util.GraknTestUtil)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Assume.assumeTrue (org.junit.Assume.assumeTrue)3 Test (org.junit.Test)3 AttributeType (ai.grakn.concept.AttributeType)2 ConceptId (ai.grakn.concept.ConceptId)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2 Graql.var (ai.grakn.graql.Graql.var)2