Search in sources :

Example 6 with AttributeType

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

the class TypeImpl method has.

/**
 * Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked.
 * @param attributeType The {@link AttributeType} which instances of this type should be allowed to play.
 * @param has the implicit relation type to build
 * @param hasValue the implicit role type to build for the {@link AttributeType}
 * @param hasOwner the implicit role type to build for the type
 * @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity
 * @return The {@link Type} itself
 */
private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required) {
    // Check if this is a met type
    checkSchemaMutationAllowed();
    // Check if attribute type is the meta
    if (Schema.MetaSchema.ATTRIBUTE.getLabel().equals(attributeType.getLabel())) {
        throw GraknTxOperationException.metaTypeImmutable(attributeType.getLabel());
    }
    Label attributeLabel = attributeType.getLabel();
    Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel));
    Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel));
    RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)).relates(ownerRole).relates(valueRole);
    // Linking with ako structure if present
    AttributeType attributeTypeSuper = attributeType.sup();
    Label superLabel = attributeTypeSuper.getLabel();
    if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) {
        // Check to make sure we dont add plays edges to meta types accidentally
        Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel));
        Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel));
        RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)).relates(ownerRoleSuper).relates(valueRoleSuper);
        // Create the super type edges from sub role/relations to super roles/relation
        ownerRole.sup(ownerRoleSuper);
        valueRole.sup(valueRoleSuper);
        relationshipType.sup(relationshipTypeSuper);
        // Make sure the supertype attribute is linked with the role as well
        ((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper);
    }
    this.plays(ownerRole, required);
    // TODO: Use explicit cardinality of 0-1 rather than just false
    ((AttributeTypeImpl) attributeType).plays(valueRole, false);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

Example 7 with AttributeType

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

the class GraknTxTest method whenCreatingAValidSchemaInSeparateThreads_EnsureValidationRulesHold.

@Test
public void whenCreatingAValidSchemaInSeparateThreads_EnsureValidationRulesHold() throws ExecutionException, InterruptedException {
    GraknSession session = Grakn.session(Grakn.IN_MEMORY, "hi");
    ExecutorService executor = Executors.newCachedThreadPool();
    executor.submit(() -> {
        // Resources
        try (GraknTx graph = session.open(GraknTxType.WRITE)) {
            AttributeType<Long> int_ = graph.putAttributeType("int", AttributeType.DataType.LONG);
            AttributeType<Long> foo = graph.putAttributeType("foo", AttributeType.DataType.LONG).sup(int_);
            graph.putAttributeType("bar", AttributeType.DataType.LONG).sup(int_);
            graph.putEntityType("FOO").attribute(foo);
            graph.commit();
        }
    }).get();
    // Relationship Which Has Resources
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        graph.putEntityType("BAR").attribute(graph.getAttributeType("bar"));
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) AttributeType(ai.grakn.concept.AttributeType) GraknSession(ai.grakn.GraknSession) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 8 with AttributeType

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

the class XMLMigratorTest method assertThingHasName.

private static void assertThingHasName(String name) {
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        EntityType thingType = graph.getEntityType("thingy");
        AttributeType nameType = graph.getAttributeType("name");
        assertEquals(1, thingType.instances().count());
        thingType.instances().forEach(thing -> {
            assertEquals(1, thing.attributes(nameType).count());
            assertEquals(name, thing.attributes(nameType).iterator().next().getValue());
        });
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) AttributeType(ai.grakn.concept.AttributeType)

Example 9 with AttributeType

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

the class ConceptMethods method fromGrpc.

