Search in sources :

Example 1 with LabelConstraint

use of com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint in project grakn by graknlabs.

the class TypeVariable method label.

public LabelConstraint label(Label label) {
    LabelConstraint labelConstraint = new LabelConstraint(this, label);
    constrain(labelConstraint);
    return labelConstraint;
}
Also used : LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint)

Example 2 with LabelConstraint

use of com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint in project grakn by graknlabs.

the class Definer method definePlays.

private void definePlays(ThingType thingType, Set<PlaysConstraint> playsConstraints) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "define_plays")) {
        playsConstraints.forEach(plays -> {
            define(plays.relation().get());
            LabelConstraint roleTypeLabel = plays.role().label().get();
            RoleType roleType = getRoleType(roleTypeLabel).asRoleType();
            if (plays.overridden().isPresent()) {
                String overriddenLabelName = plays.overridden().get().label().get().properLabel().name();
                Optional<? extends RoleType> overriddenType = roleType.getSupertypes().filter(rt -> rt.getLabel().name().equals(overriddenLabelName)).first();
                if (overriddenType.isPresent()) {
                    thingType.setPlays(roleType, overriddenType.get());
                } else {
                    throw TypeDBException.of(OVERRIDDEN_NOT_SUPERTYPE, roleTypeLabel.scopedLabel(), overriddenLabelName);
                }
            } else {
                thingType.setPlays(roleType);
            }
        });
    }
}
Also used : ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) Context(com.vaticle.typedb.core.common.parameters.Context) LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE) RelationType(com.vaticle.typedb.core.concept.type.RelationType) LogicManager(com.vaticle.typedb.core.logic.LogicManager) HashSet(java.util.HashSet) PlaysConstraint(com.vaticle.typedb.core.pattern.constraint.type.PlaysConstraint) TYPE_CONSTRAINT_UNACCEPTED(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_CONSTRAINT_UNACCEPTED) ThingType(com.vaticle.typedb.core.concept.type.ThingType) CYCLIC_TYPE_HIERARCHY(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.CYCLIC_TYPE_HIERARCHY) RelatesConstraint(com.vaticle.typedb.core.pattern.constraint.type.RelatesConstraint) TYPE_NOT_FOUND(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_NOT_FOUND) OwnsConstraint(com.vaticle.typedb.core.pattern.constraint.type.OwnsConstraint) LinkedHashSet(java.util.LinkedHashSet) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) ATTRIBUTE_VALUE_TYPE_MISSING(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MISSING) IS(com.vaticle.typeql.lang.common.TypeQLToken.Constraint.IS) ROLE_DEFINED_OUTSIDE_OF_RELATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROLE_DEFINED_OUTSIDE_OF_RELATION) SubConstraint(com.vaticle.typedb.core.pattern.constraint.type.SubConstraint) Set(java.util.Set) RegexConstraint(com.vaticle.typedb.core.pattern.constraint.type.RegexConstraint) ATTRIBUTE_VALUE_TYPE_MODIFIED(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MODIFIED) INVALID_DEFINE_SUB(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_DEFINE_SUB) RoleType(com.vaticle.typedb.core.concept.type.RoleType) FactoryTracingThreadStatic.traceOnThread(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.traceOnThread) List(java.util.List) TypeVariable(com.vaticle.typedb.core.pattern.variable.TypeVariable) Optional(java.util.Optional) VariableRegistry(com.vaticle.typedb.core.pattern.variable.VariableRegistry) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ValueType(com.vaticle.typedb.core.concept.type.AttributeType.ValueType) ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE) OVERRIDDEN_NOT_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE) LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) RoleType(com.vaticle.typedb.core.concept.type.RoleType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)

Example 3 with LabelConstraint

use of com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint in project grakn by graknlabs.

the class Definer method define.

private ThingType define(TypeVariable variable) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "define")) {
        assert variable.label().isPresent();
        LabelConstraint labelConstraint = variable.label().get();
        if (labelConstraint.scope().isPresent() && variable.constraints().size() > 1) {
            throw TypeDBException.of(ROLE_DEFINED_OUTSIDE_OF_RELATION, labelConstraint.scopedLabel());
        } else if (!variable.is().isEmpty()) {
            throw TypeDBException.of(TYPE_CONSTRAINT_UNACCEPTED, IS);
        } else if (// do nothing
        labelConstraint.scope().isPresent())
            // do nothing
            return null;
        else if (defined.contains(variable))
            return conceptMgr.getThingType(labelConstraint.scopedLabel());
        ThingType type = getThingType(labelConstraint);
        if (variable.sub().isPresent()) {
            type = defineSub(type, variable.sub().get(), variable);
        } else if (variable.valueType().isPresent()) {
            // && variable.sub().size() == 0
            String valueType = variable.valueType().get().valueType().name();
            throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_MODIFIED, valueType, labelConstraint.label());
        } else if (type == null) {
            throw TypeDBException.of(TYPE_NOT_FOUND, labelConstraint.label());
        }
        if (variable.valueType().isPresent() && !(type.isAttributeType())) {
            throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE, labelConstraint.label());
        }
        defined.add(variable);
        if (variable.abstractConstraint().isPresent())
            defineAbstract(type);
        if (variable.regex().isPresent())
            defineRegex(type.asAttributeType().asString(), variable.regex().get());
        if (!variable.relates().isEmpty())
            defineRelates(type.asRelationType(), variable.relates());
        if (!variable.owns().isEmpty())
            defineOwns(type, variable.owns());
        if (!variable.plays().isEmpty())
            definePlays(type, variable.plays());
        return type;
    }
}
Also used : LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Example 4 with LabelConstraint

