use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types.
@Test
public void unUnify_produces_cartesian_named_types() {
String conjunction = "{$r ($role: $x) isa $rel;}";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("people-are-self-friends", "{ $x isa person; }", " (friend: $x) isa friendship ", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
// test filter allows a valid answer
Relation friendship = instanceOf("friendship").asRelation();
Thing person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("friendship"), friendship.getType()), pair(Variable.label("friendship:friend"), friendship.getType().getRelates("friend")));
List<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map())).toList();
assertEquals(6, unified.size());
Set<Map<String, String>> expected = set(new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "relation:role");
}
}, new HashMap<String, String>() {
{
put("$rel", "relation");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "relation");
put("$role", "relation:role");
}
}, new HashMap<String, String>() {
{
put("$rel", "thing");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "thing");
put("$role", "relation:role");
}
});
Set<Map<String, String>> actual = new HashSet<>();
iterate(unified).forEachRemaining(answer -> {
actual.add(new HashMap<String, String>() {
{
put("$rel", answer.get("rel").asType().getLabel().name());
put("$role", answer.get("role").asType().getLabel().scopedName());
}
});
});
assertEquals(expected, actual);
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_type_and_player_unifies_rule_relation_exact.
@Test
public void relation_type_and_player_unifies_rule_relation_exact() {
Unifier unifier = uniqueUnifier("{ (employee: $y) isa $rel; }", rule("(employee: $x) isa employment", "{ $x isa person; }"));
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$y", set("$x"));
put("$rel", set("$_employment"));
put("$_0", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("relation:employee")));
assertEquals(2, unifier.requirements().types().size());
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Relation employment = instanceOf("employment").asRelation();
Thing person = instanceOf("person");
addRolePlayer(employment, "employee", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(3, unifiedAnswer.concepts().size());
assertEquals(employment.getType(), unifiedAnswer.get("rel"));
assertEquals(person, unifiedAnswer.get("y"));
assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
// filter out invalid types
Relation friendship = instanceOf("friendship").asRelation();
person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
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 UnifyRelationConcludableTest method relation_and_player_unifies_rule_relation_exact.
@Test
public void relation_and_player_unifies_rule_relation_exact() {
Unifier unifier = uniqueUnifier("{ $r (employee: $y) isa employment; }", rule(" (employee: $x) isa employment", "{ $x isa person; }"));
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$y", set("$x"));
put("$r", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.name("r")));
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
assertEquals(2, unifier.requirements().types().size());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
// code below tests unifier applied to an answer that is 1) satisfiable, 2) non-satisfiable
Relation employment = instanceOf("employment").asRelation();
Thing person = instanceOf("person");
addRolePlayer(employment, "employee", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(2, unifiedAnswer.concepts().size());
assertEquals(employment, unifiedAnswer.get("r"));
assertEquals(person, unifiedAnswer.get("y"));
// filter out invalid types
Relation friendship = instanceOf("friendship").asRelation();
person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
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 QueryTest method test_query_delete.
@Test
public void test_query_delete() 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.Session session = typedb.session(database, Arguments.Session.Type.DATA)) {
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
String insertString = "insert " + "$o isa organisation, has name 'vaticle'; " + "$t isa team, has name 'engineers', has symbol 'vaticle/engineers'; " + "$u isa user, has name 'butler', has email 'butler@vaticle.com'; " + "($o, $t) isa org-team; " + "($o, $u) isa org-member; " + "($t, $u) isa team-member;";
TypeQLInsert insertQuery = TypeQL.parseQuery(insertString);
transaction.query().insert(insertQuery);
transaction.commit();
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.WRITE)) {
String deleteString = "match $x isa thing; delete $x isa thing;";
TypeQLDelete deleteQuery = TypeQL.parseQuery(deleteString);
transaction.query().delete(deleteQuery);
transaction.commit();
}
try (TypeDB.Transaction transaction = session.transaction(Arguments.Transaction.Type.READ)) {
String matchString = "match $x isa thing;";
TypeQLMatch matchQuery = TypeQL.parseQuery(matchString);
FunctionalIterator<ConceptMap> answers = transaction.query().match(matchQuery);
assertFalse(answers.hasNext());
}
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class RuleTest method rule_has_variable_materialises_when_missing.
@Test
public void rule_has_variable_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; $a 10 isa age-in-days; }").asConjunction(), TypeQL.parseVariable("$x has $a").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();
Attribute.Long ageInDays10 = ageInDays.asLong().put(10L);
Rule rule = txn.logic().getRule("old-milk-is-not-good");
ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), milkInst), pair(Identifier.Variable.name("a"), ageInDays10)));
List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
assertEquals(1, materialisations.size());
assertEquals(2, materialisations.get(0).size());
List<? extends Attribute> ageInDaysOwned = milkInst.getHas(ageInDays).toList();
assertEquals(1, ageInDaysOwned.size());
assertEquals(ageInDays10, ageInDaysOwned.get(0));
}
}
}
}
Aggregations