%How many rules apply? (Note: List order in AllOperations is random) :- condition(possible_operations/2). possible_operations(AllOperations, N) :- findall(Operation, ( match(Operation, 1321388953) ), AllOperations), length(AllOperations, N). %Select an operation and add others to Choice Points :- rule([fire_once], [possible_operations([Operation|OtherOps], N), N > 1, match(Stack, 1321298902), match(PreviousWords, 1321291544)], [delete_all(_, 1321388953), add(Operation, 1321388953), send(push(cp(Stack, PreviousWords, OtherOps)), 1321906946)]). %Either there is still input left, or the stack is not empty :- condition(not_valid/0). not_valid :- match(_, 1321278717), not( (match(eos, 1321278717)) ). not_valid :- match(eos, 1321278717), not( (match([], 1321298902)) ). %Restore stack at last choice point :- rule([triggered(system_quiescent), fire_once], [not_valid, match(cp(Stack, _, _), 1321906946), match(OldStack, 1321298902)], [delete(OldStack, 1321298902), add(Stack, 1321298902)]). %Re-add untried alternatives to Possible Operations (Note: Fires once per operation) :- rule([triggered(system_quiescent)], [not_valid, match(cp(_, _, Operations), 1321906946), member(Alternative, Operations)], [add(Alternative, 1321388953)]). %Returns the beginning of the sentence and the words to reprocess :- condition(reprocess/2). reprocess(SentenceBegin, NewReProcess) :- match(cp(_, SentenceBegin, _), 1321906946), match(PreviousWords, 1321291544), match(ExistingContent, 1322000411), append(NewReversed, SentenceBegin, PreviousWords), reverse(NewReversed, NewContent), append(NewContent, ExistingContent, NewReProcess). %Compute the words to be re-processed (first Current Word, then ReProcess) :- rule([triggered(system_quiescent), fire_once], [not_valid, reprocess([NewCurrentWord|OldWords], ReProcess)], [delete(_, 1321278717), delete(_, 1321291544), delete(_, 1322000411), add(NewCurrentWord, 1321278717), add([NewCurrentWord|OldWords], 1321291544), add(ReProcess, 1322000411)]). %Pop last choice point :- rule([triggered(system_quiescent), fire_once], [not_valid, match(_, 1321906946)], [send(pop, 1321906946)]). %Re-process words (akin to "Word-by-Word" + "Read") :- rule([], [match([FirstWord|Rest], 1322000411), not( (match(_, 1321278717)) ), match(PreviousWords, 1321291544)], [delete([FirstWord|Rest], 1322000411), add(Rest, 1322000411), add(FirstWord, 1321278717), delete(PreviousWords, 1321291544), add([FirstWord|PreviousWords], 1321291544)]). %Send a signal to the module when a parse is found :- rule([refracted], [match([], 1321298902), match(eos, 1321278717)], [send(parse_found, 1321277146)]). %Send a signal to the module when the parser gives up :- rule([triggered(system_quiescent), refracted], [not_valid, not( (match(_, 1321906946)) )], [send(parsing_failed, 1321277146)]).