use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.
the class BasicTest method write_and_retrieve_attribute_ownership_rule.
@Test
public void write_and_retrieve_attribute_ownership_rule() 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 txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
AttributeType name = conceptMgr.putAttributeType("name", STRING);
EntityType person = conceptMgr.putEntityType("person");
RelationType friendship = conceptMgr.putRelationType("friendship");
friendship.setRelates("friend");
person.setPlays(friendship.getRelates("friend"));
person.setOwns(name);
logicMgr.putRule("people-have-names", TypeQL.parsePattern("{$x isa person; }").asConjunction(), TypeQL.parseVariable("$x has name \"i have a name\"").asThing());
txn.commit();
}
try (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
Rule rule = logicMgr.getRule("people-have-names");
Pattern when = rule.getWhenPreNormalised();
ThingVariable<?> then = rule.getThenPreNormalised();
assertEquals(TypeQL.parsePattern("{$x isa person;}"), when);
assertEquals(TypeQL.parseVariable("$x has name \"i have a name\""), then);
}
}
}
}
use of com.vaticle.typeql.lang.pattern.Pattern 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.typeql.lang.pattern.Pattern in project grakn by graknlabs.
the class BasicTest method write_and_retrieve_relation_rule.
@Test
public void write_and_retrieve_relation_rule() 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 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 (TypeDB.Transaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
ConceptManager conceptMgr = txn.concepts();
LogicManager logicMgr = txn.logic();
EntityType person = conceptMgr.getEntityType("person");
RelationType friendship = conceptMgr.getRelationType("friendship");
RoleType friend = friendship.getRelates("friend");
RelationType marriage = conceptMgr.getRelationType("marriage");
RoleType spouse = marriage.getRelates("spouse");
Rule rule = logicMgr.getRule("marriage-is-friendship");
Pattern when = rule.getWhenPreNormalised();
ThingVariable<?> then = rule.getThenPreNormalised();
assertEquals(TypeQL.parsePattern("{$x isa person; $y isa person; (spouse: $x, spouse: $y) isa marriage; }"), when);
assertEquals(TypeQL.parseVariable("(friend: $x, friend: $y) isa friendship"), then);
}
}
}
}
use of com.vaticle.typeql.lang.pattern.Pattern in project grakn by graknlabs.
the class Rule method whenPattern.
private Conjunction whenPattern(com.vaticle.typeql.lang.pattern.Conjunction<? extends Pattern> conjunction, com.vaticle.typeql.lang.pattern.variable.ThingVariable<?> then, LogicManager logicMgr) {
Disjunction when = Disjunction.create(conjunction.normalise(), VariableRegistry.createFromThings(list(then)));
assert when.conjunctions().size() == 1;
if (iterate(when.conjunctions().get(0).negations()).filter(neg -> neg.disjunction().conjunctions().size() != 1).hasNext()) {
throw TypeDBException.of(INVALID_NEGATION_CONTAINS_DISJUNCTION, getLabel());
}
logicMgr.typeInference().applyCombination(when);
return when.conjunctions().get(0);
}
Aggregations