30 |
import java.util.Collections; |
import java.util.Collections; |
31 |
import java.util.Map; |
import java.util.Map; |
32 |
import java.util.HashMap; |
import java.util.HashMap; |
33 |
|
import org.python.core.*; |
34 |
|
|
35 |
/** An object that manages a DAG of Function instances, enabling |
/** An object that manages a DAG of Function instances, enabling |
36 |
* transparent and super-lazy caching. |
* transparent and super-lazy caching. |
40 |
*/ |
*/ |
41 |
public abstract class Functional { |
public abstract class Functional { |
42 |
public static boolean dbg = false; |
public static boolean dbg = false; |
43 |
private void p(String s) { System.out.println(s); } |
static private void p(String s) { System.out.println(s); } |
44 |
|
|
45 |
/** The id of the hint for which background thread group |
/** The id of the hint for which background thread group |
46 |
* a function should use, if it's not run directly. |
* a function should use, if it's not run directly. |
140 |
Object[] parameters |
Object[] parameters |
141 |
); |
); |
142 |
|
|
143 |
|
/** Create a new node in the DAG. |
144 |
|
* @param id An identifier for the node. Used for determining caching &c. |
145 |
|
* Should be stable between invocations. |
146 |
|
* @param functionClass The class of which the Function (or NodeFunction) |
147 |
|
* object should |
148 |
|
* be created. |
149 |
|
* @param parameters The parameters for the constructor of the class. |
150 |
|
* These may contain Node objects, which will be converted |
151 |
|
* to functions or nodefunctions as appropriate. |
152 |
|
*/ |
153 |
|
public abstract FunctionInstance createFunctionInstance_Jython( |
154 |
|
Object id, |
155 |
|
org.python.core.PyClass functionClass, |
156 |
|
org.python.core.PyObject[] parameters |
157 |
|
); |
158 |
|
|
159 |
|
/* |
160 |
|
* XXX I couldn't make the following work. |
161 |
|
public FunctionInstance createFunctionInstance_Any( |
162 |
|
Object id, |
163 |
|
Object functionClass, |
164 |
|
Object[] parameters) { |
165 |
|
p("Any: Fcl = "+functionClass); |
166 |
|
if(functionClass instanceof PyObject) { |
167 |
|
p("Any: ispy"); |
168 |
|
PyObject[] params = new PyObject[parameters.length]; |
169 |
|
for(int i=0; i<params.length; i++) |
170 |
|
params[i] = Py.java2py(parameters[i]); |
171 |
|
return createFunctionInstance_Jython(id, |
172 |
|
(PyClass)functionClass, params); |
173 |
|
} else if(functionClass instanceof Class) { |
174 |
|
p("Any: isj"); |
175 |
|
return createFunctionInstance(id, |
176 |
|
(Class)functionClass, parameters); |
177 |
|
} |
178 |
|
throw new Error("CreateFunctionINstance..."); |
179 |
|
} |
180 |
|
*/ |
181 |
|
|
182 |
/** Helper function: Select a suitable constructor. |
/** Helper function: Select a suitable constructor. |
183 |
* Useful for createFunctionInstance. |
* Useful for createFunctionInstance. |
184 |
* Goes through the constructors of functionClass and |
* Goes through the constructors of functionClass and |
233 |
} |
} |
234 |
} |
} |
235 |
|
|
236 |
|
if(ptypes[j].isAssignableFrom(java.lang.Boolean.TYPE)) { |
237 |
|
if(parameters[j] instanceof java.lang.Boolean) |
238 |
|
continue PARAMETERS; |
239 |
|
} |
240 |
|
|
241 |
|
if(ptypes[j].isAssignableFrom(java.lang.Integer.TYPE)) { |
242 |
|
if(parameters[j] instanceof java.lang.Integer) |
243 |
|
continue PARAMETERS; |
244 |
|
} |
245 |
|
|
246 |
if(! ptypes[j].isAssignableFrom(givenParamType)) { |
if(! ptypes[j].isAssignableFrom(givenParamType)) { |
247 |
if (dbg) p("DISMATCH: "+givenParamType+", "+ptypes[j]); |
if (dbg) p("DISMATCH: "+givenParamType+", "+ptypes[j]); |
248 |
continue CONSTRUCTORS; |
continue CONSTRUCTORS; |
251 |
} |
} |
252 |
return constructors[i]; |
return constructors[i]; |
253 |
} |
} |
254 |
|
|
255 |
|
// Since we didn't find a constructor |
256 |
|
// and will die, we may as well print |
257 |
|
// out some useful debug info for the caller |
258 |
|
|
259 |
|
for(int i=0; i<parameters.length; i++) { |
260 |
|
p("Parameter "+i+" "+parameters[i]+" "+parameters[i].getClass()); |
261 |
|
} |
262 |
|
for(int i=0; i<constructors.length; i++) { |
263 |
|
p("Constructor "+i); |
264 |
|
Class[] ptypes = constructors[i].getParameterTypes(); |
265 |
|
for(int j=0; j<ptypes.length; j++) |
266 |
|
p(" "+ptypes[j]); |
267 |
|
} |
268 |
|
|
269 |
|
Class c = functionClass; |
270 |
|
while(c != null) { |
271 |
|
p("SUP: "+c); |
272 |
|
c = c.getSuperclass(); |
273 |
|
} |
274 |
|
Class[] in = functionClass.getInterfaces(); |
275 |
|
for(int i=0; i<in.length; i++) |
276 |
|
p("IN: "+in[i]); |
277 |
|
|
278 |
|
p("Class of class"); |
279 |
|
c = functionClass.getClass(); |
280 |
|
while(c != null) { |
281 |
|
p("SUP: "+c); |
282 |
|
c = c.getSuperclass(); |
283 |
|
} |
284 |
|
in = functionClass.getClass().getInterfaces(); |
285 |
|
for(int i=0; i<in.length; i++) |
286 |
|
p("IN: "+in[i]); |
287 |
|
|
288 |
|
|
289 |
throw new Error("No constructor found: "+functionClass); |
throw new Error("No constructor found: "+functionClass); |
290 |
} |
} |
291 |
} |
} |