Skip to content

Commit

Permalink
A view can be delta predicates with --prepare option.
Browse files Browse the repository at this point in the history
  • Loading branch information
cedretaber committed Sep 11, 2024
1 parent 05fa4ca commit df13572
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/dl2u.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let sort rules =

let simplify rules sources =
let open Result in
match Simplification.simplify rules sources with
match Simplification.simplify rules None sources with
| Error err ->
error @@ Simplification.string_of_error err
| Result.Ok rules ->
Expand Down
9 changes: 8 additions & 1 deletion bin/simplification.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
open Birds

let build_view ast =
if Array.length Sys.argv >= 3 && Sys.argv.(2) = "--prepare" then
ast.Expr.view
else
None

let _ =
if Array.length Sys.argv < 2 then
print_endline "Invalid arguments. File name must be passed."
Expand All @@ -10,11 +16,12 @@ let _ =
let ast = Parser.main Lexer.token lexbuf in
let rules = ast.rules in
let sources = ast.sources in
let view = build_view ast in
match Inlining.sort_rules rules with
| Result.Error err ->
print_endline @@ Inlining.string_of_error err
| Result.Ok rules ->
match Simplification.simplify rules sources with
match Simplification.simplify rules view sources with
| Result.Error err ->
print_endline @@ Simplification.string_of_error err
| Result.Ok rules ->
Expand Down
5 changes: 5 additions & 0 deletions examples/simplification_prepare1.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source x('A':int).
view v('A':int).

+x(A) :- x(A), v(A), A <> 42.
-x(A) :- +x(A), -v(A).
20 changes: 13 additions & 7 deletions src/simplification.ml
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ let rec simplify_rule_recursively (imrule1 : intermediate_rule) : intermediate_r
simplify_rule_recursively imrule2


let resolve_undefined_predicates (imrules : intermediate_rule list) : intermediate_rule list =
let resolve_undefined_predicates (view : PredicateSet.t) (imrules : intermediate_rule list) : intermediate_rule list =
imrules
|> List.fold_left (fun (preds_set, acc) imrule ->
let { head_predicate; positive_terms; negative_terms; _ } = imrule in
Expand Down Expand Up @@ -620,15 +620,15 @@ let resolve_undefined_predicates (imrules : intermediate_rule list) : intermedia
let preds_set = preds_set |> PredicateSet.add head_predicate in
let imrule = { imrule with positive_terms; negative_terms } in
(preds_set, imrule :: acc)
) (PredicateSet.empty, [])
) (view, [])
|> snd
|> List.rev


module TableNameSet = Set.Make(String)


let remove_unused_rules (sources : TableNameSet.t) (imrules : intermediate_rule list) : intermediate_rule list =
let remove_unused_rules (sources : TableNameSet.t) (view : PredicateSet.t) (imrules : intermediate_rule list) : intermediate_rule list =
List.fold_right (fun imrule (pred_set, acc) ->
let { positive_terms; negative_terms; _ } = imrule in
let terms = PredicateMap.union (fun _ x _ -> Some x) positive_terms negative_terms in
Expand Down Expand Up @@ -657,7 +657,7 @@ let remove_unused_rules (sources : TableNameSet.t) (imrules : intermediate_rule
else
(pred_set, acc)

) imrules (PredicateSet.empty, [])
) imrules (view, [])
|> snd


Expand Down Expand Up @@ -781,7 +781,7 @@ let remove_duplicate_rules (rules : rule list) : rule list =
with
| (rules, _) -> rules

let simplify (rules : rule list) (sources : source list) : (rule list, error) result =
let simplify (rules : rule list) (view : view option) (sources : source list) : (rule list, error) result =
let open ResultMonad in

(* Converts each rule to an intermediate rule (with unsatisfiable ones removed): *)
Expand All @@ -794,8 +794,14 @@ let simplify (rules : rule list) (sources : source list) : (rule list, error) re
(* Performs per-rule simplification: *)
let imrules = imrules |> List.map simplify_rule_recursively in

let view =
match view with
| None -> PredicateSet.empty
| Some (view, _) -> PredicateSet.of_list [ImDeltaInsert view; ImDeltaDelete view]
in

(* Removes predicates that are not defined: *)
let imrules = imrules |> resolve_undefined_predicates in
let imrules = imrules |> resolve_undefined_predicates view in

let sources =
sources
Expand All @@ -804,7 +810,7 @@ let simplify (rules : rule list) (sources : source list) : (rule list, error) re
in

(* Removes rules that are not used: *)
let imrules = imrules |> remove_unused_rules sources in
let imrules = imrules |> remove_unused_rules sources view in