public static ConceptMethod<?> fromGrpc(GrpcConceptConverter converter, GrpcConcept.ConceptMethod conceptMethod) {
    Role[] roles;
    switch(conceptMethod.getConceptMethodCase()) {
        case GETVALUE:
            return GET_VALUE;
        case GETDATATYPEOFTYPE:
            return GET_DATA_TYPE_OF_TYPE;
        case GETDATATYPEOFATTRIBUTE:
            return GET_DATA_TYPE_OF_ATTRIBUTE;
        case GETLABEL:
            return GET_LABEL;
        case SETLABEL:
            return setLabel(convert(conceptMethod.getSetLabel()));
        case ISIMPLICIT:
            return IS_IMPLICIT;
        case ISINFERRED:
            return IS_INFERRED;
        case ISABSTRACT:
            return IS_ABSTRACT;
        case GETWHEN:
            return GET_WHEN;
        case GETTHEN:
            return GET_THEN;
        case GETREGEX:
            return GET_REGEX;
        case GETROLEPLAYERS:
            return GET_ROLE_PLAYERS;
        case GETATTRIBUTETYPES:
            return GET_ATTRIBUTE_TYPES;
        case SETATTRIBUTETYPE:
            return setAttributeType(converter.convert(conceptMethod.getSetAttributeType()).asAttributeType());
        case UNSETATTRIBUTETYPE:
            return unsetAttributeType(converter.convert(conceptMethod.getUnsetAttributeType()).asAttributeType());
        case GETKEYTYPES:
            return GET_KEY_TYPES;
        case GETDIRECTTYPE:
            return GET_DIRECT_TYPE;
        case GETDIRECTSUPERCONCEPT:
            return GET_DIRECT_SUPER;
        case SETDIRECTSUPERCONCEPT:
            GrpcConcept.Concept setDirectSuperConcept = conceptMethod.getSetDirectSuperConcept();
            SchemaConcept schemaConcept = converter.convert(setDirectSuperConcept).asSchemaConcept();
            return setDirectSuperConcept(schemaConcept);
        case UNSETROLEPLAYER:
            return removeRolePlayer(converter.convert(conceptMethod.getUnsetRolePlayer()));
        case DELETE:
            return DELETE;
        case GETATTRIBUTE:
            return getAttribute(convert(conceptMethod.getGetAttribute()));
        case GETOWNERS:
            return GET_OWNERS;
        case GETTYPESTHATPLAYROLE:
            return GET_TYPES_THAT_PLAY_ROLE;
        case GETROLESPLAYEDBYTYPE:
            return GET_ROLES_PLAYED_BY_TYPE;
        case GETINSTANCES:
            return GET_INSTANCES;
        case GETRELATEDROLES:
            return GET_RELATED_ROLES;
        case GETATTRIBUTES:
            return GET_ATTRIBUTES;
        case GETSUPERCONCEPTS:
            return GET_SUPER_CONCEPTS;
        case GETRELATIONSHIPTYPESTHATRELATEROLE:
            return GET_RELATIONSHIP_TYPES_THAT_RELATE_ROLE;
        case GETATTRIBUTESBYTYPES:
            GrpcConcept.Concepts getAttributeTypes = conceptMethod.getGetAttributesByTypes();
            AttributeType<?>[] attributeTypes = convert(converter, getAttributeTypes).toArray(AttributeType[]::new);
            return getAttributesByTypes(attributeTypes);
        case GETRELATIONSHIPS:
            return GET_RELATIONSHIPS;
        case GETSUBCONCEPTS:
            return GET_SUB_CONCEPTS;
        case GETRELATIONSHIPSBYROLES:
            roles = convert(converter, conceptMethod.getGetRelationshipsByRoles()).toArray(Role[]::new);
            return getRelationshipsByRoles(roles);
        case GETROLESPLAYEDBYTHING:
            return GET_ROLES_PLAYED_BY_THING;
        case GETKEYS:
            return GET_KEYS;
        case GETKEYSBYTYPES:
            GrpcConcept.Concepts getKeyTypes = conceptMethod.getGetAttributesByTypes();
            AttributeType<?>[] keyTypes = convert(converter, getKeyTypes).toArray(AttributeType[]::new);
            return getKeysByTypes(keyTypes);
        case GETROLEPLAYERSBYROLES:
            roles = convert(converter, conceptMethod.getGetRolePlayersByRoles()).toArray(Role[]::new);
            return getRolePlayersByRoles(roles);
        case SETKEYTYPE:
            return setKeyType(converter.convert(conceptMethod.getSetKeyType()).asAttributeType());
        case UNSETKEYTYPE:
            return unsetKeyType(converter.convert(conceptMethod.getUnsetKeyType()).asAttributeType());
        case SETABSTRACT:
            return setAbstract(conceptMethod.getSetAbstract());
        case SETROLEPLAYEDBYTYPE:
            return setRolePlayedByType(converter.convert(conceptMethod.getSetRolePlayedByType()).asRole());
        case UNSETROLEPLAYEDBYTYPE:
            return unsetRolePlayedByType(converter.convert(conceptMethod.getUnsetRolePlayedByType()).asRole());
        case ADDENTITY:
            return ADD_ENTITY;
        case SETRELATEDROLE:
            return setRelatedRole(converter.convert(conceptMethod.getSetRelatedRole()).asRole());
        case UNSETRELATEDROLE:
            return unsetRelatedRole(converter.convert(conceptMethod.getUnsetRelatedRole()).asRole());
        case PUTATTRIBUTE:
            return putAttribute(convert(conceptMethod.getPutAttribute()));
        case SETREGEX:
            return setRegex(convert(conceptMethod.getSetRegex()));
        case SETATTRIBUTE:
            return setAttribute(converter.convert(conceptMethod.getSetAttribute()).asAttribute());
        case UNSETATTRIBUTE:
            return unsetAttribute(converter.convert(conceptMethod.getUnsetAttribute()).asAttribute());
        case ADDRELATIONSHIP:
            return ADD_RELATIONSHIP;
        case SETROLEPLAYER:
            return setRolePlayer(converter.convert(conceptMethod.getSetRolePlayer()));
        default:
        case CONCEPTMETHOD_NOT_SET:
            throw new IllegalArgumentException("Unrecognised " + conceptMethod);
    }
}
Also used : Role(ai.grakn.concept.Role) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) AttributeType(ai.grakn.concept.AttributeType) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 10 with AttributeType

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

the class GraknTxPropertyTest method whenCallingHasWithMetaResourceType_ThrowMetaTypeImmutableException.

@Property
public void whenCallingHasWithMetaResourceType_ThrowMetaTypeImmutableException(@Open GraknTx graph, @FromTx Type type) {
    AttributeType resource = graph.admin().getMetaAttributeType();
    exception.expect(GraknTxOperationException.class);
    if (Schema.MetaSchema.isMetaLabel(type.getLabel())) {
        exception.expectMessage(GraknTxOperationException.metaTypeImmutable(type.getLabel()).getMessage());
    } else {
        exception.expectMessage(GraknTxOperationException.metaTypeImmutable(resource.getLabel()).getMessage());
    }
    type.attribute(resource);
}
Also used : AttributeType(ai.grakn.concept.AttributeType) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Property(com.pholser.junit.quickcheck.Property)

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