use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class BasicTest method assert_transaction_read.
private static void assert_transaction_read(TypeDB.Transaction transaction) {
assertTrue(transaction.isOpen());
assertTrue(transaction.type().isRead());
ThingType rootType = transaction.concepts().getRootThingType();
EntityType rootEntityType = transaction.concepts().getRootEntityType();
RelationType rootRelationType = transaction.concepts().getRootRelationType();
AttributeType rootAttributeType = transaction.concepts().getRootAttributeType();
Util.assertNotNulls(rootType, rootEntityType, rootRelationType, rootAttributeType);
Stream<Consumer<TypeDB.Transaction>> typeAssertions = Stream.of(tx -> {
AttributeType.String name = tx.concepts().getAttributeType("name").asString();
Util.assertNotNulls(name);
assertEquals(rootAttributeType, name.getSupertype());
}, tx -> {
AttributeType.Long age = tx.concepts().getAttributeType("age").asLong();
Util.assertNotNulls(age);
assertEquals(rootAttributeType, age.getSupertype());
}, tx -> {
RelationType marriage = tx.concepts().getRelationType("marriage");
RoleType husband = marriage.getRelates("husband");
RoleType wife = marriage.getRelates("wife");
Util.assertNotNulls(marriage, husband, wife);
assertEquals(rootRelationType, marriage.getSupertype());
assertEquals(rootRelationType.getRelates("role"), husband.getSupertype());
assertEquals(rootRelationType.getRelates("role"), wife.getSupertype());
}, tx -> {
RelationType employment = tx.concepts().getRelationType("employment");
RoleType employee = employment.getRelates("employee");
RoleType employer = employment.getRelates("employer");
Util.assertNotNulls(employment, employee, employer);
assertEquals(rootRelationType, employment.getSupertype());
}, tx -> {
EntityType person = tx.concepts().getEntityType("person");
Util.assertNotNulls(person);
assertEquals(rootEntityType, person.getSupertype());
Stream<Consumer<TypeDB.Transaction>> subPersonAssertions = Stream.of(tx2 -> {
EntityType man = tx2.concepts().getEntityType("man");
Util.assertNotNulls(man);
assertEquals(person, man.getSupertype());
}, tx2 -> {
EntityType woman = tx2.concepts().getEntityType("woman");
Util.assertNotNulls(woman);
assertEquals(person, woman.getSupertype());
});
subPersonAssertions.parallel().forEach(assertions -> assertions.accept(tx));
}, tx -> {
EntityType company = tx.concepts().getEntityType("company");
Util.assertNotNulls(company);
assertEquals(rootEntityType, company.getSupertype());
});
typeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
}
use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class BasicTest method write_and_retrieve_attribute_ownership_rule.
@Test
public void write_and_retrieve_attribute_ownership_rule() 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 txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
AttributeType name = conceptMgr.putAttributeType("name", STRING);
EntityType person = conceptMgr.putEntityType("person");
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
person.setPlays(friendship.getRelates("friend"));
person.setOwns(name);
logicMgr.putRule("people-have-names", TypeQL.parsePattern("{$x isa person; }").asConjunction(), TypeQL.parseVariable("$x has name \"i have a name\"").asThing());
txn.commit();
}
try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
Rule rule = logicMgr.getRule("people-have-names");
Pattern when = rule.getWhenPreNormalised();
ThingVariable<?> then = rule.getThenPreNormalised();
assertEquals(TypeQL.parsePattern("{$x isa person;}"), when);
assertEquals(TypeQL.parseVariable("$x has name \"i have a name\""), then);
}
}
}
}
use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class BasicTest method write_types_concurrently.
@Test
public void write_types_concurrently() throws IOException, InterruptedException {
Util.resetDirectory(dataDir);
try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
typedb.create(database);
assertTrue(typedb.isOpen());
assertEquals(1, typedb.all().size());
assertEquals(database, typedb.all().iterator().next().name());
try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
assertTrue(session.isOpen());
assertEquals(database, session.database().name());
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
assertTrue(transaction.isOpen());
assertTrue(transaction.type().isRead());
Stream<Consumer<TypeDB.Transaction>> rootTypeAssertions = Stream.of(tx -> {
ThingType rootType = tx.concepts().getRootThingType();
assertNotNull(rootType);
}, tx -> {
EntityType rootEntityType = tx.concepts().getRootEntityType();
assertNotNull(rootEntityType);
}, tx -> {
RelationType rootRelationType = tx.concepts().getRootRelationType();
assertNotNull(rootRelationType);
}, tx -> {
AttributeType rootAttributeType = tx.concepts().getRootAttributeType();
assertNotNull(rootAttributeType);
});
rootTypeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
assertTrue(transaction.isOpen());
assertTrue(transaction.type().isWrite());
ThingType rootType = transaction.concepts().getRootThingType();
EntityType rootEntityType = transaction.concepts().getRootEntityType();
RelationType rootRelationType = transaction.concepts().getRootRelationType();
AttributeType rootAttributeType = transaction.concepts().getRootAttributeType();
Util.assertNotNulls(rootType, rootEntityType, rootRelationType, rootAttributeType);
Stream<Consumer<TypeDB.Transaction>> typeAssertions = Stream.of(tx -> {
AttributeType name = tx.concepts().putAttributeType("name", STRING).asString();
Util.assertNotNulls(name);
assertEquals(rootAttributeType, name.getSupertype());
}, tx -> {
AttributeType.Long age = tx.concepts().putAttributeType("age", LONG).asLong();
Util.assertNotNulls(age);
assertEquals(rootAttributeType, age.getSupertype());
}, tx -> {
RelationType marriage = tx.concepts().putRelationType("marriage");
marriage.setRelates("husband");
marriage.setRelates("wife");
Util.assertNotNulls(marriage);
assertEquals(rootRelationType, marriage.getSupertype());
}, tx -> {
RelationType employment = tx.concepts().putRelationType("employment");
employment.setRelates("employee");
employment.setRelates("employer");
Util.assertNotNulls(employment);
assertEquals(rootRelationType, employment.getSupertype());
}, tx -> {
EntityType person = tx.concepts().putEntityType("person");
Util.assertNotNulls(person);
assertEquals(rootEntityType, person.getSupertype());
Stream<Consumer<TypeDB.Transaction>> subPersonAssertions = Stream.of(tx2 -> {
EntityType man = tx2.concepts().putEntityType("man");
man.setSupertype(person);
Util.assertNotNulls(man);
assertEquals(person, man.getSupertype());
}, tx2 -> {
EntityType woman = tx2.concepts().putEntityType("woman");
woman.setSupertype(person);
Util.assertNotNulls(woman);
assertEquals(person, woman.getSupertype());
});
subPersonAssertions.parallel().forEach(assertions -> assertions.accept(tx));
}, tx -> {
EntityType company = tx.concepts().putEntityType("company");
Util.assertNotNulls(company);
assertEquals(rootEntityType, company.getSupertype());
});
typeAssertions.parallel().forEach(assertion -> assertion.accept(transaction));
transaction.commit();
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
assert_transaction_read(transaction);
}
}
}
try (TypeDB.DatabaseManager typedb = CoreDatabaseManager.open(options)) {
assertTrue(typedb.isOpen());
assertEquals(1, typedb.all().size());
assertEquals(database, typedb.all().iterator().next().name());
try (TypeDB.Session session = typedb.session(database, Arguments.Session.Type.SCHEMA)) {
assertTrue(session.isOpen());
assertEquals(database, session.database().name());
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
assert_transaction_read(transaction);
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
AttributeType.String gender = transaction.concepts().putAttributeType("gender", STRING).asString();
EntityType school = transaction.concepts().putEntityType("school");
RelationType teaching = transaction.concepts().putRelationType("teaching");
teaching.setRelates("teacher");
teaching.setRelates("student");
RoleType teacher = teaching.getRelates("teacher");
RoleType student = teaching.getRelates("student");
Util.assertNotNulls(gender, school, teaching, teacher, student);
transaction.commit();
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
assert_transaction_read(transaction);
AttributeType.String gender = transaction.concepts().getAttributeType("gender").asString();
EntityType school = transaction.concepts().getEntityType("school");
RelationType teaching = transaction.concepts().getRelationType("teaching");
RoleType teacher = teaching.getRelates("teacher");
RoleType student = teaching.getRelates("student");
Util.assertNotNulls(gender, school, teaching, teacher, student);
}
}
}
}
use of com.vaticle.typedb.core.concept.type.RelationType 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.type.RelationType in project grakn by graknlabs.
the class DataImporter method importSkippedRelations.
private void importSkippedRelations() {
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
skippedRelations.forEach(msg -> {
RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
if (relType == null)
throw TypeDBException.of(TYPE_NOT_FOUND, msg.getLabel());
Relation importedRelation = relType.create();
idMapper.put(msg.getId(), importedRelation.getIID());
int ownershipCount = 0;
for (DataProto.Item.OwnedAttribute ownership : msg.getAttributeList()) {
Thing attribute = transaction.concepts().getThing(idMapper.get(ownership.getId()));
assert attribute != null;
importedRelation.setHas(attribute.asAttribute());
ownershipCount++;
}
status.ownershipCount.addAndGet(ownershipCount);
});
skippedRelations.forEach(msg -> {
RelationType relType = transaction.concepts().getRelationType(msg.getLabel());
Relation relation = transaction.concepts().getThing(idMapper.get(msg.getId())).asRelation();
msg.getRoleList().forEach(roleMsg -> {
RoleType roleType = getRoleType(relType, roleMsg);
for (DataProto.Item.Relation.Role.Player playerMessage : roleMsg.getPlayerList()) {
Thing player = transaction.concepts().getThing(idMapper.get(playerMessage.getId()));
if (player == null)
throw TypeDBException.of(PLAYER_NOT_FOUND, relType.getLabel());
else
relation.addPlayer(roleType, player);
}
});
});
transaction.commit();
}
}
Aggregations