D
D
Daniil Miroshnichenko2014-12-07 19:32:45
Prolog
Daniil Miroshnichenko, 2014-12-07 19:32:45

How to change the code of a small expert system in prolog?

Below is an example of a Turbo Prolog program that implements a simple model expert system. A significant limitation in the example is that the system's Rule Base can only contain rules with a single fact as a prerequisite, i.e. rules of the form:
if A then B , P: A-> B
When it works, type rules are added from A follows B, facts are added. The added data can be written to the database, the record goes in the form rule("A","B") and so on.

DOMAINS
 fact       = Symbol
 condlist   = symbol*
PREDICATES
 run
 dial_win(integer)
 do(Char)
 repeat
 addrule
 addfact
 testfact(fact)
 testrule(fact,fact)
 testgoal(fact)
 do_goal
DATABASE
 rule(fact,fact)
 fact(fact)
GOAL
  run.
CLAUSES
  repeat.
  repeat:-repeat.
  run:-makewindow(1,1,31,"РЕЖИМЫ РАБОТЫ",2,2,8,30),
       makewindow(2,1,31,"Ввод правил и фактов",10,2,10,50), 
       shiftwindow(2),attribute(30),clearwindow,
       shiftwindow(1),attribute(30),clearwindow,
       field_str(0,1,25,"1 - Добавить правило"),  
       field_str(1,1,25,"2 - Добавить факт   "),  
       field_str(2,1,25,"3 - Логический вывод"),  
       field_str(3,1,25,"4 - Сохранить БЗ    "),
       field_str(4,1,25,"5 - Загрузить БЗ    "),
       field_str(5,1,25,"6 - Конец работы    "),
       retractall(_),
/* Основной цикл МЕНЮ  */
       repeat,shiftwindow(1),readchar(Key), do(Key).  
  do('1'):-addrule,fail.     
  do('2'):-addfact,fail.     
  do('3'):-do_goal,write("Цель истинна!"),nl,!,fail.     
  do('3'):-write("Цель ложна!"),nl,fail.     
  do('4'):-dial_win(31),retractall(fact(_)),
           deletefile("expert.db"),save("expert.db"),
           write("БЗ сохранена!"),nl,fail.     
  do('5'):-dial_win(31),consult("expert.db"),
           write("БЗ загружена!"),nl,fail.     
  do('6').     
  dial_win(Atr):-shiftwindow(2),attribute(Atr).
  addfact:-shiftwindow(2), attribute(30),
           write("Введите факт : "), readln(Fact),Fact<>"",
           testfact(Fact).
  addrule:-dial_win(30),
           write("Предпосылка : "), readln(Cond),Cond<>"",
           write("Заключение  : "), readln(Concl),Concl<>"",
           testrule(Cond,Concl).
  testfact(F):-fact(F), write("ERROR: Факт есть в БЗ !"), nl.         
  testfact(F):-not(fact(F)), assert(fact(F)), write("OK"), nl.
  testrule(F,Z):-rule(F,Z), write("ERROR: Правило есть в БЗ !"),
           nl.         
  testrule(F,Z):-not(rule(F,Z)), assert(rule(F,Z)), write("OK"),
           nl.
  do_goal :-dial_win(28),write("Введите цель : "), readln(G),
           G<>"", testgoal(G).
  testgoal(G):-fact(G),!.
  testgoal(G):-rule(C,G),testgoal(C).

It is necessary to change it so that:
the possibility of entering several premises leading to one conclusion, for example, from A and B and C follows D.
In the database this should be written as rule(5,[A,B,C],D). (5 is the rule number)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question