Search in sources :

Example 21 with AttributeType

use of ai.grakn.concept.AttributeType in project grakn by graknlabs.

the class BenchmarkIT method loadRuleChainData.

private void loadRuleChainData(int N) {
    final GraknClient graknClient = GraknClient.of(engine.uri());
    String entityLabel = "genericEntity";
    String attributeLabel = "index";
    String baseRelationLabel = "relation1";
    String genericRelationLabel = "relation";
    String fromRoleLabel = "fromRole";
    String toRoleLabel = "toRole";
    // load ontology
    try (GraknTx tx = session.open(GraknTxType.WRITE)) {
        Role fromRole = tx.putRole(fromRoleLabel);
        Role toRole = tx.putRole(toRoleLabel);
        AttributeType<String> index = tx.putAttributeType(attributeLabel, AttributeType.DataType.STRING);
        tx.putEntityType(entityLabel).plays(fromRole).plays(toRole).attribute(index);
        // define N relation types
        for (int i = 1; i <= N; i++) {
            tx.putRelationshipType(genericRelationLabel + i).relates(fromRole).relates(toRole);
        }
        // define N rules
        for (int i = 2; i <= N; i++) {
            Var fromVar = Graql.var().asUserDefined();
            Var intermedVar = Graql.var().asUserDefined();
            Var toVar = Graql.var().asUserDefined();
            VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), intermedVar).isa(baseRelationLabel), Graql.var().rel(Graql.label(fromRole.getLabel()), intermedVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + i)));
            tx.graql().define(rulePattern).execute();
        }
        tx.commit();
    }
    // insert N + 1 entities
    loadEntities(entityLabel, N + 1, graknClient, keyspace);
    // load initial relation instances
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        try (GraknTx tx = session.open(GraknTxType.READ)) {
            Var entityVar = var().asUserDefined();
            ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
            RelationshipType baseRelation = tx.getRelationshipType(baseRelationLabel);
            Role fromRole = tx.getRole(fromRoleLabel);
            Role toRole = tx.getRole(toRoleLabel);
            loader.add(Graql.insert(Graql.var().asUserDefined().has(attributeLabel, "first").id(instances[0]).admin().varPatterns()), keyspace).subscribe();
            for (int i = 1; i < instances.length; i++) {
                Var fromRolePlayer = Graql.var();
                Var toRolePlayer = Graql.var();
                Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(baseRelation.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[i - 1])).and(toRolePlayer.asUserDefined().id(instances[i]));
                loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
                Pattern resourceInsert = Graql.var().asUserDefined().has(attributeLabel, String.valueOf(i)).id(instances[i]);
                loader.add(Graql.insert(resourceInsert.admin().varPatterns()), keyspace).subscribe();
            }
        }
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) VarPattern(ai.grakn.graql.VarPattern) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Example 22 with AttributeType

use of ai.grakn.concept.AttributeType in project grakn by graknlabs.

the class GraknTxPutPropertyTest method whenCallingPutResourceTypeWithThePropertiesOfAnExistingResourceType_ItReturnsThatType.

@Property
public void whenCallingPutResourceTypeWithThePropertiesOfAnExistingResourceType_ItReturnsThatType(@Open GraknTx graph, @FromTx AttributeType<?> attributeType) {
    assumeFalse(attributeType.equals(graph.admin().getMetaAttributeType()));
    Label label = attributeType.getLabel();
    AttributeType.DataType<?> dataType = attributeType.getDataType();
    AttributeType<?> newType = graph.putAttributeType(label, dataType);
    assertEquals(attributeType, newType);
}
Also used : AttributeType(ai.grakn.concept.AttributeType) MetaSchema.isMetaLabel(ai.grakn.util.Schema.MetaSchema.isMetaLabel) Label(ai.grakn.concept.Label) Property(com.pholser.junit.quickcheck.Property)

Example 23 with AttributeType

use of ai.grakn.concept.AttributeType in project grakn by graknlabs.

the class CSVMigratorTest method multipleEntitiesInOneFileTest.

