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

Quick Search    Search Deep

Source code: alice/tuprolog/Prolog.java


1   /*
2    *   Prolog.java
3    *
4    * Copyright 2000-2001 deis.unibo.it
5    *
6    * This software is the proprietary information of deis.unibo.it
7    * Use is subject to license terms.
8    *
9    */
10  package alice.tuprolog;
11  
12  import  java.io.*;
13  import  java.util.*;
14  
15  /**
16   * It's the main class of the tuProlog system.
17   * <p>
18   * Its instances are prolog machine able to manage theories,
19   * loading libraries and solving goals.
20   * <p>
21   * a prolog object is instatiable everywhere and anytime.
22   * A prolog object (with default libraries) is also serializable
23   * and so transportable through the net.
24   *
25   */
26  public class Prolog implements Serializable {
27  
28      /** tuProlog version **/
29      public static final String VERSION = "1.0.0";
30  
31      // prolog vm core
32      private PrologVM imp;
33  
34      /**
35       * construction with default libraries:
36       * ISOLibrary, MetaLibrary, JavaLibrary, IOLibrary
37       */
38      public Prolog(){
39          try {
40              imp=new PrologVM(new String[]{
41                  "alice.tuprolog.lib.ISOLibrary",
42                  "alice.tuprolog.lib.MetaLibrary",
43                  "alice.tuprolog.lib.JavaLibrary",
44                  "alice.tuprolog.lib.IOLibrary"});
45          } catch (Exception ex){
46              ex.printStackTrace();
47          }
48      }
49  
50      /**
51       * construction specifying (eventually) libraries
52       * @arg libs is a string list identifying the libraries that must be linked at engine sturtup
53       * @see Library
54       */
55      public Prolog(String[] libs) throws InvalidLibraryException {
56          imp=new PrologVM(libs);
57      }
58  
59      /**
60       * clears current theory
61       */
62       public void clearTheory(){
63          imp.clearTheory();
64      }
65  
66      /**
67       * gets current dynamic theory
68       *
69       * @return a copy of the current theory
70       * @see Theory
71       */
72       public Theory getTheory(){
73          return imp.getTheory();
74      }
75  
76      /**
77       * replaces current theory with a new one
78       *
79       * @param theory is the new theory
80       * @throws InvalidTheoryException if the new theory is not valid
81       * @see Theory
82       */
83      public void setTheory(Theory theory) throws InvalidTheoryException {
84          imp.setTheory(theory);
85      }
86  
87      /**
88       * adds (appending) a theory to the current one
89       *
90       * @param theory is the theory to be added
91       * @throws InvalidTheoryException if the new theory is not valid
92       * @see Theory
93       */
94      public void addTheory(Theory theory) throws InvalidTheoryException {
95          imp.addTheory(theory);
96      }
97  
98      // demo methods
99  
100     /**
101      * tries to demonstrate a goal.
102      *
103      * @param g the term representing the goal to demonstrate
104      * @return information about demonstration result
105      * @see SolveInfo
106      **/
107     public SolveInfo   solve(Term g) {
108         return imp.solve(g);
109     }
110 
111     /**
112      * tries to demonstrate a goal.
113      *
114      * @param g the string representing the goal to demonstrate
115      * @return information about demonstration result
116      * @throws MalformedGoalException if g hasn't a properly term syntax
117      * @see SolveInfo
118      **/
119     public SolveInfo   solve(String g) throws MalformedGoalException{
120         return imp.solve(g);
121     }
122 
123     /**
124      * tries to find another solution.
125      *
126      * @return information about demonstration result
127      * @throws NoMoreSolutionException if no previous demo(goal) invoked or no alternatives present
128      * @see SolveInfo
129      **/
130     public SolveInfo   solveNext() throws NoMoreSolutionException{
131         return imp.solveNext();
132     }
133 
134     /**
135      * accepts current solution
136      */
137     public void solveEnd(){
138         imp.solveEnd();
139     }
140 
141     /**
142      * halts the demonstration
143      */
144     public void solveHalt(){
145         imp.solveHalt();
146     }
147 
148     // built-in predicate modules methods
149 
150     /**
151      *  loads a builtin library
152      *
153      * @param className the name of the java class implementing the library
154      * @throws InvalidLibraryException if the library doesn't exist or if is not valid Library
155      * @see Library
156      **/
157     public Library loadLibrary(String className) throws InvalidLibraryException {
158         return imp.loadLibrary(className);
159     }
160 
161     /**
162      * unloads previously loaded builtin library
163      *
164      * @param className the name of the java class implementing the library
165      * @throws InvalidLibraryException if the library doesn't exist or if is not valid Library
166      * @see Library
167      **/
168     public void unloadLibrary(String className) throws InvalidLibraryException {
169         imp.unloadLibrary(className);
170     }
171 
172     /**
173      * gets a previously loaded library
174      *
175      * @param className the name of the java class implementing the library
176      * @return null if the lib is not loaded, otherwise the reference to the lib
177      **/
178     public Library getLibrary(String name){
179         return imp.getLibrary(name);
180     }
181 
182     /**
183      * sets spy state: if true the engine produce SpytEvent to listeners
184      *
185      * @see SpyEvent
186      */
187     public void setSpy(boolean on){
188         imp.setSpy(on);
189     }
190 
191     /**
192      * checks if the engine is producing SpyEvent info
193      */
194     public boolean isSpy(){
195         return imp.isSpy();
196     }
197 
198     // listeners interface
199 
200     public void addOutputListener(OutputListener l){
201         imp.addOutputListener(l);
202     }
203 
204     public void addSpyListener(SpyListener l){
205         imp.addSpyListener(l);
206     }
207 
208     public void addStateListener(StateListener l){
209         imp.addStateListener(l);
210     }
211 
212     public void removeOutputListener(OutputListener l){
213         imp.removeOutputListener(l);
214     }
215 
216     public void removeSpyListener(SpyListener l){
217         imp.removeSpyListener(l);
218     }
219 
220     public void removeStateListener(StateListener l){
221         imp.removeStateListener(l);
222     }
223 
224     // helpers
225 
226     /**
227      * gets a term from a string
228      *
229      * @param s the string representing a term
230      * @return the term if a successful parsing, null otherwise
231      **/
232     public Term toTerm(String s){
233         return imp.toTerm(s);
234     }
235 
236     /** gets more advanced and technical interface to the engine */
237     public PrologVM getImpl(){
238         return imp;
239     }
240 }