use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class ReasonerTest method test_exception_kills_query.
@Test
public void test_exception_kills_query() {
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
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);
logicMgr.putRule("old-milk-is-not-good", TypeQL.parsePattern("{ $x isa milk, has age-in-days >= 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 = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 5;").asInsert());
txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
txn.reasoner().resolverRegistry().terminateResolvers(new RuntimeException());
try {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x isa is-still-good;").asMatch()).toList();
} catch (TypeDBException e) {
assertEquals(e.code().get(), RESOLUTION_TERMINATED.code());
return;
}
fail();
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class ReasonerTest method test_no_rules.
@Test
public void test_no_rules() {
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.SCHEMA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
EntityType milk = conceptMgr.putEntityType("milk");
AttributeType ageInDays = conceptMgr.putAttributeType("age-in-days", AttributeType.ValueType.LONG);
milk.setOwns(ageInDays);
txn.commit();
}
}
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 5;").asInsert());
txn.query().insert(TypeQL.parseQuery("insert $x isa milk, has age-in-days 10;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ)) {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match $x has age-in-days $a;").asMatch()).toList();
ans.iterator().forEachRemaining(a -> {
assertEquals("age-in-days", a.get("a").asThing().getType().getLabel().scopedName());
assertEquals("milk", a.get("x").asThing().getType().getLabel().scopedName());
});
assertEquals(2, ans.size());
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class RuleTest method rule_relation_does_not_materialise_when_present.
@Test
public void rule_relation_does_not_materialise_when_present() 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();
RelationType friendship = conceptMgr.getRelationType("friendship");
txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; " + "(spouse: $x, spouse: $y) isa marriage;" + "(friend: $x, friend: $y) isa friendship;").asInsert());
List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
assertEquals(1, friendshipInstances.size());
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());
friendshipInstances = friendship.getInstances().toList();
assertEquals(1, friendshipInstances.size());
assertEquals(friendshipInstances.get(0), materialisations.get(0).get(Identifier.Variable.anon(0)));
assertEquals(friendship, materialisations.get(0).get(Identifier.Variable.label("friendship")));
}
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyIsaConcludableTest method isa_exact_unifies_rule_relation_exact.
@Test
public void isa_exact_unifies_rule_relation_exact() {
String conjunction = "{ $a isa relation; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "(employee: $x) isa employment", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
HashMap<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$a", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(1, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("employment")), unifier.requirements().isaExplicit().get(Identifier.Variable.name("a")));
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.anon(0), instanceOf("employment")), pair(Identifier.Variable.label("employment"), conceptMgr.getThingType("employment")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.anon(0), instanceOf("age")), pair(Identifier.Variable.label("employment"), conceptMgr.getThingType("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyIsaConcludableTest method isa_exact_unifies_rule_has_exact.
@Test
public void isa_exact_unifies_rule_has_exact() {
String conjunction = "{ $a isa name; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Isa queryConcludable = concludables.iterator().next().asIsa();
Rule rule = createRule("isa-rule", "{ $x isa person; }", "$x has first-name \"john\"", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$a", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(1, unifier.requirements().types().size());
assertEquals(1, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().values().iterator().next());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.anon(0), instanceOf("first-name", "john")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("first-name")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(1, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.anon(0), instanceOf("age")), pair(Identifier.Variable.label("first-name"), conceptMgr.getThingType("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
Aggregations