Search in sources :

Example 1 with HAS

use of ai.grakn.util.Schema.ImplicitType.HAS 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 2 with HAS

use of ai.grakn.util.Schema.ImplicitType.HAS 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

GraknTx (ai.grakn.GraknTx)2 Label (ai.grakn.concept.Label)2 GetQuery (ai.grakn.graql.GetQuery)2 Graql (ai.grakn.graql.Graql)2 Graql.label (ai.grakn.graql.Graql.label)2 Graql.var (ai.grakn.graql.Graql.var)2 QueryBuilder (ai.grakn.graql.QueryBuilder)2 Var (ai.grakn.graql.Var)2 VarPattern (ai.grakn.graql.VarPattern)2 Answer (ai.grakn.graql.admin.Answer)2 SampleKBContext (ai.grakn.test.rule.SampleKBContext)2 GraknTestUtil (ai.grakn.util.GraknTestUtil)2 HAS (ai.grakn.util.Schema.ImplicitType.HAS)2 HAS_OWNER (ai.grakn.util.Schema.ImplicitType.HAS_OWNER)2 HAS_VALUE (ai.grakn.util.Schema.ImplicitType.HAS_VALUE)2 Sets (com.google.common.collect.Sets)2 List (java.util.List)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors.toSet (java.util.stream.Collectors.toSet)2