2

I have an exe file written in Prolog. When I just run it, it works. I need to launch it from a Python script and here I get an error. When I use system("PLANNER\Exe\planner.exe") I get

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

When I use execfile("PLANNER\Exe\planner.exe") I get

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

What am I doing wrong? Thanks in advance!

P.S. This is my code in 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

2 Answers2

0

execfile is certainly wrong; it is meant for executing another Python source code file; os.system does work, but not in this case, so I don't recommend it. The error occurs because the current working directory or cwd of the planner.exe is wrong. You want to chdir to its directory before executing it.

What you'd want is the subprocess.call, and its cwd argument:

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 will be set to the basename of the path - 'planner.exe'; and cwd will contain the folder part of it, or r'PLANNER\Exe'. Now the subprocess.call will first change the working directory to the PLANNER\Exe relative to current working directory, and run planner.exe therein.


If you want the script to work even more robust and work whether itself called in any directory, you can use the __file__ to find out the directory of script itself and find PLANNER folder relative to it:

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)

(also see this question on how to represent path names in Python)

Community
  • 1
  • 1
-1

the \ character is special in strings. You are saying to use the \E character and the \p character. You need to double the slash as \\ to escape it to get a literal \ character.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67