Skip to content

Commit

Permalink
refactor rule struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lenileiro committed Jul 27, 2024
1 parent d0be863 commit 660b17a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/ex_datalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule ExDatalog do
rule_module.__info__(:functions)
|> Enum.filter(fn {_name, arity} -> arity == 2 or arity == 1 end)
|> Enum.map(fn {name, arity} ->
%Rule{name: to_string(name), rule: rule_module, function: name, arity: arity}
%Rule{name: to_string(name), module: rule_module, function: name, arity: arity}
end)

{:ok, %ExDatalog{exDatalog | rules: rules ++ new_rules}}
Expand Down Expand Up @@ -67,16 +67,16 @@ defmodule ExDatalog do
end)
end

defp apply_rule(%Rule{rule: rule_fn}, facts) when is_function(rule_fn, 1) do
defp apply_rule(%Rule{function: rule_fn}, facts) when is_function(rule_fn, 1) do
try_apply_rule(rule_fn, facts)
end

defp apply_rule(%Rule{rule: rule_module, function: function, arity: 1}, facts) do
defp apply_rule(%Rule{module: rule_module, function: function, arity: 1}, facts) do
rule_fn = fn fact -> apply(rule_module, function, [fact]) end
try_apply_rule(rule_fn, facts)
end

defp apply_rule(%Rule{rule: rule_fn}, facts) when is_function(rule_fn, 2) do
defp apply_rule(%Rule{function: rule_fn}, facts) when is_function(rule_fn, 2) do
MapSet.new(
for fact1 <- facts,
fact2 <- facts,
Expand All @@ -86,7 +86,7 @@ defmodule ExDatalog do
)
end

defp apply_rule(%Rule{rule: rule_module, function: function, arity: 2}, facts) do
defp apply_rule(%Rule{module: rule_module, function: function, arity: 2}, facts) do
MapSet.new(
for fact1 <- facts,
fact2 <- facts,
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_datalog/rules.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule ExDatalog.Rule do
defstruct [:name, :rule, :function, :arity]
defstruct [:name, :module, :function, :arity]
end
32 changes: 16 additions & 16 deletions test/ex_datalog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule ExExDatalogTest do
}
end

ancestor_rule = %Rule{name: "ancestor", rule: ancestor_rule_fn}
ancestor_rule = %Rule{name: "ancestor", function: ancestor_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, ancestor_rule)

{:ok, datalog} =
Expand Down Expand Up @@ -139,7 +139,7 @@ defmodule ExExDatalogTest do
}
end

grandparent_rule = %Rule{name: "grandparent_rule", rule: grandparent_rule_fn}
grandparent_rule = %Rule{name: "grandparent_rule", function: grandparent_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, grandparent_rule)

# Add parent relationship facts
Expand Down Expand Up @@ -180,7 +180,7 @@ defmodule ExExDatalogTest do
%Fact{object_id: parent, subject_id: child, object_relation: "ancestor"}
end

parent_rule = %Rule{name: "ancestor_rule", rule: parent_rule_fn}
parent_rule = %Rule{name: "ancestor_rule", function: parent_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, parent_rule)

# Rule for extending ancestor relationships
Expand All @@ -190,7 +190,7 @@ defmodule ExExDatalogTest do
%Fact{object_id: ancestor, subject_id: descendant, object_relation: "ancestor"}
end

extended_ancestor_rule = %Rule{name: "ancestor_rule", rule: extended_ancestor_rule_fn}
extended_ancestor_rule = %Rule{name: "ancestor_rule", function: extended_ancestor_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, extended_ancestor_rule)

# Add facts for a family tree
Expand Down Expand Up @@ -243,7 +243,7 @@ defmodule ExExDatalogTest do
%Fact{object_id: id, object_relation: "result1"}
end

rule_1 = %Rule{name: "multi_match_rule", rule: rule_fn_1}
rule_1 = %Rule{name: "multi_match_rule", function: rule_fn_1}
{:ok, datalog} = ExDatalog.add_rule(datalog, rule_1)

