KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > qvt > util > RulePrinter


1 package org.objectweb.modfact.qvt.util;
2
3 import java.io.*;
4 import java.util.*;
5
6 import javax.jmi.reflect.*;
7 import javax.jmi.xmi.XmiReader;
8
9 import org.objectweb.modfact.jmi.reflect.RefPackageImpl;
10 import org.objectweb.modfact.jmi.xmi.XmiReaderImpl;
11
12 public class RulePrinter {
13     
14   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
15     String JavaDoc xmiURL = args[0];
16     String JavaDoc outFile = args[1];
17     
18     RefPackage p = RefPackageImpl.getM1Repository(
19       RefPackageImpl.createMetaObject(
20         ClassLoader.getSystemResource(
21             "org/objectweb/modfact/qvt/metamodel_SimpleTRL.xml"
22          ).toString()
23        )
24     );
25     XmiReader r = new XmiReaderImpl();
26     r.read(xmiURL, p);
27     printer.printRules(p, outFile);
28   }
29     
30     
31
32   public static final RulePrinter printer = new RulePrinter(0);
33   
34   int indent;
35
36   RulePrinter(int i) {
37     indent = i;
38   }
39   
40   public void printRules(RefPackage pkg, String JavaDoc outputFileName) throws Exception JavaDoc {
41     FileWriter writer = new FileWriter(outputFileName);
42     Iterator it = pkg.refClass("RulesUnit").refAllOfClass().iterator();
43      while(it.hasNext()) {
44         printRules((RefObject)it.next(), writer);
45      }
46   }
47
48   public void printRules(RefObject unit, Writer writer) throws Exception JavaDoc {
49     writer.write("Unit " +unit.refGetValue("name") +"\n" );
50     writer.write("source " +unit.refGetValue("srcMm") +"\n" );
51     writer.write("target " +unit.refGetValue("targetMm") +"\n" );
52     RefObject[] rules = (RefObject[])
53             ( (Collection)unit.refGetValue("rules") ).toArray(new RefObject[0]);
54     for(int i=0; i<rules.length; i++) {
55       writer.write("\n------------------------\n");
56       writer.write(printRule(rules[i]));
57     }
58     writer.close();
59   }
60
61   public String JavaDoc printRule(RefObject rule) {
62     try {
63       RulePrinter p = new RulePrinter(indent+1);
64       String JavaDoc end = "{"+ p.printStatements( (Collection)rule.refGetValue("statements") )
65                   +"\n" +"}";
66       if( isOfType(rule, "EntryPointRule") ) {
67           return entryPointRule(rule) +end;
68
69       } else if( isOfType(rule, "CalledRule") ) {
70           return calledRule(rule) +end;
71       }
72       return null;
73     } catch(RuntimeException JavaDoc e) {
74       System.err.println("RulePrinter: fail at rule " + rule.refGetValue("name"));
75       throw e;
76     }
77   }
78
79   public String JavaDoc entryPointRule(RefObject rule) {
80     return "EntryPointRule " + rule.refGetValue("name")
81             +( (rule.refGetValue("resultType").equals(""))? ""
82                       : ("\ncreates " +rule.refGetValue("resultType") )
83             );
84   }
85
86   public String JavaDoc calledRule(RefObject rule) {
87     String JavaDoc resultType = (String JavaDoc) rule.refGetValue("resultType");
88     return "Rule " + rule.refGetValue("name") + printArgs((Collection)rule.refGetValue("params"))
89             +( (resultType!=null && resultType.length()>0 )?
90                 "\ncreates " +rule.refGetValue("resultType") : ""
91             )
92             +( (rule.refGetValue("returnVal")==null)? ""
93                       : ("\nreturn " +print((RefObject)rule.refGetValue("returnVal")) )
94             );
95
96   }
97
98   public String JavaDoc printShort(RefObject exp) {
99     if( isOfType(exp ,"IfExp") ) {
100       return "(IfExp) " +print((RefObject)exp.refGetValue("cond") );
101     }
102     return print(exp);
103   }
104
105   public String JavaDoc print(RefObject exp) {
106     if(exp==null) return "";
107     try {
108       if( isOfType(exp ,"ParameterExp") ) {
109         return parameterExp( exp );
110
111       } else if( isOfType(exp ,"IfExp") ) {
112         return ifExp( exp );
113
114       } else if( isOfType(exp ,"ModelReferenceExp") ) {
115         return modelReferenceExp( exp );
116
117       } else if( isOfType(exp ,"LiteralExp") ) {
118         return literalExp( exp );
119
120       } else if(isOfType(exp ,"ContextExp") ) {
121         return contextExp( exp);
122
123       }
124       throw new RuntimeException JavaDoc("unknown exp: " +getTypeName(exp) );
125     } catch(RuntimeException JavaDoc e) {
126       System.err.println("Cannot print " + exp.refMetaObject().refGetValue("name") );
127       throw e;
128     }
129   }
130
131   public String JavaDoc contextExp(RefObject exp) {
132     String JavaDoc begin = print((RefObject)exp.refGetValue("context")) + ".";
133
134     if( isOfType(exp ,"AbstractOperationCallExp") ) {
135       return begin +abstractOperationCallExp(exp);
136
137     } else if( isOfType(exp ,"PropertyCallExp") ) {
138       return begin +propertyCallExp(exp);
139
140     } else if( isOfType(exp ,"PropertyAssignExp") ) {
141       return begin +propertyAssignExp(exp);
142
143     } else if( isOfType(exp ,"CollectionExp") ) {
144       return begin +collectionExp(exp);
145
146     } else if( isOfType(exp ,"IsOfTypeExp") ) {
147         return begin +isOfTypeExp(exp);
148
149     } else if( isOfType(exp ,"CompareExp") ) {
150         return begin +compareExp(exp);
151     }
152     return null;
153   }
154
155   public String JavaDoc abstractOperationCallExp(RefObject exp) {
156     String JavaDoc end = printArgs((Collection)exp.refGetValue("args"));
157     if( isOfType(exp ,"RuleCallExp") ) {
158       return ruleCallExp(exp) +end;
159
160     } else {
161       return operationCallExp(exp) +end;
162     }
163   }
164
165   String JavaDoc printArgs(Collection c) {
166     RefObject[] args = (RefObject[]) c.toArray(new RefObject[0]);
167     String JavaDoc r = "(";
168     for(int i=0; i<args.length; i++) {
169       r = r + ((i==0)? "" : " ,") + print(args[i]);
170     }
171     return r + ")";
172   }
173
174   public String JavaDoc ruleCallExp(RefObject exp) {
175     return (String JavaDoc)((RefObject)exp.refGetValue("rule")).refGetValue("name");
176   }
177
178   public String JavaDoc operationCallExp(RefObject exp) {
179     return (String JavaDoc) exp.refGetValue("opName");
180   }
181
182   public String JavaDoc parameterExp(RefObject exp) {
183     return "#" + exp.refGetValue("name");
184   }
185
186   public String JavaDoc propertyAssignExp(RefObject exp) {
187     return exp.refGetValue("propName") +"=" + print((RefObject)exp.refGetValue("value"));
188   }
189
190   public String JavaDoc collectionExp(RefObject exp) {
191     String JavaDoc op = ( isOfType(exp ,"AddCollectionExp") )? "add" : "remove";
192     return op + "(" + print((RefObject)exp.refGetValue("member")) + ")";
193   }
194
195   public String JavaDoc ifExp(RefObject exp) {
196     if( isOfType(exp ,"IfStatementExp") ) {
197       return ifStatementExp(exp);
198
199     } else if( isOfType(exp ,"IfValueExp") ) {
200       return ifValueExp(exp);
201     }
202     return null;
203   }
204
205   public String JavaDoc ifStatementExp(RefObject exp) {
206     RulePrinter p = new RulePrinter(indent+1);
207     return "if(" +print((RefObject)exp.refGetValue("cond")) +") {"
208                 +p.printStatements((Collection)exp.refGetValue("thenStatements"))
209                 +"\n" +doIndent() +"} else {"
210                 +p.printStatements((Collection)exp.refGetValue("elseStatements"))
211                 +"\n" +doIndent() +"}";
212   }
213
214   public String JavaDoc printStatements(Collection c) {
215     RefObject[] statements = (RefObject[]) c.toArray(new RefObject[0]);
216     String JavaDoc r = "";
217     for(int i=0; i<statements.length; i++) {
218       try {
219         r = r + "\n" + doIndent() + print(statements[i]);
220       } catch(RuntimeException JavaDoc e) {
221         System.err.println("RulePrinter.printStatements: fail at "+ (i+1));
222         throw e;
223       }
224     }
225     return r;
226   }
227
228   public String JavaDoc ifValueExp(RefObject exp) {
229     return "(" +print((RefObject)exp.refGetValue("cond")) +")? "
230             +print((RefObject)exp.refGetValue("thenExp")) +" : " +print((RefObject)exp.refGetValue("elseExp"));
231   }
232
233   public String JavaDoc modelReferenceExp(RefObject exp) {
234     return "allOfType(" + exp.refGetValue("conceptName") +")";
235   }
236
237
238   public String JavaDoc propertyCallExp(RefObject exp) {
239     return (String JavaDoc)exp.refGetValue("propName");
240   }
241
242   public String JavaDoc compareExp(RefObject exp) {
243     return "equals(" + print((RefObject)exp.refGetValue("compareVal")) +")";
244   }
245
246   public String JavaDoc isOfTypeExp(RefObject exp) {
247     return "isOfType(" +exp.refGetValue("type") +")";
248   }
249
250   public String JavaDoc literalExp( RefObject exp) {
251       if( isOfType(exp ,"DoubleLiteral") ) {
252           return "" + exp.refGetValue("val");
253
254       } else if( isOfType(exp ,"IntegerLiteral") ) {
255           return "" + exp.refGetValue("val");
256
257       } else if( isOfType(exp ,"BooleanLiteral") ) {
258           return "" + exp.refGetValue("val");
259
260       } else if( isOfType(exp ,"StringLiteral") ) {
261           return "" + exp.refGetValue("val");
262
263       } else if( isOfType(exp ,"EnumLiteral") ) {
264                   return "enum("+exp.refGetValue("val") +": "
265                   + srcOrTarget((Boolean JavaDoc) exp.refGetValue("isFromMmSrc")) +", "
266                   + exp.refGetValue("type") + ")";
267           }
268       return null;
269   }
270   
271   String JavaDoc srcOrTarget(Boolean JavaDoc isFromMmSrc) {
272     return (isFromMmSrc.equals(Boolean.TRUE))? "src" :"target";
273   }
274
275   public String JavaDoc getTypeName(RefObject o) {
276     return (String JavaDoc) o.refMetaObject().refGetValue("name");
277   }
278   
279   boolean isOfType(RefObject o, String JavaDoc type) {
280      RefObject c = o.refOutermostPackage().refClass(type).refMetaObject();
281      return o.refIsInstanceOf(c ,true);
282   }
283
284   public String JavaDoc doIndent(int indent) {
285           String JavaDoc r = "";
286           for(int i=0; i<indent; i++) {
287             r = r + " ";
288           }
289           return r;
290   }
291
292   private String JavaDoc doIndent() {
293     return doIndent(indent);
294   }
295
296 }
Popular Tags