Mini Project.
Subject description
SOLUTION
Please load the file ui.pl in order to test the program.
Explanations for each predicate can be found in the
commented source code below.
1. Mini.pl
/* making the numbers function global such that it can be
accessed from within another prolog file*/
:- module(kb,[numbers/3]).
/*sublist predicate checks if a list is a sublist of another*/
is_sublist([], _).
is_sublist([H|R], [H|T]) :is_sublist(R, T).
is_sublist([H|R], [_|T]) :is_sublist([H|R], T).
/*checks if a list is a permutation of another*/
is_permutation(L1,L2):permutation(L1,L2).
evaluate(S,D).
/*numbers recieves a list and an element and a variable
returns all possible arithemetic opertations to obtain the
element */
numbers(L,S,[S|D]):compute(L,[S|D]).
2.
Ui.pl
new(Expected_Result,
text_item(expected_result)),
/*specify that the data returned from text box
above should be int*/
send(Expected_Result, type(int)),
/*add the two text boxes, to the first dialog
created above and place a label beneath them*/
send_list(Input_Dialog, append,
[List_of_Numbers,
Expected_Result,
label(list_of_expressions, "Arithmetic
Expressions:", bold)
]),
/*add three command buttons to the second
dialog box*/
/*one to close the window*/
/*one to delete all entries*/
/*one to perform the game logic and determine
all possible arithmetic expresions using predicates in
the loaded knowledge base*/
send_list(Action_Dialog, append,
[
button(quit, and(
message(Window, destroy))),
button(new_game, and(
message(List_of_Numbers, clear),
message(Expected_Result, clear),
message(Expressions_Browser, clear))),
button(generate, message(
@prolog, process_input,
List_of_Numbers?selection,
Expected_Result?selection,
Expressions_Browser))
]),
send(Window, open).
/*this predicate is called whenever the user
clicks on the generate button.*/
/*its converts the string entered into a list and
passes it to the numbers predicate along with the
expected result*/
/*the numbers predicate then returns all possible
arithmetic expressions with the help of find all */
/* and the results are formated to look
mathematical*/
process_input(List_of_Numbers, Expected_Result,
Expression_Browser) :get(List_of_Numbers, split, ,, Initial_Chain),
new(Final_Chain, chain()),
send(Initial_Chain, for_all, message(
Final_Chain,
append,
?(number(0), convert, @arg1)?value)),
chain_list(Final_Chain, List),
findall(Expression_as_String,
(mini:numbers(List, Expected_Result,
[Expected_Result|Expression]),
swritef(Expression_as_String, '%w',
[Expression])),
List_of_Expressions),
chain_list(Chain_of_Expressions,
List_of_Expressions),
send(Chain_of_Expressions,
for_all,
message(@arg1, delete, 0, 1)),
send(Chain_of_Expressions,
for_all,
message(@arg1, delete, @arg1?size-1, 1)),
send(Chain_of_Expressions,
for_all,
message(@arg1, translate, ',', '\s')),
send(Chain_of_Expressions,
for_all,
message(@arg1, translate, '[', '(')),
send(Chain_of_Expressions,
for_all,
message(@arg1, translate, ']', ')')),
send(Expression_Browser, members,
Chain_of_Expressions).
/*thats it*/