001: package org.drools.decisiontable.parser;
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.util.Properties;
020:
021: import org.drools.StatefulSession;
022: import org.drools.decisiontable.model.DRLOutput;
023:
024: /**
025: * SheetListener for creating rules from a template
026: *
027: * @author <a href="mailto:stevearoonie@gmail.com">Steven Williams</a>
028: */
029: public class ExternalSheetListener implements RuleSheetListener {
030:
031: private int startRow = -1;
032:
033: private boolean tableFinished = false;
034:
035: private Row currentRow;
036:
037: private Column[] columns;
038:
039: private StatefulSession session;
040:
041: private TemplateContainer templateContainer;
042:
043: private int startCol;
044:
045: private Generator generator;
046:
047: public ExternalSheetListener(final int startRow,
048: final int startCol, final String template) {
049: this (startRow, startCol, new DefaultTemplateContainer(template));
050: }
051:
052: public ExternalSheetListener(final int startRow,
053: final int startCol, final TemplateContainer tc) {
054: this (startRow, startCol, tc, new DefaultTemplateRuleBase(tc));
055: }
056:
057: public ExternalSheetListener(final int startRow,
058: final int startCol, final TemplateContainer tc,
059: final TemplateRuleBase rb) {
060: this (startRow, startCol, tc, rb, new DefaultGenerator(tc
061: .getTemplates()));
062: }
063:
064: public ExternalSheetListener(final int startRow,
065: final int startCol, final TemplateContainer tc,
066: final TemplateRuleBase ruleBase, final Generator generator) {
067: this .startRow = startRow - 1;
068: this .startCol = startCol - 1;
069: columns = tc.getColumns();
070: this .templateContainer = tc;
071: session = ruleBase.newStatefulSession();
072: this .generator = generator;
073: session.setGlobal("generator", generator);
074: }
075:
076: public Properties getProperties() {
077: return null;
078: }
079:
080: public org.drools.decisiontable.model.Package getRuleSet() {
081: return null;
082: }
083:
084: public void finishSheet() {
085: if (currentRow != null) {
086: session.insert(currentRow);
087: }
088: session.fireAllRules();
089: session.dispose();
090: }
091:
092: public void newCell(int row, int column, String value,
093: int mergedColStart) {
094: if (currentRow != null && column >= startCol && value != null
095: && value.trim().length() > 0) {
096:
097: int columnIndex = column - startCol;
098: // System.out.println("asserting cell " + row + ", " + column + ": "
099: // + value);
100: //Column col = columns[columnIndex];
101: //Cell cell = new Cell(currentRow, col, value);
102: //currentRow.addCell(cell);
103: Cell cell = currentRow.getCell(columnIndex);
104: cell.setValue(value);
105: session.insert(cell);
106:
107: }
108: }
109:
110: public void newRow(int rowNumber, int columnCount) {
111: if (!tableFinished && rowNumber >= startRow) {
112: if (currentRow != null && currentRow.isEmpty()) {
113: currentRow = null;
114: tableFinished = true;
115: } else {
116: if (currentRow != null)
117: session.insert(currentRow);
118: currentRow = new Row(rowNumber, columns);
119: }
120: }
121: }
122:
123: public void startSheet(String name) {
124:
125: }
126:
127: public String renderDRL() {
128: DRLOutput out = new DRLOutput();
129: out.writeLine(templateContainer.getHeader());
130:
131: out.writeLine(generator.getDrl());
132: // System.err.println(out.getDRL());
133: return out.getDRL();
134: }
135:
136: }
|