Search in sources :

Example 16 with AttributeType

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

the class AttributeTest method whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource.

// this is deliberately an incorrect type for the test
@SuppressWarnings("unchecked")
@Test
public void whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource() {
    AttributeType longAttributeType = tx.putAttributeType("long", AttributeType.DataType.LONG);
    try {
        longAttributeType.putAttribute("Invalid Thing");
        fail("Expected to throw");
    } catch (GraknTxOperationException e) {
    // expected failure
    }
    Collection<Attribute> instances = (Collection<Attribute>) longAttributeType.instances().collect(toSet());
    assertThat(instances, empty());
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Collection(java.util.Collection) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Test(org.junit.Test)

Example 17 with AttributeType

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

the class EntityTypeTest method whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes.

@Test
public void whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes() {
    EntityType entityType1 = tx.putEntityType("Entity Type 1");
    EntityType entityType2 = tx.putEntityType("Entity Type 2");
    Label superLabel = Label.of("Super Attribute Type");
    Label label = Label.of("Attribute Type");
    AttributeType rtSuper = tx.putAttributeType(superLabel, AttributeType.DataType.STRING);
    AttributeType rt = tx.putAttributeType(label, AttributeType.DataType.STRING).sup(rtSuper);
    entityType1.attribute(rtSuper);
    entityType2.attribute(rt);
    // Check role types are only built explicitly
    assertThat(entityType1.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(superLabel).getValue())));
    assertThat(entityType2.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(label).getValue())));
    // Check Implicit Types Follow SUB Structure
    RelationshipType rtSuperRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rtSuper.getLabel()));
    Role rtSuperRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rtSuper.getLabel()));
    Role rtSuperRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rtSuper.getLabel()));
    RelationshipType rtRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rt.getLabel()));
    Role reRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rt.getLabel()));
    Role reRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rt.getLabel()));
    assertEquals(rtSuperRoleOwner, reRoleOwner.sup());
    assertEquals(rtSuperRoleValue, reRoleValue.sup());
    assertEquals(rtSuperRelation, rtRelation.sup());
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 18 with AttributeType

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

the class EntityTypeTest method checkThatResourceTypesCanBeRetrievedFromTypes.

@Test
public void checkThatResourceTypesCanBeRetrievedFromTypes() {
    EntityType e1 = tx.putEntityType("e1");
    AttributeType r1 = tx.putAttributeType("r1", AttributeType.DataType.STRING);
    AttributeType r2 = tx.putAttributeType("r2", AttributeType.DataType.LONG);
    AttributeType r3 = tx.putAttributeType("r3", AttributeType.DataType.BOOLEAN);
    assertTrue("Entity is linked to resources when it shouldn't", e1.attributes().collect(toSet()).isEmpty());
    e1.attribute(r1);
    e1.attribute(r2);
    e1.attribute(r3);
    assertThat(e1.attributes().collect(toSet()), containsInAnyOrder(r1, r2, r3));
}
Also used : EntityType(ai.grakn.concept.EntityType) AttributeType(ai.grakn.concept.AttributeType) Test(org.junit.Test)

Example 19 with AttributeType

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

the class AbstractThingGenerator method generateFromTx.

@Override
protected final T generateFromTx() {
    T thing;
    S type = genFromTx(generatorClass).makeExcludeAbstractTypes().excludeMeta().generate(random, status);
    // noinspection unchecked
    Collection<T> instances = (Collection<T>) type.instances().collect(toSet());
    if (instances.isEmpty()) {
        thing = newInstance(type);
    } else {
        thing = random.choose(instances);
    }
    if (withResource && !thing.attributes().findAny().isPresent()) {
        // A new attribute type is created every time a attribute is lacking.
        // Existing attribute types and resources of those types are not used because we end up mutating the
        // the schema in strange ways. This approach is less complex but ensures everything has a attribute
        // without conflicting with the schema
        // Create a new attribute type
        AttributeType.DataType<?> dataType = gen(AttributeType.DataType.class);
        Label label = genFromTx(Labels.class).mustBeUnused().generate(random, status);
        AttributeType attributeType = tx().putAttributeType(label, dataType);
        // Create new attribute
        Attribute attribute = newResource(attributeType);
        // Link everything together
        type.attribute(attributeType);
        thing.attribute(attribute);
    }
    return thing;
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) Collection(java.util.Collection)