// Ignored because this feature is not yet supported
@Ignore
@Test
public void multipleEntitiesInOneFileTest() throws IOException {
    load(factory, getFile("csv", "single-file/schema.gql"));
    // Re Open Transaction
    GraknTx graph = factory.open(GraknTxType.WRITE);
    assertNotNull(graph.getEntityType("make"));
    String template = getFileAsString("csv", "single-file/template.gql");
    declareAndLoad(template, "single-file/data/cars.csv", defaultMigrator);
    // test
    Stream<Entity> makes = graph.getEntityType("make").instances();
    assertEquals(3, makes.count());
    Stream<Entity> models = graph.getEntityType("model").instances();
    assertEquals(4, models.count());
    // test empty value not created
    AttributeType description = graph.getAttributeType("description");
    Entity venture = graph.getConcept(ConceptId.of("Venture"));
    assertEquals(1, venture.attributes(description).count());
    Entity ventureLarge = graph.getConcept(ConceptId.of("Venture Large"));
    assertEquals(0, ventureLarge.attributes(description).count());
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) AttributeType(ai.grakn.concept.AttributeType) MigratorTestUtils.getFileAsString(ai.grakn.test.migration.MigratorTestUtils.getFileAsString) Matchers.containsString(org.hamcrest.Matchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with AttributeType

use of ai.grakn.concept.AttributeType in project grakn by graknlabs.

the class GraknTxTest method whenGettingTypesByName_ReturnTypes.

@Test
public void whenGettingTypesByName_ReturnTypes() {
    String entityTypeLabel = "My Entity Type";
    String relationTypeLabel = "My Relationship Type";
    String roleTypeLabel = "My Role Type";
    String resourceTypeLabel = "My Attribute Type";
    String ruleTypeLabel = "My Rule Type";
    assertNull(tx.getEntityType(entityTypeLabel));
    assertNull(tx.getRelationshipType(relationTypeLabel));
    assertNull(tx.getRole(roleTypeLabel));
    assertNull(tx.getAttributeType(resourceTypeLabel));
    assertNull(tx.getRule(ruleTypeLabel));
    EntityType entityType = tx.putEntityType(entityTypeLabel);
    RelationshipType relationshipType = tx.putRelationshipType(relationTypeLabel);
    Role role = tx.putRole(roleTypeLabel);
    AttributeType attributeType = tx.putAttributeType(resourceTypeLabel, AttributeType.DataType.STRING);
    assertEquals(entityType, tx.getEntityType(entityTypeLabel));
    assertEquals(relationshipType, tx.getRelationshipType(relationTypeLabel));
    assertEquals(role, tx.getRole(roleTypeLabel));
    assertEquals(attributeType, tx.getAttributeType(resourceTypeLabel));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 25 with AttributeType

use of ai.grakn.concept.AttributeType in project grakn by graknlabs.

the class DefineQueryTest method whenSpecifyingExistingTypeWithIncorrectDataType_Throw.

@Test
public void whenSpecifyingExistingTypeWithIncorrectDataType_Throw() {
    AttributeType name = movies.tx().getAttributeType("name");
    exception.expect(GraqlQueryException.class);
    exception.expectMessage(GraqlQueryException.insertPropertyOnExistingConcept("datatype", BOOLEAN, name).getMessage());
    movies.tx().graql().define(label("name").datatype(BOOLEAN)).execute();
}
Also used : AttributeType(ai.grakn.concept.AttributeType) Test(org.junit.Test)

Aggregations

AttributeType (ai.grakn.concept.AttributeType)27 Test (org.junit.Test)13 EntityType (ai.grakn.concept.EntityType)7 Label (ai.grakn.concept.Label)7 RelationshipType (ai.grakn.concept.RelationshipType)7 GraknTx (ai.grakn.GraknTx)6 Role (ai.grakn.concept.Role)6 Attribute (ai.grakn.concept.Attribute)5 Property (com.pholser.junit.quickcheck.Property)5 Matchers.hasProperty (org.hamcrest.Matchers.hasProperty)4 GraknSession (ai.grakn.GraknSession)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 GraknTxType (ai.grakn.GraknTxType)2 Keyspace (ai.grakn.Keyspace)2 Concept (ai.grakn.concept.Concept)2 ConceptId (ai.grakn.concept.ConceptId)2 Entity (ai.grakn.concept.Entity)2 EmbeddedGraknSession (ai.grakn.factory.EmbeddedGraknSession)2 EngineContext (ai.grakn.test.rule.EngineContext)2 Collection (java.util.Collection)2