-
Notifications
You must be signed in to change notification settings - Fork 0
/
klover.pl
2475 lines (2092 loc) · 79.8 KB
/
klover.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
:- set_prolog_flag(discontiguous_warnings,off).
:- set_prolog_flag(single_var_warnings,off).
:- use_module(library(tcltk)).
%dynamic predicates for tcl GUI and verification of rule base:
:- dynamic tcl/1, tcl_question/5, answer_options/1, num_answers/1, attr_cf/3, num_attr_cf/2, subsumed_rules/4, missing_rules/2, non_reached_rule/2, not_unique/1, redundant_rule/2, no_goal/1, missing_goal/1.
% Window 1 - INTERPRETER
% *******************************************
% ************* Top level **********************
%activate/deactivate tcl graphic user interface, change value "on" to something else to turn off tcl GUI
tcl_GUI(on).
%tcl_GUI(off).
%Clean window and displays Options menu (tcl).
expert:-
tcl_GUI(on),
retractall(tcl(_)),
delete_all_tcl,
tk_new([top_level_events,name('Klöver')],Tcl),
assert(tcl(Tcl)),
Source = 'source C:/Users/boron/Desktop/klover/menu.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event),
start_manipulate(Event),
control_new_option(Event).
%Clean window and displays Options menu (SICStus).
expert:-
\+tcl_GUI(on),
nl,
write('Options:'),nl,nl,
menue_write(['Consult' ,'Consult with given answers',
'Save answers', 'Save session', 'Fetch old answers',
'Fetch old session','List database', 'Verify rules', 'Quit']),
read(Answer),
start_manipulate(Answer),
control_new_option(Answer).
start_manipulate(Answer):-
run_option(Answer).
control_new_option(Answer):-
Answer \== 'Quit',
Answer \== 'Interrupt',
new_menue(New_Answer),
start_manipulate(New_Answer),
control_new_option(New_Answer).
control_new_option(New_Answer).
% Menu represented with the results presentation (Tcl).
new_menue(Event):-
tcl_GUI(on),
delete_all_tcl,
Source = 'source C:/Users/boron/Desktop/klover/new_menu.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event).
% Menu represented with the results presentation (SICStus).
new_menue(Answer):-
\+tcl_GUI(on),
nl,
menue_write(['New session' ,'Consult with given answers',
'Change answers','Save answers', 'Save session',
'Fetch old answers','Fetch old session','List database', 'Verify rules',
'How explanation', 'Interrupt', 'Select an option, please! ']),
read(Answer).
menue_write([]):- nl.
menue_write([First|List]):-
write(First),nl,
menue_write(List).
% Gives consultation without old answers.
run_option('Consult'):-
retractall(answer(_,_)),
consultation.
% Consultation with already given answers.
run_option('Consult with given answers'):-
consultation.
run_option('New session'):-
run_option('Consult').
run_option('Change answers'):-
\+tcl_GUI(on),
setof((Attribute,=,Value), Value^answer(Attribute, Value), List_of_answers),
menue_write(List_of_answers),
read_input(List_of_answers, To_change),
delete_answers(To_change),
ask_user(To_change).
run_option('Change answers'):-
tcl_GUI(on),
bagof((Attribute,Value), Value^answer(Attribute, Value), List_of_answers),
assert_each_option(List_of_answers),
assert_num_options(List_of_answers),
Source = 'source C:/Users/boron/Desktop/klover/change_answers.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event),
tcl_list_to_prolog_list(Event,ASCII_Ans),
get_answer_list(List_of_answers, ASCII_Ans, To_change),
convert_to_objects(To_change, New),
delete_answers(New),
ask_user(New).
run_option('Change answers'):-
tcl_GUI(on).
run_option('Change answers'):-
\+tcl_GUI(on),
write_no_answers.
% Saves answers and complete session on file.
run_option('Save answers'):-
store_answers,!.
%if user choose to not save new file (i.e. store_answers fails)
run_option('Save answers'):-
tcl_GUI(on).
run_option('Save session'):-
store_session,!.
%if store_session fails
run_option('Save session'):-
tcl_GUI(on).
% Runs old answers and a session from file and makes a list of both.
run_option('Fetch old answers') :-
\+tcl_GUI(on),
retractall(answer(_,_)),
retractall(trigged(_,_,_)),
retractall(how(_,_,_,_)),
read_answerfile,
write('Given answers'),nl,
listing(answer).
run_option('Fetch old answers') :-
tcl_GUI(on),
read_answerfile,!,
setof((Attribute,Value), Value^answer(Attribute, Value), List_of_answers),
assert_each_option(List_of_answers),
assert_num_options(List_of_answers),
Source = 'source C:/Users/boron/Desktop/klover/show_old.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event).
%if user choose to not open new file (i.e. read_answers fails)
run_option('Fetch old answers') :-
tcl_GUI(on).
run_option('Fetch old session') :-
\+tcl_GUI(on),
retractall(answer(_,_)),
retractall(trigged(_,_,_)),
retractall(how(_,_,_,_)),
read_answerfile,
run_option('List database').
run_option('Fetch old session') :-
tcl_GUI(on),
read_answerfile,!,
run_option('List database').
run_option('Fetch old session') :-
tcl_GUI(on).
% Lists database content.
run_option('List database') :-
\+tcl_GUI(on),
delete_garbage,
nl,
write('Given answers'), nl,
(listing(answer); write('Exists no')), nl,
write('Conclusions'), nl,
(listing(trigged); write('Exists no')), nl,
write('The result of all rules'), nl,
(listing(how); write('Exists no')).
%Assert database and display it in tcl file
run_option('List database') :-
tcl_GUI(on),
delete_garbage,
(
setof((Attribute,Value), Value^answer(Attribute, Value), List_of_answers),
assert_each_option(List_of_answers),
assert_num_options(List_of_answers);
assert(answer_options(('No answers exists',_)))),
(
setof(trigged(Object,Attribute,Value), trigged(Object,Attribute,Value), List_of_trigged),
assert_each_option(List_of_trigged),
assert_num_options(List_of_trigged);
assert(answer_options(trigged('No conclusions drawn',_,_)))),
(
setof(how(FR,Object,Attribute,TF), how(FR,Object,Attribute,TF), List_of_how),
assert_each_option(List_of_how),
assert_num_options(List_of_how);
assert(answer_options(how(_,'No how explanations exists',_,_)))),
Source = 'source C:/Users/boron/Desktop/klover/show_result.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event).
% Verification of rule-base:
run_option('Verify rules'):-
\+tcl_GUI(on),
nl,
write('*************************'),nl,
write('VERIFICATION TOOL MENU'),nl,nl,
write('Check:'), nl,nl,
menue_write(['Redundancy','Subsumed',
'Completeness', 'Help','Back']),
read(Input),
go_verify(Input).
run_option('Verify rules'):-
tcl_GUI(on),
Source = 'source C:/Users/boron/Desktop/klover/verify.tcl',
run_tcl(Source, Event),
retract_and_destroy(Event),
go_verify(Event).
run_option('How explanation'):-
answer_howquestion.
run_option('Interrupt'):-
\+tcl_GUI(on),
abort.
run_option('Interrupt'):-
tcl_GUI(on),
tcl(Tcl),
tcl_delete(Tcl),
abort.
%Case: Tcl GUI closed down by user - then Klöver stops.
run_option([]):-
tcl_GUI(on),
abort.
% Terminates Options.
run_option('Quit'):-
\+tcl_GUI(on),
abort.
run_option('Quit'):-
tcl_GUI(on),
tcl(Tcl),
tcl_delete(Tcl),
abort.
run_option(Answer):-
write('That option does not exist, please try another one! '), nl.
read_input(List_of_answers, Answer):-
write('Which answers do you want to change? Select the object, please! '),
write('Please insert an object and finish the insertion with the word stop. '), nl,
read_all_answers(_,Answer, start).
write_no_answers:-
write('There are no answers!'),!, nl.
delete_answers([]).
delete_answers([stop]):- \+tcl_GUI(on).
delete_answers([Answer| Rest]):-
retractall(answer(Answer,_)),
delete_answers(Rest).
delete_garbage:-
retractall(answer),
retractall(trigged),
retractall(how).
% ***************************************************
% ********** Predicates called by 'Options' *********
% Makes a consultation with or without questions.
consultation:-
retractall(trigged(_,_,_)),
retractall(how(_,_,_,_)),
create_knowledgebase, % question generator
call_goal_parameters, % examines all goal parameters
presentation_result. % presents the result
changed_consultation:-
retractall(trigged(_,_,_)),
retractall(how(_,_,_,_)),
create_knowledgebase, % question generatoer
call_goal_parameters. % examines all goal parameters
% Reads a file with answers and gives possibilities to change answers.
% Saves answers and complete session on file.
store_answers:-
\+tcl_GUI(on),
write('Give a file name: '),
read(Filename),
tell(Filename),
write(':-dynamic answer/2.'),nl,
listing(answer),nl,
told.
%Built-in function in tcl/tk tk_getSaveFile that create a
%window where the user can choose where to save a file on computer
store_answers:-
tcl_GUI(on),
%Source = 'source C:/Users/boron/Desktop/klover/fetchfile.tcl',
%run_tcl(Source, Filename),
%retract_and_destroy(Filename),
tcl(Tcl),
tcl_eval(Tcl,['tk_getSaveFile -defaultextension ".pl" -filetypes {{{Perl Files} {.pl} TEXT} {{Text Files} {.txt} TEXT} {{Text Files} {""} TEXT}}'],File),
(
File == [],!,
fail
;
atom_codes(Filename, File),
tell(Filename),
write(':-dynamic answer/2.'),nl,
listing(answer),nl,
told).
%tcl_delete(Tcl).
store_session:-
\+tcl_GUI(on),
write('Give a file name: '),
read(Filename),
tell(Filename),
write(':-dynamic how/4,answer/2,trigged/3.'),nl,
listing(answer),
listing(trigged),
listing(how),nl,
told.
%Built-in function in tcl/tk tk_getSaveFile that create a
%window where the user can choose where to save a file on computer
store_session:-
tcl_GUI(on),
%Source = 'source C:/Users/boron/Desktop/klover/fetchfile.tcl',
%run_tcl(Source, Filename),
%retract_and_destroy(Filename),
tcl(Tcl),
tcl_eval(Tcl,['tk_getSaveFile -defaultextension ".pl" -filetypes {{{Perl Files} {.pl} TEXT} {{Text Files} {.txt} TEXT} {{Text Files} {""} TEXT}}'],File),
(
File == [],!,
fail
;
atom_codes(Filename, File),
tell(Filename),
write(':-dynamic how/4,answer/2,trigged/3.'),nl,
listing(answer),
listing(trigged),
listing(how),nl,
told).
%tcl_delete(Tcl).
read_answerfile:-
\+tcl_GUI(on),
write('The name of the file is: '),
read(Filename),
consult(Filename).
%Built-in function in tcl/tk tk_getOpenFile that create a
%window where the user can choose a file on computer
read_answerfile:-
tcl_GUI(on),
tcl(Tcl),
tcl_eval(Tcl,['tk_getOpenFile -filetypes {{{Perl Files} {.pl} TEXT} {{Text Files} {.txt} TEXT} {{Text Files} {""} TEXT}}'],File),
(
File == [],!,
fail
;
atom_codes(Filename, File),
retractall(answer(_,_)),
retractall(trigged(_,_,_)),
retractall(how(_,_,_,_)),
%Source = 'source C:/Users/boron/Desktop/klover/fetchfile.tcl',
%run_tcl(Source, Filename),
%retract_and_destroy(Filename),
consult(Filename)).
% Fetches list of goal parameters from KB and checks them.
call_goal_parameters:-
goal_conclusions(List),
check_all(List).
% Conditionless examination of all goal paramameters.
check_all([]).
check_all([Parameter|Rest]):-
check(Parameter,_, goal_parameter,_),
check_all(Rest).
check_all([Parameter|Rest]):-
assert(trigged(Parameter, Attribute, 0)),
check_all(Rest).
% ***************************************************
% General predicates used for tcl_tk GUI
% abort tcl and sicstus if user closed the tcl/tk window
tcl_abort_case([]):-
tcl(Tcl),
tcl_delete(Tcl),
abort,!.
tcl_abort_case(Var).
% load a new tcl window, and then waits for a user-event from tcl side
run_tcl(Source, Event):-
tcl(Tcl),
tcl_eval(Tcl,Source,_),
tk_next_event(Tcl, Event).
%retract asserted facts, and remove tcl window
retract_and_destroy(Event):-
tcl(Tcl),
delete_tcl_facts,
tcl_abort_case(Event),!,
tcl_eval(Tcl,'destroy .t',_).
%total clean-up of asserted data used for tcl GUI
delete_all_tcl:-
retractall(answer_options(_)),
retractall(num_answers(_)),
retractall(tcl_question(_,_,_,_,_)),
retractall(attr_cf(_,_,_)),
retractall(num_attr_cf(_,_)).
delete_tcl_facts:-
retractall(answer_options(_)),
retractall(num_answers(_)),
retractall(tcl_question(_,_,_,_,_)).
%converts a string to an integer
string_to_integer(Ans, Int):-
atom_codes(Ans,ASCII_List),!,
name(Int,ASCII_List).
%A list in tcl is a string where elements are separated by empty space
%This predicate transforms the string to a list of ascii-numbers
tcl_list_to_prolog_list(Atom,Answers):-
atom_codes(Atom, ASCII),
remove_spaces(ASCII, Answers).
%create new ascii-list without empty spaces (ascii nr 32)
remove_spaces([],[]).
remove_spaces([32|List], Rest):-
remove_spaces(List,Rest).
remove_spaces([E|List], [E|Rest]):-
remove_spaces(List,Rest).
%assert_each_option/1: assert each element in list
%asserted facts are used by tcl_tk to retrieve answers from Prolog without deleting original answer.
assert_each_option([]).
assert_each_option([E|Rest]):-
assert(answer_options(E)),
assert_each_option(Rest).
%assert_num_options(+AnswerList): count elems in list and assert number
%this fact is used by tcl_tk
assert_num_options(X):-
count_elems(X,Num),!,
assert(num_answers(Num)).
%retract_predicate/2 is needed to be able to retract temporarily facts in klöver from tcl_tk side
retract_answer(Num,M):-
retract(answer_options(_)).
retract_number:-
retract(num_answers(_)).
% For TCL: Assert all conclusions set to 'true' which means
% they have propability > 100
assert_attr_list(_,[]).
assert_attr_list(Object,[(Attribute, Value)|Rest]):-
tcl_GUI(on),
assert(attr_cf(Object,Attribute, Value)),
assert_attr_list(Object,Rest).
retract_attr_list(Object):-
retract(attr_cf(Object,_,_)).
assert_num_attributes(Object, AttrList):-
count_elems(AttrList,Num),!,
assert(num_attr_cf(Object,Num)).
%get_single_answer/3
%retrieve the element on "index place" in a list.
get_single_answer(0, [Ans|_], Ans).
get_single_answer(Num, [E|Rest], Ans):-
Num1 is Num - 1,
get_single_answer(Num1, Rest, Ans).
%get_answer_list/3 convert each ASCII-nr to integer, retrieve and store each object in new list
get_answer_list(_, [], []).
get_answer_list(Options, [ASCII|Rest], [A|List]):-
name(Int,[ASCII]),
get_single_answer(Int, Options, A),
get_answer_list(Options, Rest, List).
convert_to_objects([], []).
convert_to_objects([(A,V)|Rest], [A|New]):-
convert_to_objects(Rest, New).
% **************************************************
% ************* Inference mechanism *****************
% Checks the control of all conditions in the knowledge base:
% (Not system related conditions.)
% when given answer exists
check(Object, Condition, Value):-
answer(Object,Answer),
check_condition(Object, Condition, Value).
% when the value is set by rules
check(Object, Attribute, Condition, CF):-
trigged(Object, Attribute, CF2),
check_condition(Object, Attribute, Condition, CF).
% Checks true and false on conclusions set by rules.
check(Object, Attribute, Truth_value) :-
(Truth_value = sant; Truth_value = falskt),
trigged(Object, Attribute, CF),
check_condition(Object, Attribute, Truth_value).
% Asks questions that have not been asked by the question generator and possible
%follow-up questions. Main question conditionless and follow-up questions with conditions.
check(Object,Condition, Value):-
tcl_GUI(on),
\+ answer(Object,_),
question_info(Object, Prompt, Meny, Type, Fragevillkor ),
question_condition(Fragevillkor,Back),
%assert(tcl_question(Object, Prompt, Meny, Type, _)),
type_control(Type, Object),
%retract(tcl_question(Object, Prompt, Meny, Type, _)),
((question_sequence(Object, Foljdfragelista),
ask_user(Foljdfragelista)) ; true), !,
check_condition(Object, Condition, Value).
check(Object,Condition, Value):-
\+ tcl_GUI(on),
\+ answer(Object,_),
question_info(Object, Prompt, Meny, Type, Fragevillkor ),nl,
question_condition(Fragevillkor,Back),
write(Object),nl,
write(Prompt),nl,
menue_write(Meny),nl,
write('Enter Why if you want a text based explanation. Then answer the question!'),nl,nl,
type_control(Type, Object),
((question_sequence(Object, Foljdfragelista),
ask_user(Foljdfragelista)) ; true), !,
check_condition(Object, Condition, Value).
% Goes through all rules for objects that can be determined by rules but have yet not
% been evalutated.
check(Object, Attribute, Condition, Value):-
\+ how(_,Object, Attribute, _),
rule_info(Object, Rule_numbers),
solve_all(Object, Rule_numbers,Partial_answer, Answer),!,
check_condition(Object, Attribute, Condition, Value).
% Checks rules for conditions with true and false.
check(Object, Attribute, Truth_value):-
\+ how(_,Object, Attribute, _),
rule_info(Object, Rule_numbers),
solve_all(Object, Rule_numbers,Partial_answer, Answer),!,
check_condition(Object, Attribute, Truth_value).
% No more rules for the object.
solve_all(Object, [], Answer, Answer).
%Ruels that set an object-attribute combination to a value
% that has proven completely true or completely false do not affect the truth value.
solve_all(Object, [FR|RR], Partial_answer, Answer):-
(trigged(Object, Attribute, 1000);
trigged(Object, Attribute, -1000)),
clause(rule(FR,Object, Attribute, CF), Condition) ,
solve_all(Object, RR, Partial_answer, Answer).
% Examines the first rule in the rules list, if succseeded CF is updated and
% we move on to the rest of the list.
solve_all(Object, [FR|RR], Partial_answer, Answer):-
rule(FR,Object, Attribute, CF),
update_CF(FR,Object, Attribute, CF, Partial_answer, New_partial_answer),
solve_all(Object, RR, New_partial_answer, Answer).
% If the rule failes the result is stored as rule rejected.
% this in order to inform that the object has been examined even if no rule succeeded.
solve_all(Object, [FR|RR], Partial_answer,Answer):-
clause(rule(FR,Object,Attribute,CF),Premises), % to bind the attribute
asserta(how(FR,Object,Attribute,rejected)),
solve_all(Object, RR, Partial_answer, Answer).
solve_all(Object, [FR|RR], Partial_answer,Answer):-
\+ rule(FR,Object,_,_),
solve_all(Object,RR,Partial_answer,Answer).
% check_condition controls that the condition in the check really is fullfilled
% by the value that the object and attribute has been alocated.
% All goal parameters are to be checked.
check_condition(Object,_, goal_parameter,_).
% The followin is applicated on user given answers.
check_condition(Object, = , Value):-
!,
answer(Object,Value).
check_condition(Object, >=, Value1):-
answer(Object, Value2),
number(Value2),
Value2 >= Value1.
check_condition(Object,>,Value1):-
answer(Object,Value2),
number(Value2),
Value2 > Value1.
check_condition(Object,=<,Value1):-
answer(Object,Value2),
number(Value2),
Value2 =< Value1.
check_condition(Object,<, Value1):-
answer(Object, Value2),
number(Value2),
Value2 < Value1.
check_condition(Object,'<>',Value):-
\+ answer(Object,Value).
% The followeing is applicated on conclusions drawn by the system.
% The following two cases are applied on conclusions and will compare their
% certainty factors with a specific value.
check_condition(Object, Attribute, 'cf>=', CF1):-
trigged(Object,Attribute,CF2),!,
number(CF2),
CF2 >= CF1.
check_condition(Object, Attribute, 'cf<' , CF1):-
trigged(Object,Attribute,CF2),!,
number(CF2),
CF2 < CF1.
% The following three will check the conclusions drawn by numeric rules for example
% belopp_guld and compares given value with another value.
check_condition(Object, Attribute, 'Value>=', Value):-
trigged(Object,Attribute,CF),
number(Attribute),
Attribute >= Value.
check_condition(Object, Attribute, 'Value<', Value):-
trigged(Object,Attribute,CF),
number(Attribute),
Attribute < Value.
check_condition(Object, Attribute, 'Value=', Value):-
trigged(Object,Attribute,CF),
number(Attribute),
Attribute = Value.
% To make is easier to check if conclusions are completely true or false.
check_condition(Object, Attribute, sant):-
trigged(Object,Attribute,1000).
check_condition(Object, Attribute, falskt):-
trigged(Object,Attribute,-1000).
% Takes care of conditions on the form "least n of the following" for conclusions.
check_condition(Object, Less_num,outof ,Attribute_list):-
count_attribute(Object,Attribute_list,Num_in_list),
Num_in_list >= Less_num.
% Takes care of conditions on the form "least n of the following" for user given info.
check_condition(Object, Less_num, Attribute_list):-
count_attribute(Object,Attribute_list,Num_in_list),
Num_in_list >= Less_num.
% Calculates number of attributes for an object that exist in a list.
% Called by check_condition for that type of condition.
count_attribute(Object,[],0).
count_attribute(Object,[Attribute| Rest],Number):-
(check(Object, =, Attribute) ;
check(Object,Attribute, 'cf>=',1000)),!,
count_attribute(Object,Rest,Number1),
Number is Number1 + 1.
count_attribute(Object,[Attribute|Rest],Number):-
count_attribute(Object,Rest,Number).
% ***********************************************
% *********** Certainity handling ***************
% Saves CF for an attribute for the first time.
update_CF(Regelnr, Object, Attribute, CF, [], [(Attribute, CF)]):-
asserta(trigged(Object, Attribute, CF)),
asserta(how(Regelnr, Object, Attribute, CF)).
% Keeps the rule that has been shown
% and its CF for use with "how"-questions
% The attribute has not been shown by any former rule because it does not exist in the
% list Partial_answer. Therefore given CF for the attribute is saved.
update_CF(Regelnr,Object, Attribute, CF, Partial_answer,[(Attribute, CF)|Partial_answer] ):-
not_memb((Attribute, X), Partial_answer),
asserta(trigged(Object, Attribute, CF)),
asserta(how(Regelnr, Object, Attribute, CF)).
% Attribute proven completely true or completely false before.
% Possible new rules do not affect this.
update_CF(Regelnr, Object, Attribute, CF, Partial_answer, Partial_answer ):-
memb((Attribute, X), Partial_answer),
(trigged(Object, Attribute, 1000);
trigged(Object, Attribute, -1000)),!.
% Attribute given CF before. New CF is always to be
% calculated as a contexture of the new and the old value.
update_CF(Regelnr, Object, Attribute, CF, Partial_answer, New_partial_answer ):-
memb((Attribute, X), Partial_answer),
calculate_CF((Attribute, CF), Partial_answer, New_partial_answer, New_CF),
retract(trigged(Object, Attribute, Old_CF)), % earlier CF deleted
asserta(trigged(Object, Attribute, New_CF)), % combined CF lagras
asserta(how(Regelnr, Object, Attribute, New_CF)). % info for "how"-questions%
% Calculates CF as a contexture of old and new value.
% All attributes and their CF are stored in a list as arguments.
% to the predcate. Current predicate first in list.
calculate_CF((Attribute, CF), [(Attribute, Old_CF)|Partial_answer],
[(Attribute, New_CF)|Partial_answer], New_CF):-
cf(CF, Old_CF, New_CF).
% Current predicate not first in list.
% Continue to go through list Partial_answer to find it.
calculate_CF((Attribute, CF), [(X, Y)|Partial_answer], [(X,Y) | New_partial_answer], New_CF):-
Attribute \== X,
calculate_CF((Attribute, CF), Partial_answer, New_partial_answer, New_CF).
% Contexture of CF. Both new and old value for CF < 0.
cf(CF, Old_CF, New_CF):-
Old_CF < 0, CF < 0, !,
New_CF is Old_CF + ((CF * (1000 + Old_CF )) // 1000).
% Both new and old value for CF >= 0.
cf(CF, Old_CF, New_CF):-
Old_CF >= 0, CF >= 0, !,
New_CF is Old_CF + (( CF * (1000 - Old_CF)) // 1000).
% One of the values for old or new CF < 0.
cf(CF, Old_CF, New_CF):-
min(CF, Old_CF, Min_CF),
New_CF is (((CF + Old_CF) * 1000)// (1000 - Min_CF)).
% Minimum of two values for CF.
min(CF1, CF2, ACF1):-
abs_value(CF1,ACF1),
abs_value(CF2,ACF2),
ACF1 < ACF2.
min(CF1, CF2, ACF2):-
abs_value(CF1,ACF1),
abs_value(CF2,ACF2),
ACF2 =< ACF1.
abs_value(CF,CF):-
CF >= 0.
abs_value(CF1,CF2):-
CF1 < 0,
CF2 is -1*CF1.
% *************************************************
% ************* Various predicates ****************
not_memb((Attribute, X), Partial_answer):-
memb((Attribute, X), Partial_answer),!,
fail.
not_memb((Attribute, X), Partial_answer).
equal(Attribute,Solution):-
Attribute == Solution.
memb(Elem,[Elem | List]):- !.
memb(Elem,[X | List]):-
memb(Elem,List).
'<ERROR>'(2) :- !, fail.
clean:-
retractall(answer(_,_)),
retractall(trigged(_,_,_)).
%*****************************************************************
%*****************************************************************
% Window 2 - Generates questions
% **************************************************
% ************* Question generator *****************
:-dynamic goal_temp/1,how/4,question_temp/1,menu_temp/1,menu_temp/2,ok/1,redo/1,answer/2,temp/1,text/2,trigged/3.
% Examines all introducing questions.
create_knowledgebase:-
question_order(List),
ask_user(List).
% When going backwards in questioning or with "redo" as condition on a question.
create_database:-
redo(true),
retractall(redo(true)),
create_database.
% Examins wether a question is to be asked and then asks the question.
ask_user([]).
ask_user([Object| Rest]):-
answer(Object,Answer),!,
ask_user(Rest).
%GUI deactivated
ask_user([Object| Rest]):-
\+tcl_GUI(on),
question_info(Object,Prompt,Meny,Type,Condition),
question_condition(Condition,Back),
write(Object), nl,
write(Prompt),nl,
menue_write(Meny),nl,
write('Print Why if you want an explanation. Then answer the question!'),nl,nl,
type_control(Type, Object),
back_check(Back),!,
ask_user(Rest).
%GUI activated
ask_user([Object|Rest]):-
tcl_GUI(on),
question_info(Object, Prompt, Meny, Type, Condition),
question_condition(Condition,Back),
%assert(tcl_question(Object, Prompt, Meny, Type, _)),
type_control(Type, Object),
%retract(tcl_question(Object, Prompt, Meny, Type, _)),
back_check(Back),!,
ask_user(Rest).
%GUI activated
% single answer questions
type_control(s, Object):-
tcl_GUI(on),
question_info(Object,Prompt,Options,Type,Condition),
assert(tcl_question(Object, Prompt, Options, Type, _)),
assert_each_option(Options),
assert_num_options(Options),
Source = 'source C:/Users/boron/Desktop/klover/s_questions.tcl',
run_tcl(Source, Meny_Index),
retract_and_destroy(Meny_Index),
(
Meny_Index = change_answers,
run_option('Change answers'),
changed_consultation;
%type_control(s, Object);
get_single_answer(Meny_Index, Options, Answer),
arrange_answer(Object,Answer)).
%GUI activated
% w-questions where user write an arbitrary answer
type_control(w, Object):-
tcl_GUI(on),
question_info(Object,Prompt,Options,Type,Condition),
assert(tcl_question(Object, Prompt, Options, Type, _)),
Source = 'source C:/Users/boron/Desktop/klover/w_questions.tcl',
run_tcl(Source, Answer),
retract_and_destroy(Answer),
(
Answer = change_answers,
run_option('Change answers'),
changed_consultation;
%type_control(w, Object);
(
string_to_integer(Answer, Int),
arrange_answer(Object,Int);
arrange_answer(Object, Answer))).
%GUI activated
% multiple answers questions
type_control(m, Object):-
tcl_GUI(on),
question_info(Object,Prompt,Options,Type,Condition),
assert(tcl_question(Object, Prompt, Options, Type, _)),
assert_each_option(Options),
assert_num_options(Options),
Source = 'source C:/Users/boron/Desktop/klover/m_questions.tcl',
run_tcl(Source,Event),
retract_and_destroy(Event),
(
Event = change_answers,
run_option('Change answers'),
changed_consultation;
%type_control(m, Object);
tcl_list_to_prolog_list(Event,ASCII_Ans),
get_answer_list(Options, ASCII_Ans, Answers),
store_one_answer(Object, Answers)).
%GUI deactivated
type_control(X, Object):-
\+tcl_GUI(on),
(X == s ; X == w),
read(Answer),nl,
arrange_answer(Object,Answer).
%GUI deactivated
type_control(Type, Object):-
\+tcl_GUI(on),
write('This question can be answered with multiple answers! '),
nl,
write('Please insert an answer and finish the insertion with the word stop. '), nl,
read(Ans),
read_all_answers(Object,Answers, Ans),
store_one_answer(Object, Answers).
read_all_answers(Object,Answers,'Why'):-
why_explanation(Object),!,
read(Ans),
read_all_answers(Object,Answers,Ans).
read_all_answers(Object,[], stop):- \+tcl_GUI(on), !.
read_all_answers(Object,[Ans|Answer], Ans):-
read(New_answer),
read_all_answers(Object,Answer, New_answer).
% When going backwards it will fail and it starts from the beginning.
ask_user(_):-
redo(true),!,
fail.
ask_user([Object| Rest]):-
ask_user(Rest).
arrange_answer(Object,'Why'):-
!,why_explanation(Object),
read(New_answer),
arrange_answer(Object,New_answer).
arrange_answer(Object,Answer):-
store_one_answer(Object, Answer).
% Saves user-given answers as separate relations.
store_one_answer(Object, []).
store_one_answer(Object, [stop]).
store_one_answer(Object, [First|Rest]) :-
asserta(answer(Object, First)),
store_one_answer(Object, Rest).
store_one_answer(Object, Answer):-
asserta(answer(Object, Answer)).
% When going backwards in a dialogue or with "redo" as question condition.
back_check(t):-
asserta(redo(true)),!,
fail.
back_check(f).
% Takes back all conclusions and makes new ones when going backwards in the dialogue.
revise_ev_concl:-
((trigged(_,_,_), retractall(trigged(_,_,_))) ;
true),
((how(_,_,_,_) , retractall(how(_,_,_,_))) ;
true),
goal_conclusions(Goal_param_list),
check_all(Goal_param_list).
% Conditions that can be put on questions in the question generator.
% "redo" makes back true and back_check
% will assert redo in the databasen and the complete fraga_ev_anvandaren will fail
% and the whole question base will be examined again, beautiful dont you think? Ever seen it before?
question_condition([redo],t).
% no conditions
question_condition([],f).
% not-condition
question_condition([not(Condition)| Rest], Go_back):-
\+ single_condition(Condition),
question_condition(Rest, Go_back).
% or-condition
question_condition([or(Condition1,Condition2) | Rest],Go_back):-
or_condition([Condition1,Condition2]),
question_condition(Rest,Go_back).
% Only single questions. (No composed questions.)
question_condition([Condition | Rest],Go_back):-
single_condition(Condition),
question_condition(Rest, Go_back).
% Control of single question conditions.
or_condition([Condition | Rest]):-
question_condition([Condition],_).
or_condition([Condition | Rest]):-
or_condition(Rest).
single_condition((Object = Answer)):-
answer(Object,Argument),
equal(Argument,Answer).
single_condition((Object > Value)):-
answer(Object, Answer),
Answer > Value.
single_condition((Object < Value)):-
answer(Object, Answer),
Answer < Value.
single_condition((Object >= Value)):-
answer(Object, Answer),
Answer >= Value.
single_condition((Object =< Value)):-
answer(Object, Answer),
Answer =< Value.
single_condition((Object = Value)):-
answer(Object, Answer),
Answer == Value.
single_condition((Object > Value)):-
answer(Object, Answer),
Answer > Value.
%*****************************************************************
%*****************************************************************
% Window 3 - Question base
%************************INFO ABOUT QUESTIONS**********************
% question_info/5.
% question_info/5.
question_info(capital,'Amount of accesible capital?',[],w,[]).
question_info(revenuetime,'Acceptable time for return',
['1 month','1 year','5 years'],s,[not(capital<0)]).
question_info(accessibility,'Do you need access to the capital with short notice?',
[yes,no,'dont know'],s,[(capital>0)]).
question_info(riskwillingness,'how big a risk are you willing to take?',
[high,reasonable,low],s,[(capital>0)]).