Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #10 from vinodkiran/master

External API clean-up;removal of sys-outs; addition of loggers and other changes
  • Loading branch information...
commit 96d5bcdd61ae86c5693083f3ebb62054bad094f4 2 parents d5c1da9 + dda16a1
Davide Sottara sotty authored

Showing 15 changed files with 119 additions and 160 deletions. Show diff stats Hide diff stats

  1. +0 24 drools-scorecards/src/main/java/org/drools/scorecards/EventDataCollector.java
  2. +77 54 drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java
  3. +1 5 drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java
  4. +1 1  drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java
  5. +1 1  drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/MergedCellRange.java
  6. +1 4 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java
  7. +1 6 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java
  8. +2 4 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java
  9. +0 2  drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java
  10. +2 1  drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java
  11. +8 18 drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java
  12. +6 16 drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java
  13. +5 3 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardParseErrorsTest.java
  14. +14 21 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java
  15. BIN  drools-scorecards/src/test/resources/scoremodel_externalmodel.xls
24 drools-scorecards/src/main/java/org/drools/scorecards/EventDataCollector.java
... ... @@ -1,24 +0,0 @@
1   -/*
2   - * Copyright 2012 JBoss Inc
3   - *
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
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
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.
15   - */
16   -
17   -package org.drools.scorecards;
18   -
19   -import org.dmg.pmml_4_1.Scorecard;
20   -
21   -public interface EventDataCollector {
22   -
23   - public Scorecard getScorecard();
24   -}
131 drools-scorecards/src/main/java/org/drools/scorecards/ScorecardCompiler.java
@@ -16,89 +16,113 @@
16 16
17 17 package org.drools.scorecards;
18 18
  19 +import java.io.BufferedInputStream;
  20 +import java.io.FileInputStream;
  21 +import java.io.FileNotFoundException;
19 22 import java.io.InputStream;
  23 +import java.io.StringWriter;
20 24 import java.util.List;
  25 +import javax.xml.bind.JAXBContext;
  26 +import javax.xml.bind.JAXBException;
  27 +import javax.xml.bind.Marshaller;
21 28
22 29 import org.dmg.pmml_4_1.PMML;
23 30 import org.drools.scorecards.drl.DeclaredTypesDRLEmitter;
24 31 import org.drools.scorecards.drl.ExternalModelDRLEmitter;
25 32 import org.drools.scorecards.parser.AbstractScorecardParser;
26 33 import org.drools.scorecards.parser.ScorecardParseException;
27   -import org.drools.scorecards.parser.xls.XLSEventDataCollector;
28 34 import org.drools.scorecards.parser.xls.XLSScorecardParser;
  35 +import org.slf4j.Logger;
  36 +import org.slf4j.LoggerFactory;
