28 |
import org.fenfire.util.*; |
import org.fenfire.util.*; |
29 |
import org.fenfire.swamp.*; |
import org.fenfire.swamp.*; |
30 |
import java.lang.reflect.*; |
import java.lang.reflect.*; |
31 |
|
import org.python.core.*; |
32 |
|
|
33 |
/** An implementation of Functional that |
/** An implementation of Functional that |
34 |
* just uses direct calls. |
* just uses direct calls. |
35 |
*/ |
*/ |
36 |
public class DirectFunctional extends Functional { |
public class DirectFunctional extends Functional { |
37 |
|
public static boolean dbg = false; |
38 |
|
private static void p(String s) { System.out.println(s); } |
39 |
|
|
40 |
protected final ConstGraph constGraph; |
protected final ConstGraph constGraph; |
41 |
|
|
42 |
public DirectFunctional(ConstGraph constGraph) { |
public DirectFunctional(ConstGraph constGraph) { |
84 |
return o; |
return o; |
85 |
} |
} |
86 |
|
|
87 |
|
/** Map a parameter (that may be a wrapped FunctionInstance) to a real |
88 |
|
* object for the parameter list of the Function class. |
89 |
|
*/ |
90 |
|
protected PyObject mapParameterToFunction_Jython(PyObject o) { |
91 |
|
if(o instanceof PyJavaInstance) { |
92 |
|
Object dfni = o.__tojava__(DirectFunctionInstance.class); |
93 |
|
if(dfni != Py.NoConversion) |
94 |
|
return Py.java2py(((DirectFunctionInstance)dfni).f); |
95 |
|
|
96 |
|
Object dnfni = o.__tojava__(DirectNodeFunctionInstance.class); |
97 |
|
if(dnfni != Py.NoConversion) |
98 |
|
return Py.java2py(((DirectNodeFunctionInstance)dnfni).func); |
99 |
|
|
100 |
|
if((o.__tojava__(Function.class) != Py.NoConversion) || |
101 |
|
(o.__tojava__(NodeFunction.class) != Py.NoConversion)) |
102 |
|
throw new Error( |
103 |
|
"Can't use functions as parameters to Functional. "+o); |
104 |
|
|
105 |
|
} |
106 |
|
return o; |
107 |
|
} |
108 |
|
|
109 |
protected void instError(Exception e, Constructor constructor, Object[] params) { |
protected void instError(Exception e, Constructor constructor, Object[] params) { |
110 |
e.printStackTrace(); |
e.printStackTrace(); |
111 |
Class[] types = constructor.getParameterTypes(); |
Class[] types = constructor.getParameterTypes(); |
151 |
return new DirectNodeFunctionInstance(f); |
return new DirectNodeFunctionInstance(f); |
152 |
} |
} |
153 |
} |
} |
154 |
|
|
155 |
|
public FunctionInstance createFunctionInstance_Jython( |
156 |
|
Object id, |
157 |
|
org.python.core.PyClass functionClass, |
158 |
|
org.python.core.PyObject[] parameters0 |
159 |
|
) { |
160 |
|
PyObject[] parameters = new PyObject[parameters0.length]; |
161 |
|
System.arraycopy(parameters0, 0, parameters, 0, parameters0.length); |
162 |
|
|
163 |
|
for(int i=0; i<parameters.length; i++) |
164 |
|
parameters[i] = mapParameterToFunction_Jython(parameters[i]); |
165 |
|
|
166 |
|
PyObject instance = functionClass.__call__(parameters, |
167 |
|
new String[] {}); |
168 |
|
|
169 |
|
if(dbg) p("Instance: "+instance+" "+ |
170 |
|
instance.__tojava__(Function.class)+" "+ |
171 |
|
instance.__tojava__(NodeFunction.class)); |
172 |
|
|
173 |
|
Object f = instance.__tojava__(Function.class); |
174 |
|
if(f != Py.NoConversion) |
175 |
|
return new DirectFunctionInstance((Function)f); |
176 |
|
Object nf = instance.__tojava__(NodeFunction.class); |
177 |
|
if(nf != Py.NoConversion) |
178 |
|
return new DirectNodeFunctionInstance((NodeFunction)nf); |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
/* |
183 |
|
org.python.core.PyTuple bases = functionClass.__bases__; |
184 |
|
for(int i=0; i<bases.list.length; i++) { |
185 |
|
Class c = (Class)bases.list[i].__tojava__(java.lang.Class.class); |
186 |
|
p("BASE: "+bases.list[i]+" "+bases.list[i].getClass()+" "+ |
187 |
|
c+" "+c.getClass()); |
188 |
|
|
189 |
|
if(Function.class.isAssignableFrom(c)) { |
190 |
|
} else if(NodeFunction.class.isAssignableFrom(c)) { |
191 |
|
} |
192 |
|
} |
193 |
|
*/ |
194 |
|
throw new Error("No suitable base class! "+instance+" "+ |
195 |
|
instance.getClass()); |
196 |
|
} |
197 |
|
|
198 |
} |
} |