Search in sources :

Example 1 with Attribute

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

the class QueryTest method test_query_undefine.

@Test
public void test_query_undefine() throws IOException {
    Util.resetDirectory(dataDir);
    try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
        typedb.create(database);
        try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                TypeQLDefine query = TypeQL.parseQuery(new String(Files.readAllBytes(Paths.get("test/integration/schema.tql")), UTF_8));
                transaction.query().define(query);
                transaction.commit();
            }
            try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
                String queryString = "undefine analysis abstract, owns created, plays commit-analysis:analysis;";
                TypeQLUndefine query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule performance-tracker-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine performance-tracker relates tracker;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine email regex '.+\\@.+\\..+';";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine index sub attribute, value long;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                // undefine first 4 rules
                queryString = "undefine rule repo-fork-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-dependency-transitive-type-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                queryString = "undefine rule repo-collaborator-org-rule;";
                query = TypeQL.parseQuery(queryString);
                transaction.query().undefine(query);
                transaction.commit();
            }
            try (TypeDB.Transaction tx = session.transaction(Arguments.Transaction.Type.READ)) {
                EntityType analysis = tx.concepts().getEntityType("analysis");
                RelationType performanceTracker = tx.concepts().getRelationType("performance-tracker");
                RoleType commitAnalysisAnalysis = tx.concepts().getRelationType("commit-analysis").getRelates("analysis");
                AttributeType.DateTime created = tx.concepts().getAttributeType("created").asDateTime();
                AttributeType.String email = tx.concepts().getAttributeType("email").asString();
                assertNotNulls(analysis, performanceTracker, commitAnalysisAnalysis, created, email);
                assertFalse(analysis.isAbstract());
                assertTrue(analysis.getOwns().noneMatch(att -> att.equals(created)));
                assertTrue(analysis.getPlays().noneMatch(rol -> rol.equals(commitAnalysisAnalysis)));
                assertTrue(performanceTracker.getRelates().noneMatch(rol -> rol.getLabel().name().equals("tracker")));
                assertNull(email.getRegex());
                AttributeType index = tx.concepts().getAttributeType("index");
                assertNull(index);
                assertNull(tx.logic().getRule("repo-fork-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-rule"));
                assertNull(tx.logic().getRule("repo-dependency-transitive-type-rule"));
                assertNull(tx.logic().getRule("repo-collaborator-org-rule"));
                // check total count
                assertEquals(15 - 5, tx.logic().rules().toList().size());
                // a query that used to trigger a rule should not cause an error
                List<ConceptMap> answers = tx.query().match(TypeQL.parseQuery("match $x isa repo-fork;").asMatch()).toList();
            }
        }
    }
}
Also used : TypeQLDelete(com.vaticle.typeql.lang.query.TypeQLDelete) TypeQLInsert(com.vaticle.typeql.lang.query.TypeQLInsert) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) Util.assertNotNulls(com.vaticle.typedb.core.test.integration.util.Util.assertNotNulls) RelationType(com.vaticle.typedb.core.concept.type.RelationType) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) Path(java.nio.file.Path) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) TypeQL(com.vaticle.typeql.lang.TypeQL) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Database(com.vaticle.typedb.core.common.parameters.Options.Database) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLMatch(com.vaticle.typeql.lang.query.TypeQLMatch) Entity(com.vaticle.typedb.core.concept.thing.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) Util(com.vaticle.typedb.core.test.integration.util.Util) TypeQLUndefine(com.vaticle.typeql.lang.query.TypeQLUndefine) RoleType(com.vaticle.typedb.core.concept.type.RoleType) TypeDB(com.vaticle.typedb.core.TypeDB) EntityType(com.vaticle.typedb.core.concept.type.EntityType) TypeQLDefine(com.vaticle.typeql.lang.query.TypeQLDefine) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) RelationType(com.vaticle.typedb.core.concept.type.RelationType) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 2 with Attribute

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

the class StatisticsTest method updateAges.