29 37
30 38 public class ScorecardCompiler {
31 39
32 40 private PMML pmmlDocument = null;
33 41 public static final String DEFAULT_SHEET_NAME = "scorecards";
34   - private EventDataCollector eventDataCollector;
35 42 private List<ScorecardError> scorecardErrors;
  43 + private DrlType drlType;
  44 + private final static Logger logger = LoggerFactory.getLogger(ScorecardCompiler.class);
36 45
37   - public ScorecardCompiler() {
38   -
39   - }
40   -
41   - public boolean compileFromExcel(final String classPathResource) {
42   - return compile(classPathResource, ScorecardFormat.XLS, DEFAULT_SHEET_NAME);
  46 + public ScorecardCompiler(DrlType drlType) {
  47 + this.drlType = drlType;
43 48 }
44 49
45   - public boolean compileFromExcel(final String classPathResource, final String worksheetName) {
46   - return compile(classPathResource, ScorecardFormat.XLS, worksheetName);
  50 + public ScorecardCompiler() {
  51 + this(DrlType.INTERNAL_DECLARED_TYPES);
47 52 }
48 53
49   - public boolean compileFromExcel(final InputStream stream) {
50   - return compile(stream, ScorecardFormat.XLS, DEFAULT_SHEET_NAME);
  54 + /* method for use from Guvnor */
  55 + protected void setPMMLDocument(PMML pmmlDocument){
  56 + this.pmmlDocument = pmmlDocument;
51 57 }
52 58
53   - public boolean compileFromExcel(final InputStream stream, final String worksheetName) {
54   - return compile(stream, ScorecardFormat.XLS, worksheetName);
  59 + public boolean compileFromExcel(final String pathToFile) {
  60 + return compileFromExcel(pathToFile, DEFAULT_SHEET_NAME);
55 61 }
56 62
57   - public boolean compile(final String classPathResource, ScorecardFormat format) {
58   - return compile(classPathResource, format, DEFAULT_SHEET_NAME);
59   - }
60   -
61   - public boolean compile(final InputStream stream, ScorecardFormat format) {
62   - return compile(stream, format, DEFAULT_SHEET_NAME);
  63 + public boolean compileFromExcel(final String pathToFile, final String worksheetName) {
  64 + FileInputStream inputStream = null;
  65 + BufferedInputStream bufferedInputStream = null;
  66 + try {
  67 + inputStream = new FileInputStream(pathToFile);
  68 + bufferedInputStream = new BufferedInputStream(inputStream);
  69 + return compileFromExcel(bufferedInputStream, worksheetName);
  70 + } catch (FileNotFoundException e) {
  71 + logger.error(e.getMessage(), e);
  72 + } finally {
  73 + closeStream(bufferedInputStream);
  74 + closeStream(inputStream);
  75 + }
  76 + return false;
63 77 }
64 78
65   - public boolean compile(final String classPathResource, ScorecardFormat format, final String worksheetName) {
66   - InputStream is = getClass().getResourceAsStream(classPathResource);
67   - return compile(is, format, worksheetName);
  79 + public boolean compileFromExcel(final InputStream stream) {
  80 + return compileFromExcel(stream, DEFAULT_SHEET_NAME);
68 81 }
69 82
70   - public EventDataCollector getEventDataCollector() {
71   - return eventDataCollector;
  83 + public boolean compileFromExcel(final InputStream stream, final String worksheetName) {
  84 + try {
  85 + AbstractScorecardParser parser = new XLSScorecardParser();
  86 + scorecardErrors = parser.parseFile(stream, worksheetName);
  87 + if ( scorecardErrors.isEmpty() ) {
  88 + pmmlDocument = parser.getPMMLDocument();
  89 + return true;
  90 + }
  91 + } catch (ScorecardParseException e) {
  92 + logger.error(e.getMessage(), e);
  93 + } finally {
  94 + closeStream(stream);
  95 + }
  96 + return false;
72 97 }
73 98
74 99 public PMML getPMMLDocument() {
75 100 return pmmlDocument;
76 101 }
77 102
78   - public boolean compile(final InputStream stream, ScorecardFormat format, final String worksheetName) {
79   - if (format == ScorecardFormat.XLS) {
80   - AbstractScorecardParser parser = new XLSScorecardParser();
81   - try {
82   - this.eventDataCollector = new XLSEventDataCollector();
83   - scorecardErrors = parser.parseFile(eventDataCollector, stream, worksheetName);
84   - if ( scorecardErrors.isEmpty() ) {
85   - pmmlDocument = parser.getPMMLDocument();
86   - return true;
87   - }
88   - } catch (ScorecardParseException e) {
89   - e.printStackTrace();
90   - } finally {
91   - closeStream(stream);
92   - }
  103 + public String getPMML(){
  104 + if (pmmlDocument == null ) {
  105 + return null;
93 106 }
94   - return false;
  107 + // create a JAXBContext for the PMML class
  108 + JAXBContext ctx = null;
  109 + try {
  110 + ctx = JAXBContext.newInstance(PMML.class);
  111 + Marshaller marshaller = ctx.createMarshaller();
  112 + // the property JAXB_FORMATTED_OUTPUT specifies whether or not the
  113 + // marshalled XML data is formatted with linefeeds and indentation
  114 + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  115 + // marshal the data in the Java content tree
  116 + StringWriter stringWriter = new StringWriter();
  117 + marshaller.marshal(pmmlDocument, stringWriter);
  118 + return stringWriter.toString();
  119 + } catch (JAXBException e) {
  120 + logger.error(e.getMessage(), e);
  121 + }
  122 + return null;
95 123 }
96 124
97 125 public String getDRL(){
98   - return getDRL(DrlType.INTERNAL_DECLARED_TYPES);
99   - }
100   -
101   - public String getDRL(DrlType drlType){
102 126 if (pmmlDocument != null) {
103 127 if (drlType == DrlType.INTERNAL_DECLARED_TYPES) {
104 128 return new DeclaredTypesDRLEmitter().emitDRL(pmmlDocument);
@@ -106,18 +130,17 @@ public String getDRL(DrlType drlType){
106 130 return new ExternalModelDRLEmitter().emitDRL(pmmlDocument);
107 131 }
108 132 }
109   - return null;
  133 + return null;
110 134 }
111 135
112   - public static String convertToDRL(PMML pmml, DrlType drlType){
  136 + /* convienence method for use from Guvnor*/
  137 + public static String convertToDRL(PMML pmml, DrlType drlType) {
113 138 if (pmml != null) {
114   - if (drlType == DrlType.INTERNAL_DECLARED_TYPES) {
115   - return new DeclaredTypesDRLEmitter().emitDRL(pmml);
116   - } else if (drlType == DrlType.EXTERNAL_OBJECT_MODEL) {
117   - return new ExternalModelDRLEmitter().emitDRL(pmml);
118   - }
  139 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(drlType);
  140 + scorecardCompiler.setPMMLDocument(pmml);
  141 + return scorecardCompiler.getDRL();
119 142 }
120   - return null;
  143 + return null;
121 144 }
122 145
123 146 public List<ScorecardError> getScorecardParseErrors() {
@@ -130,7 +153,7 @@ private void closeStream(final InputStream stream) {
130 153 stream.close();
131 154 }
132 155 } catch (final Exception e) {
133   - System.err.print("WARNING: Wasn't able to " + "correctly close stream for scorecard. " + e.getMessage());
  156 + logger.error(e.getMessage(), e);
134 157 }
135 158 }
136 159
6 drools-scorecards/src/main/java/org/drools/scorecards/parser/AbstractScorecardParser.java
@@ -20,15 +20,11 @@
20 20 import java.util.List;
21 21
22 22 import org.dmg.pmml_4_1.PMML;
23   -import org.drools.scorecards.EventDataCollector;
24 23 import org.drools.scorecards.ScorecardError;
25 24
26 25 public abstract class AbstractScorecardParser {
27 26
28   - /**
29   - * Parse an input stream, store the resulting rulebase.
30   - */
31   - public abstract List<ScorecardError> parseFile(EventDataCollector eventDataCollector, InputStream inStream, String worksheetName) throws ScorecardParseException;
  27 + public abstract List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException;
32 28
33 29 public abstract PMML getPMMLDocument();
34 30 }
2  drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/ExcelScorecardValidator.java
@@ -28,7 +28,7 @@
28 28 import org.drools.scorecards.pmml.PMMLExtensionNames;
29 29 import org.drools.scorecards.pmml.ScorecardPMMLUtils;
30 30
31   -public class ExcelScorecardValidator {
  31 +class ExcelScorecardValidator {
32 32
33 33 private Scorecard scorecard;
34 34 private List<ScorecardError> parseErrors;
2  drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/MergedCellRange.java
@@ -16,7 +16,7 @@
16 16
17 17 package org.drools.scorecards.parser.xls;
18 18
19   -public class MergedCellRange {
  19 +class MergedCellRange {
20 20
21 21 private int firstRow;
22 22 private int firstCol;
5 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSEventDataCollector.java
@@ -33,17 +33,15 @@
33 33 import org.dmg.pmml_4_1.Output;
34 34 import org.dmg.pmml_4_1.Scorecard;
35 35 import org.drools.core.util.StringUtils;
36   -import org.drools.scorecards.EventDataCollector;
37 36 import org.drools.scorecards.ScorecardError;
38 37 import org.drools.scorecards.parser.ScorecardParseException;
39 38 import org.drools.scorecards.pmml.PMMLExtensionNames;
40 39 import org.drools.scorecards.pmml.ScorecardPMMLUtils;
41 40
42   -public class XLSEventDataCollector implements EventDataCollector {
  41 +class XLSEventDataCollector {
43 42
44 43 private List<DataExpectation> expectations = new ArrayList<DataExpectation>();
45 44 private List<MergedCellRange> cellRangeList;
46   - //private String worksheetName;
47 45 private Scorecard scorecard;
48 46 private Characteristics characteristics;
49 47 private Characteristic _characteristic; //stateMachine variables
@@ -270,7 +268,6 @@ private void addExpectation(int row, int column, String property, Object ref, St
270 268
271 269 public void newCell(int currentRowCtr, int currentColCtr, String stringCellValue) throws ScorecardParseException {
272 270 setAdditionalExpectation(currentRowCtr, currentColCtr, stringCellValue);
273   - //System.out.println(currentRowCtr+", "+currentColCtr+" : "+stringCellValue);
274 271 fulfillExpectation(currentRowCtr, currentColCtr, stringCellValue, String.class);
275 272 }
276 273
7 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSKeywords.java
@@ -16,12 +16,9 @@
16 16
17 17 package org.drools.scorecards.parser.xls;
18 18
19   -public class XLSKeywords {
  19 +public interface XLSKeywords {
20 20
21 21 public static final String SCORECARD_NAME = "Scorecard Name";
22   - //public static final String SCORECARD_TYPE = "Scorecard Type";
23   - //public static final String SCORECARD_OBJECT = "Object";
24   - //public static final String SCORECARD_BOUND_VARIABLE = "Bound Variable";
25 22 public static final String SCORECARD_RESULTANT_SCORE_CLASS = "Resultant Score Class";
26 23 public static final String SCORECARD_RESULTANT_SCORE_FIELD = "Resultant Score Field";
27 24 public static final String SCORECARD_CHARACTERISTIC_EXTERNAL_CLASS = "Full Class Name";
@@ -36,8 +33,6 @@
36 33 public static final String SCORECARD_CHARACTERISTIC_NAME = "Name";
37 34 public static final String SCORECARD_CHARACTERISTIC_DATATYPE = "Data Type";
38 35 public static final String SCORECARD_CHARACTERISTIC_BASELINE_SCORE = "Baseline Score";
39   - //public static final String SCORECARD_GROUP_REASONCODE = "Reason Code";
40   - //public static final String SCORECARD_CHARACTERISTIC_WEIGHT = "Weight";
41 36
42 37 public static final String SCORECARD_CHARACTERISTIC_BIN_ATTRIBUTE = "Characteristic";
43 38 public static final String SCORECARD_CHARACTERISTIC_BIN_OPERATOR = "Operator";
6 drools-scorecards/src/main/java/org/drools/scorecards/parser/xls/XLSScorecardParser.java
@@ -29,7 +29,6 @@
29 29 import org.apache.poi.ss.util.CellRangeAddress;
30 30 import org.dmg.pmml_4_1.PMML;
31 31 import org.dmg.pmml_4_1.Scorecard;
32   -import org.drools.scorecards.EventDataCollector;
33 32 import org.drools.scorecards.ScorecardError;
34 33 import org.drools.scorecards.parser.AbstractScorecardParser;
35 34 import org.drools.scorecards.parser.ScorecardParseException;
@@ -44,9 +43,9 @@
44 43 private HSSFSheet currentWorksheet;
45 44
46 45 @Override
47   - public List<ScorecardError> parseFile(EventDataCollector eventDataCollector, InputStream inStream, String worksheetName) throws ScorecardParseException {
  46 + public List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException {
48 47 try {
49   - excelDataCollector = (XLSEventDataCollector) eventDataCollector;
  48 + excelDataCollector = new XLSEventDataCollector();
50 49 excelDataCollector.setParser(this);
51 50 HSSFWorkbook workbook = new HSSFWorkbook(inStream);
52 51 HSSFSheet worksheet = workbook.getSheet(worksheetName);
@@ -93,7 +92,6 @@ private void processSheet(HSSFSheet worksheet) throws ScorecardParseException {
93 92 }
94 93 break;
95 94 case Cell.CELL_TYPE_BOOLEAN:
96   - //System.out.println(currentColCtr+" "+currentRowCtr+" "+Boolean.valueOf(cell.getBooleanCellValue()).toString());
97 95 excelDataCollector.newCell(currentRowCtr, currentColCtr, Boolean.valueOf(cell.getBooleanCellValue()).toString());
98 96 break;
99 97 case Cell.CELL_TYPE_FORMULA:
2  drools-scorecards/src/main/java/org/drools/scorecards/pmml/PMMLExtensionNames.java
@@ -23,8 +23,6 @@
23 23 public static final String SCORECARD_CELL_REF = "cellRef";
24 24 public static final String SCORECARD_RESULTANT_SCORE_FIELD = "final";
25 25 public static final String SCORECARD_RESULTANT_SCORE_CLASS = "externalClass";
26   -
27   - //public static final String SCORECARD_BOUND_VAR_NAME = "boundVariableName";
28 26 public static final String SCORECARD_IMPORTS = "importsFromDelimitedString";
29 27
30 28 public static final String CHARACTERTISTIC_EXTERNAL_CLASS = "externalClass";
3  drools-scorecards/src/test/java/org/drools/scorecards/DrlFromPMMLTest.java
@@ -14,6 +14,7 @@
14 14 import org.junit.Test;
15 15
16 16 import static junit.framework.Assert.*;
  17 +import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES;
17 18
18 19 public class DrlFromPMMLTest {
19 20
@@ -21,7 +22,7 @@
21 22
22 23 @Before
23 24 public void setUp() throws Exception {
24   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  25 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
25 26 if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls")) ) {
26 27 PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
27 28 assertNotNull(pmmlDocument);
26 drools-scorecards/src/test/java/org/drools/scorecards/ExternalObjectModelTest.java
... ... @@ -1,9 +1,6 @@
1 1 package org.drools.scorecards;
2 2
3   -import java.io.StringWriter;
4 3 import java.util.List;
5   -import javax.xml.bind.JAXBContext;
6   -import javax.xml.bind.Marshaller;
7 4
8 5 import org.dmg.pmml_4_1.Extension;
9 6 import org.dmg.pmml_4_1.Output;
@@ -25,18 +22,19 @@
25 22 import org.junit.Test;
26 23
27 24 import static junit.framework.Assert.*;
  25 +import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
28 26
29 27 public class ExternalObjectModelTest {
30 28 private static String drl;
31 29 private PMML pmmlDocument;
32   -
  30 + private static ScorecardCompiler scorecardCompiler;
33 31 @Before
34 32 public void setUp() throws Exception {
35   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  33 + scorecardCompiler = new ScorecardCompiler(EXTERNAL_OBJECT_MODEL);
36 34 if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls")) ) {
37 35 pmmlDocument = scorecardCompiler.getPMMLDocument();
38 36 assertNotNull(pmmlDocument);
39   - drl = scorecardCompiler.getDRL(ScorecardCompiler.DrlType.EXTERNAL_OBJECT_MODEL);
  37 + drl = scorecardCompiler.getDRL();
40 38 //System.out.println(drl);
41 39 } else {
42 40 fail("failed to parse scoremodel Excel.");
@@ -50,17 +48,9 @@ public void testPMMLNotNull() throws Exception {
50 48
51 49 @Test
52 50 public void testPMMLToString() throws Exception {
53   - // create a JAXBContext for the PMML class
54   - JAXBContext ctx = JAXBContext.newInstance(PMML.class);
55   - Marshaller marshaller = ctx.createMarshaller();
56   - // the property JAXB_FORMATTED_OUTPUT specifies whether or not the
57   - // marshalled XML data is formatted with linefeeds and indentation
58   - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
59   - // marshal the data in the Java content tree
60   - StringWriter stringWriter = new StringWriter();
61   - marshaller.marshal(pmmlDocument, stringWriter);
62   - assertTrue(stringWriter.toString().length() > 0);
63   - System.out.println(stringWriter.toString());
  51 + String pmml = scorecardCompiler.getPMML();
  52 + assertNotNull(pmml);
  53 + assertTrue(pmml.length() > 0);
64 54 }
65 55
66 56 @Test
@@ -94,7 +84,7 @@ public void testPMMLCustomOutput() throws Exception {
94 84 public void testDrlNoNull() throws Exception {
95 85 assertNotNull(drl);
96 86 assertTrue(drl.length() > 0);
97   - System.out.println(drl);
  87 + //System.out.println(drl);
98 88 }
99 89
100 90 @Test
22 drools-scorecards/src/test/java/org/drools/scorecards/PMMLDocumentTest.java
... ... @@ -1,9 +1,5 @@
1 1 package org.drools.scorecards;
2 2
3   -import java.io.StringWriter;
4   -import javax.xml.bind.JAXBContext;
5   -import javax.xml.bind.Marshaller;
6   -
7 3 import junit.framework.Assert;
8 4 import org.dmg.pmml_4_1.Attribute;
9 5 import org.dmg.pmml_4_1.Characteristics;
@@ -19,14 +15,16 @@
19 15 import org.junit.Test;
20 16
21 17 import static junit.framework.Assert.*;
  18 +import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
22 19
23 20 public class PMMLDocumentTest {
24 21
25 22 private static PMML pmmlDocument;
  23 + private static ScorecardCompiler scorecardCompiler;
26 24
27 25 @Before
28 26 public void setUp() throws Exception {
29   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  27 + scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
30 28 scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls"));
31 29 pmmlDocument = scorecardCompiler.getPMMLDocument();
32 30 }
@@ -34,17 +32,9 @@ public void setUp() throws Exception {
34 32 @Test
35 33 public void testPMMLDocument() throws Exception {
36 34 Assert.assertNotNull(pmmlDocument);
37   - // create a JAXBContext for the PMML class
38   - JAXBContext ctx = JAXBContext.newInstance(PMML.class);
39   - Marshaller marshaller = ctx.createMarshaller();
40   - // the property JAXB_FORMATTED_OUTPUT specifies whether or not the
41   - // marshalled XML data is formatted with linefeeds and indentation
42   - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
43   - // marshal the data in the Java content tree
44   - StringWriter stringWriter = new StringWriter();
45   - marshaller.marshal(pmmlDocument, stringWriter);
46   - assertTrue(stringWriter.toString().length() > 0);
47   - //System.out.println(stringWriter.toString());
  35 + String pmml = scorecardCompiler.getPMML();
  36 + Assert.assertNotNull(pmml);
  37 + Assert.assertTrue(pmml.length() > 0);
48 38 }
49 39
50 40 @Test
8 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardParseErrorsTest.java
@@ -3,11 +3,13 @@
3 3 import junit.framework.Assert;
4 4 import org.junit.Test;
5 5
  6 +import static org.drools.scorecards.ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES;
  7 +
6 8 public class ScorecardParseErrorsTest {
7 9
8 10 @Test
9 11 public void testErrorCount() throws Exception {
10   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  12 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
11 13 boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"));
12 14 Assert.assertFalse(compileResult);
13 15 Assert.assertEquals(4, scorecardCompiler.getScorecardParseErrors().size());
@@ -36,7 +38,7 @@ public void testWrongData() throws Exception {
36 38
37 39 @Test
38 40 public void testMissingDataType() throws Exception {
39   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  41 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES);
40 42 boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"), "missingDataType");
41 43 // for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){
42 44 // System.out.println("testMissingDataType :"+error.getErrorLocation()+"->"+error.getErrorMessage());
@@ -49,7 +51,7 @@ public void testMissingDataType() throws Exception {
49 51
50 52 @Test
51 53 public void testMissingAttributes() throws Exception {
52   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  54 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(ScorecardCompiler.DrlType.INTERNAL_DECLARED_TYPES);
53 55 boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_errors.xls"), "incomplete_noAttr");
54 56 Assert.assertFalse(compileResult);
55 57 // Assert.assertEquals(2, scorecardCompiler.getScorecardParseErrors().size());
35 drools-scorecards/src/test/java/org/drools/scorecards/ScorecardReasonCodeTest.java
... ... @@ -1,9 +1,5 @@
1 1 package org.drools.scorecards;
2 2
3   -import java.io.StringWriter;
4   -import javax.xml.bind.JAXBContext;
5   -import javax.xml.bind.Marshaller;
6   -
7 3 import junit.framework.Assert;
8 4 import org.dmg.pmml_4_1.Attribute;
9 5 import org.dmg.pmml_4_1.Characteristic;
@@ -23,44 +19,41 @@
23 19 import org.junit.Test;
24 20
25 21 import static junit.framework.Assert.*;
  22 +import static org.drools.scorecards.ScorecardCompiler.DrlType.*;
26 23
27 24 public class ScorecardReasonCodeTest {
28 25 private static PMML pmmlDocument;
29 26 private static String drl;
30   -
  27 + private static ScorecardCompiler scorecardCompiler;
31 28 @Before
32 29 public void setUp() throws Exception {
33   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  30 + scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
34 31 boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"));
35 32 if (!compileResult) {
36 33 for(ScorecardError error : scorecardCompiler.getScorecardParseErrors()){
37 34 System.out.println("setup :"+error.getErrorLocation()+"->"+error.getErrorMessage());
38 35 }
39 36 }
40   - pmmlDocument = scorecardCompiler.getPMMLDocument();
41 37 drl = scorecardCompiler.getDRL();
  38 + Assert.assertNotNull(drl);
  39 + assertTrue(drl.length() > 0);
42 40 //System.out.println(drl);
43 41 }
44 42
45 43 @Test
46 44 public void testPMMLDocument() throws Exception {
  45 + pmmlDocument = scorecardCompiler.getPMMLDocument();
47 46 Assert.assertNotNull(pmmlDocument);
48   - // create a JAXBContext for the PMML class
49   - JAXBContext ctx = JAXBContext.newInstance(PMML.class);
50   - Marshaller marshaller = ctx.createMarshaller();
51   - // the property JAXB_FORMATTED_OUTPUT specifies whether or not the
52   - // marshalled XML data is formatted with linefeeds and indentation
53   - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
54   - // marshal the data in the Java content tree
55   - StringWriter stringWriter = new StringWriter();
56   - marshaller.marshal(pmmlDocument, stringWriter);
57   - assertTrue(stringWriter.toString().length() > 0);
58   - //System.out.println(stringWriter.toString());
  47 +
  48 + String pmml = scorecardCompiler.getPMML();
  49 + Assert.assertNotNull(pmml);
  50 + assertTrue(pmml.length() > 0);
  51 + //System.out.println(pmml);
59 52 }
60 53
61 54 @Test
62 55 public void testAbsenceOfReasonCodes() throws Exception {
63   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  56 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
64 57 scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls"));
65 58 PMML pmml = scorecardCompiler.getPMMLDocument();
66 59 for (Object serializable : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()){
@@ -134,7 +127,7 @@ public void testMissingReasonCodes() throws Exception {
134 127
135 128 @Test
136 129 public void testMissingBaselineScores() throws Exception {
137   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  130 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
138 131 scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_reason_error");
139 132 assertEquals(3, scorecardCompiler.getScorecardParseErrors().size());
140 133 assertEquals("$D$30", scorecardCompiler.getScorecardParseErrors().get(2).getErrorLocation());
@@ -142,7 +135,7 @@ public void testMissingBaselineScores() throws Exception {
142 135
143 136 @Test
144 137 public void testReasonCodesCombinations() throws Exception {
145   - ScorecardCompiler scorecardCompiler = new ScorecardCompiler();
  138 + ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
146 139 scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"), "scorecards_char_reasoncode");
147 140 assertEquals(0, scorecardCompiler.getScorecardParseErrors().size());
148 141 String drl = scorecardCompiler.getDRL();
BIN  drools-scorecards/src/test/resources/scoremodel_externalmodel.xls
Binary file not shown

0 comments on commit 96d5bcd

Please sign in to comment.
Something went wrong with that request. Please try again.