Source code: alice/tuprolog/Library.java
1 /*
2 * Library.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.Serializable;
13 import java.util.*;
14
15 /**
16 *
17 * this abstract class is the base class for developing
18 * tuProlog built-in libraries, which can be dynamically
19 * loaded by prolog objects.
20 * <p>
21 * each library can expose to engine:
22 * <ul>
23 * <li> a theory (as a string assigned to theory field)
24 * <li> builtin predicates: each method whose signature is
25 * boolean name_arity(Struct goal)
26 * is considered a built-in predicate provided by the library
27 * <li> builtin evaluable functors: each method whose signature is
28 * Term name_arity(Struct goal)
29 * is considered a built-in functors provided by the library
30 * </ul>
31 * <p>
32 * typically theory is assigned in library constructor
33 */
34 public abstract class Library implements Serializable {
35
36 /** prolog core which loaded the library */
37 PrologVM engine;
38
39 /** theory provided by the library */
40 protected String theory;
41
42 /**
43 * unification of two terms
44 */
45 public final boolean unify(Term t0,Term t1){
46 RTContext e=engine.getContext();
47 if (e!=null){
48 Term newT1=e.getRenamedTermCopy(t1);
49 return (t0.unify(newT1,e.mark++));
50 } else {
51 return (t0.unify(t1,0));
52 }
53 }
54
55 /**
56 * gets a copy of a term with variables renamed.
57 */
58 public final Term getRenamedCopy(Term t0){
59 RTContext e=engine.getContext();
60 if (e!=null){
61 return e.getRenamedTermCopy(t0);
62 } else {
63 return (Term)t0.clone();
64 }
65 }
66
67 public final String getTheory(){
68 return theory;
69 }
70
71 /**
72 * used by core engine to retrieve built-in predicates
73 */
74 final String[] getPredicates(){
75 try {
76 java.lang.reflect.Method[] mlist=this.getClass().getMethods();
77 ArrayList list=new ArrayList();
78 for (int i=0; i<mlist.length; i++){
79 String name=mlist[i].getName();
80 Class[] clist=mlist[i].getParameterTypes();
81 Class rclass=mlist[i].getReturnType();
82 if (clist.length==1 && rclass.getName().equals("boolean")
83 && clist[0].getName().endsWith(".Struct")){
84 int index=name.lastIndexOf('_');
85 if (index!=-1){
86 try {
87 int arity=Integer.parseInt(name.substring(index+1,name.length()));
88 list.add(name);
89 } catch (Exception ex){
90 }
91 }
92 }
93 }
94 String[] slist=new String[list.size()];
95 Iterator it=list.listIterator();
96 for (int i=0; i<slist.length; i++){
97 slist[i]=(String)it.next();
98 }
99 return slist;
100 } catch (Exception ex){
101 return null;
102 }
103 }
104
105 /**
106 * used by core engine to retrieve built-in functors
107 */
108 final String[] getFunctors(){
109 try {
110 java.lang.reflect.Method[] mlist=this.getClass().getMethods();
111 ArrayList list=new ArrayList();
112 for (int i=0; i<mlist.length; i++){
113 String name=mlist[i].getName();
114 Class[] clist=mlist[i].getParameterTypes();
115 Class rclass=mlist[i].getReturnType();
116 if (clist.length==1 && rclass.getName().endsWith(".Term")
117 && clist[0].getName().endsWith(".Struct")){
118 int index=name.lastIndexOf('_');
119 if (index!=-1){
120 try {
121 int arity=Integer.parseInt(name.substring(index+1,name.length()));
122 list.add(name);
123 } catch (Exception ex){
124 }
125 }
126 }
127 }
128 String[] slist=new String[list.size()];
129 ListIterator it=list.listIterator();
130 for (int i=0; i<slist.length; i++){
131 slist[i]=(String)it.next();
132 }
133 return slist;
134 } catch (Exception ex){
135 return null;
136 }
137 }
138
139 /**
140 * method invoked by prolog engine when library is
141 * going to be removed
142 */
143 public void dismiss(){
144 }
145
146 /**
147 * method invoked when the engine is going
148 * to demonstrate a goal
149 */
150 public void onSolveBegin(Term goal){
151 }
152
153 /**
154 * method invoked when the engine has
155 * finished a demostration
156 */
157 public void onSolveEnd(){
158 }
159
160 public final void setEngine(PrologVM e){
161 engine=e;
162 }
163
164 public final PrologVM getEngine(){
165 return engine;
166 }
167 }