Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: alice/tuprolog/runtime/tucson/Proxy.java


1   package alice.tuprolog.runtime.tucson;
2   import alice.tucson.*;
3   import alice.tuprolog.*;
4   import alice.logictuple.*;
5   import alice.infrastructure.Tucson;
6   import  java.io.*;
7   import  java.util.*;
8   
9   
10  public class Proxy implements OutputAutomatonListener {
11  
12      TupleArgument me=new Value("proxy-"+Thread.currentThread()+"-"+System.currentTimeMillis());
13      Vector outputListeners=new Vector();
14      Vector stateListeners=new Vector();
15      Vector spyListeners=new Vector();
16      OutputAutomaton outputActor;
17      TupleCentreId tid;
18  
19      public Proxy() throws Exception {
20          tid=new TupleCentreId("prolog-daemon-tc");
21          outputActor=new OutputAutomaton(tid);
22          outputActor.addOutputAutomatonListener(this);
23          new Thread(outputActor).start();
24      }
25  
26      synchronized public void clearTheory() throws Exception {
27          Tucson.out(tid, new LogicTuple("pe-ask",me,new Value("clearTheory")));
28      }
29  
30      synchronized public Theory getTheory() throws Exception {
31          LogicTuple tuple=ask(new Value("getTheory"));
32          return new Theory(new ByteArrayInputStream(tuple.getString(1).getBytes()));
33      }
34  
35      synchronized public void setTheory(Theory th) throws Exception {
36          LogicTuple tuple=ask(new Value("setTheory",new Value(th.toString())));
37          if (!tuple.getArg(1).toTerm().isTrue())
38              new InvalidTheoryException();
39      }
40  
41      synchronized public void addTheory(Theory th) throws Exception {
42          LogicTuple tuple=ask(new Value("addTheory",new Value(th.toString())));
43          if (!tuple.getArg(1).toTerm().isTrue())
44              new InvalidTheoryException();
45      }
46  
47      synchronized public SolveInfo solve(String goal) throws Exception {
48          LogicTuple tuple=ask(new Value("solveString",new Value(goal)));
49          Term info=tuple.getArg(1).toTerm();
50          if (((Struct)info).functor.equals("error"))
51              throw new MalformedGoalException();
52          else
53              return new SolveResultInfo(info);
54      }
55  
56      synchronized public SolveInfo solve(Term goal) throws Exception {
57          LogicTuple tuple=ask(new Value("solveTerm",new TupleArgument(goal)));
58          Term info=tuple.getArg(1).toTerm();
59          return new SolveResultInfo(info);
60      }
61  
62      synchronized public SolveInfo solveNext() throws Exception {
63          LogicTuple tuple=ask(new Value("solveNext"));
64          Term info=tuple.getArg(1).toTerm();
65          if (((Struct)info).functor.equals("error"))
66              throw new NoSolutionException();
67          else
68              return new SolveResultInfo(info);
69      }
70  
71      synchronized public void solveHalt() throws Exception {
72          tell(new Value("solveHalt"));
73      }
74  
75      synchronized public void solveEnd()  throws Exception {
76          tell(new Value("solveEnd"));
77      }
78  
79      synchronized public void loadLibrary(String st) throws Exception{
80          LogicTuple tuple=ask(new Value("loadLibrary", new Value(st)));
81          Term info=tuple.getArg(1).toTerm();
82          if (((Struct)info).functor.equals("error"))
83              throw new InvalidLibraryException();
84      }
85  
86      synchronized public void unloadLibrary(String st) throws Exception {
87          LogicTuple tuple=ask(new Value("unloadLibrary", new Value(st)));
88          Term info=tuple.getArg(1).toTerm();
89          if (((Struct)info).functor.equals("error"))
90              throw new InvalidLibraryException();
91      }
92  
93      synchronized public void setSpy(boolean set) throws Exception {
94          if (set)
95              tell(new Value("spy",new Value("true")));
96          else
97              tell(new Value("spy",new Value("false")));
98      }
99  
100     synchronized public boolean isSpy() throws Exception {
101         LogicTuple tuple=ask(new Value("isSpy"));
102         return tuple.getArg(1).toTerm().isTrue();
103     }
104 
105     synchronized public Term toTerm(String st) throws Exception {
106         LogicTuple tuple=ask(new Value("toTerm", new Value(st)));
107         return tuple.getArg(1).toTerm();
108     }
109 
110 
111     synchronized public void addOutputListener(OutputListener l){
112         outputListeners.addElement(l);
113     }
114 
115     synchronized public void addSpyListener(SpyListener l){
116         spyListeners.addElement(l);
117     }
118 
119     synchronized public void addStateListener(StateListener l){
120         stateListeners.addElement(l);
121     }
122 
123     synchronized public void removeOutputListener(OutputListener l){
124         outputListeners.removeElement(l);
125     }
126 
127     synchronized public void removeSpyListener(SpyListener l){
128         spyListeners.removeElement(l);
129     }
130 
131     synchronized public void removeStateListener(StateListener l){
132         stateListeners.removeElement(l);
133     }
134 
135     public void onOutputAutomaton(OutputAutomatonEvent ev) {
136         if (ev.getType()==OutputAutomatonEvent.OUTPUT){
137             OutputEvent e=new OutputEvent(this,ev.getMsg());
138             for (int i=0; i<outputListeners.size(); i++)
139                 ((OutputListener)outputListeners.elementAt(i)).onOutput(e);
140         } else if (ev.getType()==OutputAutomatonEvent.STATE){
141             StateEvent e=new StateEvent(this,ev.getMsg());
142             for (int i=0; i<stateListeners.size(); i++)
143                 ((StateListener)stateListeners.elementAt(i)).onState(e);
144         } else if (ev.getType()==OutputAutomatonEvent.SPY){
145             SpyEvent e=new SpyEvent(this,ev.getMsg());
146             for (int i=0; i<spyListeners.size(); i++)
147                 ((SpyListener)spyListeners.elementAt(i)).onSpy(e);
148         }
149     }
150 
151     /*
152     synchronized public void addSolutionListener(SolutionListener l){
153     solutionListeners.addElement(l);
154     }
155 
156     synchronized public void removeSolutionListener(SolutionListener l){
157     solutionListeners.removeElement(l);
158     }
159 
160     synchronized public void onTheory(TheoryEvent e) {
161     try {
162     imp.addTheory(e.getTheory());
163     } catch (InvalidTheoryException ex){}
164     }
165 
166     synchronized public void onGoal(GoalEvent e) {
167     try {
168     notifySolution(new SolutionEvent(this,imp.solve(e.getGoal())));
169     } catch (MalformedGoalException ex){}
170     }
171 
172     protected void notifySolution(SolutionEvent e){
173     for (int i=0; i<solutionListeners.size(); i++)
174     ((SolutionListener)solutionListeners.elementAt(i)).onSolution(e);
175     }
176      */
177 
178 
179 
180     // helpers
181 
182     // protocol
183 
184     public LogicTuple ask(TupleArgument what){
185         try {
186             Tucson.out(tid,new LogicTuple("pe-ask",me,what));
187             return (LogicTuple)Tucson.in(tid,new LogicTuple("pe-answer",me,new alice.logictuple.Var("Result")));
188         } catch (Exception ex){
189             return null;
190         }
191     }
192 
193     public void tell(TupleArgument what){
194         try {
195             Tucson.out(tid,new LogicTuple("pe-ask",me,what));
196         } catch (Exception ex){}
197     }
198 
199 
200     /** testing funct */
201     public static void main(String args[]){
202         try {
203             Proxy core=new Proxy();
204             core.setTheory(new Theory(new FileInputStream("test.pl")));
205             core.addStateListener(new StateListener(){
206                 public void onState(StateEvent e){
207                     System.out.println(e.getMsg());
208                 }});
209                 core.addOutputListener(new OutputListener(){
210                     public void onOutput(OutputEvent e){
211                         System.out.print(e.getMsg());
212                     }});
213                     core.addSpyListener(new SpyListener(){
214                         public void onSpy(SpyEvent e){
215                             System.out.println(e.getMsg());
216                         }});
217 
218                         SolveInfo info=core.solve("pippo(X),write(X),nl.");
219                         while (info.success()){
220                             System.out.println(info.getSolution());
221                             if (info.hasOpenAlternatives())
222                                 info=core.solveNext();
223                             else
224                                 break;
225                         }
226                         System.out.println(core.getTheory());
227                         core.loadLibrary("alice.tuprolog.lib.LogicTSLibrary");
228                         info=core.solve("out(pippo(10)),inp(pippo(X)),write(X),nl.");
229 
230 
231         } catch (Exception ex){
232             ex.printStackTrace();
233         }
234 
235     }
236 
237 }
238 
239 final class SolveResultInfo implements SolveInfo {
240 
241     private int flag;
242     private alice.tuprolog.Var[] bindings;
243     private Term goal;
244     private int haltCode=0;
245 
246     SolveResultInfo(Term tt){
247         Struct t=(Struct)tt;
248         if (t.getTerm(0).isTrue()){
249             flag=PrologVM.TRUE;
250         } else {
251             flag=PrologVM.FALSE;
252         }
253         if (t.getTerm(1).isTrue()){
254             flag=PrologVM.TRUE_CP;
255         }
256         Struct vs=((Struct)t.getTerm(2));
257         bindings=new alice.tuprolog.Var[vs.arity];
258         for (int i=0; i<vs.arity; i++){
259             try {
260                 bindings[i]=new alice.tuprolog.Var(((Struct)vs.arg[i]).arg[0].getName());
261                 bindings[i].unify(((Struct)vs.arg[i]).arg[1],0);
262              } catch (alice.tuprolog.InvalidVarNameException ex){
263              }
264         }
265         goal=(Struct)t.getTerm(3);
266         haltCode=0;
267     }
268 
269     public boolean success(){
270         return flag==PrologVM.TRUE || flag==PrologVM.TRUE_CP;
271     }
272 
273     public boolean hasOpenAlternatives(){
274         return flag==PrologVM.TRUE_CP;
275     }
276 
277     public Substitution getSubstitution() throws NoSolutionException {
278         if (flag==PrologVM.TRUE_CP || flag==PrologVM.TRUE)
279             return new Substitution(bindings);
280         else
281             throw new NoSolutionException();
282     }
283 
284     public Term  getSolution() throws NoSolutionException {
285         if (flag==PrologVM.TRUE_CP || flag==PrologVM.TRUE)
286             return (Term)goal.clone();
287         else
288             throw new NoSolutionException();
289     }
290 
291     public boolean halt(){
292         return flag==PrologVM.HALT;
293     }
294 
295     public int getHaltCode() throws NoHaltException {
296         if (flag!=PrologVM.HALT)
297             throw new NoHaltException();
298         return haltCode;
299     }
300 
301 }
302