private void updateAges(CoreDatabaseManager databaseMgr, Set<Long> ages) {
    try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
        try (CoreTransaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
            TypeQLQuery query = TypeQL.parseQuery("match $x isa person, has age $y;");
            List<ConceptMap> conceptMaps = tx.query().match(query.asMatch()).toList();
            conceptMaps.forEach(cm -> {
                Attribute.Long attribute = cm.get("y").asAttribute().asLong();
                cm.get("x").asEntity().unsetHas(attribute);
                long newAge = attribute.getValue() + 1;
                Attribute.Long newAttribute = attribute.getType().asLong().put(newAge);
                cm.get("x").asEntity().setHas(newAttribute);
                cm.get("x").asEntity().setHas(newAttribute);
                ages.add(newAge);
            });
            tx.commit();
        }
    }
}
Also used : Attribute(com.vaticle.typedb.core.concept.thing.Attribute) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) TypeQLQuery(com.vaticle.typeql.lang.query.TypeQLQuery)

Example 3 with Attribute

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

the class ThingService method setHas.

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

Example 4 with Attribute

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

the class TypeService method put.

private void put(AttributeType attributeType, ConceptProto.Attribute.Value protoValue, UUID reqID) {
    Attribute attribute;
    switch(protoValue.getValueCase()) {
        case STRING:
            attribute = attributeType.asString().put(protoValue.getString());
            break;
        case DOUBLE:
            attribute = attributeType.asDouble().put(protoValue.getDouble());
            break;
        case LONG:
            attribute = attributeType.asLong().put(protoValue.getLong());
            break;
        case DATE_TIME:
            LocalDateTime dateTime = ofEpochMilli(protoValue.getDateTime()).atOffset(UTC).toLocalDateTime();
            attribute = attributeType.asDateTime().put(dateTime);
            break;
        case BOOLEAN:
            attribute = attributeType.asBoolean().put(protoValue.getBoolean());
            break;
        case VALUE_NOT_SET:
            throw TypeDBException.of(MISSING_FIELD, "value");
        default:
            throw TypeDBException.of(BAD_VALUE_TYPE, protoValue.getValueCase());
    }
    transactionSvc.respond(putRes(reqID, attribute));
}
Also used : LocalDateTime(java.time.LocalDateTime) Attribute(com.vaticle.typedb.core.concept.thing.Attribute)

Example 5 with Attribute

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

the class ThingTypeImpl method ownsKey.

