use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class ExplanationTest method test_relation_explainable_multiple_ways.
@Test
public void test_relation_explainable_multiple_ways() {
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 person = conceptMgr.putEntityType("person");
AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
person.setOwns(name);
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
RelationType marriage = conceptMgr.putRelationType("marriage");
marriage.setRelates("husband");
marriage.setRelates("wife");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("husband"));
person.setPlays(marriage.getRelates("wife"));
logicMgr.putRule("marriage-is-friendship", TypeQL.parsePattern("{ $x isa person; $y isa person; (husband: $x, wife: $y) isa marriage; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship").asThing());
logicMgr.putRule("everyone-is-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; not { $x is $y; }; }").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 = singleThreadElgTransaction(session, Arguments.Transaction.Type.WRITE)) {
txn.query().insert(TypeQL.parseQuery("insert $x isa person, has name 'Zack'; $y isa person, has name 'Yasmin'; (husband: $x, wife: $y) isa marriage;").asInsert());
txn.commit();
}
try (CoreTransaction txn = singleThreadElgTransaction(session, Arguments.Transaction.Type.READ, (new Options.Transaction().explain(true)))) {
List<ConceptMap> ans = txn.query().match(TypeQL.parseQuery("match (friend: $p1, friend: $p2) isa friendship; $p1 has name $na;").asMatch()).toList();
assertEquals(2, ans.size());
assertFalse(ans.get(0).explainables().isEmpty());
assertFalse(ans.get(1).explainables().isEmpty());
assertSingleExplainableExplanations(ans.get(0), 1, 1, 3, txn);
assertSingleExplainableExplanations(ans.get(1), 1, 1, 3, txn);
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class ExplanationTest method applyMapping.
private ConceptMap applyMapping(Map<Retrievable, Set<Retrievable>> mapping, ConceptMap completeMap) {
Map<Retrievable, Concept> concepts = new HashMap<>();
mapping.forEach((from, tos) -> {
assertTrue(completeMap.contains(from));
Concept concept = completeMap.get(from);
tos.forEach(mapped -> {
assertTrue(!concepts.containsKey(mapped) || concepts.get(mapped).equals(concept));
concepts.put(mapped, concept);
});
});
return new ConceptMap(concepts);
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap 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.answer.ConceptMap in project grakn by graknlabs.
the class TypeQLSteps method answer_groups_are.
@Then("answer groups are")
public void answer_groups_are(List<Map<String, String>> answerIdentifierTable) {
Set<AnswerIdentifierGroup> answerIdentifierGroups = answerIdentifierTable.stream().collect(Collectors.groupingBy(x -> x.get(AnswerIdentifierGroup.GROUP_COLUMN_NAME))).values().stream().map(AnswerIdentifierGroup::new).collect(Collectors.toSet());
assertEquals(String.format("Expected [%d] answer groups, but found [%d].", answerIdentifierGroups.size(), answerGroups.size()), answerIdentifierGroups.size(), answerGroups.size());
for (AnswerIdentifierGroup answerIdentifierGroup : answerIdentifierGroups) {
String[] identifier = answerIdentifierGroup.ownerIdentifier.split(":", 2);
UniquenessCheck checker;
switch(identifier[0]) {
case "label":
checker = new LabelUniquenessCheck(identifier[1]);
break;
case "key":
checker = new KeyUniquenessCheck(identifier[1]);
break;
case "value":
checker = new ValueUniquenessCheck(identifier[1]);
break;
default:
throw new IllegalStateException("Unexpected value: " + identifier[0]);
}
ConceptMapGroup answerGroup = answerGroups.stream().filter(ag -> checker.check(ag.owner())).findAny().orElse(null);
assertNotNull(String.format("The group identifier [%s] does not match any of the answer group owners.", answerIdentifierGroup.ownerIdentifier), answerGroup);
List<Map<String, String>> answersIdentifiers = answerIdentifierGroup.answersIdentifiers;
for (ConceptMap answer : answerGroup.conceptMaps()) {
List<Map<String, String>> matchingIdentifiers = new ArrayList<>();
for (Map<String, String> answerIdentifiers : answersIdentifiers) {
if (matchAnswerConcept(answerIdentifiers, answer)) {
matchingIdentifiers.add(answerIdentifiers);
}
}
assertEquals(String.format("An identifier entry (row) should match 1-to-1 to an answer, but there were [%d] matching identifier entries for answer with variables %s.", matchingIdentifiers.size(), answer.concepts().keySet().toString()), 1, matchingIdentifiers.size());
}
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class MaterialiserTest method testDeduplicationOfInferredConcepts.
@Test
public void testDeduplicationOfInferredConcepts() {
loadTransitivityExample(databaseMgr);
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
Materialiser materialiser = Materialiser.materialise(session);
TypeQLMatch inferredAnswersQuery = TypeQL.match(TypeQL.var("lh").isa("location-hierarchy"));
List<ConceptMap> inferredAnswers = iterate(materialiser.query(inferredAnswersQuery).entrySet()).flatMap(Map.Entry::getValue).toList();
assertEquals(6, inferredAnswers.size());
}
}
Aggregations