Search in sources :

Example 41 with Var

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

the class QueryParserTest method testCustomAggregate.

@Test
public void testCustomAggregate() {
    QueryBuilder qb = withoutGraph();
    QueryParser parser = qb.parser();
    parser.registerAggregate("get-any", args -> new GetAny((Var) args.get(0)));
    AggregateQuery<Concept> expected = qb.match(var("x").isa("movie")).aggregate(new GetAny(Graql.var("x")));
    AggregateQuery<Concept> parsed = qb.parse("match $x isa movie; aggregate get-any $x;");
    assertEquals(expected, parsed);
}
Also used : Concept(ai.grakn.concept.Concept) QueryParser(ai.grakn.graql.QueryParser) Var(ai.grakn.graql.Var) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 42 with Var

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

the class QueryParserTest method whenParsingDeleteQuery_ResultIsSameAsJavaGraql.

@Test
public void whenParsingDeleteQuery_ResultIsSameAsJavaGraql() {
    Var x = var("x");
    Var y = var("y");
    DeleteQuery expected = match(x.isa("movie").has("title", "The Title"), y.isa("movie")).delete(x, y);
    DeleteQuery parsed = parse("match $x isa movie has title 'The Title'; $y isa movie; delete $x, $y;");
    assertEquals(expected, parsed);
}
Also used : Var(ai.grakn.graql.Var) DeleteQuery(ai.grakn.graql.DeleteQuery) Test(org.junit.Test)

Example 43 with Var

use of ai.grakn.graql.Var 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 44 with Var

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

Example 45 with Var

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

the class QueryOperationExecutor method sortProperties.

/**
 * Produce a valid ordering of the properties by using the given dependency information.
 *
 * <p>
 *     This method uses a topological sort (Kahn's algorithm) in order to find a valid ordering.
 * </p>
 */
private ImmutableList<VarAndProperty> sortProperties() {
    ImmutableList.Builder<VarAndProperty> sorted = ImmutableList.builder();
    // invertedDependencies is intended to just be a 'view' on dependencies, so when dependencies is modified
    // we should always also modify invertedDependencies (and vice-versa).
    Multimap<VarAndProperty, VarAndProperty> dependencies = HashMultimap.create(this.dependencies);
    Multimap<VarAndProperty, VarAndProperty> invertedDependencies = HashMultimap.create();
    Multimaps.invertFrom(dependencies, invertedDependencies);
    Queue<VarAndProperty> propertiesWithoutDependencies = new ArrayDeque<>(Sets.filter(properties, property -> dependencies.get(property).isEmpty()));
    VarAndProperty property;
    // Retrieve the next property without any dependencies
    while ((property = propertiesWithoutDependencies.poll()) != null) {
        sorted.add(property);
        // We copy this into a new list because the underlying collection gets modified during iteration
        Collection<VarAndProperty> dependents = Lists.newArrayList(invertedDependencies.get(property));
        for (VarAndProperty dependent : dependents) {
            // Because the property has been removed, the dependent no longer needs to depend on it
            dependencies.remove(dependent, property);
            invertedDependencies.remove(property, dependent);
            boolean hasNoDependencies = dependencies.get(dependent).isEmpty();
            if (hasNoDependencies) {
                propertiesWithoutDependencies.add(dependent);
            }
        }
    }
    if (!dependencies.isEmpty()) {
        // This means there must have been a loop. Pick an arbitrary remaining var to display
        Var var = dependencies.keys().iterator().next().var();
        throw GraqlQueryException.insertRecursive(printableRepresentation(var));
    }
    return sorted.build();
}
Also used : PropertyExecutor(ai.grakn.graql.internal.pattern.property.PropertyExecutor) Concept(ai.grakn.concept.Concept) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Partition(ai.grakn.graql.internal.util.Partition) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) Patterns(ai.grakn.graql.internal.pattern.Patterns) VarPropertyInternal(ai.grakn.graql.internal.pattern.property.VarPropertyInternal) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) Maps(com.google.common.collect.Maps) Query(ai.grakn.graql.Query) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) DefineQuery(ai.grakn.graql.DefineQuery) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ImmutableList(com.google.common.collect.ImmutableList) Var(ai.grakn.graql.Var) ArrayDeque(java.util.ArrayDeque)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17