private void ownsKey(AttributeTypeImpl attributeType) {
    validateIsNotDeleted();
    TypeVertex attVertex = attributeType.vertex;
    TypeEdge ownsEdge, ownsKeyEdge;
    if (vertex.outs().edge(OWNS_KEY, attVertex) != null)
        return;
    if (!attributeType.isKeyable()) {
        throw exception(TypeDBException.of(OWNS_KEY_VALUE_TYPE, attributeType.getLabel(), attributeType.getValueType().name()));
    } else if (link(getSupertype().getOwns(attributeType.getValueType(), true), getSupertype().overriddenOwns(false, true)).anyMatch(a -> a.equals(attributeType))) {
        throw exception(TypeDBException.of(OWNS_KEY_NOT_AVAILABLE, attributeType.getLabel()));
    }
    if ((ownsEdge = vertex.outs().edge(OWNS, attVertex)) != null) {
        // TODO: These ownership and uniqueness checks should be parallelised to scale better
        getInstances().forEachRemaining(thing -> {
            FunctionalIterator<? extends Attribute> attrs = thing.getHas(attributeType);
            if (!attrs.hasNext())
                throw exception(TypeDBException.of(OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY, vertex.label(), attVertex.label()));
            Attribute attr = attrs.next();
            if (attrs.hasNext())
                throw exception(TypeDBException.of(OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING, vertex.label(), attVertex.label()));
            else if (compareSize(attr.getOwners(this), 1) != 0) {
                throw exception(TypeDBException.of(OWNS_KEY_PRECONDITION_UNIQUENESS, attVertex.label(), vertex.label()));
            }
        });
        ownsEdge.delete();
    } else if (getInstances().first().isPresent()) {
        throw exception(TypeDBException.of(OWNS_KEY_PRECONDITION_NO_INSTANCES, vertex.label(), attVertex.label()));
    }
    ownsKeyEdge = vertex.outs().put(OWNS_KEY, attVertex);
    if (getSupertype().declaredOwns(false).findFirst(attributeType).isPresent())
        ownsKeyEdge.overridden(attVertex);
}
Also used : TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) INVALID_UNDEFINE_NONEXISTENT_OWNS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_OWNS) RelationImpl(com.vaticle.typedb.core.concept.thing.impl.RelationImpl) ASC(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.ASC) ThingType(com.vaticle.typedb.core.concept.type.ThingType) Iterators.loop(com.vaticle.typedb.core.common.iterator.Iterators.loop) OWNS_KEY_PRECONDITION_UNIQUENESS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_PRECONDITION_UNIQUENESS) FunctionalIterator(com.vaticle.typedb.core.common.iterator.FunctionalIterator) OWNS_KEY_PRECONDITION_NO_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_PRECONDITION_NO_INSTANCES) OWNS_KEY_VALUE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_VALUE_TYPE) SUB(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.SUB) EntityImpl(com.vaticle.typedb.core.concept.thing.impl.EntityImpl) INVALID_UNDEFINE_NONEXISTENT_PLAYS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_NONEXISTENT_PLAYS) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable) INVALID_UNDEFINE_PLAYS_HAS_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_PLAYS_HAS_INSTANCES) ThingVertex(com.vaticle.typedb.core.graph.vertex.ThingVertex) Objects(java.util.Objects) List(java.util.List) ThingImpl(com.vaticle.typedb.core.concept.thing.impl.ThingImpl) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) PLAYS_ROLE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.PLAYS_ROLE_NOT_AVAILABLE) OVERRIDDEN_NOT_SUPERTYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDDEN_NOT_SUPERTYPE) Iterators.link(com.vaticle.typedb.core.common.iterator.Iterators.link) INVALID_UNDEFINE_INHERITED_OWNS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_OWNS) Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) Function(java.util.function.Function) Forwardable.iterateSorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.iterateSorted) INVALID_UNDEFINE_OWNS_HAS_INSTANCES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_OWNS_HAS_INSTANCES) OWNS_ABSTRACT_ATT_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ABSTRACT_ATT_TYPE) Forwardable.emptySorted(com.vaticle.typedb.core.common.iterator.sorted.SortedIterators.Forwardable.emptySorted) OWNS_ATT_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_ATT_NOT_AVAILABLE) OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWS_KEY_PRECONDITION_OWNERSHIP_KEY_TOO_MANY) TYPE_HAS_SUBTYPES(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_SUBTYPES) INVALID_UNDEFINE_INHERITED_PLAYS(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.INVALID_UNDEFINE_INHERITED_PLAYS) TYPE_HAS_INSTANCES_DELETE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_DELETE) Encoding(com.vaticle.typedb.core.graph.common.Encoding) Nullable(javax.annotation.Nullable) PLAYS(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.PLAYS) GraphManager(com.vaticle.typedb.core.graph.GraphManager) UNRECOGNISED_VALUE(com.vaticle.typedb.core.common.exception.ErrorMessage.Internal.UNRECOGNISED_VALUE) OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWS_KEY_PRECONDITION_OWNERSHIP_KEY_MISSING) Iterators(com.vaticle.typedb.core.common.iterator.Iterators) OWNS_KEY(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.OWNS_KEY) AttributeImpl(com.vaticle.typedb.core.concept.thing.impl.AttributeImpl) RoleType(com.vaticle.typedb.core.concept.type.RoleType) OWNS_KEY_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OWNS_KEY_NOT_AVAILABLE) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) Iterators.compareSize(com.vaticle.typedb.core.common.iterator.Iterators.compareSize) PLAYS_ABSTRACT_ROLE_TYPE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.PLAYS_ABSTRACT_ROLE_TYPE) ROOT_TYPE_MUTATION(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.ROOT_TYPE_MUTATION) OWNS(com.vaticle.typedb.core.graph.common.Encoding.Edge.Type.OWNS) AttributeType(com.vaticle.typedb.core.concept.type.AttributeType) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex) Collections(java.util.Collections) TYPE_HAS_INSTANCES_SET_ABSTRACT(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.TYPE_HAS_INSTANCES_SET_ABSTRACT) Type(com.vaticle.typedb.core.concept.type.Type) OVERRIDE_NOT_AVAILABLE(com.vaticle.typedb.core.common.exception.ErrorMessage.TypeWrite.OVERRIDE_NOT_AVAILABLE) TypeEdge(com.vaticle.typedb.core.graph.edge.TypeEdge) Attribute(com.vaticle.typedb.core.concept.thing.Attribute) TypeVertex(com.vaticle.typedb.core.graph.vertex.TypeVertex)

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