从 Python 调用用 Prolog 编写的执行程序

Call an executive written in Prolog from Python

我有一个用 Prolog 编写的 exe 文件。当我 运行 它时,它起作用了。我需要从 Python 脚本启动它,但在这里出现错误。 当我使用 system("PLANNER\Exe\planner.exe") 我得到

Exception in module: planner.exe
c:\program files\visual prolog 7.4 pe\pfc\exception\exception.pro(229) : error r000

当我使用 execfile("PLANNER\Exe\planner.exe") 我得到

Traceback (most recent call last):
  File "main.py", line 29, in <module>
    execfile("PLANNER\Exe\planner.exe")
  File "PLANNER\Exe\planner.exe", line 1
SyntaxError: Non-ASCII character '\x8b' in file PLANNER\Exe\planner.exe on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

我做错了什么?提前致谢!

P.S。这是我在 Prolog

中的代码
implement shift
    open core, console, list, file, string

constants
    className = "shift/shift".
    classVersion = "".

clauses
    classInfo(className, classVersion).

domains
   obj=char.
   block=obj.
   state=on(block, obj);  clear(obj).
   action=action(obj From, block Block, obj To).

class facts
   isObj:(char).
   isBlock:(char).

clauses
   isBlock('a').
   isBlock('b').
   isBlock('c').

   isObj('1').
   isObj('2').
   isObj('3').
   isObj('a').
   isObj('b').
   isObj('c').

class predicates
   can : (action, state* Condition) determ (i,o).
clauses
   can(action(From, Block, To), C):-
      C = [clear(To),clear(Block),on(Block, From)],
      write("!!! ", C),nl,
      if isMember(clear('c'), C) then
         write("@@@")
    end if,
      To <> Block,
      From <> To,
      Block <> From.

class predicates
   plan : (state* State, state* Goals , state* ProtectedGoals, action* Plan, state* FinalState)
                                                                                                      nondeterm (i,i,i,o,o).
clauses
   plan(State, Goals, _, [], State):-
     % write("!!State: ", State, " Goals: ", Goals),nl,
      satisfied(State, Goals),
      %write("yes"),nl,
      !.
    plan(State, Goals, Protected, Plan, FinalState):-

       select(State, Goals, Goal),
write("State: ", State, " Goals: ", Goals,  " Goal: ", Goal), nl,
       achieves(Action, Goal, State),
%write("Action: ", Action, "Goal: ", Goal), nl,
       write("Action: ", Action), nl,
       preserves(Action, Protected),
       can(Action, Condition),
      %  write(" Condition: ", Condition), nl,
       plan(State, Condition, Protected, Preplan, Midstate1),
%write("Midstate1: ", Midstate1),nl,
       Midstate2 = apply(Midstate1, Action),
       %write("Action: ", Action, " Midstate2: ", Midstate2), nl,
       plan(Midstate2, Goals, [Goal|Protected], Postplan, FinalState),
       conc(Preplan, Action, Postplan, Plan).

class predicates
   preserves : (action, state*) determ.
clauses
   preserves(_, []) :- !.
   preserves(action(From, Block, To), [Goal|Goals]) :-
       Goal <> on(Block, From),
       Goal <> clear(To),
       preserves(action(From, Block, To), Goals).

class predicates
   apply : (state*, action) -> state*.
clauses
   apply([], _) = [] :- !.
   apply([clear(To)|State], action(From, Block, To)) = [clear(From)|apply(State, action(From, Block, To))] :- !.
   apply([S|State], action(From, Block, To)) = [S1|apply(State, action(From, Block, To))] :-
      S = on(Block, From),
      S1 = on(Block, To),
      !.
    apply([S|State], Action) = [S|apply(State, Action)].

class predicates
   achieves : (action, state Goal, state* State) nondeterm (o,i, i).
clauses
   achieves(action(Object, B, To), clear(Object), State) :-
      isBlock(B),
      isObj(To),
      Object <> B,
      Object <> To,
      B <> To,
      isMember(on(B, Object), State),
      isMember(clear(To), State).
   achieves(action(From, Block, Object), on(Block, Object), State) :-
      isObj(From),
      From <> Block,
      From <> Object,
      isMember(on(Block, From), State).

class predicates
   select : (state*, state*, state) nondeterm (i,i,o).
clauses
   select(State, Goals, Goal) :-
      list::memberIndex_nd(Goal, _, Goals),
      not(isMember(Goal, State)). % цель не достигнута

class predicates
   satisfied : (state*, state* Goals) determ.
clauses
   satisfied(_State, []) :- !.
   satisfied(State, [H|Goals]) :-
     write("Satisfied State: ", State, " H: ", H), nl,
     if H = clear('c') then
        write("sdfg"),nl
    end if,
     isMember(H, State),
     write("yes"),nl,
     satisfied(State, Goals).

class predicates
   conc : (action*, action, action*, action*) nondeterm (i,i,i,o).
clauses
   conc([], Action, [], [Action]).
   conc([], Action, Plan, [Action|Plan]).
   conc([H|Preplan], Action, Postplan, [H|Plan]) :-
      conc(Preplan, Action, Postplan, Plan).

class predicates
   toStates : (string*) -> state* determ.
clauses
   toStates([]) = [] :- !.
   toStates([H|S]) = L :-
      H1 = toTerm(state, H),
      L = [H1|toStates(S)].

class predicates
   optimize : (action*) -> action*.
clauses
    optimize([]) = [] :- !.
    optimize([A]) = [A] :- !.
    optimize([A1|[A2| Tail]]) = optimize([A3|Tail]) :-
       A1 = action(From, O, To1),
       A2 = action(To1, O, To2),
       !,
       A3 = action(From, O, To2).
    optimize([H|T]) = [H|optimize(T)].

clauses
   run():-
    (Start = toStates(split(readString("start.txt"), ";")),
    write(Start),
    Goals= toStates(split(readString("goals.txt"), ";")),
    plan(Start, Goals, [], Plan, _),
    PlanO = optimize(Plan),
    write("Plan: ", PlanO),nl,
    writeString("plan.txt", toString(PlanO)),
    !,
    _ = readLine());
    write("It didn't work out!"),
    _=readline().

end implement shift

\字符在字符串中比较特殊。你是说使用 \E 字符和 \p 字符。您需要将斜杠加倍为 \ 以将其转义以获得文字 \ 字符。

execfile肯定是错的;它用于执行另一个 Python 源代码文件; os.system 确实有效,但在这种情况下无效,所以我不推荐它。出现错误是因为当前工作目录或planner.execwd错误。你想在执行它之前chdir到它的目录。

您想要的是 subprocess.call 及其 cwd 参数:

import subprocess
impot os

path = r'PLANNER\Exe\planner.exe'
exe = os.path.basename(path)
cwd = os.path.dirname(path)
subprocess.call([exe], cwd=cwd)

exe 将设置为 path - 'planner.exe' 的基本名称; cwd 将包含它的文件夹部分,或 r'PLANNER\Exe'。现在subprocess.call会先把工作目录改成相对于当前工作目录的PLANNER\Exe,运行planner.exe


如果你想让脚本工作得更稳健,无论在任何目录中调用它自己都可以工作,你可以使用__file__找到脚本本身的目录并找到PLANNER文件夹相对对它:

import subprocess
impot os

relpath = r'PLANNER\Exe\planner.exe'
path = os.path.join(os.path.dirname(__file__), relpath)
exe = os.path.basename(path)
cwd = os.path.dirname(path)
subprocess.call([exe], cwd=cwd)

(还有 see this question 关于如何在 Python 中表示路径名)