Search in sources :

Example 41 with VarPattern

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

the class DefineQueryTest method whenDefiningANonRuleWithAWhenPattern_Throw.

@Test
public void whenDefiningANonRuleWithAWhenPattern_Throw() {
    VarPattern rule = label("yes").sub(label(ENTITY.getLabel())).when(var("x"));
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(anyOf(// Either we see "entity" and an unexpected "when"...
    allOf(containsString("unexpected property"), containsString("when")), // ...or we see "when" and don't find the expected "then"
    containsString(GraqlQueryException.insertNoExpectedProperty("then", rule.admin()).getMessage())));
    qb.define(rule).execute();
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 42 with VarPattern

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

the class UndefineQueryTest method whenUndefiningComplexSchema_TheEntireSchemaIsRemoved.

@Test
public void whenUndefiningComplexSchema_TheEntireSchemaIsRemoved() {
    Collection<VarPattern> schema = ImmutableList.of(label("pokemon").sub(ENTITY).has("pokedex-no").plays("ancestor").plays("descendant"), label("pokedex-no").sub(ATTRIBUTE).datatype(INTEGER), label("evolution").sub(RELATIONSHIP).relates("ancestor").relates("descendant"), label("ancestor").sub(ROLE), label("descendant").sub(ROLE));
    qb.define(schema).execute();
    EntityType pokemon = tx.getEntityType("pokemon");
    RelationshipType evolution = tx.getRelationshipType("evolution");
    AttributeType<Long> pokedexNo = tx.getAttributeType("pokedex-no");
    Role ancestor = tx.getRole("ancestor");
    Role descendant = tx.getRole("descendant");
    assertThat(pokemon.attributes().toArray(), arrayContaining(pokedexNo));
    assertThat(evolution.relates().toArray(), arrayContainingInAnyOrder(ancestor, descendant));
    assertThat(pokemon.plays().filter(r -> !r.isImplicit()).toArray(), arrayContainingInAnyOrder(ancestor, descendant));
    qb.undefine(schema).execute();
    assertNull(tx.getEntityType("pokemon"));
    assertNull(tx.getEntityType("evolution"));
    assertNull(tx.getAttributeType("pokedex-no"));
    assertNull(tx.getRole("ancestor"));
    assertNull(tx.getRole("descendant"));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) VarPattern(ai.grakn.graql.VarPattern) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 43 with VarPattern

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

the class InsertQueryTest method testInsertRepeat.

@Test
public void testInsertRepeat() {
    VarPattern language = var("x").has("name", "123").isa("language");
    InsertQuery query = qb.insert(language);
    assertEquals(0, qb.match(language).stream().count());
    query.execute();
    assertEquals(1, qb.match(language).stream().count());
    query.execute();
    assertEquals(2, qb.match(language).stream().count());
    query.execute();
    assertEquals(3, qb.match(language).stream().count());
    qb.match(language).delete("x").execute();
    assertEquals(0, qb.match(language).stream().count());
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 44 with VarPattern

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

the class InsertQueryTest method testInsertRelation.

@Test
public void testInsertRelation() {
    VarPattern rel = var("r").isa("has-genre").rel("genre-of-production", "x").rel("production-with-genre", "y");
    VarPattern x = var("x").has("title", "Godfather").isa("movie");
    VarPattern y = var("y").has("name", "comedy").isa("genre");
    VarPattern[] vars = new VarPattern[] { rel, x, y };
    Pattern[] patterns = new Pattern[] { rel, x, y };
    assertNotExists(qb.match(patterns));
    qb.insert(vars).execute();
    assertExists(qb, patterns);
    qb.match(patterns).delete("r").execute();
    assertNotExists(qb, patterns);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 45 with VarPattern

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

the class DefineQueryTest method whenDefiningARule_TheRuleIsInTheKB.

@Test
public void whenDefiningARule_TheRuleIsInTheKB() {
    Pattern when = qb.parser().parsePattern("$x isa entity");
    Pattern then = qb.parser().parsePattern("$x isa entity");
    VarPattern vars = label("my-rule").sub(label(RULE.getLabel())).when(when).then(then);
    qb.define(vars).execute();
    assertNotNull(movies.tx().getRule("my-rule"));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) 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