aggregate - SWI Prolog usage of agregation -
i created simple database on swi prolog. task count how long each of departments work depending on production plan. finished, don't know how sum results. getting this
department amount
b 20
a 5
c 50
c 30
how can transform this?
b 20
a 5
c 80
my code https://gist.github.com/senioroman4uk/d19fe00848889a84434b
the code provided won't interpret count predicate on account of bad format. should rewrite count:- instead of count():-. far know, zero-ary predicates need defined this.
second, count predicate not collect results in list upon operate. here's how can change collect department-amount pairs in list findall:
count_sum(depamounts):-     findall((department,sum),             (   productionplan(finishedproduct, amount),                 resultof(finishedproduct, operation),                 executedin(operation, department, time),                 sum amount * time             ),             depamounts     ).   then, on list, can use swi-prolog's aggregate:
 ?- count_sum(l), aggregate(sum(a),l,member((d,a),l),x).   which yield, backtracing, departments in d , sum of amounts in x:
d = a, x = 15 ; d = b, x = 20 ; d = c, x = 80.   btw, if i'd replace double-quoted strings department names , operations , etc. atoms, simplicity.
Comments
Post a Comment