use of com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint in project grakn by graknlabs.

the class Definer method defineSub.

private ThingType defineSub(ThingType thingType, SubConstraint subConstraint, TypeVariable var) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "define_sub")) {
        LabelConstraint labelConstraint = var.label().get();
        ThingType supertype = define(subConstraint.type()).asThingType();
        if (supertype.isEntityType()) {
            if (thingType == null)
                thingType = conceptMgr.putEntityType(labelConstraint.label());
            thingType.asEntityType().setSupertype(supertype.asEntityType());
        } else if (supertype.isRelationType()) {
            if (thingType == null)
                thingType = conceptMgr.putRelationType(labelConstraint.label());
            thingType.asRelationType().setSupertype(supertype.asRelationType());
        } else if (supertype.isAttributeType()) {
            ValueType valueType;
            if (var.valueType().isPresent())
                valueType = ValueType.of(var.valueType().get().valueType());
            else if (!supertype.isRoot())
                valueType = supertype.asAttributeType().getValueType();
            else
                throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_MISSING, labelConstraint.label());
            if (thingType == null)
                thingType = conceptMgr.putAttributeType(labelConstraint.label(), valueType);
            thingType.asAttributeType().setSupertype(supertype.asAttributeType());
        } else {
            throw TypeDBException.of(INVALID_DEFINE_SUB, labelConstraint.scopedLabel(), supertype.getLabel());
        }
        return thingType;
    }
}
Also used : LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ValueType(com.vaticle.typedb.core.concept.type.AttributeType.ValueType) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Example 5 with LabelConstraint

use of com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint in project grakn by graknlabs.

the class Undefiner method undefine.

private void undefine(TypeVariable variable) {
    try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "undefine")) {
        assert variable.label().isPresent();
        LabelConstraint labelConstraint = variable.label().get();
        if (labelConstraint.scope().isPresent() && variable.constraints().size() > 1) {
            throw TypeDBException.of(ROLE_DEFINED_OUTSIDE_OF_RELATION, labelConstraint.scopedLabel());
        } else if (!variable.is().isEmpty()) {
            throw TypeDBException.of(TYPE_CONSTRAINT_UNACCEPTED, IS);
        } else if (// do nothing
        labelConstraint.scope().isPresent())
            // do nothing
            return;
        else // do nothing
        if (undefined.contains(variable))
            return;
        ThingType type = getThingType(labelConstraint);
        if (!variable.plays().isEmpty())
            undefinePlays(type, variable.plays());
        if (!variable.owns().isEmpty())
            undefineOwns(type, variable.owns());
        if (!variable.relates().isEmpty())
            undefineRelates(type.asRelationType(), variable.relates());
        if (variable.regex().isPresent())
            undefineRegex(type.asAttributeType().asString(), variable.regex().get());
        if (variable.abstractConstraint().isPresent())
            undefineAbstract(type);
        if (variable.sub().isPresent())
            undefineSub(type, variable.sub().get());
        else if (variable.valueType().isPresent()) {
            throw TypeDBException.of(ATTRIBUTE_VALUE_TYPE_UNDEFINED, variable.valueType().get().valueType().name(), variable.label().get().label());
        }
        undefined.add(variable);
    }
}
Also used : LabelConstraint(com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint) ThreadTrace(com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace) ThingType(com.vaticle.typedb.core.concept.type.ThingType)

Aggregations

LabelConstraint (com.vaticle.typedb.core.pattern.constraint.type.LabelConstraint)5 ThreadTrace (com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.ThreadTrace)4 ThingType (com.vaticle.typedb.core.concept.type.ThingType)4 ValueType (com.vaticle.typedb.core.concept.type.AttributeType.ValueType)2 FactoryTracingThreadStatic.traceOnThread (com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.traceOnThread)1 ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.ROLE_TYPE_SCOPE_IS_NOT_RELATION_TYPE)1 TYPE_NOT_FOUND (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeRead.TYPE_NOT_FOUND)1 ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_DEFINED_NOT_ON_ATTRIBUTE_TYPE)1 ATTRIBUTE_VALUE_TYPE_MISSING (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MISSING)1 ATTRIBUTE_VALUE_TYPE_MODIFIED (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ATTRIBUTE_VALUE_TYPE_MODIFIED)1 CYCLIC_TYPE_HIERARCHY (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.CYCLIC_TYPE_HIERARCHY)1 INVALID_DEFINE_SUB (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_DEFINE_SUB)1 OVERRIDDEN_NOT_SUPERTYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE)1 ROLE_DEFINED_OUTSIDE_OF_RELATION (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROLE_DEFINED_OUTSIDE_OF_RELATION)1 TYPE_CONSTRAINT_UNACCEPTED (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_CONSTRAINT_UNACCEPTED)1 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)1 Context (com.vaticle.typedb.core.common.parameters.Context)1 ConceptManager (com.vaticle.typedb.core.concept.ConceptManager)1 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)1 RelationType (com.vaticle.typedb.core.concept.type.RelationType)1