我正在使用http://www.gnu.org/software/gnuprologjava/从Java应用程序执行prolog。
在这种情况下,我将使用以下prolog文件:
/* ghoul is a prolog fact representing the goal state in flat notation */
ghoul(cube('WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE','WHITE', /*White*/
'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN','GREEN', /*Green*/
'RED', 'RED', 'RED', 'RED', 'RED', 'RED', 'RED', 'RED','RED', /*Red*/
'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE','BLUE', /*Blue*/
'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE','ORANGE', /*Orange, Yellow*/
'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW','YELLOW')).
/* pieces converts from flat to pieces notation and vice versa*/
pieces(cube(X01, X02, X03, X04, X05, X06, X07, X08, X09, /*White*/
X10, X11, X12, X13, X14, X15, X16, X17, X18, /*Green*/
X19, X20, X21, X22, X23, X24, X25, X26, X27, /*Red*/
X28, X29, X30, X31, X32, X33, X34, X35, X36, /*Blue*/
X37, X38, X39, X40, X41, X42, X43, X44, X45, /*Orange*/
X46, X47, X48, X49, X50, X51, X52, X53, X54), /*Yellow*/
[p(X05), p(X14), p(X23), p(X32), p(X41),p(X50),
p(X11, X08), p(X15, X22), p(X17, X47), p(X13, X42), /* Green edges */
p(X38, X04), p(X44, X49), /* Orange edges */
p(X20, X06), p(X26, X51), /* Red edges */
p(X29, X02), p(X33, X40), p(X35, X53), p(X31, X24), /* Blue edges */
p(X10, X07, X39), p(X12, X19, X09), p(X18, X48, X25), p(X16, X45, X46), /* Green coins */
p(X28, X03, X21), p(X30, X37, X01), p(X36, X52, X43), p(X34, X27, X54)]).
/* Move of white face */
mov(w, cube(X01, X02, X03, X04, X05, X06, X07, X08, X09, /*White*/
X10, X11, X12, X13, X14, X15, X16, X17, X18, /*Green*/
X19, X20, X21, X22, X23, X24, X25, X26, X27, /*Red*/
X28, X29, X30, X31, X32, X33, X34, X35, X36, /*Blue*/
X37, X38, X39, X40, X41, X42, X43, X44, X45, /*Orange*/
X46, X47, X48, X49, X50, X51, X52, X53, X54),
cube(X03, X06, X09, X02, X05, X08, X01, X04, X07, /*White*/
X19, X20, X21, X13, X14, X15, X16, X17, X18, /*Green*/
X28, X29, X30, X22, X23, X24, X25, X26, X27, /*Red*/
X37, X38, X39, X31, X32, X33, X34, X35, X36, /*Blue*/
X10, X11, X12, X40, X41, X42, X43, X44, X45, /*Orange*/
X46, X47, X48, X49, X50, X51, X52, X53, X54)).
/* Opposite Moves */
move(+M, OldState, NewState):- mov(M, OldState, NewState).
move(-M, OldState, NewState):- mov(M, NewState, OldState).
为什么以下Java代码总是返回-1(在prolog解释器语法中为false),因为解释器能够计算NewState值:
try {
URL url = FileLocator.find(Activator.getDefault().getBundle(),
new Path("cube.pl"), null);
url = FileLocator.toFileURL(url);
Environment env = new Environment();
// Run the initialization
env.runInitialization(interpreter);
env.ensureLoaded(AtomTerm.get(url.getFile()));
Interpreter interpreter = env.createInterpreter();
VariableTerm cube = new VariableTerm("cube");
Term[] args = { cube };
CompoundTerm goalTerm = new CompoundTerm(AtomTerm.get("ghoul"),
args);
interpreter.runOnce(goalTerm);
VariableTerm newCube = new VariableTerm("NewCube");
Term[] args2 = { AtomTerm.get("+w"), cube,
newCube };
CompoundTerm move = new CompoundTerm(AtomTerm.get("move"), args2);
int runOnce = interpreter.runOnce(move);
System.out.println(runOnce);
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
在调用runInitialization之前,你不应该调用ensureLoaded吗? javadoc说: “在使用它之前和期待答案之前,你必须使用runInitialization(Interpreter)。”
另外,您传递给runInitialization的解释器是否与为运行查询而创建的解释器相同?