如何将基于prolog的专家系统连接到Java

时间:2014-02-17 07:45:46

标签: java netbeans prolog swi-prolog expert-system

我正在建立一个重大决策专家系统,它类似于动物检测专家系统,所以逻辑很简单。实际上在prolog中,在“咨询”之后我可以输入“? - go”。要开始提问,但是在我构建了java接口之后,我唯一可以做的查询是“consult”,它显示程序已成功加载,但下一步是无限运行。我不能输入“go”。继续该计划。我的意思是,在构建java接口之后,我无法再与程序交互。 序言代码是:

 go :- hypothesize(Major),
      write('I suggest you to choose: ['),
      write(Major),
      write('] as your major'),
      nl,
      undo.

/* hypotheses to be tested */
hypothesize(education)   :- education, !.
hypothesize(history)   :- history, !.
hypothesize(cs)     :- cs, !.
hypothesize(mathematics)   :- mathematics, !.
hypothesize(biology)     :- biology, !.
hypothesize(accounting)   :- accounting, !.
hypothesize(economics) :- economics, !.
hypothesize(unknown).             /* no diagnosis */

/* animal identification rules */
education :- social,
           verify(teaching_children),
           verify(be_a_teacher).

history :- social,
           verify(antique),
           verify(ancient_story).

cs :- engineeringscience,
         engineering,
         verify(coding),
         verify(diy_desktop).

mathematics :- engineeringscience,

           verify(successione_di_fibonacci),
           verify(solving_equations).

biology :- engineeringscience,

         verify(origin_of_life).


accounting :- business,
           verify(excel),
           verify(being_an_accountant),
           verify(auditing).

economics :- business,
             verify(monetary),
             verify(being_an_ecomomist).

/* classification rules */
social             :- verify(editorial), !.
social                 :- verify(writing_more_than_mathematical_logic).
engineeringscience     :- verify(quantitative_analysis), !.
engineeringscience     :- verify(natural_phenomenon),
                          verify(discovery).
engineering            :- engineeringscience,
                      verify(reparing_machine), !.
engineering            :- engineeringscience,
                      verify(fixing_electronic_components),
                          verify(computer_games).
business               :- verify(making_a_lot_of_money), !.
business               :- verify(trading).

/* how to ask questions */
ask(Question) :-
    write('Do you like(or good at): '),
    write(Question),
    write('? (y/n)'),
    read(Response),
    nl,
    ( (Response == yes ; Response == y)
      ->
       assert(yes(Question)) ;
       assert(no(Question)), fail).

:- dynamic yes/1,no/1.

/* How to verify something */
verify(S) :-
   (yes(S)
    ->
    true ;
    (no(S)
     ->
     fail ;
     ask(S))).

/* undo all yes/no assertions */
undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.

java代码是:

     package javaapplication1;

/**
 *
 * @author Adrian
 */

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import jpl.Atom;
import jpl.Compound;
import jpl.Variable;
import jpl.Term;
import jpl.Query;
import jpl.JPL;


@SuppressWarnings({ "unchecked", "deprecation", "serial" })
public class expertsystem extends JFrame {

 JButton  startButton = new JButton("Start");
 JTextArea  textArea = new JTextArea("A Major Decision Expert System \n" +
           "for WOU students.");

 /**
  */
 expertsystem(){
  Container cp=getContentPane();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  setLocation (200,200);
  setSize  (300,200);
  setLayout (new FlowLayout());


  startButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    startDiagnose();
   }
  });

  cp.add(textArea);
  cp.add(startButton);

  setVisible(true); 
 }

 private void startDiagnose(){
  Term consult_arg[] = { 
          new Atom( "C:/Users/Adrian/Desktop/WOU/wou.pl" ) 
      };
      Query consult_query = 
          new Query( 
              "consult", 
              consult_arg );

      boolean consulted = consult_query.query();

      if ( !consulted ){
          System.err.println( "Consult failed" );
          System.exit( 1 );
      }

 }

 public static void main( String argv[] ){
  JPL.init();
  expertsystem jpTest = new expertsystem();

}
}

0 个答案:

没有答案