2 * Copyright 2004-2007 MTBJ, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import java
.io
.FileInputStream
;
20 import java
.io
.FileNotFoundException
;
21 import java
.io
.FileOutputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.io
.OutputStream
;
25 import java
.io
.PrintStream
;
26 import java
.nio
.channels
.FileChannel
;
27 import java
.util
.Iterator
;
28 import java
.util
.List
;
30 import com
.dtrules
.compiler
.cup
.Compiler
;
31 import com
.dtrules
.compiler
.decisiontables
.DTCompiler
;
32 import com
.dtrules
.decisiontables
.RDecisionTable
;
33 import com
.dtrules
.decisiontables
.DTNode
.Coordinate
;
34 import com
.dtrules
.entity
.IREntity
;
35 import com
.dtrules
.infrastructure
.RulesException
;
36 import com
.dtrules
.interpreter
.RName
;
37 import com
.dtrules
.mapping
.MapGenerator
;
38 import com
.dtrules
.session
.EntityFactory
;
39 import com
.dtrules
.session
.ICompiler
;
40 import com
.dtrules
.session
.ICompilerError
;
41 import com
.dtrules
.session
.IRSession
;
42 import com
.dtrules
.session
.RSession
;
43 import com
.dtrules
.session
.RuleSet
;
44 import com
.dtrules
.session
.RulesDirectory
;
46 public class Excel2XML
{
48 private RuleSet ruleSet
;
49 private final String UDTFilename
= "Uncompiled_DecisionTables.xml";
51 String rulesDirectoryXML
;
53 DTCompiler dtcompiler
= null;
56 * Returns the ruleSet being used by this compiler
59 public RuleSet
getRuleSet() {
64 * We assume that the Rule Set provided gives us all the parameters needed
65 * to open a set of Excel Files and convert them into XML files.
68 public Excel2XML(RuleSet ruleSet
) {
69 this.ruleSet
= ruleSet
;
73 * Opens the RulesDirectory for the caller.
74 * @param rulesDirectoryXML
77 public Excel2XML(String path
, String rulesDirectoryXML
, String ruleset
){
79 this.rulesDirectoryXML
= rulesDirectoryXML
;
80 this.ruleset
= ruleset
;
85 * We reset the Rules Directory after we have modified the XML used to
86 * define the Rules Directory.
89 RulesDirectory rd
= new RulesDirectory(path
,rulesDirectoryXML
);
90 this.ruleSet
= rd
.getRuleSet(ruleset
);
94 * Return the Last DecisionTable Compiler used. Null if none exists.
96 * @return Return the Last DecisionTable Compiler used.
98 public DTCompiler
getDTCompiler(){
102 * Converts the RuleSet
105 public void convertRuleset() throws Exception
{
106 ImportRuleSets dt
= new ImportRuleSets();
107 dt
.convertEDD (ruleSet
.getSystemPath()+"/"+ruleSet
.getExcel_edd(), ruleSet
.getFilepath()+ruleSet
.getEDD_XMLName());
108 dt
.convertDecisionTables(ruleSet
.getSystemPath()+"/"+ruleSet
.getExcel_dtfolder(),ruleSet
.getFilepath()+UDTFilename
);
109 copyFile(ruleSet
.getFilepath()+UDTFilename
,ruleSet
.getFilepath()+ruleSet
.getDT_XMLName());
115 * Throws a runtime exception if anything goes wrong.
117 public static void copyFile(String file1
, String file2
) {
118 FileChannel inChannel
= null;
119 FileChannel outChannel
= null;
121 inChannel
= new FileInputStream(file1
).getChannel();
122 outChannel
= new FileOutputStream(file2
).getChannel();
124 // Stupid fix for Windows -- 64Mb - 1024 bytes to avoid a bug.
125 int blksize
= (64 * 1024 * 1024)-1024;
126 long size
= inChannel
.size();
128 while (position
< size
) {
129 position
+= inChannel
.transferTo(position
, blksize
, outChannel
);
133 } catch (IOException e
) {
134 throw new RuntimeException(e
.getMessage());
136 }catch(FileNotFoundException e
) {
137 throw new RuntimeException(e
);
142 * Compiles the RuleSet
143 * @param NumErrorsToReport How many errors to print
144 * @param err The output stream which to print the errors
146 public void compile(int NumErrorsToReport
, PrintStream err
) {
149 IRSession session
= new RSession(ruleSet
);
150 Compiler compiler
= new Compiler(session
);
151 dtcompiler
= new DTCompiler(compiler
);
153 InputStream inDTStream
= new FileInputStream(ruleSet
.getFilepath()+"/"+UDTFilename
);
154 OutputStream outDTStream
= new FileOutputStream(ruleSet
.getFilepath()+"/"+ruleSet
.getDT_XMLName());
156 dtcompiler
.compile(inDTStream
, outDTStream
);
158 RulesDirectory rd
= new RulesDirectory(path
, rulesDirectoryXML
);
159 RuleSet rs
= rd
.getRuleSet(RName
.getRName(ruleset
));
160 EntityFactory ef
= rs
.newSession().getEntityFactory();
161 IREntity dt
= ef
.getDecisiontables();
162 Iterator
<RName
> idt
= ef
.getDecisionTableRNameIterator();
164 while(idt
.hasNext()){
165 RDecisionTable t
= (RDecisionTable
) dt
.get(idt
.next());
166 List
<ICompilerError
> errs
= t
.compile();
167 Coordinate err_RowCol
= t
.validate();
168 if(!t
.isCompiled() || err_RowCol
!=null){
171 if(err_RowCol
!=null){
172 row
= err_RowCol
.getRow();
173 column
= err_RowCol
.getCol();
176 t
.getName().stringValue(),
179 "Decision Table did not compile",
181 "A problem may have been found on row "+row
+" and column "+column
);
185 dtcompiler
.printErrors(err
, NumErrorsToReport
);
186 err
.println("Total Errors Found: "+dtcompiler
.getErrors().size());
188 } catch (Exception e
) {
194 * Generates a default Mapping File. This file is not complete, but
195 * requires adjustments to match XML tags if they differ from the
196 * Attribute names used in the EDD. Also, the initial Entity Stack and
197 * frequence of Entity types must be examined and updated.
199 * @param mapping Tag for the mapping file. Attributes with this tag in the
200 * EDD will be mapped in the generated mapping
201 * @param filename File name for the mapping file generated
204 public void generateMap(String mapping
, String filename
) throws Exception
{
205 RuleSet rs
= getRuleSet();
206 MapGenerator mgen
= new MapGenerator();
207 mgen
.generateMapping(
209 rs
.getFilepath()+rs
.getEDD_XMLName(),
210 rs
.getWorkingdirectory()+"map.xml");