
               %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
               % Simulate a simple workflow. %
               %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% The simulate transaction creates and executes a workflow instance
% for every work item, W.
%
trans(simulate) :- seq(getItem(W),
                        conc(workflow(W),simulate)).

trans(simulate) :- finished.

% Work items are stored in the predicate source/1.
%
trans(getItem(W)) :- del(source(W)).


% The transaction workflow(W) applies the workflow to work item W.
% The workflow itself is a sequence of three tasks.
%
trans(workflow(W)) :- seq(task(W,1),
                       seq(task(W,2),
                            task(W,3))).

% Transaction task(I,W) applies task I to work item W.  Since this is
% a simulation, the task does nothing but monitor its own execution.
%
trans(task(I,W)) :- monitor(task(I,W)).


% These (Prolog) rules are in the database, not the transaction base.
% They describe when there are no more work items left.
%
finished :- not(unfinished).
unfinished :- source(W).



% Declare source/1 to be an updatable base predicate.
%
updatable(source(X)).
source(X) :- db(source(X)).

% The following dummy rules tell Prolog that certain predicates are
% defined.
%
simulate :- !, fail.
getItem(dummy) :- !, fail.
workflow(dummy) :- !, fail.
task(dummy,dummy) :- !, fail.



% Create some work items.  This must be done before starting the workflow.
%
create1 :- ins(source(a)).

create2 :- ins(source(a)), ins(source(b)).

create10 :- ins(source(a)), ins(source(b)), ins(source(c)), ins(source(d)), 
            ins(source(e)), ins(source(f)), ins(source(g)), ins(source(h)),
            ins(source(i)), ins(source(j)).

