vaticle / typedb

TypeDB: the polymorphic database powered by types
https://typedb.com
Mozilla Public License 2.0
3.73k stars 338 forks source link

Running a scoped set of rules #6345

Open maydanw opened 3 years ago

maydanw commented 3 years ago

Problem to Solve

Currently, we either run all the rules or none.

For example:
Say a user has a set of rules which are quite expensive but when making most of the queries this set of rules is irrelevant and could be ignored.

Current Workaround

Turning it fully on or off

Proposed Solution

It can be beneficial to have the ability to add tags/labels to the rules and when running the transaction instead of setting inference off/on as boolean send a list of the relevant tags, * (which means all) or None/empty list (for no inference at all). The sent list can be kept short as a user can put multiple tags on a single rule in the schema (this exclusion is not required).

How it should look?
An option I can think of is at the moment is below but there can be better solutions:

rule user_in_sup_group:
  when {
    $g1 isa user_group;
    $g2 isa user_group;
    (inside: $user, container: $g1) isa containment;
    (inside: $g1, container: $g2) isa containment;
  }
  then {
    (inside: $user, container: $g2) isa containment;
  }
  settings {
     labels: ["permission", "containment"]
  };
...
typedb_options = TypeDBOptions.core()
typedb_options.inference_scope = ["containment"]

with session.transaction(
            TransactionType.READ, options=get_db_options(typedb_options)
        ) as tx:
...
flyingsilverfin commented 2 years ago

To clarify, we already only run the set of rules that may be applicable to a query, rather than all rules. We do this primarily by filtering the types the query may require, and only run rules that can conclude those types.

A further feature request would be run a subset of rules according to user's domain knowledge (eg. ignore this set of rules for this query) - this is a feature though, and not necessarily an optimisation.