use of com.vaticle.typedb.core.concept.type.RelationType 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.concept.type.RelationType in project grakn by graknlabs.
the class RuleTest method rule_relation_does_not_materialise_when_present.
@Test
public void rule_relation_does_not_materialise_when_present() 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");
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 (CoreSession session = databaseMgr.session(database, Arguments.Session.Type.DATA)) {
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
RelationType friendship = conceptMgr.getRelationType("friendship");
txn.query().insert(TypeQL.parseQuery("insert $x isa person; $y isa person; " + "(spouse: $x, spouse: $y) isa marriage;" + "(friend: $x, friend: $y) isa friendship;").asInsert());
List<? extends Relation> friendshipInstances = friendship.getInstances().toList();
assertEquals(1, friendshipInstances.size());
EntityType person = conceptMgr.getEntityType("person");
List<? extends Entity> people = person.getInstances().toList();
assertEquals(2, people.size());
Rule rule = txn.logic().getRule("marriage-is-friendship");
ConceptMap whenAnswer = new ConceptMap(map(pair(Identifier.Variable.name("x"), people.get(0)), pair(Identifier.Variable.name("y"), people.get(1))));
List<Map<Identifier.Variable, Concept>> materialisations = rule.conclusion().materialise(whenAnswer, txn.traversal(), conceptMgr).toList();
assertEquals(1, materialisations.size());
assertEquals(5, materialisations.get(0).size());
friendshipInstances = friendship.getInstances().toList();
assertEquals(1, friendshipInstances.size());
assertEquals(friendshipInstances.get(0), materialisations.get(0).get(Identifier.Variable.anon(0)));
assertEquals(friendship, materialisations.get(0).get(Identifier.Variable.label("friendship")));
}
}
}
}
use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class ThingTypeSteps method thing_type_set_supertype.
@When("{root_label}\\( ?{type_label} ?) set supertype: {type_label}")
public void thing_type_set_supertype(RootLabel rootLabel, String typeLabel, String superLabel) {
switch(rootLabel) {
case ENTITY:
EntityType entitySuperType = tx().concepts().getEntityType(superLabel);
tx().concepts().getEntityType(typeLabel).setSupertype(entitySuperType);
break;
case ATTRIBUTE:
AttributeType attributeSuperType = tx().concepts().getAttributeType(superLabel);
tx().concepts().getAttributeType(typeLabel).setSupertype(attributeSuperType);
break;
case RELATION:
RelationType relationSuperType = tx().concepts().getRelationType(superLabel);
tx().concepts().getRelationType(typeLabel).setSupertype(relationSuperType);
break;
}
}
use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class RelationTypeImpl method setRelates.
@Override
public void setRelates(String roleLabel) {
validateIsNotDeleted();
TypeVertex roleTypeVertex = graphMgr.schema().getType(roleLabel, vertex.label());
if (roleTypeVertex == null) {
if (getSupertypes().filter(t -> !t.equals(this) && t.isRelationType()).map(TypeImpl::asRelationType).flatMap(RelationType::getRelates).anyMatch(role -> role.getLabel().name().equals(roleLabel))) {
throw exception(TypeDBException.of(RELATION_RELATES_ROLE_FROM_SUPERTYPE, roleLabel, getLabel()));
} else {
RoleTypeImpl roleType = RoleTypeImpl.of(graphMgr, roleLabel, vertex.label());
assert roleType.getSupertype() != null;
if (this.isAbstract())
roleType.setAbstract();
vertex.outs().put(RELATES, roleType.vertex);
vertex.outs().edge(RELATES, roleType.vertex).overridden(((TypeImpl) roleType.getSupertype()).vertex);
}
}
}
use of com.vaticle.typedb.core.concept.type.RelationType in project grakn by graknlabs.
the class Util method getRoleType.
public static RoleType getRoleType(Relation relation, Thing player, RelationConstraint.RolePlayer rolePlayer) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "get_role_type")) {
RoleType roleType;
Set<? extends RoleType> inferred;
if (rolePlayer.roleType().isPresent()) {
RelationType relationType = relation.getType();
TypeVariable var = rolePlayer.roleType().get();
if ((roleType = relationType.getRelates(var.label().get().label())) == null) {
throw TypeDBException.of(TYPE_NOT_FOUND, Label.of(var.label().get().label(), relationType.getLabel().name()));
}
} else if ((inferred = player.getType().getPlays().filter(rt -> rt.getRelationType().equals(relation.getType())).toSet()).size() == 1) {
roleType = inferred.iterator().next();
} else if (inferred.size() > 1) {
throw TypeDBException.of(ROLE_TYPE_AMBIGUOUS, rolePlayer.player().reference());
} else {
throw TypeDBException.of(ROLE_TYPE_MISSING, rolePlayer.player().reference());
}
return roleType;
}
}
Aggregations