Search in sources :

Example 71 with Label

use of ai.grakn.concept.Label 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 72 with Label

use of ai.grakn.concept.Label 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 73 with Label

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

the class RemoteConceptsTest method whenSettingLabel_ExecuteAConceptMethod.

@Test
public void whenSettingLabel_ExecuteAConceptMethod() {
    Label label = Label.of("Dunstan");
    assertEquals(schemaConcept, schemaConcept.setLabel(label));
    verifyConceptMethodCalled(ConceptMethods.setLabel(label));
}
Also used : Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 74 with Label

use of ai.grakn.concept.Label 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 75 with Label

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

the class MigratorTestUtils method getProperties.

public static Collection<Thing> getProperties(GraknTx graph, Thing thing, String label) {
    RelationshipType relation = graph.getRelationshipType(label);
    Set<Thing> things = new HashSet<>();
    relation.instances().filter(i -> i.rolePlayers().anyMatch(t -> t.equals(thing))).forEach(i -> i.rolePlayers().forEach(things::add));
    things.remove(thing);
    return things;
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet)

Aggregations

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10