Prolog命令行运行演示

使用方法:

=事实

:规则

?查询

使用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(">");
}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值