org.dmg.pmml.CompoundPredicate Java Examples

The following examples show how to use org.dmg.pmml.CompoundPredicate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TreeModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private Boolean evaluateNode(Trail trail, Node node, EvaluationContext context){
	EmbeddedModel embeddedModel = node.getEmbeddedModel();
	if(embeddedModel != null){
		throw new UnsupportedElementException(embeddedModel);
	}

	Predicate predicate = PredicateUtil.ensurePredicate(node);

	// A compound predicate whose boolean operator is "surrogate" represents a special case
	if(predicate instanceof CompoundPredicate){
		CompoundPredicate compoundPredicate = (CompoundPredicate)predicate;

		PredicateUtil.CompoundPredicateResult result = PredicateUtil.evaluateCompoundPredicateInternal(compoundPredicate, context);
		if(result.isAlternative()){
			trail.addMissingLevel();
		}

		return result.getResult();
	} else

	{
		return PredicateUtil.evaluate(predicate, context);
	}
}
 
Example #2
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void evaluateSurrogateCompoundPredicate(){
	FieldName temperature = FieldName.create("temperature");
	FieldName humidity = FieldName.create("humidity");

	CompoundPredicate temperaturePredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, null)
		.addPredicates(
			new SimplePredicate(temperature, SimplePredicate.Operator.LESS_THAN, "90"),
			new SimplePredicate(temperature, SimplePredicate.Operator.GREATER_THAN, "50")
		);

	SimplePredicate humidityPredicate = new SimplePredicate(humidity, SimplePredicate.Operator.GREATER_OR_EQUAL, "80");

	CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.SURROGATE, null)
		.addPredicates(temperaturePredicate, humidityPredicate);

	assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, 70, humidity, null));
	assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, 40, humidity, null));
	assertEquals(Boolean.FALSE, evaluate(compoundPredicate,  temperature, 100, humidity, null));

	assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, null, humidity, 90));
	assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, null, humidity, 70));

	assertEquals(null, evaluate(compoundPredicate, temperature, null, humidity, null));
}
 
Example #3
Source File: PredicateFilterer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(CompoundPredicate compoundPredicate){

	if(compoundPredicate.hasPredicates()){
		filterAll(compoundPredicate.getPredicates());
	}

	return super.visit(compoundPredicate);
}
 
Example #4
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
Boolean evaluatePredicate(Predicate predicate, EvaluationContext context){

	if(predicate instanceof SimplePredicate){
		return evaluateSimplePredicate((SimplePredicate)predicate, context);
	} else

	if(predicate instanceof SimpleSetPredicate){
		return evaluateSimpleSetPredicate((SimpleSetPredicate)predicate, context);
	} else

	if(predicate instanceof CompoundPredicate){
		return evaluateCompoundPredicate((CompoundPredicate)predicate, context);
	} else

	if(predicate instanceof True){
		return evaluateTrue((True)predicate);
	} else

	if(predicate instanceof False){
		return evaluateFalse((False)predicate);
	} // End if

	if(predicate instanceof JavaPredicate){
		return evaluateJavaPredicate((JavaPredicate)predicate, context);
	}

	throw new UnsupportedElementException(predicate);
}
 
Example #5
Source File: StringInternerTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void intern(){
	SimplePredicate left = new CustomSimplePredicate(FieldName.create("x"), SimplePredicate.Operator.LESS_THAN, new String("0"));

	SimplePredicate right = new CustomSimplePredicate(FieldName.create("y"), SimplePredicate.Operator.LESS_THAN, new String("0"));

	assertNotSame(left.getValue(), right.getValue());

	CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.OR, null)
		.addPredicates(left, right);

	StringInterner interner = new StringInterner();
	interner.applyTo(compoundPredicate);

	assertSame(left.getValue(), right.getValue());
}
 
Example #6
Source File: GolfingTreeModelExample.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static
private CompoundPredicate createCompoundPredicate(CompoundPredicate.BooleanOperator booleanOperator, Predicate... predicates){
	return new CompoundPredicate(booleanOperator, null)
		.addPredicates(predicates);
}
 
Example #7
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public Boolean evaluateCompoundPredicate(CompoundPredicate compoundPredicate, EvaluationContext context){
	CompoundPredicateResult result = evaluateCompoundPredicateInternal(compoundPredicate, context);

	return result.getResult();
}
 
Example #8
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public CompoundPredicateResult evaluateCompoundPredicateInternal(CompoundPredicate compoundPredicate, EvaluationContext context){
	CompoundPredicate.BooleanOperator booleanOperator = compoundPredicate.getBooleanOperator();
	if(booleanOperator == null){
		throw new MissingAttributeException(compoundPredicate, PMMLAttributes.COMPOUNDPREDICATE_BOOLEANOPERATOR);
	} // End if

	if(!compoundPredicate.hasPredicates()){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(compoundPredicate.getClass()) + "/<Predicate>"), compoundPredicate);
	}

	List<Predicate> predicates = compoundPredicate.getPredicates();
	if(predicates.size() < 2){
		throw new InvalidElementListException(predicates);
	}

	Predicate predicate = predicates.get(0);

	Boolean result = evaluate(predicate, context);

	switch(booleanOperator){
		case AND:
		case OR:
		case XOR:
			break;
		case SURROGATE:
			if(result != null){
				return new CompoundPredicateResult(result, false);
			}
			break;
		default:
			throw new UnsupportedAttributeException(compoundPredicate, booleanOperator);
	}

	for(int i = 1, max = predicates.size(); i < max; i++){
		predicate = predicates.get(i);

		Boolean value = evaluate(predicate, context);

		switch(booleanOperator){
			case AND:
				result = PredicateUtil.binaryAnd(result, value);
				break;
			case OR:
				result = PredicateUtil.binaryOr(result, value);
				break;
			case XOR:
				result = PredicateUtil.binaryXor(result, value);
				break;
			case SURROGATE:
				if(value != null){
					return new CompoundPredicateResult(value, true);
				}
				break;
			default:
				throw new UnsupportedAttributeException(compoundPredicate, booleanOperator);
		}
	}

	return new CompoundPredicateResult(result, false);
}
 
Example #9
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void evaluateBooleanCompoundPredicate(){
	CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, null)
		.addPredicates(True.INSTANCE, False.INSTANCE);

	assertEquals(Boolean.FALSE, evaluate(compoundPredicate));

	compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.OR);

	assertEquals(Boolean.TRUE, evaluate(compoundPredicate));

	compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);

	assertEquals(Boolean.TRUE, evaluate(compoundPredicate));
}