use of com.vaticle.typedb.core.pattern.variable.Variable 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.pattern.variable.Variable in project grakn by graknlabs.
the class ResolutionTest method createRootAndAssertResponses.
private void createRootAndAssertResponses(CoreTransaction transaction, Conjunction conjunction, long answerCount, long explainableAnswers) throws InterruptedException {
ResolverRegistry registry = transaction.reasoner().resolverRegistry();
LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
AtomicLong doneReceived = new AtomicLong(0L);
Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
Actor.Driver<RootResolver.Conjunction> root;
try {
root = registry.root(conjunction, responses::add, iterDone -> doneReceived.incrementAndGet(), (throwable) -> fail());
} catch (TypeDBException e) {
fail();
return;
}
assertResponses(root, filter, responses, doneReceived, answerCount, explainableAnswers);
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class Rule method pruneThenResolvedTypes.
/**
* Remove type hints in the `then` pattern that are not valid in the `when` pattern
*/
private void pruneThenResolvedTypes() {
then.variables().stream().filter(variable -> variable.id().isName()).forEach(thenVar -> {
Variable whenVar = when.variable(thenVar.id());
thenVar.retainInferredTypes(whenVar.inferredTypes());
if (thenVar.inferredTypes().isEmpty())
then.setCoherent(false);
});
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class Reasoner method mayReason.
private boolean mayReason(Disjunction disjunction) {
for (Conjunction conj : disjunction.conjunctions()) {
Set<Variable> vars = conj.variables();
List<Negation> negs = conj.negations();
if (iterate(vars).flatMap(v -> iterate(v.inferredTypes())).distinct().anyMatch(this::hasRule))
return true;
if (!negs.isEmpty() && iterate(negs).anyMatch(n -> mayReason(n.disjunction())))
return true;
}
return false;
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class RuleTest method new_type_updates_rule_conclusion_index.
@Test
public void new_type_updates_rule_conclusion_index() 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 marriage = conceptMgr.putRelationType("marriage");
AttributeType name = conceptMgr.putAttributeType("name", AttributeType.ValueType.STRING);
name.setAbstract();
AttributeType firstName = conceptMgr.putAttributeType("first-name", AttributeType.ValueType.STRING);
firstName.setSupertype(name);
marriage.setRelates("spouse");
person.setPlays(marriage.getRelates("spouse"));
person.setOwns(firstName);
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("first-name")), nameAttr.inferredTypes());
txn.commit();
}
// add a new subtype of an attribute in a rule
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
AttributeType lastName = conceptMgr.putAttributeType("last-name", AttributeType.ValueType.STRING);
lastName.setSupertype(conceptMgr.getAttributeType("name"));
conceptMgr.getEntityType("person").setOwns(lastName);
txn.commit();
}
// check the new attribute type is re-indexed in the conclusions index
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
LogicManager logicMgr = txn.logic();
Rule marriageSameName = logicMgr.getRule("marriage-same-name");
assertEquals(set(marriageSameName), logicMgr.rulesConcludingHas(Label.of("last-name")).toSet());
}
}
}
}
Aggregations