use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class RuleTest method rule_indexes_created_and_readable.
// ------------ Rule conclusion indexing ------------
@Test
public void rule_indexes_created_and_readable() 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);
AttributeType age = conceptMgr.putAttributeType("age", AttributeType.ValueType.LONG);
marriage.setRelates("spouse");
person.setPlays(friendship.getRelates("friend"));
person.setPlays(marriage.getRelates("spouse"));
person.setOwns(name);
person.setOwns(age);
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 allFriendsRule = logicMgr.putRule("all-people-are-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; $t type friendship; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa $t").asThing());
Conjunction allFriendsThen = allFriendsRule.then();
Variable allFriendsRelation = getVariable(allFriendsThen.variables(), Identifier.Variable.anon(0));
assertEquals(set(Label.of("friendship")), allFriendsRelation.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());
Rule peopleHaveAge10 = logicMgr.putRule("people-have-age-10", TypeQL.parsePattern("{ $x isa person; }").asConjunction(), TypeQL.parseVariable("$x has age 10").asThing());
Conjunction age10 = peopleHaveAge10.then();
Variable ageAttr = getVariable(age10.variables(), Identifier.Variable.anon(0));
assertEquals(set(Label.of("age")), ageAttr.inferredTypes());
txn.commit();
}
}
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
LogicManager logicMgr = txn.logic();
Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
Rule allFriendsRule = txn.logic().getRule("all-people-are-friends");
assertEquals(set(marriageFriendsRule, allFriendsRule), friendshipRules);
Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
Rule marriageSameName = txn.logic().getRule("marriage-same-name");
assertEquals(set(marriageSameName), hasNameRules);
Set<Rule> hasAgeRules = logicMgr.rulesConcludingHas(Label.of("age")).toSet();
Set<Rule> ageRules = logicMgr.rulesConcluding(Label.of("age")).toSet();
Rule peopleHaveAge10 = txn.logic().getRule("people-have-age-10");
assertEquals(set(peopleHaveAge10), hasAgeRules);
assertEquals(set(peopleHaveAge10), ageRules);
}
}
}
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class RuleTest method rule_indexes_update_on_rule_delete.
@Test
public void rule_indexes_update_on_rule_delete() 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 allFriendsRule = logicMgr.putRule("all-people-are-friends", TypeQL.parsePattern("{ $x isa person; $y isa person; $t type friendship; }").asConjunction(), TypeQL.parseVariable("(friend: $x, friend: $y) isa $t").asThing());
Conjunction allFriendsThen = allFriendsRule.then();
Variable allFriendsRelation = getVariable(allFriendsThen.variables(), Identifier.Variable.anon(0));
assertEquals(set(Label.of("friendship")), allFriendsRelation.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();
}
// check index after commit, and delete some rules
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
LogicManager logicMgr = txn.logic();
Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
Rule allFriendsRule = txn.logic().getRule("all-people-are-friends");
assertEquals(set(marriageFriendsRule, allFriendsRule), friendshipRules);
allFriendsRule.delete();
Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
Rule marriageSameName = txn.logic().getRule("marriage-same-name");
assertEquals(set(marriageSameName), hasNameRules);
marriageSameName.delete();
txn.commit();
}
}
// check indexed types, should only includes rules that are still present
try (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
LogicManager logicMgr = txn.logic();
Set<Rule> friendshipRules = logicMgr.rulesConcluding(Label.of("friendship")).toSet();
Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
assertEquals(set(marriageFriendsRule), friendshipRules);
Set<Rule> hasNameRules = logicMgr.rulesConcludingHas(Label.of("name")).toSet();
assertEquals(set(), hasNameRules);
}
}
}
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class Negation method create.
public static Negation create(com.vaticle.typeql.lang.pattern.Negation<?> typeql, VariableRegistry bounds) {
try (ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "create")) {
Disjunction disjunction = Disjunction.create(typeql.normalise().pattern(), bounds);
disjunction.conjunctions().forEach(conjunction -> {
if (iterate(conjunction.variables()).map(Variable::reference).filter(Reference::isName).noneMatch(bounds::contains)) {
throw TypeDBException.of(UNBOUNDED_NEGATION);
}
});
return new Negation(disjunction);
}
}
use of com.vaticle.typedb.core.pattern.variable.Variable in project grakn by graknlabs.
the class Resolver method compatibleBounds.
private Optional<ConceptMap> compatibleBounds(Conjunction conjunction, ConceptMap bounds) {
Map<Retrievable, Concept> newBounds = new HashMap<>();
for (Map.Entry<Retrievable, ? extends Concept> entry : bounds.concepts().entrySet()) {
Retrievable id = entry.getKey();
Concept bound = entry.getValue();
Variable conjVariable = conjunction.variable(id);
assert conjVariable != null;
if (conjVariable.isThing()) {
if (!conjVariable.asThing().iid().isPresent())
newBounds.put(id, bound);
else if (!conjVariable.asThing().iid().get().iid().equals(bound.asThing().getIID())) {
return Optional.empty();
}
} else if (conjVariable.isType()) {
if (!conjVariable.asType().label().isPresent())
newBounds.put(id, bound);
else if (!conjVariable.asType().label().get().properLabel().equals(bound.asType().getLabel())) {
return Optional.empty();
}
} else {
throw TypeDBException.of(ILLEGAL_STATE);
}
}
return Optional.of(new ConceptMap(newBounds));
}
Aggregations