Example 20 with AttributeType

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

the class PostProcessingTest method whenCreatingDuplicateResources_EnsureTheyAreMergedInPost.

@Test
public void whenCreatingDuplicateResources_EnsureTheyAreMergedInPost() throws InvalidKBException, InterruptedException, JsonProcessingException {
    String value = "1";
    String sample = "Sample";
    // Create GraknTx With Duplicate Resources
    EmbeddedGraknTx<?> tx = session.open(GraknTxType.WRITE);
    AttributeType<String> attributeType = tx.putAttributeType(sample, AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute(value);
    tx.commitSubmitNoLogs();
    tx = session.open(GraknTxType.WRITE);
    assertEquals(1, attributeType.instances().count());
    // Check duplicates have been created
    Set<Vertex> resource1 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource2 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource3 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource4 = createDuplicateResource(tx, attributeType, attribute);
    assertEquals(5, attributeType.instances().count());
    // Attribute vertex index
    String resourceIndex = resource1.iterator().next().value(INDEX.name()).toString();
    // Merge the attribute sets
    Set<Vertex> merged = Sets.newHashSet();
    merged.addAll(resource1);
    merged.addAll(resource2);
    merged.addAll(resource3);
    merged.addAll(resource4);
    tx.close();
    // Casting sets as ConceptIds
    Set<ConceptId> resourceConcepts = merged.stream().map(c -> ConceptId.of(Schema.PREFIX_VERTEX + c.id().toString())).collect(toSet());
    // Create Commit Log
    CommitLog commitLog = CommitLog.createDefault(tx.keyspace());
    commitLog.attributes().put(resourceIndex, resourceConcepts);
    // Submit it
    postProcessor.submit(commitLog);
    // Force running the PP job
    engine.server().backgroundTaskRunner().tasks().forEach(BackgroundTask::run);
    Thread.sleep(2000);
    tx = session.open(GraknTxType.READ);
    // Check it's fixed
    assertEquals(1, tx.getAttributeType(sample).instances().count());
    tx.close();
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) CountPostProcessor(ai.grakn.engine.task.postprocessing.CountPostProcessor) SampleKBLoader(ai.grakn.util.SampleKBLoader) Keyspace(ai.grakn.Keyspace) Concept(ai.grakn.concept.Concept) PostProcessingTestUtils.createDuplicateResource(ai.grakn.test.engine.postprocessing.PostProcessingTestUtils.createDuplicateResource) EntityType(ai.grakn.concept.EntityType) GraknConfig(ai.grakn.engine.GraknConfig) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) After(org.junit.After) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) RedisCountStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisCountStorage) INDEX(ai.grakn.util.Schema.VertexProperty.INDEX) Collectors.toSet(java.util.stream.Collectors.toSet) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) MetricRegistry(com.codahale.metrics.MetricRegistry) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Sets(com.google.common.collect.Sets) PostProcessor(ai.grakn.engine.task.postprocessing.PostProcessor) CommitLog(ai.grakn.kb.log.CommitLog) GraknConfigKey(ai.grakn.GraknConfigKey) IndexPostProcessor(ai.grakn.engine.task.postprocessing.IndexPostProcessor) BackgroundTask(ai.grakn.engine.task.BackgroundTask) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Assume.assumeTrue(org.junit.Assume.assumeTrue) RedisIndexStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisIndexStorage) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) BackgroundTask(ai.grakn.engine.task.BackgroundTask) 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