Search in sources :

Example 91 with Match

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

the class DefineQueryTest method testDefineReferenceByName.

@Test
public void testDefineReferenceByName() {
    String roleTypeLabel = HAS_OWNER.getLabel("title").getValue();
    qb.define(label("new-type").sub(Schema.MetaSchema.ENTITY.getLabel().getValue()), label("new-type").plays(roleTypeLabel)).execute();
    qb.insert(var("x").isa("new-type")).execute();
    Match typeQuery = qb.match(var("n").label("new-type"));
    assertEquals(1, typeQuery.stream().count());
    // We checked count ahead of time
    // noinspection OptionalGetWithoutIsPresent
    EntityType newType = typeQuery.get("n").findFirst().get().asEntityType();
    assertTrue(newType.plays().anyMatch(role -> role.equals(movies.tx().getRole(roleTypeLabel))));
    assertExists(qb, var().isa("new-type"));
}
Also used : EntityType(ai.grakn.concept.EntityType) VarPattern(ai.grakn.graql.VarPattern) BOOLEAN(ai.grakn.concept.AttributeType.DataType.BOOLEAN) KEY_VALUE(ai.grakn.util.Schema.ImplicitType.KEY_VALUE) Graql(ai.grakn.graql.Graql) RELATIONSHIP(ai.grakn.util.Schema.MetaSchema.RELATIONSHIP) EntityType(ai.grakn.concept.EntityType) GraqlTestUtil.assertExists(ai.grakn.util.GraqlTestUtil.assertExists) ENTITY(ai.grakn.util.Schema.MetaSchema.ENTITY) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) After(org.junit.After) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Match(ai.grakn.graql.Match) KEY(ai.grakn.util.Schema.ImplicitType.KEY) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Matchers.is(org.hamcrest.Matchers.is) Matchers.anyOf(org.hamcrest.Matchers.anyOf) RULE(ai.grakn.util.Schema.MetaSchema.RULE) ValueProperty(ai.grakn.graql.internal.pattern.property.ValueProperty) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Role(ai.grakn.concept.Role) Answer(ai.grakn.graql.admin.Answer) ROLE(ai.grakn.util.Schema.MetaSchema.ROLE) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) AttributeType(ai.grakn.concept.AttributeType) CoreMatchers.allOf(org.hamcrest.CoreMatchers.allOf) ExpectedException(org.junit.rules.ExpectedException) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) KEY_OWNER(ai.grakn.util.Schema.ImplicitType.KEY_OWNER) Before(org.junit.Before) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.hasItemInArray(org.hamcrest.Matchers.hasItemInArray) Graql.label(ai.grakn.graql.Graql.label) MovieKB(ai.grakn.test.kbs.MovieKB) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Rule(org.junit.Rule) Graql.parse(ai.grakn.graql.Graql.parse) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty) GraqlTestUtil.assertNotExists(ai.grakn.util.GraqlTestUtil.assertNotExists) GraknException(ai.grakn.exception.GraknException) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 92 with Match

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

the class DefineQueryTest method testDefineSubResourceType.

@Test
public void testDefineSubResourceType() {
    qb.define(label("my-type").sub(Schema.MetaSchema.ATTRIBUTE.getLabel().getValue()).datatype(AttributeType.DataType.STRING), label("sub-type").sub("my-type")).execute();
    Match match = qb.match(var("x").label("sub-type"));
    AttributeType.DataType datatype = match.iterator().next().get("x").asAttributeType().getDataType();
    Assert.assertEquals(AttributeType.DataType.STRING, datatype);
}
Also used : AttributeType(ai.grakn.concept.AttributeType) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 93 with Match

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

the class DefineQueryTest method testResourceTypeRegex.

@Test
public void testResourceTypeRegex() {
    qb.define(label("greeting").sub(Schema.MetaSchema.ATTRIBUTE.getLabel().getValue()).datatype(AttributeType.DataType.STRING).regex("hello|good day")).execute();
    Match match = qb.match(var("x").label("greeting"));
    assertEquals("hello|good day", match.get("x").findFirst().get().asAttributeType().getRegex());
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 94 with Match

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

the class DeleteQueryTest method afterDeletingAllInstances_TheTypeCanBeUndefined.

@Test
public void afterDeletingAllInstances_TheTypeCanBeUndefined() {
    Match movie = qb.match(x.isa("movie"));
    assertNotNull(movieKB.tx().getEntityType("movie"));
    assertExists(movie);
    movie.delete(x).execute();
    assertNotNull(movieKB.tx().getEntityType("movie"));
    assertNotExists(movie);
    qb.undefine(label("movie").sub("production")).execute();
    assertNull(movieKB.tx().getEntityType("movie"));
}
Also used : Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 95 with Match

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

the class QueryErrorTest method testGetNonExistentVariable.

@Test
public void testGetNonExistentVariable() {
    Match match = qb.match(var("x").isa("movie"));
    Stream<Concept> concepts = match.get("y");
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(ErrorMessage.VARIABLE_NOT_IN_QUERY.getMessage(Graql.var("y")));
    // noinspection ResultOfMethodCallIgnored
    concepts.count();
}
Also used : Concept(ai.grakn.concept.Concept) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Aggregations

Match (ai.grakn.graql.Match)103 Test (org.junit.Test)98 Var (ai.grakn.graql.Var)8 ConceptId (ai.grakn.concept.ConceptId)5 Concept (ai.grakn.concept.Concept)4 SchemaConcept (ai.grakn.concept.SchemaConcept)4 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 AttributeType (ai.grakn.concept.AttributeType)3 Label (ai.grakn.concept.Label)3 Relationship (ai.grakn.concept.Relationship)3 Printer (ai.grakn.graql.Printer)3 QueryBuilder (ai.grakn.graql.QueryBuilder)3 Answer (ai.grakn.graql.admin.Answer)3 MatchableConcept (ai.grakn.matcher.MatchableConcept)3 Matcher (org.hamcrest.Matcher)3 RelationshipType (ai.grakn.concept.RelationshipType)2 Role (ai.grakn.concept.Role)2 Thing (ai.grakn.concept.Thing)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2