Search in sources :

Example 16 with VarPattern

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

the class InsertQueryTest method testIterateMatchInsertResults.

@Test
public void testIterateMatchInsertResults() {
    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);
    InsertQuery query = qb.match(var("x").isa("language")).insert(var("x").has("name", "HELLO"));
    Iterator<Answer> results = query.iterator();
    assertNotExists(qb, var().isa("language").has("name", "123").has("name", "HELLO"));
    assertNotExists(qb, var().isa("language").has("name", "456").has("name", "HELLO"));
    Answer result1 = results.next();
    assertEquals(ImmutableSet.of(var("x")), result1.vars());
    boolean query123 = qb.match(var().isa("language").has("name", "123").has("name", "HELLO")).iterator().hasNext();
    boolean query456 = qb.match(var().isa("language").has("name", "456").has("name", "HELLO")).iterator().hasNext();
    // Check if one of the matches have had the insert executed correctly
    boolean oneExists = query123 != query456;
    assertTrue("A match insert was not executed correctly for only one match", oneExists);
    // Check that both are inserted correctly
    Answer result2 = results.next();
    assertEquals(ImmutableSet.of(var("x")), result1.vars());
    assertExists(qb, var().isa("language").has("name", "123").has("name", "HELLO"));
    assertExists(qb, var().isa("language").has("name", "456").has("name", "HELLO"));
    assertFalse(results.hasNext());
    assertNotEquals(result1.get("x"), result2.get("x"));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Answer(ai.grakn.graql.admin.Answer) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 17 with VarPattern

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

the class InsertQueryTest method whenSettingTwoTypes_Throw.

@Test
public void whenSettingTwoTypes_Throw() {
    EntityType movie = movieKB.tx().getEntityType("movie");
    EntityType person = movieKB.tx().getEntityType("person");
    // We have to construct it this way because you can't have two `isa`s normally
    // TODO: less bad way?
    VarPattern varPattern = Patterns.varPattern(var("x"), ImmutableSet.of(IsaProperty.of(label("movie").admin()), IsaProperty.of(label("person").admin())));
    // We don't know in what order the message will be
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(isOneOf(GraqlQueryException.insertMultipleProperties(varPattern, "isa", movie, person).getMessage(), GraqlQueryException.insertMultipleProperties(varPattern, "isa", person, movie).getMessage()));
    movieKB.tx().graql().insert(var("x").isa("movie"), var("x").isa("person")).execute();
}
Also used : EntityType(ai.grakn.concept.EntityType) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 18 with VarPattern

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

the class PatternPropertyTests method theConjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern.

@Property
public void theConjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern(Pattern pattern1, Pattern pattern2) {
    Set<VarPattern> union = Sets.union(pattern1.admin().varPatterns(), pattern2.admin().varPatterns());
    Pattern conjunction = pattern1.and(pattern2);
    assertEquals(union, conjunction.admin().varPatterns());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) VarPattern(ai.grakn.graql.VarPattern) Property(com.pholser.junit.quickcheck.Property) VarProperty(ai.grakn.graql.admin.VarProperty)

Example 19 with VarPattern

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

the class GraqlTraversalTest method whenPlanningSimpleBinaryRelationQuery_ApplyRolePlayerOptimisation.

@Test
public void whenPlanningSimpleBinaryRelationQuery_ApplyRolePlayerOptimisation() {
    VarPattern rel = var("x").rel("y").rel("z");
    GraqlTraversal graqlTraversal = semiOptimal(rel);
    assertThat(graqlTraversal, anyOf(matches("\\{§x-\\[" + ROLE_PLAYER_EDGE + ":#.*]->§.* §x-\\[" + ROLE_PLAYER_EDGE + ":#.*]->§.* #.*\\[neq:#.*]}"), matches("\\{§.*<-\\[" + ROLE_PLAYER_EDGE + ":#.*]-§x-\\[" + ROLE_PLAYER_EDGE + ":#.*]->§.* #.*\\[neq:#.*]}")));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 20 with VarPattern

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

the class GraqlTraversalTest method whenPlanningBinaryRelationQueryWithType_ApplyRolePlayerOptimisation.

@Test
public void whenPlanningBinaryRelationQueryWithType_ApplyRolePlayerOptimisation() {
    VarPattern rel = var("x").rel("y").rel("z").isa("marriage");
    GraqlTraversal graqlTraversal = semiOptimal(rel);
    assertThat(graqlTraversal, anyOf(matches(".*§x-\\[" + ROLE_PLAYER_EDGE + ":#.* rels:marriage]->§.* §x-\\[" + ROLE_PLAYER_EDGE + ":#.* rels:marriage]->§.* #.*\\[neq:#.*].*"), matches(".*§.*<-\\[" + ROLE_PLAYER_EDGE + ":#.* rels:marriage]-§x-\\[" + ROLE_PLAYER_EDGE + ":#.* rels:marriage]->§.* #.*\\[neq:#.*].*")));
}
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