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: }
|