Search in sources :

Example 26 with Attribute

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

the class TestKB method putResource.

public static <T> void putResource(Thing thing, AttributeType<T> attributeType, T resource) {
    Attribute attributeInstance = attributeType.putAttribute(resource);
    thing.attribute(attributeInstance);
}
Also used : Attribute(ai.grakn.concept.Attribute)

Example 27 with Attribute

use of ai.grakn.concept.Attribute 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 28 with Attribute

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

the class ThingImpl method deleteAttribute.

@Override
public T deleteAttribute(Attribute attribute) {
    Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().getLabel()));
    Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().getLabel()));
    Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().getLabel()));
    Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().getLabel()));
    Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner));
    relationships.filter(relationship -> {
        Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue));
        return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute));
    }).forEach(Concept::delete);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Cacheable(ai.grakn.kb.internal.cache.Cacheable) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) LabelId(ai.grakn.concept.LabelId) RelationshipType(ai.grakn.concept.RelationshipType) Cache(ai.grakn.kb.internal.cache.Cache) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) P(org.apache.tinkerpop.gremlin.process.traversal.P) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) VertexElement(ai.grakn.kb.internal.structure.VertexElement) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Casting(ai.grakn.kb.internal.structure.Casting) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship) Stream(java.util.stream.Stream)

Example 29 with Attribute

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

the class ThingImpl method attributeRelationship.

private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
    Schema.ImplicitType has = Schema.ImplicitType.HAS;
    Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
    Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
    // Is this attribute a key to me?
    if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
        has = Schema.ImplicitType.KEY;
        hasValue = Schema.ImplicitType.KEY_VALUE;
        hasOwner = Schema.ImplicitType.KEY_OWNER;
    }
    Label label = attribute.type().getLabel();
    RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
    Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
    Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
    if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
        throw GraknTxOperationException.hasNotAllowed(this, attribute);
    }
    EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
    if (isInferred)
        attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
    return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
Also used : Role(ai.grakn.concept.Role) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Cacheable(ai.grakn.kb.internal.cache.Cacheable) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) LabelId(ai.grakn.concept.LabelId) RelationshipType(ai.grakn.concept.RelationshipType) Cache(ai.grakn.kb.internal.cache.Cache) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) P(org.apache.tinkerpop.gremlin.process.traversal.P) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) VertexElement(ai.grakn.kb.internal.structure.VertexElement) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Casting(ai.grakn.kb.internal.structure.Casting) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) Schema(ai.grakn.util.Schema) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

Example 30 with Attribute

use of ai.grakn.concept.Attribute 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

Attribute (ai.grakn.concept.Attribute)36 Test (org.junit.Test)26 GraknTx (ai.grakn.GraknTx)15 EntityType (ai.grakn.concept.EntityType)15 Entity (ai.grakn.concept.Entity)14 RelationshipType (ai.grakn.concept.RelationshipType)10 Set (java.util.Set)10 Role (ai.grakn.concept.Role)9 AttributeType (ai.grakn.concept.AttributeType)8 Relationship (ai.grakn.concept.Relationship)8 Label (ai.grakn.concept.Label)7 Thing (ai.grakn.concept.Thing)6 Concept (ai.grakn.concept.Concept)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 Schema (ai.grakn.util.Schema)4 GraknTxOperationException (ai.grakn.exception.GraknTxOperationException)3 Sets (com.google.common.collect.Sets)3 Collection (java.util.Collection)3 Collectors (java.util.stream.Collectors)3