(* Removes rules that have a contradicting body: *)
let imrules = imrules |> List.filter (fun imrule -> not (has_contradicting_body imrule)) in
Expand Down
2 changes: 1 addition & 1 deletion src/simplification.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ open Expr

type error

val simplify : rule list -> source list -> (rule list, error) result
val simplify : rule list -> view option -> source list -> (rule list, error) result

val string_of_error : error -> string
2 changes: 1 addition & 1 deletion test/ast2sql_operation_based_conversion_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let main () =
assert false
| Ok rules ->
let sources = [ "a", []; "b", []; "v", [] ] in
match Simplification.simplify rules sources with
match Simplification.simplify rules None sources with
| Error err ->
Printf.printf "Error: %s\n" (Simplification.string_of_error err);
assert false
Expand Down
17 changes: 13 additions & 4 deletions test/simplification_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Expr

type test_case = {
title : string;
input : rule list * source list;
input : rule list * view option * source list;
expected : rule list;
}

Expand All @@ -16,12 +16,12 @@ type test_result =

let run_test (test_case : test_case) =
let open ResultMonad in
let rules, sources = test_case.input in
let rules, view, sources = test_case.input in
match Inlining.sort_rules rules with
| Error err ->
return (Fail { expected = "no error when sorting rules."; got = Inlining.string_of_error err })
| Ok rules ->
Simplification.simplify rules sources >>= fun got ->
Simplification.simplify rules view sources >>= fun got ->
let s_got = got |> List.map string_of_rule |> String.concat "; " in
let s_expected = test_case.expected |> List.map string_of_rule |> String.concat "; " in
if String.equal s_got s_expected then
Expand Down Expand Up @@ -61,7 +61,7 @@ let main () =
run_tests [
{
title = "empty";
input = [], [];
input = [], None, [];
expected = [];
};
{
Expand All @@ -82,6 +82,7 @@ let main () =
Equat (Equation ("=", Var rating, Const (Int 1)));
]);
],
None,
[ "tracks", [] ];
expected = [
(* (1) simplified:
Expand Down Expand Up @@ -118,6 +119,7 @@ let main () =
Not (Pred ("tracks", [ NamedVar "V31"; NamedVar "V32"; NamedVar "V33"; album ]));
]);
],
None,
[ "tracks", [] ];
expected = [];
};
Expand All @@ -139,6 +141,7 @@ let main () =
Equat (Equation ("=", Var (NamedVar "V6855"), Const (Int 1)));
]);
],
None,
[ "albums", [] ];
expected = [
(* (7) simplified:
Expand Down Expand Up @@ -173,6 +176,7 @@ let main () =
Noneq (Equation ("=", Var rating, Const (Int 1)));
]);
],
None,
[ "albums", [] ];
expected = [
(* (32) simplified:
Expand Down Expand Up @@ -209,6 +213,7 @@ let main () =
Not (Pred ("eed", [NamedVar "E"; NamedVar "D"]));
]);
],
None,
[ "eed", []; "ed", [] ];
expected = [
(* Boolean body simplified:
Expand Down Expand Up @@ -257,6 +262,7 @@ let main () =
(Pred ("g", [NamedVar "X"]), [Equat (Equation ("=", (Var (NamedVar "X")), (Const (Int 1))))]);
(Pred ("f", [NamedVar "X"]), [Equat (Equation ("=", (Var (NamedVar "X")), (Const (Int 1))))]);
],
None,
[ "f", []; "g", []; "a", []; "b", []; "c", []; "d", [] ];
expected = [
(Deltadelete ("g", [NamedVar "X"]), [Rel (Pred ("g", [NamedVar "X"]))]);
Expand Down Expand Up @@ -309,6 +315,7 @@ let main () =
]);
(Pred ("v", [NamedVar "A"]), [Rel (Pred ("s", [NamedVar "A"; NamedVar "B"]))]);
],
None,
[ "v", []; "s", [] ];
expected = [
(Deltainsert ("s", [NamedVar "A"; NamedVar "B"]), [
Expand Down Expand Up @@ -394,6 +401,7 @@ let main () =
(Equat (Equation ("<>", Var (NamedVar "A"), Const (Int 4))));
]);
],
None,
["ps", []];
expected = [
(*
Expand Down Expand Up @@ -507,6 +515,7 @@ let main () =
Not (Pred ("__updated__v", [NamedVar "A"; NamedVar "B"; NamedVar "GENV1"]))
]);
],
None,
["a", []; "b", []];
expected = [
(*
Expand Down

0 comments on commit df13572

Please sign in to comment.