Search in sources :

Example 6 with Attribute

use of com.vaticle.typedb.core.concept.thing.Attribute in project grakn by graknlabs.

the class ThingService method unsetHas.

private void unsetHas(Thing thing, ConceptProto.Thing protoAttribute, UUID reqID) {
    Attribute attribute = getThing(protoAttribute).asAttribute();
    thing.unsetHas(attribute);
    transactionSvc.respond(unsetHasRes(reqID));
}
Also used : Attribute(com.vaticle.typedb.core.concept.thing.Attribute)

Example 7 with Attribute

use of com.vaticle.typedb.core.concept.thing.Attribute in project grakn by graknlabs.

the class TypeService method get.

private void get(AttributeType attributeType, ConceptProto.Attribute.Value protoValue, UUID reqID) {
    Attribute attribute;
    switch(protoValue.getValueCase()) {
        case STRING:
            attribute = attributeType.asString().get(protoValue.getString());
            break;
        case DOUBLE:
            attribute = attributeType.asDouble().get(protoValue.getDouble());
            break;
        case LONG:
            attribute = attributeType.asLong().get(protoValue.getLong());
            break;
        case DATE_TIME:
            LocalDateTime dateTime = ofEpochMilli(protoValue.getDateTime()).atOffset(UTC).toLocalDateTime();
            attribute = attributeType.asDateTime().get(dateTime);
            break;
        case BOOLEAN:
            attribute = attributeType.asBoolean().get(protoValue.getBoolean());
            break;
        case VALUE_NOT_SET:
        default:
            // TODO: Unify our exceptions - they should either all be TypeDBException or all be StatusRuntimeException
            throw TypeDBException.of(BAD_VALUE_TYPE);
    }
    transactionSvc.respond(getRes(reqID, attribute));
}
Also used : LocalDateTime(java.time.LocalDateTime) Attribute(com.vaticle.typedb.core.concept.thing.Attribute)

Example 8 with Attribute

use of com.vaticle.typedb.core.concept.thing.Attribute in project grakn by graknlabs.

the class RuleTest method rule_has_variable_materialises_when_missing.

@Test
public void rule_has_variable_materialises_when_missing() throws IOException {
    Util.resetDirectory(dataDir);
    try (CoreDatabaseManager databaseMgr = CoreDatabaseManager.open(options)) {
        databaseMgr.create(database);
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.putEntityType("milk");
                AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
                AttributeType isStillGood = conceptMgr.putAttributeType("is-still-good", AttributeType.ValueType.BOOLEAN);
                milk.setOwns(ageInDays);
                milk.setOwns(isStillGood);
                txn.logic().putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk; $a 10 isa age-in-days; }").asConjunction(), TypeQL.parseVariable("$x has $a").asThing());
                txn.commit();
            }
        }
        try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
            try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
                ConceptManager conceptMgr = txn.concepts();
                EntityType milk = conceptMgr.getEntityType("milk");
                AttributeType ageInDays = conceptMgr.getAttributeType("age-in-days");
                Entity milkInst = milk.create();
                Attribute.Long ageInDays10 = ageInDays.asLong().put(10L);
                Rule rule = txn.logic().getRule("old-milk-is-not-good");
                ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst), pair(Identifier.Variable.name("a"), ageInDays10)));
                List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
                assertEquals(1, materialisations.size());
                assertEquals(2, materialisations.get(0).size());
                List<? extends Attribute> ageInDaysOwned = milkInst.getHas(ageInDays).toList();
                assertEquals(1, ageInDaysOwned.size());
                assertEquals(ageInDays10, ageInDaysOwned.get(0));
            }
        }
    }
}
Also used : Entity(com.vaticle.typedb.core.concept.thing.Entity) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) ConceptManager(com.vaticle.typedb.core.concept.ConceptManager) EntityType(com.vaticle.typedb.core.concept.type.EntityType) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) CoreSession(com.vaticle.typedb.core.database.CoreSession) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Aggregations

Attribute (com.vaticle.typedb.core.concept.thing.Attribute)8 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)3 AttributeType (com.vaticle.typedb.core.concept.type.AttributeType)3 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)2 RoleType (com.vaticle.typedb.core.concept.type.RoleType)2 List (java.util.List)2 TypeDB (com.vaticle.typedb.core.TypeDB)1 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)1 UNRECOGNISED_VALUE (com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.UNRECOGNISED_VALUE)1 INVALID_UNDEFINE_INHERITED_OWNS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_OWNS)1 INVALID_UNDEFINE_INHERITED_PLAYS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_PLAYS)1 INVALID_UNDEFINE_NONEXISTENT_OWNS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_OWNS)1 INVALID_UNDEFINE_NONEXISTENT_PLAYS (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_PLAYS)1 INVALID_UNDEFINE_OWNS_HAS_INSTANCES (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_OWNS_HAS_INSTANCES)1 INVALID_UNDEFINE_PLAYS_HAS_INSTANCES (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_PLAYS_HAS_INSTANCES)1 OVERRIDDEN_NOT_SUPERTYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE)1 OVERRIDE_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDE_NOT_AVAILABLE)1 OWNS_ABSTRACT_ATT_TYPE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ABSTRACT_ATT_TYPE)1 OWNS_ATT_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ATT_NOT_AVAILABLE)1 OWNS_KEY_NOT_AVAILABLE (com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_NOT_AVAILABLE)1