Lesson 10.1: Rules as views
Abstracting complex patterns
In Lesson 5.4, we learned how to define rules for TypeDB’s rule inference engine to infer new relations and attributes. In this lesson, we’ll learn how to design rules that capture domain logic, in this case for the domain of an online bookstore. To begin with, let’s consider a piece of domain logic expressed in natural language.
A review is verified if: the review is for a book that has been previously bought by the user who made the review.
This could be easily translated into TypeQL, allowing us to query for reviews that are verified as follows.
match
($review, $book) isa rating;
($order, $book) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
fetch
$review: id;
Now let’s consider how we might apply this domain logic. Suppose we want to query for the average review score of each book, as we did in Lesson 8.3, except this time we only want to include verified reviews in the average.
match
$book isa book;
fetch
$book: title;
average-verified-score: {
match
$review isa review, has score $score;
($review, $book) isa rating;
($order, $book) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
get $review, $score;
mean $score;
};
The addition of the verification check makes the query significantly more complicated, and it’s not straightforward to read through. As queries become larger and retrieve more complex data patterns, they can become harder to maintain. This is a good use case for rule inference, which can significantly simplify queries. For this particular case, we’ll use the following rule.
rule review-verified-by-purchase:
when {
($review, $product) isa rating;
($order, $product) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
} then {
$review has verified true;
};
If we compare this rule to the natural language expression of the domain logic above, we find they match perfectly. This rule captures the domain logic of review verification using predicate logic, which allows it to be faithfully encoded in the database. TypeDB’s rule inference will now generate the statement in the conclusion at query-time wherever the constraints in the condition are satisfied in the data. This allows us to substitute the condition for the conclusion in any queries, as the former implies the latter. Doing so for our previous query, it is now far more readable and maintainable.
match
$book isa book;
fetch
$book: title;
average-verified-score: {
match
$review isa review,
has verified true,
has score $score;
get;
mean $score;
};
Using the rule to generate verified
attributes also comes with a further benefit. We might have many different queries in which we check if a review is verified. If the conditions for review verification change, we would have to modify every query in which the conditions are described. By describing them in a rule instead, we would only have to modify them in that rule, and the results of all the queries that leverage it will update automatically to reflect this.
Using rule inference to abstract a complex query pattern into a more easily queried form is one of the most common use cases for rules in TypeDB. In principle this process is no different from using a view in a relational database.
Write a Fetch query that lists users. For each user, retrieve their user ID and the number of validated reviews they have written. Write the query once without making use of rule inference, and again using the above rule. Other than the ordering of results, the queries should give identical outputs.
You may find it useful to refer to the bookstore’s schema.
Schema
define
book sub entity,
abstract,
owns isbn-13 @key,
owns isbn-10 @unique,
owns title,
owns page-count,
owns genre,
owns price,
plays contribution:work,
plays publishing:published,
plays promotion-inclusion:item,
plays order-line:item,
plays rating:rated,
plays recommendation:recommended;
hardback sub book,
owns stock;
paperback sub book,
owns stock;
ebook sub book;
contributor sub entity,
owns name,
plays contribution:contributor,
plays authoring:author,
plays editing:editor,
plays illustrating:illustrator;
company sub entity,
abstract,
owns name;
publisher sub company,
plays publishing:publisher;
courier sub company,
plays delivery:deliverer;
publication sub entity,
owns year,
plays publishing:publication,
plays locating:located;
user sub entity,
owns id @key,
owns name,
owns birth-date,
plays action-execution:executor,
plays locating:located,
plays recommendation:recipient;
order sub entity,
owns id @key,
owns status,
plays order-line:order,
plays action-execution:action,
plays delivery:delivered;
promotion sub entity,
owns code @key,
owns name,
owns start-timestamp,
owns end-timestamp,
plays promotion-inclusion:promotion;
review sub entity,
owns id @key,
owns score,
owns verified,
plays rating:review,
plays action-execution:action;
login sub entity,
owns success,
plays action-execution:action;
address sub entity,
owns street,
plays delivery:destination,
plays locating:located;
place sub entity,
abstract,
owns name,
plays locating:located,
plays locating:location;
city sub place;
state sub place;
country sub place;
contribution sub relation,
relates contributor,
relates work;
authoring sub contribution,
relates author as contributor;
editing sub contribution,
relates editor as contributor;
illustrating sub contribution,
relates illustrator as contributor;
publishing sub relation,
relates publisher,
relates published,
relates publication;
promotion-inclusion sub relation,
relates promotion,
relates item,
owns discount;
order-line sub relation,
relates order,
relates item,
owns quantity,
owns price;
rating sub relation,
relates review,
relates rated;
action-execution sub relation,
relates action,
relates executor,
owns timestamp;
delivery sub relation,
relates deliverer,
relates delivered,
relates destination;
locating sub relation,
relates located,
relates location;
recommendation sub relation,
relates recommended,
relates recipient;
isbn sub attribute, abstract, value string;
isbn-13 sub isbn;
isbn-10 sub isbn;
title sub attribute, value string;
page-count sub attribute, value long;
genre sub attribute, value string;
stock sub attribute, value long;
price sub attribute, value double;
discount sub attribute, value double;
id sub attribute, value string;
code sub attribute, value string;
name sub attribute, value string;
birth-date sub attribute, value datetime;
street sub attribute, value string;
year sub attribute, value long;
quantity sub attribute, value long;
score sub attribute, value long;
verified sub attribute, value boolean;
timestamp sub attribute, value datetime;
start-timestamp sub attribute, value datetime;
end-timestamp sub attribute, value datetime;
status sub attribute, value string, regex "^(paid|dispatched|delivered|returned|canceled)$";
success sub attribute, value boolean;
rule review-verified-by-purchase:
when {
($review, $product) isa rating;
($order, $product) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
} then {
$review has verified true;
};
rule review-unverified:
when {
$review isa review;
not { $review has verified true; };
} then {
$review has verified false;
};
rule book-recommendation-by-genre:
when {
$user isa user;
$liked-book isa book;
{
($user, $order) isa action-execution;
($order, $liked-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $liked-book) isa rating;
$review has score >= 7;
};
$new-book isa book;
not { {
($user, $order) isa action-execution;
($order, $new-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $new-book) isa rating;
}; };
$liked-book has genre $shared-genre;
$new-book has genre $shared-genre;
not { {
$shared-genre == "fiction";
} or {
$shared-genre == "nonfiction";
}; };
} then {
(recommended: $new-book, recipient: $user) isa recommendation;
};
rule book-recommendation-by-author:
when {
$user isa user;
$liked-book isa book;
{
($user, $order) isa action-execution;
($order, $liked-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $liked-book) isa rating;
$review has score >= 7;
};
$new-book isa book;
not { {
($user, $order) isa action-execution;
($order, $new-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $new-book) isa rating;
}; };
($liked-book, $shared-author) isa authoring;
($new-book, $shared-author) isa authoring;
} then {
(recommended: $new-book, recipient: $user) isa recommendation;
};
rule order-line-total-retail-price:
when {
($order) isa action-execution, has timestamp $order-time;
$line ($order, $item) isa order-line;
not {
($promotion, $item) isa promotion-inclusion;
$promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
};
$item has price $retail-price;
$line has quantity $quantity;
?line-total = $quantity * $retail-price;
} then {
$line has price ?line-total;
};
rule order-line-total-discounted-price:
when {
($order) isa action-execution, has timestamp $order-time;
$line ($order, $item) isa order-line;
($best-promotion, $item) isa promotion-inclusion, has discount $best-discount;
$best-promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
not {
($other-promotion, $item) isa promotion-inclusion, has discount > $best-discount;
$other-promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
};
$item has price $retail-price;
?discounted-price = round(100 * $retail-price * (1 - $best-discount)) / 100;
$line has quantity $quantity;
?line-total = $quantity * ?discounted-price;
} then {
$line has price ?line-total;
};
rule transitive-location:
when {
(location: $parent-place, located: $child-place) isa locating;
(location: $child-place, located: $x) isa locating;
} then {
(location: $parent-place, located: $x) isa locating;
};
Sample solution
Without rule inference:
match
$user isa user, has id $id;
fetch
$id;
verified-reviews: {
match
($review, $book) isa rating;
($order, $book) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
get $review;
count;
};
With rule inference:
match
$user isa user, has id $id;
fetch
$id;
verified-reviews: {
match
$review isa review, has verified true;
($user, $review) isa action-execution;
get;
count;
};
Ensuring logical equivalence
It is important to note that the above query expresses something slightly different to the original. It checks if $review
owns a verified
attribute with value true
, but this attribute could also exist on disk or have been inserted by another rule with a different condition. While the condition of a rule implies its conclusion, the latter does not imply the former. In order for the two to be logically equivalent, we need to ensure that we do not ever manually insert an instance of verified
owned by an instance of review
, nor generate one with another rule. As long as we keep to these restrictions, the two cases will be logically equivalent and the rule’s condition and conclusion will be completely interchangeable in patterns.
Another thing to note is that, while we generate the verification values of true
with this rule, we are not generating the values of false
! Currently, we could query for unverified views with the first of the following queries, but we could not do the same with the second.
match
$review isa review;
not { $review has verified true; };
fetch
$review: id;
match
$review isa review, has verified false;
fetch
$review: id;
Luckily, we can use the same approach we have taken before, and use a rule as a view to make the two query patterns equivalent.
rule review-unverified:
when {
$review isa review;
not { $review has verified true; };
} then {
$review has verified false;
};
Now, as long as we continue not to insert or generate instances of verified
, the two above queries will generate identical results. This allows us to easily constrain reviews as verified or unverified in any other query by matching a single attribute.
Ensuring data consistency
Much like views, rules can be a powerful tool for ensuring data consistency. Let’s review a query we previously encountered in Lesson 7.5, which calculates and inserts a line total for a given order.
match
$order isa order, has id "o0014";
$book isa book, has price $retail-price;
$line ($order, $book) isa order-line,
has quantity $quantity;
?line-total = $quantity * $retail-price;
insert
$line has price ?line-total;
The issue with this approach is that if the price of the book or the quantity ordered are updated, then this line total will be incorrect. This means that whenever one of these two values changes, we’ll need to check and update any affected line totals. Instead of manually inserting and updating line totals, we can delegate this to a rule, which will generate the line totals dynamically whenever they are queried for.
rule order-line-total:
when {
$book isa book, has price $retail-price;
$line ($book) isa order-line, has quantity $quantity;
?line-total = $quantity * $retail-price;
} then {
$line has price ?line-total;
};
Now with this rule in place, the line totals will always reflect the most recent data available. As with the previous rules, we should be sure not to also manually insert line totals. Rule inference will not generate data that already exists, so if a manually inserted line total is up-to-date then the rule will have no effect. But if the manually inserted total is outdated, then this rule will generate a new one with a different value, and the order line will have two totals!
Write a Fetch query that lists orders. For each order, retrieve the order ID, and the grand total by making use of the above rule. Sort the results by order ID.
You may find it useful to refer to the bookstore’s schema.
Schema
define
book sub entity,
abstract,
owns isbn-13 @key,
owns isbn-10 @unique,
owns title,
owns page-count,
owns genre,
owns price,
plays contribution:work,
plays publishing:published,
plays promotion-inclusion:item,
plays order-line:item,
plays rating:rated,
plays recommendation:recommended;
hardback sub book,
owns stock;
paperback sub book,
owns stock;
ebook sub book;
contributor sub entity,
owns name,
plays contribution:contributor,
plays authoring:author,
plays editing:editor,
plays illustrating:illustrator;
company sub entity,
abstract,
owns name;
publisher sub company,
plays publishing:publisher;
courier sub company,
plays delivery:deliverer;
publication sub entity,
owns year,
plays publishing:publication,
plays locating:located;
user sub entity,
owns id @key,
owns name,
owns birth-date,
plays action-execution:executor,
plays locating:located,
plays recommendation:recipient;
order sub entity,
owns id @key,
owns status,
plays order-line:order,
plays action-execution:action,
plays delivery:delivered;
promotion sub entity,
owns code @key,
owns name,
owns start-timestamp,
owns end-timestamp,
plays promotion-inclusion:promotion;
review sub entity,
owns id @key,
owns score,
owns verified,
plays rating:review,
plays action-execution:action;
login sub entity,
owns success,
plays action-execution:action;
address sub entity,
owns street,
plays delivery:destination,
plays locating:located;
place sub entity,
abstract,
owns name,
plays locating:located,
plays locating:location;
city sub place;
state sub place;
country sub place;
contribution sub relation,
relates contributor,
relates work;
authoring sub contribution,
relates author as contributor;
editing sub contribution,
relates editor as contributor;
illustrating sub contribution,
relates illustrator as contributor;
publishing sub relation,
relates publisher,
relates published,
relates publication;
promotion-inclusion sub relation,
relates promotion,
relates item,
owns discount;
order-line sub relation,
relates order,
relates item,
owns quantity,
owns price;
rating sub relation,
relates review,
relates rated;
action-execution sub relation,
relates action,
relates executor,
owns timestamp;
delivery sub relation,
relates deliverer,
relates delivered,
relates destination;
locating sub relation,
relates located,
relates location;
recommendation sub relation,
relates recommended,
relates recipient;
isbn sub attribute, abstract, value string;
isbn-13 sub isbn;
isbn-10 sub isbn;
title sub attribute, value string;
page-count sub attribute, value long;
genre sub attribute, value string;
stock sub attribute, value long;
price sub attribute, value double;
discount sub attribute, value double;
id sub attribute, value string;
code sub attribute, value string;
name sub attribute, value string;
birth-date sub attribute, value datetime;
street sub attribute, value string;
year sub attribute, value long;
quantity sub attribute, value long;
score sub attribute, value long;
verified sub attribute, value boolean;
timestamp sub attribute, value datetime;
start-timestamp sub attribute, value datetime;
end-timestamp sub attribute, value datetime;
status sub attribute, value string, regex "^(paid|dispatched|delivered|returned|canceled)$";
success sub attribute, value boolean;
rule review-verified-by-purchase:
when {
($review, $product) isa rating;
($order, $product) isa order-line;
($user, $review) isa action-execution, has timestamp $review-time;
($user, $order) isa action-execution, has timestamp $order-time;
$review-time > $order-time;
} then {
$review has verified true;
};
rule review-unverified:
when {
$review isa review;
not { $review has verified true; };
} then {
$review has verified false;
};
rule book-recommendation-by-genre:
when {
$user isa user;
$liked-book isa book;
{
($user, $order) isa action-execution;
($order, $liked-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $liked-book) isa rating;
$review has score >= 7;
};
$new-book isa book;
not { {
($user, $order) isa action-execution;
($order, $new-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $new-book) isa rating;
}; };
$liked-book has genre $shared-genre;
$new-book has genre $shared-genre;
not { {
$shared-genre == "fiction";
} or {
$shared-genre == "nonfiction";
}; };
} then {
(recommended: $new-book, recipient: $user) isa recommendation;
};
rule book-recommendation-by-author:
when {
$user isa user;
$liked-book isa book;
{
($user, $order) isa action-execution;
($order, $liked-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $liked-book) isa rating;
$review has score >= 7;
};
$new-book isa book;
not { {
($user, $order) isa action-execution;
($order, $new-book) isa order-line;
} or {
($user, $review) isa action-execution;
($review, $new-book) isa rating;
}; };
($liked-book, $shared-author) isa authoring;
($new-book, $shared-author) isa authoring;
} then {
(recommended: $new-book, recipient: $user) isa recommendation;
};
rule order-line-total-retail-price:
when {
($order) isa action-execution, has timestamp $order-time;
$line ($order, $item) isa order-line;
not {
($promotion, $item) isa promotion-inclusion;
$promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
};
$item has price $retail-price;
$line has quantity $quantity;
?line-total = $quantity * $retail-price;
} then {
$line has price ?line-total;
};
rule order-line-total-discounted-price:
when {
($order) isa action-execution, has timestamp $order-time;
$line ($order, $item) isa order-line;
($best-promotion, $item) isa promotion-inclusion, has discount $best-discount;
$best-promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
not {
($other-promotion, $item) isa promotion-inclusion, has discount > $best-discount;
$other-promotion has start-timestamp <= $order-time,
has end-timestamp >= $order-time;
};
$item has price $retail-price;
?discounted-price = round(100 * $retail-price * (1 - $best-discount)) / 100;
$line has quantity $quantity;
?line-total = $quantity * ?discounted-price;
} then {
$line has price ?line-total;
};
rule transitive-location:
when {
(location: $parent-place, located: $child-place) isa locating;
(location: $child-place, located: $x) isa locating;
} then {
(location: $parent-place, located: $x) isa locating;
};
Sample solution
match
$order isa order, has id $id;
fetch
$id;
order-total: {
match
($order) isa order-line, has price $line-total;
get;
sum $line-total;
};
sort $id;