Permalink
Browse files

Scorecards: Refactoring and cleanup of external API. Changes to class…

… access levels; removal of comments; other changes
  • Loading branch information...
1 parent 8186951 commit dda16a1f03c4db4c85de136e269d305d6d92ec27 @vinodkiran vinodkiran committed Sep 11, 2012
@@ -1,24 +0,0 @@
-/*
- * Copyright 2012 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.drools.scorecards;
-
-import org.dmg.pmml_4_1.Scorecard;
-
-public interface EventDataCollector {
-
- public Scorecard getScorecard();
-}
@@ -16,108 +16,131 @@
package org.drools.scorecards;
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.io.StringWriter;
import java.util.List;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
import org.dmg.pmml_4_1.PMML;
import org.drools.scorecards.drl.DeclaredTypesDRLEmitter;
import org.drools.scorecards.drl.ExternalModelDRLEmitter;
import org.drools.scorecards.parser.AbstractScorecardParser;
import org.drools.scorecards.parser.ScorecardParseException;
-import org.drools.scorecards.parser.xls.XLSEventDataCollector;
import org.drools.scorecards.parser.xls.XLSScorecardParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class ScorecardCompiler {
private PMML pmmlDocument = null;
public static final String DEFAULT_SHEET_NAME = "scorecards";
- private EventDataCollector eventDataCollector;
private List<ScorecardError> scorecardErrors;
+ private DrlType drlType;
+ private final static Logger logger = LoggerFactory.getLogger(ScorecardCompiler.class);
- public ScorecardCompiler() {
-
- }
-
- public boolean compileFromExcel(final String classPathResource) {
- return compile(classPathResource, ScorecardFormat.XLS, DEFAULT_SHEET_NAME);
+ public ScorecardCompiler(DrlType drlType) {
+ this.drlType = drlType;
}
- public boolean compileFromExcel(final String classPathResource, final String worksheetName) {
- return compile(classPathResource, ScorecardFormat.XLS, worksheetName);
+ public ScorecardCompiler() {
+ this(DrlType.INTERNAL_DECLARED_TYPES);
}
- public boolean compileFromExcel(final InputStream stream) {
- return compile(stream, ScorecardFormat.XLS, DEFAULT_SHEET_NAME);
+ /* method for use from Guvnor */
+ protected void setPMMLDocument(PMML pmmlDocument){
+ this.pmmlDocument = pmmlDocument;
}
- public boolean compileFromExcel(final InputStream stream, final String worksheetName) {
- return compile(stream, ScorecardFormat.XLS, worksheetName);
+ public boolean compileFromExcel(final String pathToFile) {
+ return compileFromExcel(pathToFile, DEFAULT_SHEET_NAME);
}
- public boolean compile(final String classPathResource, ScorecardFormat format) {
- return compile(classPathResource, format, DEFAULT_SHEET_NAME);
- }
-
- public boolean compile(final InputStream stream, ScorecardFormat format) {
- return compile(stream, format, DEFAULT_SHEET_NAME);
+ public boolean compileFromExcel(final String pathToFile, final String worksheetName) {
+ FileInputStream inputStream = null;
+ BufferedInputStream bufferedInputStream = null;
+ try {
+ inputStream = new FileInputStream(pathToFile);
+ bufferedInputStream = new BufferedInputStream(inputStream);
+ return compileFromExcel(bufferedInputStream, worksheetName);
+ } catch (FileNotFoundException e) {
+ logger.error(e.getMessage(), e);
+ } finally {
+ closeStream(bufferedInputStream);
+ closeStream(inputStream);
+ }
+ return false;
}
- public boolean compile(final String classPathResource, ScorecardFormat format, final String worksheetName) {
- InputStream is = getClass().getResourceAsStream(classPathResource);
- return compile(is, format, worksheetName);
+ public boolean compileFromExcel(final InputStream stream) {
+ return compileFromExcel(stream, DEFAULT_SHEET_NAME);
}
- public EventDataCollector getEventDataCollector() {
- return eventDataCollector;
+ public boolean compileFromExcel(final InputStream stream, final String worksheetName) {
+ try {
+ AbstractScorecardParser parser = new XLSScorecardParser();
+ scorecardErrors = parser.parseFile(stream, worksheetName);
+ if ( scorecardErrors.isEmpty() ) {
+ pmmlDocument = parser.getPMMLDocument();
+ return true;
+ }
+ } catch (ScorecardParseException e) {
+ logger.error(e.getMessage(), e);
+ } finally {
+ closeStream(stream);
+ }
+ return false;
}
public PMML getPMMLDocument() {
return pmmlDocument;
}
- public boolean compile(final InputStream stream, ScorecardFormat format, final String worksheetName) {
- if (format == ScorecardFormat.XLS) {
- AbstractScorecardParser parser = new XLSScorecardParser();
- try {
- this.eventDataCollector = new XLSEventDataCollector();
- scorecardErrors = parser.parseFile(eventDataCollector, stream, worksheetName);
- if ( scorecardErrors.isEmpty() ) {
- pmmlDocument = parser.getPMMLDocument();
- return true;
- }
- } catch (ScorecardParseException e) {
- e.printStackTrace();
- } finally {
- closeStream(stream);
- }
+ public String getPMML(){
+ if (pmmlDocument == null ) {
+ return null;
}
- return false;
+ // create a JAXBContext for the PMML class
+ JAXBContext ctx = null;
+ try {
+ ctx = JAXBContext.newInstance(PMML.class);
+ Marshaller marshaller = ctx.createMarshaller();
+ // the property JAXB_FORMATTED_OUTPUT specifies whether or not the
+ // marshalled XML data is formatted with linefeeds and indentation
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+ // marshal the data in the Java content tree
+ StringWriter stringWriter = new StringWriter();
+ marshaller.marshal(pmmlDocument, stringWriter);
+ return stringWriter.toString();
+ } catch (JAXBException e) {
+ logger.error(e.getMessage(), e);
+ }
+ return null;
}
public String getDRL(){
- return getDRL(DrlType.INTERNAL_DECLARED_TYPES);
- }
-
- public String getDRL(DrlType drlType){
if (pmmlDocument != null) {
if (drlType == DrlType.INTERNAL_DECLARED_TYPES) {
return new DeclaredTypesDRLEmitter().emitDRL(pmmlDocument);
} else if (drlType == DrlType.EXTERNAL_OBJECT_MODEL) {
return new ExternalModelDRLEmitter().emitDRL(pmmlDocument);
}
}
- return null;
+ return null;
}
- public static String convertToDRL(PMML pmml, DrlType drlType){
+ /* convienence method for use from Guvnor*/
+ public static String convertToDRL(PMML pmml, DrlType drlType) {
if (pmml != null) {
- if (drlType == DrlType.INTERNAL_DECLARED_TYPES) {
- return new DeclaredTypesDRLEmitter().emitDRL(pmml);
- } else if (drlType == DrlType.EXTERNAL_OBJECT_MODEL) {
- return new ExternalModelDRLEmitter().emitDRL(pmml);
- }
+ ScorecardCompiler scorecardCompiler = new ScorecardCompiler(drlType);
+ scorecardCompiler.setPMMLDocument(pmml);
+ return scorecardCompiler.getDRL();
}
- return null;
+ return null;
}
public List<ScorecardError> getScorecardParseErrors() {
@@ -130,7 +153,7 @@ private void closeStream(final InputStream stream) {
stream.close();
}
} catch (final Exception e) {
- System.err.print("WARNING: Wasn't able to " + "correctly close stream for scorecard. " + e.getMessage());
+ logger.error(e.getMessage(), e);
}
}
@@ -20,15 +20,11 @@
import java.util.List;
import org.dmg.pmml_4_1.PMML;
-import org.drools.scorecards.EventDataCollector;
import org.drools.scorecards.ScorecardError;
public abstract class AbstractScorecardParser {
- /**
- * Parse an input stream, store the resulting rulebase.
- */
- public abstract List<ScorecardError> parseFile(EventDataCollector eventDataCollector, InputStream inStream, String worksheetName) throws ScorecardParseException;
+ public abstract List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException;
public abstract PMML getPMMLDocument();
}
@@ -28,7 +28,7 @@
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
-public class ExcelScorecardValidator {
+class ExcelScorecardValidator {
private Scorecard scorecard;
private List<ScorecardError> parseErrors;
@@ -16,7 +16,7 @@
package org.drools.scorecards.parser.xls;
-public class MergedCellRange {
+class MergedCellRange {
private int firstRow;
private int firstCol;
@@ -33,17 +33,15 @@
import org.dmg.pmml_4_1.Output;
import org.dmg.pmml_4_1.Scorecard;
import org.drools.core.util.StringUtils;
-import org.drools.scorecards.EventDataCollector;
import org.drools.scorecards.ScorecardError;
import org.drools.scorecards.parser.ScorecardParseException;
import org.drools.scorecards.pmml.PMMLExtensionNames;
import org.drools.scorecards.pmml.ScorecardPMMLUtils;
-public class XLSEventDataCollector implements EventDataCollector {
+class XLSEventDataCollector {
private List<DataExpectation> expectations = new ArrayList<DataExpectation>();
private List<MergedCellRange> cellRangeList;
- //private String worksheetName;
private Scorecard scorecard;
private Characteristics characteristics;
private Characteristic _characteristic; //stateMachine variables
@@ -270,7 +268,6 @@ private void addExpectation(int row, int column, String property, Object ref, St
public void newCell(int currentRowCtr, int currentColCtr, String stringCellValue) throws ScorecardParseException {
setAdditionalExpectation(currentRowCtr, currentColCtr, stringCellValue);
- //System.out.println(currentRowCtr+", "+currentColCtr+" : "+stringCellValue);
fulfillExpectation(currentRowCtr, currentColCtr, stringCellValue, String.class);
}
@@ -16,12 +16,9 @@
package org.drools.scorecards.parser.xls;
-public class XLSKeywords {
+public interface XLSKeywords {
public static final String SCORECARD_NAME = "Scorecard Name";
- //public static final String SCORECARD_TYPE = "Scorecard Type";
- //public static final String SCORECARD_OBJECT = "Object";
- //public static final String SCORECARD_BOUND_VARIABLE = "Bound Variable";
public static final String SCORECARD_RESULTANT_SCORE_CLASS = "Resultant Score Class";
public static final String SCORECARD_RESULTANT_SCORE_FIELD = "Resultant Score Field";
public static final String SCORECARD_CHARACTERISTIC_EXTERNAL_CLASS = "Full Class Name";
@@ -36,8 +33,6 @@
public static final String SCORECARD_CHARACTERISTIC_NAME = "Name";
public static final String SCORECARD_CHARACTERISTIC_DATATYPE = "Data Type";
public static final String SCORECARD_CHARACTERISTIC_BASELINE_SCORE = "Baseline Score";
- //public static final String SCORECARD_GROUP_REASONCODE = "Reason Code";
- //public static final String SCORECARD_CHARACTERISTIC_WEIGHT = "Weight";
public static final String SCORECARD_CHARACTERISTIC_BIN_ATTRIBUTE = "Characteristic";
public static final String SCORECARD_CHARACTERISTIC_BIN_OPERATOR = "Operator";
@@ -29,7 +29,6 @@
import org.apache.poi.ss.util.CellRangeAddress;
import org.dmg.pmml_4_1.PMML;
import org.dmg.pmml_4_1.Scorecard;
-import org.drools.scorecards.EventDataCollector;
import org.drools.scorecards.ScorecardError;
import org.drools.scorecards.parser.AbstractScorecardParser;
import org.drools.scorecards.parser.ScorecardParseException;
@@ -44,9 +43,9 @@
private HSSFSheet currentWorksheet;
@Override
- public List<ScorecardError> parseFile(EventDataCollector eventDataCollector, InputStream inStream, String worksheetName) throws ScorecardParseException {
+ public List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException {
try {
- excelDataCollector = (XLSEventDataCollector) eventDataCollector;
+ excelDataCollector = new XLSEventDataCollector();
excelDataCollector.setParser(this);
HSSFWorkbook workbook = new HSSFWorkbook(inStream);
HSSFSheet worksheet = workbook.getSheet(worksheetName);
@@ -93,7 +92,6 @@ private void processSheet(HSSFSheet worksheet) throws ScorecardParseException {
}
break;
case Cell.CELL_TYPE_BOOLEAN:
- //System.out.println(currentColCtr+" "+currentRowCtr+" "+Boolean.valueOf(cell.getBooleanCellValue()).toString());
excelDataCollector.newCell(currentRowCtr, currentColCtr, Boolean.valueOf(cell.getBooleanCellValue()).toString());
break;
case Cell.CELL_TYPE_FORMULA:
@@ -23,8 +23,6 @@
public static final String SCORECARD_CELL_REF = "cellRef";
public static final String SCORECARD_RESULTANT_SCORE_FIELD = "final";
public static final String SCORECARD_RESULTANT_SCORE_CLASS = "externalClass";
-
- //public static final String SCORECARD_BOUND_VAR_NAME = "boundVariableName";
public static final String SCORECARD_IMPORTS = "importsFromDelimitedString";
public static final String CHARACTERTISTIC_EXTERNAL_CLASS = "externalClass";
@@ -14,14 +14,15 @@
import org.junit.Test;
import static junit.framework.Assert.*;
+import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES;
public class DrlFromPMMLTest {
private static String drl;
@Before
public void setUp() throws Exception {
- ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
+ ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls")) ) {
PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
assertNotNull(pmmlDocument);
Oops, something went wrong.

0 comments on commit dda16a1

Please sign in to comment.