use of com.vaticle.typedb.core.graph.GraphManager in project grakn by graknlabs.
the class GraphProcedure method producer.
@Override
public FunctionalProducer<VertexMap> producer(GraphManager graphMgr, Traversal.Parameters params, Set<Identifier.Variable.Retrievable> filter, int parallelisation) {
if (LOG.isTraceEnabled()) {
LOG.trace(params.toString());
LOG.trace(this.toString());
}
assertWithinFilterBounds(filter);
if (startVertex().id().isRetrievable() && filter.contains(startVertex().id().asVariable().asRetrievable())) {
return async(startVertex().iterator(graphMgr, params).map(// TODO we can reduce the size of the distinct() set if the traversal engine doesn't overgenerate as much
v -> new GraphIterator(graphMgr, v, this, params, filter).distinct()), parallelisation);
} else {
// TODO we can reduce the size of the distinct() set if the traversal engine doesn't overgenerate as much
return async(startVertex().iterator(graphMgr, params).map(v -> new GraphIterator(graphMgr, v, this, params, filter)), parallelisation).distinct();
}
}
use of com.vaticle.typedb.core.graph.GraphManager in project grakn by graknlabs.
the class RuleTest method rule_contains_indexes_allow_deleting_type_after_deleting_rule.
@Test
public void rule_contains_indexes_allow_deleting_type_after_deleting_rule() 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();
GraphManager graphMgr = logicMgr.graph();
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());
assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("friend", "friendship"), Label.of("friendship")), marriageFriendsRule.getLabel(), graphMgr);
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());
assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("name")), marriageSameName.getLabel(), graphMgr);
txn.commit();
}
// check the rule index is still established after commit
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
Rule marriageFriendsRule = txn.logic().getRule("marriage-is-friendship");
assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("friend", "friendship"), Label.of("friendship")), marriageFriendsRule.getLabel(), txn.logic().graph());
Rule marriageSameName = txn.logic().getRule("marriage-same-name");
assertIndexTypesContainRule(set(Label.of("person"), Label.of("spouse", "marriage"), Label.of("marriage"), Label.of("name")), marriageSameName.getLabel(), txn.logic().graph());
}
// deleting a relation type used in a rule should throw
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
RelationType friendship = conceptMgr.getRelationType("friendship");
assertThrowsTypeDBException(friendship::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
assertTrue(!txn.isOpen());
}
// deleting an attribute type used in a rule should throw
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
AttributeType name = conceptMgr.getAttributeType("name");
assertThrowsTypeDBException(name::delete, ErrorMessage.TypeWrite.TYPE_REFERENCED_IN_RULES.code());
assertTrue(!txn.isOpen());
}
// deleting a rule, then an attribute type used in the rule is allowed
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
AttributeType name = conceptMgr.getAttributeType("name");
LogicManager logicMgr = txn.logic();
Rule marriageSameName = logicMgr.getRule("marriage-same-name");
marriageSameName.delete();
assertNotThrows(name::delete);
txn.commit();
}
// deleting a rule, then an entity type used in the rule is allowed
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.WRITE)) {
ConceptManager conceptMgr = txn.concepts();
RelationType person = conceptMgr.getRelationType("friendship");
LogicManager logicMgr = txn.logic();
Rule marriageIsFriendship = logicMgr.getRule("marriage-is-friendship");
marriageIsFriendship.delete();
assertNotThrows(person::delete);
txn.commit();
}
// after all rules are deleted, no rules should exist in the index
try (CoreTransaction txn = session.transaction(Arguments.Transaction.Type.READ)) {
LogicManager logicMgr = txn.logic();
GraphManager graphMgr = logicMgr.graph();
// no types should be in the index
graphMgr.schema().thingTypes().forEachRemaining(type -> {
assertFalse(graphMgr.schema().rules().references().get(graphMgr.schema().getType(type.properLabel())).hasNext());
});
}
}
}
}
Aggregations