# Define second rule
Expand All @@ -252,7 +252,7 @@ defmodule ExExDatalogTest do
%Fact{object_id: id, object_relation: "result2"}
end

rule_2 = %Rule{name: "multi_match_rule", rule: rule_fn_2}
rule_2 = %Rule{name: "multi_match_rule", function: rule_fn_2}
{:ok, datalog} = ExDatalog.add_rule(datalog, rule_2)

# Add facts
Expand Down Expand Up @@ -300,7 +300,7 @@ defmodule ExExDatalogTest do
end

# Add complex rule
complex_rule = %Rule{name: "complex_relation", rule: complex_rule_fn}
complex_rule = %Rule{name: "complex_relation", function: complex_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, complex_rule)

# Add facts
Expand Down Expand Up @@ -349,7 +349,7 @@ defmodule ExExDatalogTest do
%Fact{object_id: id, object_relation: i}
end

rule = %Rule{name: "rule_#{i}", rule: rule_fn}
rule = %Rule{name: "rule_#{i}", function: rule_fn}

case ExDatalog.add_rule(acc, rule) do
{:ok, updated_datalog} -> updated_datalog
Expand Down Expand Up @@ -399,8 +399,8 @@ defmodule ExExDatalogTest do
%Fact{object_id: object_id, subject_id: subject_id, object_relation: "result2"}
end

rule_1 = %Rule{name: "same_name_rule", rule: rule_fn_1}
rule_2 = %Rule{name: "same_name_rule", rule: rule_fn_2}
rule_1 = %Rule{name: "same_name_rule", function: rule_fn_1}
rule_2 = %Rule{name: "same_name_rule", function: rule_fn_2}

{:ok, datalog} = ExDatalog.add_rule(datalog, rule_1)
{:ok, datalog} = ExDatalog.add_rule(datalog, rule_2)
Expand Down Expand Up @@ -445,8 +445,8 @@ defmodule ExExDatalogTest do
%Fact{object_id: admin, object_relation: "leader_of", subject_id: employee}
end

admin_rule_1 = %Rule{name: "admin_rule", rule: admin_rule_fn_1}
admin_rule_2 = %Rule{name: "admin_rule", rule: admin_rule_fn_2}
admin_rule_1 = %Rule{name: "admin_rule", function: admin_rule_fn_1}
admin_rule_2 = %Rule{name: "admin_rule", function: admin_rule_fn_2}

{:ok, datalog} = ExDatalog.add_rule(datalog, admin_rule_1)
{:ok, datalog} = ExDatalog.add_rule(datalog, admin_rule_2)
Expand Down Expand Up @@ -508,7 +508,7 @@ defmodule ExExDatalogTest do
}
end

parent_rule = %Rule{name: "parent", rule: parent_rule_fn}
parent_rule = %Rule{name: "parent", function: parent_rule_fn}
{:ok, datalog} = ExDatalog.add_rule(datalog, parent_rule)

ancestor_rule_fn_1 = fn
Expand Down Expand Up @@ -540,9 +540,9 @@ defmodule ExExDatalogTest do
}
end

ancestor_rule_1 = %Rule{name: "ancestor", rule: ancestor_rule_fn_1}
ancestor_rule_2 = %Rule{name: "ancestor", rule: ancestor_rule_fn_2}
ancestor_rule_3 = %Rule{name: "ancestor", rule: ancestor_rule_fn_3}
ancestor_rule_1 = %Rule{name: "ancestor", function: ancestor_rule_fn_1}
ancestor_rule_2 = %Rule{name: "ancestor", function: ancestor_rule_fn_2}
ancestor_rule_3 = %Rule{name: "ancestor", function: ancestor_rule_fn_3}

{:ok, datalog} = ExDatalog.add_rule(datalog, ancestor_rule_1)
{:ok, datalog} = ExDatalog.add_rule(datalog, ancestor_rule_2)
Expand Down

0 comments on commit 660b17a

Please sign in to comment.