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();
}
}
}
}
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();
}
}
}
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));
}
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));
}
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);
}
Aggregations