%% Imperative Programs % Schema %------- database({assembly(Part:string, Subpart:string, Quantity:integer), part_cost(Basic_Part:string, Supplier:string, Cost:real, Time:integer), cost(Part:string, Cost:real) }). %Derivated predicates and rules %------------------------------ % Update cost with all basic parts and their costs. basic_costs <- part_cost(Basic_part, _, Cost, _), +cost(Basic_part, Cost). export basic_costs. % Update cost with all assembled (non-basic) parts and their costs. assembly_costs <- forever( assembly(Part, _, _), ~unresolved_part(Part), tally_costs(Part, Total), +cost(Part, Total) ). export assembly_costs. % unresolved_part contains those parts for which % we do not have as yet a cost in the cost relation. unresolved_part(P) <- assembly(P, Sub, _), ~cost(Sub, _). export unresolved_part(X). % tally_costs sums up the costs of all sub parts making up a part. tally_costs(P, Total) <- get_all(P, Set_of_subs), aggregate(sum, Set_of_subs, Total). export tally_costs(X,Y). % get_all computes the sub cost of a sub part by multiplying its % quantity times the unit sub part cost. get_all(P, ) <- assembly(P, Sp, Qty), cost(Sp, SpCost), Prod = SpCost * Qty. export get_all(X,Y).