Cybernetic1

Untitled

Apr 16th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %% Backward chaining
  2.  
  3. -module(backward).
  4. -export([start/0,mainLoop/0,solveGoal/2,solveRule/2,collect/3]).
  5.  
  6. start() ->
  7.     %% Initialize KB
  8.     ets:new(rules, [bag, named_table]),
  9.     ets:insert(rules, {g,[d,e,f,h,i,j],1.0}),
  10.     ets:insert(rules, {g,[a,b,c],1.1}),
  11.     ets:insert(rules, {a,[],0.1}),
  12.     ets:insert(rules, {b,[],0.2}),
  13.     ets:insert(rules, {c,[],0.3}),
  14.     ets:insert(rules, {d,[],0.4}),
  15.     ets:insert(rules, {e,[],0.5}),
  16.     ets:insert(rules, {f,[],0.6}),
  17.     ets:insert(rules, {h,[],0.7}),
  18.     ets:insert(rules, {i,[],0.8}),
  19.     ets:insert(rules, {j,[],0.9}),
  20.     mainLoop().
  21.  
  22. mainLoop() ->
  23.     Query = io:read("Enter query: "),
  24.     solveGoal(element(2, Query), self()),
  25.     receive
  26.         {_, Truth} -> io:format("Answer is: ~w~n", [Truth])
  27.     end,
  28.     mainLoop().
  29.  
  30. solveGoal(Goal, Caller) ->
  31.     %% Fetch rules...
  32.     ApplicableRules = ets:match(rules,{Goal, '$1', '$2'}),
  33.     lists:foreach(
  34.         fun([RuleBody,Truth]) ->
  35.         if
  36.             %% If RHS is null
  37.             RuleBody == [] -> Caller ! {Goal, Truth};
  38.             %% If RHS contains subgoals
  39.             true -> spawn(backward, solveRule, [RuleBody, self()])
  40.         end end,
  41.         ApplicableRules),
  42.     receive
  43.         %% Each message is of the form {goal,truth}
  44.         %% Just accept the 1st answer
  45.         {_, Truth} -> Caller ! {Goal, Truth}
  46.     end.
  47.  
  48. solveRule(RuleTail, Caller) ->
  49.     N = length(RuleTail),
  50.     Collector = spawn(backward, collect, [[], N, Caller]),
  51.     %% For each subgoal in Rule
  52.     %% Spawn a process to solve each subgoal
  53.     lists:foreach(
  54.         fun(Subgoal) -> spawn(backward, solveGoal, [Subgoal, Collector]) end,
  55.         RuleTail).
  56.  
  57. %% Accumulate results
  58. collect(Truths, N, Caller) ->
  59.     %% If we have N messages send back the answer
  60.     %% Answer = min of truth values, equivalent to fuzzy-logic AND
  61.     if length(Truths) == N -> Caller ! {dont_care, lists:min(Truths)};
  62.        true -> true
  63.     end,
  64.     receive
  65.         {_, Truth} -> collect([Truth | Truths], N, Caller)
  66.     end.
Advertisement
Add Comment
Please, Sign In to add comment