KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > scripting > PythonEvaluator


1 package org.enhydra.shark.scripting;
2
3 import org.enhydra.shark.api.RootException;
4 import org.enhydra.shark.api.SharkTransaction;
5 import org.enhydra.shark.api.internal.scripting.Evaluator;
6 import org.enhydra.shark.api.internal.working.CallbackUtilities;
7
8 import java.util.*;
9
10
11 import org.python.core.*;
12 import org.python.util.PythonInterpreter;
13
14
15 /**
16  * Implementation of the Evaluator interface which evaluates the conditions body
17  * as a Python expression.
18  */

19 public class PythonEvaluator implements Evaluator {
20
21    private static final String JavaDoc LOG_CHANNEL="Scripting";
22
23    private CallbackUtilities cus;
24    public void configure (CallbackUtilities cus) throws RootException {
25       this.cus=cus;
26    }
27
28
29    /**
30     * Evaluate the condition using python as the expression language. This
31     * method returns true if the condition is satisfied.
32     * @param t a SharkTransaction
33     * @param condition The condition
34     * @param context The context
35     * @return True if the condition is true
36     */

37    public boolean evaluateCondition (SharkTransaction t,String JavaDoc condition,Map context) throws RootException {
38       if(condition==null || condition.trim().length()==0){
39          return true;
40       }
41
42       java.lang.Object JavaDoc eval=evaluateExpression(t,condition,context,java.lang.Boolean JavaDoc.class);
43       try {
44          //return new Boolean((((Integer)eval).intValue()==1) ? true:false).booleanValue();
45
return ((Boolean JavaDoc)eval).booleanValue();
46       } catch (Exception JavaDoc ex) {
47          cus.error(LOG_CHANNEL,"PythonEvaluator -> The result of condition "+condition+" cannot be converted to boolean");
48          cus.error("PythonEvaluator -> The result of condition "+condition+" cannot be converted to boolean");
49          throw new RootException("Result cannot be converted to boolean",ex);
50       }
51
52    }
53
54    /**
55     * Evaluates the given expression.
56     *
57     * @param t a SharkTransaction
58     * @param expr The expression String
59     * @param context The workflow context
60     * @param resultClass Returned object should be the instance of this Java class
61     * @return True if the expression evaluates to true
62     */

63    public java.lang.Object JavaDoc evaluateExpression (SharkTransaction t,String JavaDoc expr,Map context,Class JavaDoc resultClass) throws RootException {
64       PythonInterpreter interpreter = new PythonInterpreter();
65
66       PyObject eval;
67       try {
68          prepareContext(interpreter,context);
69          //System.err.println("Evaluating python condition:" + expr);
70
eval = interpreter.eval(expr);
71          //System.err.println("Evaluated to -- " + eval);
72
java.lang.Object JavaDoc result=null;
73          if (resultClass!=null) {
74             result=eval.__tojava__(resultClass);
75          } else {
76             result=eval.__tojava__(java.lang.Object JavaDoc.class);
77          }
78          //System.err.println("Eval obj class is "+result.getClass().getName());
79
//System.err.println("Result is "+result);
80
cus.debug(LOG_CHANNEL,"PythonScriptEvaluator -> Python scrip expression "+expr+" is evaluated to "+eval);
81
82          return result;
83
84       } catch (Exception JavaDoc ee) {
85          cus.error(LOG_CHANNEL,"PythonEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+ee.getMessage());
86          cus.error("PythonEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+ee.getMessage());
87          throw new RootException("Result cannot be evaluated",ee);
88       }
89    }
90
91    private void prepareContext (PythonInterpreter interpreter,Map context) throws Exception JavaDoc {
92       Iterator iter = context.entrySet().iterator();
93       while(iter.hasNext()){
94          Map.Entry me=(Map.Entry)iter.next();
95          String JavaDoc key = me.getKey().toString();
96          java.lang.Object JavaDoc value = me.getValue();
97          //System.err.println("Value for key "+key+" is "+value);
98
interpreter.set(key,value);
99       }
100    }
101
102 }
103
Popular Tags