Search in sources :

Example 11 with VarPattern

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

the class DefineQueryTest method assertDefine.

private void assertDefine(VarPattern... vars) {
    // Make sure vars don't exist
    for (VarPattern var : vars) {
        assertNotExists(qb, var);
    }
    // Define all vars
    qb.define(vars).execute();
    // Make sure all vars exist
    for (VarPattern var : vars) {
        assertExists(qb, var);
    }
    // Undefine all vars
    qb.undefine(vars).execute();
    // Make sure vars don't exist
    for (VarPattern var : vars) {
        assertNotExists(qb, var);
    }
}
Also used : VarPattern(ai.grakn.graql.VarPattern)

Example 12 with VarPattern

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

the class ReasoningTests method whenReasoningWithResourcesWithRelationVar_ResultsAreComplete.

@Test
public void whenReasoningWithResourcesWithRelationVar_ResultsAreComplete() {
    QueryBuilder qb = testSet14.tx().graql().infer(true);
    VarPattern has = var("x").has(Label.of("resource"), var("y"), var("r"));
    List<Answer> answers = qb.match(has).get().execute();
    assertEquals(answers.size(), 3);
    answers.forEach(a -> assertTrue(a.vars().contains(var("r"))));
}
Also used : Answer(ai.grakn.graql.admin.Answer) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 13 with VarPattern

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

the class RelationshipAtom method rewriteWithRelationVariable.

@Override
public RelationshipAtom rewriteWithRelationVariable() {
    VarPattern newVar = Graql.var().asUserDefined();
    VarPattern relVar = getPattern().admin().getProperty(IsaProperty.class).map(prop -> newVar.isa(prop.type())).orElse(newVar);
    for (RelationPlayer c : getRelationPlayers()) {
        VarPatternAdmin roleType = c.getRole().orElse(null);
        if (roleType != null) {
            relVar = relVar.rel(roleType, c.getRolePlayer());
        } else {
            relVar = relVar.rel(c.getRolePlayer());
        }
    }
    return create(relVar.admin(), getPredicateVariable(), getTypeId(), getParentQuery());
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) VarPattern(ai.grakn.graql.VarPattern) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Example 14 with VarPattern

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

the class InsertQueryTest method testMatchInsertQuery.

@Test
public void testMatchInsertQuery() {
    VarPattern language1 = var().isa("language").has("name", "123");
    VarPattern language2 = var().isa("language").has("name", "456");
    qb.insert(language1, language2).execute();
    assertExists(qb, language1);
    assertExists(qb, language2);
    qb.match(var("x").isa("language")).insert(var("x").has("name", "HELLO")).execute();
    assertExists(qb, var().isa("language").has("name", "123").has("name", "HELLO"));
    assertExists(qb, var().isa("language").has("name", "456").has("name", "HELLO"));
    qb.match(var("x").isa("language")).delete("x").execute();
    assertNotExists(qb, language1);
    assertNotExists(qb, language2);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 15 with VarPattern

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

the class InsertQueryTest method whenInsertingAResourceWithMultipleValues_Throw.

@Test
public void whenInsertingAResourceWithMultipleValues_Throw() {
    VarPattern varPattern = var().val("123").val("456").isa("title");
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(isOneOf(GraqlQueryException.insertMultipleProperties(varPattern, "val", "123", "456").getMessage(), GraqlQueryException.insertMultipleProperties(varPattern, "val", "456", "123").getMessage()));
    qb.insert(varPattern).execute();
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Aggregations

VarPattern (ai.grakn.graql.VarPattern)46 Test (org.junit.Test)28 Var (ai.grakn.graql.Var)12 Pattern (ai.grakn.graql.Pattern)11 Answer (ai.grakn.graql.admin.Answer)9 GraknTx (ai.grakn.GraknTx)8 Role (ai.grakn.concept.Role)8 Label (ai.grakn.concept.Label)7 RelationshipType (ai.grakn.concept.RelationshipType)7 Graql (ai.grakn.graql.Graql)7 Set (java.util.Set)7 ConceptId (ai.grakn.concept.ConceptId)6 EntityType (ai.grakn.concept.EntityType)6 List (java.util.List)6 Collectors.toSet (java.util.stream.Collectors.toSet)6 SchemaConcept (ai.grakn.concept.SchemaConcept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5 QueryBuilder (ai.grakn.graql.QueryBuilder)5 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5