use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.
the class RuleTest method rule_contains_indexes_prevent_undefining_contained_types.
@Test
public void rule_contains_indexes_prevent_undefining_contained_types() 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();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.putEntityType("person");
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
marriage.setRelates("spouse");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("spouse"));
person.setOwns(name);
Rule marriageFriendsRule = logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
Conjunction marriageFriendsThen = marriageFriendsRule.then();
Variable marriageFriendsRelation = getVariable(marriageFriendsThen.variables(), Identifier.Variable.anon(0));
assertEquals(set(Label.of("friendship")), marriageFriendsRelation.inferredTypes());
Rule marriageSameName = logicMgr.putRule("marriage-same-name", TypeQL.parsePattern("{ $x isa person, has name $a; $y isa person; (spouse:$x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("$y has $a").asThing());
Conjunction sameName = marriageSameName.then();
Variable nameAttr = getVariable(sameName.variables(), Identifier.Variable.name("a"));
assertEquals(set(Label.of("name")), nameAttr.inferredTypes());
txn.commit();
}
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
EntityType person = conceptMgr.getEntityType("person");
assertThrowsTypeDBException(person::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
}
}
}
}
use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.
the class RuleTest method rule_has_explicit_materialises_when_missing.
@Test
public void rule_has_explicit_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, has age-in-days $a; $a >= 10; }").asConjunction(), TypeQL.parseVariable("$x has is-still-good false").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();
milkInst.setHas(ageInDays.asLong().put(20L));
Rule rule = txn.logic().getRule("old-milk-is-not-good");
ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst)));
List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
assertEquals(1, materialisations.size());
assertEquals(3, materialisations.get(0).size());
AttributeType isStillGood = conceptMgr.getAttributeType("is-still-good");
List<? extends Attribute> isStillGoodOwned = milkInst.getHas(isStillGood).toList();
assertEquals(1, isStillGoodOwned.size());
assertEquals(isStillGood.asBoolean().getInstances().first().get(), isStillGoodOwned.get(0));
}
}
}
}
use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.
the class RuleTest method rule_then_that_cannot_be_satisfied_throws_an_error.
@Test
public void rule_then_that_cannot_be_satisfied_throws_an_error() 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)) {
final ConceptManager conceptMgr = txn.concepts();
final EntityType person = conceptMgr.putEntityType("person");
final EntityType dog = conceptMgr.putEntityType("dog");
final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
person.setOwns(name);
ThingVariable<?> then = TypeQL.parseVariable("$x has name 'fido'").asThing();
assertThrowsTypeDBException(() -> txn.logic().putRule("dogs-are-named-fido", TypeQL.parsePattern("{$x isa dog;}").asConjunction(), then), RULE_THEN_CANNOT_BE_SATISFIED.code());
}
}
}
}
use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.
the class RuleTest method rule_relation_materialises_when_missing.
@Test
public void rule_relation_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();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.putEntityType("person");
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
marriage.setRelates("spouse");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("spouse"));
logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").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();
txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage;").asInsert());
EntityType person = conceptMgr.getEntityType("person");
List<? extends Entity> people = person.getInstances().toList();
assertEquals(2, people.size());
Rule rule = txn.logic().getRule("marriage-is-friendship");
ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), people.get(0)), pair(Identifier.Variable.name("y"), people.get(1))));
List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
assertEquals(1, materialisations.size());
assertEquals(5, materialisations.get(0).size());
RelationType friendship = conceptMgr.getRelationType("friendship");
List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
assertEquals(1, friendshipInstances.size());
assertEquals(set(people.get(0), people.get(1)), friendshipInstances.get(0).getPlayers().toSet());
}
}
}
}
use of com.vaticle.typedb.core.concept.type.EntityType in project grakn by graknlabs.
the class RuleTest method rule_that_cannot_be_inserted_throws_an_error.
@Test
public void rule_that_cannot_be_inserted_throws_an_error() 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)) {
final ConceptManager conceptMgr = txn.concepts();
final EntityType animal = conceptMgr.putEntityType("animal");
final EntityType person = conceptMgr.putEntityType("person");
final EntityType dog = conceptMgr.putEntityType("dog");
final AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
person.setOwns(name);
person.setSupertype(animal);
dog.setSupertype(animal);
assertThrows(() -> txn.logic().putRule("animals-are-named-fido", TypeQL.parsePattern("{$x isa animal;}").asConjunction(), TypeQL.parseVariable("$x has name 'fido'").asThing()));
}
}
}
}
Aggregations