Source Code Cross Referenced for SpreadsheetCompiler.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » decisiontable » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Rule Engine » drolls Rule Engine » org.drools.decisiontable 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.drools.decisiontable;
002:
003:        /*
004:         * Copyright 2005 JBoss Inc
005:         * 
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *      http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */
018:
019:        import java.io.InputStream;
020:        import java.util.ArrayList;
021:        import java.util.HashMap;
022:        import java.util.List;
023:        import java.util.Map;
024:
025:        import org.drools.decisiontable.model.DRLOutput;
026:        import org.drools.decisiontable.model.Package;
027:        import org.drools.decisiontable.parser.DecisionTableParser;
028:        import org.drools.decisiontable.parser.DefaultRuleSheetListener;
029:        import org.drools.decisiontable.parser.RuleSheetListener;
030:        import org.drools.decisiontable.parser.xls.ExcelParser;
031:
032:        /**
033:         * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale </a>
034:         * 
035:         * This class handles the input XLS and CSV and extracts the rule DRL, ready for
036:         * pumping into drools.
037:         */
038:        public class SpreadsheetCompiler {
039:
040:            /**
041:             * Generates DRL from the input stream containing the spreadsheet.
042:             * 
043:             * @param pkg 
044:             *            Uses this package definition as the default definitions for the spreadsheet
045:             * @param xlsStream
046:             *            The stream to the spreadsheet. Uses the first worksheet found
047:             *            for the decision tables, ignores others.
048:             * @return DRL xml, ready for use in drools.
049:             */
050:            public String compile(final org.drools.rule.Package pkg,
051:                    final InputStream xlsStream, final InputType type) {
052:                return compile(xlsStream, type, new DefaultRuleSheetListener(
053:                        pkg));
054:            }
055:
056:            /**
057:             * Generates DRL from the input stream containing the spreadsheet.
058:             * 
059:             * @param xlsStream
060:             *            The stream to the spreadsheet. Uses the first worksheet found
061:             *            for the decision tables, ignores others.
062:             * @return DRL xml, ready for use in drools.
063:             */
064:            public String compile(final InputStream xlsStream,
065:                    final InputType type) {
066:                return compile(xlsStream, type, new DefaultRuleSheetListener());
067:            }
068:
069:            /**
070:             * Generates DRL from the input stream containing the spreadsheet.
071:             *
072:             * @param xlsStream
073:             *            The stream to the spreadsheet. Uses the first worksheet found
074:             *            for the decision tables, ignores others.
075:             * @param type
076:             *            The type of the file - InputType.CSV or InputType.XLS
077:             * @param listener
078:             *            
079:             * @return DRL xml, ready for use in drools.
080:             */
081:            public String compile(final InputStream xlsStream,
082:                    final InputType type, final RuleSheetListener listener) {
083:                final DecisionTableParser parser = type.createParser(listener);
084:                parser.parseFile(xlsStream);
085:                final Package rulePackage = listener.getRuleSet();
086:                final DRLOutput out = new DRLOutput();
087:                rulePackage.renderDRL(out);
088:                return out.getDRL();
089:            }
090:
091:            /**
092:             * Convenience implementation, taking rules from the classpath. It is
093:             * recommended to use the stream version, as you can then change rules
094:             * dynamically. (that is a lot of the benefit of rule engines !).
095:             * 
096:             * @param classPathResource
097:             *            full class path to the spreadsheet you wish to convert to DRL.
098:             *            Uses the first worksheet for the decision tables.
099:             * @return DRL.
100:             */
101:            public String compile(final String classPathResource,
102:                    final InputType inputType) {
103:                final InputStream stream = this .getClass().getResourceAsStream(
104:                        classPathResource);
105:                try {
106:                    final String drl = compile(stream, inputType);
107:                    return drl;
108:                } finally {
109:                    closeStream(stream);
110:                }
111:            }
112:
113:            /**
114:             * Looks for a named worksheet to find the decision tables on. Only works
115:             * with XLS format spreadsheets (as they have multiple worksheets).
116:             * 
117:             * @param stream
118:             *            The stream of the decision tables (spreadsheet) IN XLS format !!
119:             * @param worksheetName
120:             *            The name of the worksheet that the decision tables live on.
121:             * @return DRL, ready to go.
122:             */
123:            public String compile(final InputStream stream,
124:                    final String worksheetName) {
125:                final RuleSheetListener listener = getRuleSheetListener(stream,
126:                        worksheetName);
127:                final Package rulePackage = listener.getRuleSet();
128:                final DRLOutput out = new DRLOutput();
129:                rulePackage.renderDRL(out);
130:                return out.getDRL();
131:            }
132:
133:            private RuleSheetListener getRuleSheetListener(
134:                    final InputStream stream, final String worksheetName) {
135:                final RuleSheetListener listener = new DefaultRuleSheetListener();
136:                final Map sheetListeners = new HashMap();
137:                final List listeners = new ArrayList();
138:                listeners.add(listener);
139:                sheetListeners.put(worksheetName, listeners);
140:                final ExcelParser parser = new ExcelParser(sheetListeners);
141:                parser.parseFile(stream);
142:                return listener;
143:            }
144:
145:            private void closeStream(final InputStream stream) {
146:                try {
147:                    stream.close();
148:                } catch (final Exception e) {
149:                    System.err.print("WARNING: Wasn't able to "
150:                            + "correctly close stream for decision table. "
151:                            + e.getMessage());
152:                }
153:            }
154:
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.