protected List<Rule> createRuleList(PMML pmmlDocument) { List<Rule> ruleList = new ArrayList<Rule>(); for (Object obj : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) { if (obj instanceof Scorecard) { Scorecard scorecard = (Scorecard) obj; Characteristics characteristics = getCharacteristicsFromScorecard(scorecard); createInitialRule(ruleList, scorecard); for (org.dmg.pmml.pmml_4_1.descr.Characteristic c : characteristics.getCharacteristics()) { int attributePosition = 0; for (org.dmg.pmml.pmml_4_1.descr.Attribute scoreAttribute : c.getAttributes()) { String name = formRuleName( pmmlDocument, scorecard.getModelName().replaceAll(" ", ""), c, scoreAttribute); Rule rule = new Rule(name, 99, 1); String desc = ScorecardPMMLUtils.getExtensionValue(scoreAttribute.getExtensions(), "description"); if (desc != null) { rule.setDescription(desc); } attributePosition++; populateLHS(rule, pmmlDocument, scorecard, c, scoreAttribute); populateRHS(rule, pmmlDocument, scorecard, c, scoreAttribute, attributePosition); ruleList.add(rule); } } createSummationRules(ruleList, scorecard); } } return ruleList; }
@Test public void characteristicsWithEmptyList() { List<Listing> listings = new ArrayList<>(); Characteristics charac = TradingPost.getCharacteristics(listings); Assert.assertEquals(0, charac.getMinimum()); Assert.assertEquals(0, charac.getMaximum()); Assert.assertTrue(charac.getAverage() + " != " + 0.0, 0.0 == charac.getAverage()); Assert.assertTrue(charac.getMedian() + " != " + 0.0, 0.0 == charac.getMedian()); }
@Test public void characteristics() { List<Listing> listings = new ArrayList<>(); listings.add(new Listing(1, 1)); listings.add(new Listing(1, 10)); Characteristics charac = TradingPost.getCharacteristics(listings); Assert.assertEquals(1, charac.getMinimum()); Assert.assertEquals(10, charac.getMaximum()); Assert.assertTrue(charac.getAverage() + " != " + 5.5, 5.5 == charac.getAverage()); Assert.assertTrue(charac.getMedian() + " != " + 5.0, 5.5 == charac.getMedian()); }
@Test public void characteristicsWithQuantity() { List<Listing> listings = new ArrayList<>(); listings.add(new Listing(10, 1)); listings.add(new Listing(1, 2)); listings.add(new Listing(1, 3)); Characteristics charac = TradingPost.getCharacteristics(listings); Assert.assertEquals(1, charac.getMinimum()); Assert.assertEquals(3, charac.getMaximum()); Assert.assertTrue( charac.getAverage() + " != " + 15.0 / 12.0, 15.0 / 12.0 == charac.getAverage()); Assert.assertTrue(charac.getMedian() + " != " + 1.0, 1.0 == charac.getMedian()); }
protected void createInitialRule(List<Rule> ruleList, Scorecard scorecard) { if (scorecard.getInitialScore() > 0 || scorecard.isUseReasonCodes()) { String objectClass = scorecard.getModelName().replaceAll(" ", ""); String ruleName = objectClass + "_init"; Rule rule = new Rule(ruleName, 999, 1); rule.setDescription("set the initial score"); Condition condition = createInitialRuleCondition(scorecard, objectClass); rule.addCondition(condition); if (scorecard.getInitialScore() > 0) { Consequence consequence = new Consequence(); // consequence.setSnippet("$sc.setInitialScore(" + scorecard.getInitialScore() + ");"); consequence.setSnippet( "insertLogical(new InitialScore(\"" + objectClass + "\"," + scorecard.getInitialScore() + "));"); rule.addConsequence(consequence); } if (scorecard.isUseReasonCodes()) { for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) { if (obj instanceof Characteristics) { Characteristics characteristics = (Characteristics) obj; for (Characteristic characteristic : characteristics.getCharacteristics()) { String field = ScorecardPMMLUtils.extractFieldNameFromCharacteristic(characteristic); Consequence consequence = new Consequence(); if (characteristic.getBaselineScore() == null || characteristic.getBaselineScore() == 0) { consequence.setSnippet( "insertLogical(new BaselineScore(\"" + objectClass + "\",\"" + field + "\"," + scorecard.getBaselineScore() + "));"); // consequence.setSnippet("$sc.setBaselineScore(\"" + field + // "\","+scorecard.getBaselineScore()+");"); } else { consequence.setSnippet( "insertLogical(new BaselineScore(\"" + objectClass + "\",\"" + field + "\"," + characteristic.getBaselineScore() + "));"); // consequence.setSnippet("$sc.setBaselineScore(\"" + field + // "\","+characteristic.getBaselineScore()+");"); } rule.addConsequence(consequence); } } } if (scorecard.getReasonCodeAlgorithm() != null) { Consequence consequence = new Consequence(); if ("pointsAbove".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) { // TODO: ReasonCode Algorithm consequence.setSnippet( "//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSABOVE);"); } else if ("pointsBelow".equalsIgnoreCase(scorecard.getReasonCodeAlgorithm())) { consequence.setSnippet( "//$sc.setReasonCodeAlgorithm(DroolsScorecard.REASON_CODE_ALGORITHM_POINTSBELOW);"); } rule.addConsequence(consequence); } } ruleList.add(rule); } }
@Override public Characteristics getCharacteristics() { Characteristics ret = super.getCharacteristics(); ret.sourceID = this.sourceID; return ret; }