1 package org.objectweb.modfact.qvt.engine; 2 3 import javax.jmi.reflect.*; 4 import org.objectweb.modfact.qvt.util.RulePrinter; 5 import java.util.*; 6 7 class RuleBinding { 8 9 static boolean verbose = false; 10 11 Hashtable bindingTable; 12 13 RefObject rule; 14 Hashtable args; 15 RefPackage src; 16 RefPackage target; 17 18 int indent; 19 20 Object returnVal = null; 21 ExpEvaluator evaluator; 22 String key; 23 24 RuleBinding( 25 Hashtable bindingTable 26 ,RefObject rule, RefPackage src, RefPackage target 27 ,Hashtable args, int indent) { 28 29 this.bindingTable = bindingTable; 30 this.rule = rule; 31 this.args = args; 32 this.src = src; 33 this.target = target; 34 this.indent = indent; 35 } 36 37 boolean isApplied() { 39 key = key + "rule=" + rule.refGetValue("name") 40 +",context=" +getId(args.get("context")); 41 if(rule.refMetaObject().refGetValue("name").equals("CalledRule")) { 42 Iterator it = ((List)rule.refGetValue("params")).iterator(); 43 while(it.hasNext()) { 44 String argName = (String )((RefObject)it.next()).refGetValue("name"); 45 Object arg = args.get(argName); 46 key = key +"," + argName +"="+ getId(arg); 47 } 48 } 49 if(!bindingTable.containsKey(key)) { 50 bindingTable.put(key,this); 51 return false; 52 } 53 return true; 54 } 55 56 String getId(Object o) { 57 if(o==null) return "null"; 58 if(o instanceof RefObject) 59 return ((RefObject)o).refMofId(); 60 return "" +o; 61 } 62 63 Object execute() throws Exception { 64 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent) +"rule binding: " + rule.refGetValue("name")); 65 66 if(isApplied()) { 67 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent+1) +"already applied to this context ->skip"); 68 RuleBinding b = (RuleBinding)bindingTable.get(key); 69 return b.returnVal; 70 } 71 72 Object result = createResult(rule); 73 74 RefObject[] statements = (RefObject[]) ((Collection)rule.refGetValue("statements")).toArray(new RefObject[0]); 75 76 evaluator = new ExpEvaluator(this); 77 78 for(int i = 0; i<statements.length; i++) { 79 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent+1) 80 +(i+1) + ": " +RulePrinter.printer.printShort(statements[i]) ); 82 try { 83 evaluator.eval(statements[i]); 84 } catch(Exception e) { 85 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent) 86 +"ERROR at " +rule.refGetValue("name") +" " +(i+1) 87 +": " + RulePrinter.printer.printShort(statements[i]) ); 88 throw e; 89 } 90 } 91 RefObject returnValExp = (Reflection.isOfType(rule,"CalledRule"))? (RefObject)rule.refGetValue("returnVal") : null; 92 93 if(returnValExp!=null) { 94 try { 95 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent+1) 96 +"return: " + RulePrinter.printer.printShort(returnValExp) ); 98 returnVal = evaluator.eval(returnValExp); 99 } catch(Exception e) { 100 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent) 101 +"ERROR at " +rule.refGetValue("name") 102 +": could not evaluate returned value" ); 103 throw e; 104 } 105 } 106 107 if(verbose) System.err.println( RulePrinter.printer.doIndent(indent) 108 +"end rule: " +rule.refGetValue("name") 109 ); 110 return returnVal; 111 } 112 113 114 Object createResult(RefObject rule) throws Exception { 116 String resultString = (String )rule.refGetValue("resultType"); 117 if(resultString==null || resultString.equals("")) { 118 return null; 119 } 120 Object result = Reflection.createObject( 121 target 122 , resultString 123 ); 124 args.put("result",result); 125 return result; 126 } 127 128 } 129 | Popular Tags |