public static PredicateRuleProducer getPredicateProducer(Serializable serializable) { if (serializable instanceof SimplePredicate) return new SimpleSegmentPredicate((SimplePredicate)serializable); if (serializable instanceof SimpleSetPredicate) return new SimpleSetSegmentPredicate((SimpleSetPredicate)serializable); if (serializable instanceof CompoundPredicate) return new CompoundSegmentPredicate((CompoundPredicate)serializable); if (serializable instanceof True) return new BooleanSegmentPredicate((True)serializable); if (serializable instanceof False) return new BooleanSegmentPredicate((False)serializable); throw new IllegalArgumentException("Serializable object [" + serializable.getClass().getName() + "] is not one of the known predicate types"); } }
public CompoundSegmentPredicate(CompoundPredicate predicate) { subpredicates = new ArrayList<>(); for (Serializable serializable : predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates()) { if (serializable instanceof SimplePredicate) { subpredicates.add(new SimpleSegmentPredicate((SimplePredicate)serializable)); } else if (serializable instanceof SimpleSetPredicate) { subpredicates.add(new SimpleSetSegmentPredicate((SimpleSetPredicate)serializable)); } else if (serializable instanceof CompoundPredicate) { subpredicates.add(new CompoundSegmentPredicate((CompoundPredicate)serializable)); } } booleanOperator = predicate.getBooleanOperator(); if (booleanOperator == null) { throw new IllegalStateException("PMML-CompoundPredicate: Missing the booleanOperator attribute"); } if (!booleanOperator.equalsIgnoreCase("and") && !booleanOperator.equalsIgnoreCase("or") && !booleanOperator.equalsIgnoreCase("xor") && !booleanOperator.equalsIgnoreCase("surrogate")) { throw new IllegalStateException("PMML-CompoundPredicate: Invalid value ("+booleanOperator+") for the booleanOperator attribute"); } }
@Test public void testMixedPredUsingOr() { CompoundPredicate predicate = new CompoundPredicate(); predicate.setBooleanOperator("or"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.GREATER,"100"); Array valueSet = getNewArray("int", new BigInteger("4"), 1, 8, 16, 21); setupSimpleSetPredicate(0,"TF2","isNotIn",valueSet); predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[0]); predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simpleSetPredicate[0]); CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(predicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); assertEquals(text,"((( mTF1 == false ) && ( vTF1 > 100 )) || (( mTF2 == false ) && ( vTF2 not in ( 1, 8, 16, 21 ) )))"); }
@Test public void testCompoundWithCompound() { CompoundPredicate outerPredicate = new CompoundPredicate(); outerPredicate.setBooleanOperator("or"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.LESSER_EQUAL,"150"); outerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[0]); CompoundPredicate innerPredicate = new CompoundPredicate(); innerPredicate.setBooleanOperator("and"); setupSimplePredicate(1, "TF1", SimpleSegmentPredicate.GREATER, "150"); Array valueSet = getNewArray("string", new BigInteger("3"), "red", "white", "blue"); setupSimpleSetPredicate(0, "TF2", "isIn", valueSet); innerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[1]); innerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simpleSetPredicate[0]); outerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(innerPredicate); CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(outerPredicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); StringBuilder bldr = new StringBuilder("("); bldr.append("(( mTF1 == false ) && ( vTF1 <= 150 )) || (("); bldr.append("(( mTF1 == false ) && ( vTF1 > 150 )) && "); bldr.append("(( mTF2 == false ) && ( vTF2 in ( \"red\" , \"white\" , \"blue\" ) )))"); bldr.append("))"); assertEquals(bldr.toString(),text); }
@Test public void testMixedPredUsingAnd() { CompoundPredicate predicate = new CompoundPredicate(); predicate.setBooleanOperator("and"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.EQUAL,"ABC"); Array valueSet = getNewArray("int", new BigInteger("3"), 10, 12, 1); setupSimpleSetPredicate(0,"TF2","isIn",valueSet); predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[0]); predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simpleSetPredicate[0]); CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(predicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); assertEquals(text,"((( mTF1 == false ) && ( vTF1 == \"ABC\" )) && (( mTF2 == false ) && ( vTF2 in ( 10, 12, 1 ) )))"); }
@Test public void testSimplePredUsingOr() { CompoundPredicate predicate = new CompoundPredicate(); predicate.setBooleanOperator("or"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.LESSER_EQUAL,"0"); setupSimplePredicate(1,"TF1",SimpleSegmentPredicate.GREATER,"4"); for (int x = 0; x < 2; x++) { predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[x]); } CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(predicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); assertEquals(text,"((( mTF1 == false ) && ( vTF1 <= 0 )) || (( mTF1 == false ) && ( vTF1 > 4 )))"); }
@Test public void testSimplePredUsingAnd() { CompoundPredicate predicate = new CompoundPredicate(); predicate.setBooleanOperator("and"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.EQUAL,"ABC"); setupSimplePredicate(1,"TF2",SimpleSegmentPredicate.GREATER,"4"); for (int x = 0; x < 2; x++) { predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[x]); } CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(predicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); assertEquals(text,"((( mTF1 == false ) && ( vTF1 == \"ABC\" )) && (( mTF2 == false ) && ( vTF2 > 4 )))"); }
@Test public void testSimplePredUsingXor() { CompoundPredicate predicate = new CompoundPredicate(); predicate.setBooleanOperator("xor"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.LESSER_EQUAL,"0"); setupSimplePredicate(1,"TF1",SimpleSegmentPredicate.GREATER,"4"); for (int x = 0; x < 2; x++) { predicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[x]); } CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(predicate); String text = testPredicate.getPredicateRule(); assertNotNull(text); assertEquals(text,"((( mTF1 == false ) && ( vTF1 <= 0 )) ^ (( mTF1 == false ) && ( vTF1 > 4 )))"); }
public String getPredicate(Attribute attrib) { String predicateText = null; if (attrib.getSimplePredicate() != null) { SimpleSegmentPredicate ssp = new SimpleSegmentPredicate(attrib.getSimplePredicate()); predicateText = ssp.getPredicateRule(); return "( "+predicateText+" )"; } else if (attrib.getSimpleSetPredicate() != null) { SimpleSetSegmentPredicate sssp = new SimpleSetSegmentPredicate(attrib.getSimpleSetPredicate()); return sssp.getPredicateRule(); } else if (attrib.getCompoundPredicate() != null) { CompoundSegmentPredicate csp = new CompoundSegmentPredicate(attrib.getCompoundPredicate()); if (csp.hasSurrogation()) { return getSurrogationPredicateText(csp,-1); } else { return csp.getPredicateRule(); } } else if (attrib.getTrue() != null) { return "( 1 == 1 )"; } else if (attrib.getFalse() != null) { return "( 1 == 0 )"; } throw new IllegalStateException("Unable to determine predicate for Attribute with reason code: "+attrib.getReasonCode()); }
@Test public void testSimplePredWithSurrogate() { CompoundPredicate outerPredicate = new CompoundPredicate(); outerPredicate.setBooleanOperator("surrogate"); setupSimplePredicate(0,"TF1",SimpleSegmentPredicate.LESSER_EQUAL,"150"); outerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[0]); CompoundPredicate innerPredicate = new CompoundPredicate(); innerPredicate.setBooleanOperator("and"); setupSimplePredicate(1, "TF2", SimpleSegmentPredicate.GREATER, "150"); Array valueSet = getNewArray("string", new BigInteger("3"), "red", "white", "blue"); setupSimpleSetPredicate(0, "TF3", "isIn", valueSet); setupSimplePredicate(2, "TF4", SimpleSegmentPredicate.GREATER_EQUAL,"10"); innerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[1]); innerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simpleSetPredicate[0]); outerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(innerPredicate); outerPredicate.getSimplePredicatesAndCompoundPredicatesAndSimpleSetPredicates().add(simplePredicate[2]); CompoundSegmentPredicate testPredicate = new CompoundSegmentPredicate(outerPredicate); String text = testPredicate.getPrimaryPredicateRule(); assertNotNull(text); assertEquals(text,"( mTF1 == false ) && ( vTF1 <= 150 )"); assertEquals(2,testPredicate.getSubpredicateCount()); text = testPredicate.getNextPredicateRule(0); assertEquals(text,"( (mTF1 == true) && ( ((( mTF2 == false ) && ( vTF2 > 150 )) && (( mTF3 == false ) && ( vTF3 in ( \"red\" , \"white\" , \"blue\" ) ))) ) )"); } }
public MiningSegment( MiningSegmentation owner, Segment segment, int segmentIndex) { this.owner = owner; this.internalModel = PMML4ModelFactory.getInstance().getModel(segment,owner); this.segmentId = segment.getId(); this.segmentIndex = segmentIndex; this.weight = segment.getWeight(); if (segment.getSimplePredicate() != null) { predicateRuleProducer = new SimpleSegmentPredicate(segment.getSimplePredicate()); } else if (segment.getSimpleSetPredicate() != null) { predicateRuleProducer = new SimpleSetSegmentPredicate(segment.getSimpleSetPredicate()); } else if (segment.getCompoundPredicate() != null) { predicateRuleProducer = new CompoundSegmentPredicate(segment.getCompoundPredicate()); } else if (segment.getTrue() != null) { predicateRuleProducer = new BooleanSegmentPredicate(segment.getTrue()); } else if (segment.getFalse() != null) { predicateRuleProducer = new BooleanSegmentPredicate(segment.getFalse()); } }