Prolog命令行运行演示

来源:互联网 发布:网络流行说唱歌曲大全 编辑:程序博客网 时间:2024/06/05 15:52

使用方法:

=事实

:规则

 

?查询

 

使用prolog语法.

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import alice.tuprolog.InvalidTheoryException;
import alice.tuprolog.MalformedGoalException;
import alice.tuprolog.NoMoreSolutionException;
import alice.tuprolog.NoSolutionException;
import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Theory;


public class Main {

 /**
  * @param args
  * @throws IOException
  * @throws InvalidTheoryException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub

  InputStreamReader reader = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(reader);
  
  String line = null;
  Prolog engine = new Prolog();
  
  System.out.print(">");
  while(true) {
   line = br.readLine();
   //System.out.println(">"+line);
   if(line != null) {
    if(line.startsWith("=")) {
     try {
      engine.addTheory(new Theory(line.substring(1)));
     } catch (InvalidTheoryException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } else if(line.startsWith("!=")) {
     try {
      Theory theory = engine.getTheory();
      Iterator it = theory.iterator(engine);
      
      while(it.hasNext()) {
       String temp = it.next().toString();
       if(line.substring(2).trim().equalsIgnoreCase(temp)) {
        it.remove();
       }
       System.out.println(temp);
      }
      
      //System.out.println(theory.iterator(engine).toString());
     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }     
    } else if(line.startsWith(":")) {
     try {
      engine.addTheory(new Theory(line.substring(1)));
     } catch (InvalidTheoryException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } else if(line.startsWith("?")) {
     try {
      SolveInfo info = engine.solve(line.substring(1));
      while(info.isSuccess()) {
       System.out.println(info.getSolution());
       info = engine.solveNext();
      }
     } catch (MalformedGoalException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (NoSolutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (NoMoreSolutionException e) {
      // TODO Auto-generated catch block
      //e.printStackTrace();
     }
    } else {
     try {
      SolveInfo info = engine.solve("talk("+line+",X).");
      while(info.isSuccess()) {
       System.out.println(info.getSolution());
       info = engine.solveNext();
      }
     } catch (MalformedGoalException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (NoSolutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (NoMoreSolutionException e) {
      // TODO Auto-generated catch block
      //e.printStackTrace();
     }
    }
   }
      
   System.out.print(">");
  }
  
 }

}

原创粉丝点击