org.dmg.pmml.Predicate Java Examples

The following examples show how to use org.dmg.pmml.Predicate. 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: TreeModelCompactor.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void exitNode(Node node){
	Number recordCount = node.getRecordCount();
	Predicate predicate = node.getPredicate();

	if(recordCount != null){
		node.setRecordCount(null);
	} // End if

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		initScore(parentNode, node);
		replaceChildWithGrandchildren(parentNode, node);
	}
}
 
Example #2
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 #3
Source File: PredicateInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
public Predicate intern(Predicate predicate){

		if(predicate instanceof SimplePredicate){
			return intern((SimplePredicate)predicate);
		} else

		if(predicate instanceof SimpleSetPredicate){
			return intern((SimpleSetPredicate)predicate);
		} else

		if(predicate instanceof True){
			return True.INSTANCE;
		} else

		if(predicate instanceof False){
			return False.INSTANCE;
		}

		return predicate;
	}
 
Example #4
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Boolean evaluate(Predicate predicate, Map<FieldName, ?> arguments){
	EvaluationContext context = new VirtualEvaluationContext();
	context.declareAll(arguments);

	return PredicateUtil.evaluate(predicate, context);
}
 
Example #5
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public <E extends PMMLObject & HasPredicate<E>> Predicate ensurePredicate(E hasPredicate){
	Predicate predicate = hasPredicate.getPredicate();
	if(predicate == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(hasPredicate.getClass()) + "/<Predicate>"), hasPredicate);
	}

	return predicate;
}
 
Example #6
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @return The {@link Boolean} value of the predicate, or <code>null</code> if the value is unknown.
 */
static
public Boolean evaluate(Predicate predicate, EvaluationContext context){

	try {
		return evaluatePredicate(predicate, context);
	} catch(PMMLException pe){
		throw pe.ensureContext(predicate);
	}
}
 
Example #7
Source File: RDFUpdate.java    From oryx with Apache License 2.0 5 votes vote down vote up
private Predicate buildPredicate(Split split,
                                 CategoricalValueEncodings categoricalValueEncodings) {
  if (split == null) {
    // Left child always applies, but is evaluated second
    return new True();
  }

  int featureIndex = inputSchema.predictorToFeatureIndex(split.feature());
  FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(featureIndex));

  if (split.featureType().equals(FeatureType.Categorical())) {
    // Note that categories in MLlib model select the *left* child but the
    // convention here will be that the predicate selects the *right* child
    // So the predicate will evaluate "not in" this set
    // More ugly casting
    @SuppressWarnings("unchecked")
    Collection<Double> javaCategories = (Collection<Double>) (Collection<?>)
        JavaConversions.seqAsJavaList(split.categories());
    Set<Integer> negativeEncodings = javaCategories.stream().map(Double::intValue).collect(Collectors.toSet());

    Map<Integer,String> encodingToValue =
        categoricalValueEncodings.getEncodingValueMap(featureIndex);
    List<String> negativeValues = negativeEncodings.stream().map(encodingToValue::get).collect(Collectors.toList());

    String joinedValues = TextUtils.joinPMMLDelimited(negativeValues);
    return new SimpleSetPredicate(fieldName,
                                  SimpleSetPredicate.BooleanOperator.IS_NOT_IN,
                                  new Array(Array.Type.STRING, joinedValues));

  } else {
    // For MLlib, left means <= threshold, so right means >
    return new SimplePredicate(fieldName,
        SimplePredicate.Operator.GREATER_THAN,
        Double.toString(split.threshold()));
  }
}
 
Example #8
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 #9
Source File: RegressionTree.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static
private Predicate encodePredicate(Feature feature, Node node, boolean left){
    FieldName name = feature.getName();
    SimplePredicate.Operator operator;
    String value;

    if(feature instanceof BinaryFeature){
        BinaryFeature binaryFeature = (BinaryFeature)feature;

        operator = (left ? SimplePredicate.Operator.NOT_EQUAL : SimplePredicate.Operator.EQUAL);
        value = binaryFeature.getValue();
    } else

    {
        ContinuousFeature continuousFeature = feature.toContinuousFeature();

        Number splitValue = node.getThreshold();

        DataType dataType = continuousFeature.getDataType();
        switch(dataType){
            case INTEGER:
                splitValue = (int)(splitValue.floatValue() + 1f);
                break;
            case FLOAT:
                break;
            default:
                throw new IllegalArgumentException();
        }

        operator = (left ? SimplePredicate.Operator.LESS_OR_EQUAL : SimplePredicate.Operator.GREATER_THAN);
        value = ValueUtil.formatValue(splitValue);
    }

    SimplePredicate simplePredicate = new SimplePredicate(name, operator)
            .setValue(value);

    return simplePredicate;
}
 
Example #10
Source File: GBMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private List<Object> selectValues(List<?> values, java.util.function.Predicate<Object> valueFilter, List<Integer> splitValues, boolean left){

	if(values.size() != splitValues.size()){
		throw new IllegalArgumentException();
	}

	List<Object> result = new ArrayList<>();

	for(int i = 0; i < values.size(); i++){
		Object value = values.get(i);
		Integer splitValue = splitValues.get(i);

		boolean append;

		if(left){
			append = (splitValue == -1);
		} else

		{
			append = (splitValue == 1);
		} // End if

		if(append && valueFilter.test(value)){
			result.add(value);
		}
	}

	return result;
}
 
Example #11
Source File: PredicateInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Predicate filter(Predicate predicate){

	if(predicate == null || ExtensionUtil.hasExtensions(predicate)){
		return predicate;
	}

	return intern(predicate);
}
 
Example #12
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<Segment> getActiveHead(List<Segment> segments){

		for(int i = 0, max = segments.size(); i < max; i++){
			Segment segment = segments.get(i);

			Predicate predicate = PredicateUtil.ensurePredicate(segment);

			if(predicate instanceof True){
				return segments.subList(0, i + 1);
			}
		}

		return segments;
	}
 
Example #13
Source File: TreeModelCompactor.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void exitNode(Node node){
	Predicate predicate = node.getPredicate();

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		if((MiningFunction.REGRESSION).equals(this.miningFunction)){
			initScore(parentNode, node);
			replaceChildWithGrandchildren(parentNode, node);
		} else

		if((MiningFunction.CLASSIFICATION).equals(this.miningFunction)){

			// Replace intermediate nodes, but not terminal nodes
			if(node.hasNodes()){
				replaceChildWithGrandchildren(parentNode, node);
			}
		} else

		{
			throw new IllegalArgumentException();
		}
	}
}
 
Example #14
Source File: RandomForestCompactor.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void exitNode(Node node){
	Predicate predicate = node.getPredicate();

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		initScore(parentNode, node);
		replaceChildWithGrandchildren(parentNode, node);
	}
}
 
Example #15
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
List<Object> selectValues(List<?> values, java.util.function.Predicate<Object> valueFilter, Double split, boolean left){
	UnsignedLong bits = toUnsignedLong(split.doubleValue());

	List<Object> result = new ArrayList<>();

	for(int i = 0; i < values.size(); i++){
		Object value = values.get(i);

		boolean append;

		// Send "true" categories to the left
		if(left){
			// Test if the least significant bit (LSB) is 1
			append = (bits.mod(RandomForestConverter.TWO)).equals(UnsignedLong.ONE);
		} else

		// Send all other categories to the right
		{
			// Test if the LSB is 0
			append = (bits.mod(RandomForestConverter.TWO)).equals(UnsignedLong.ZERO);
		} // End if

		if(append && valueFilter.test(value)){
			result.add(value);
		}

		bits = bits.dividedBy(RandomForestConverter.TWO);
	}

	return result;
}
 
Example #16
Source File: PredicateInternerTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void internSimplePredicate(){
	FieldName name = FieldName.create("x");

	Predicate left = new SimplePredicate(name, SimplePredicate.Operator.EQUAL, "1");
	Predicate right = new SimplePredicate(name, SimplePredicate.Operator.EQUAL, "1");

	checkTree(left, right);
}
 
Example #17
Source File: RuleSetClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public RuleSetModel encodeModel(Schema schema){
	String defaultScore = getDefaultScore();
	List<Object[]> rules = getRules();

	Label label = schema.getLabel();
	List<? extends Feature> features = schema.getFeatures();

	RuleSelectionMethod ruleSelectionMethod = new RuleSelectionMethod(RuleSelectionMethod.Criterion.FIRST_HIT);

	RuleSet ruleSet = new RuleSet()
		.addRuleSelectionMethods(ruleSelectionMethod);

	if(defaultScore != null){
		ruleSet
			.setDefaultConfidence(1d)
			.setDefaultScore(defaultScore);
	}

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	for(Object[] rule : rules){
		String predicate = TupleUtil.extractElement(rule, 0, String.class);
		String score = TupleUtil.extractElement(rule, 1, String.class);

		Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope);

		SimpleRule simpleRule = new SimpleRule(score, pmmlPredicate);

		ruleSet.addRules(simpleRule);
	}

	RuleSetModel ruleSetModel = new RuleSetModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(label), ruleSet);

	return ruleSetModel;
}
 
Example #18
Source File: SelectFirstUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private MiningModel encodeModel(MiningFunction miningFunction, List<Object[]> steps, Schema schema){

	if(steps.size() < 1){
		throw new IllegalArgumentException();
	}

	Label label = schema.getLabel();
	List<? extends Feature> features = schema.getFeatures();

	Segmentation segmentation = new Segmentation(Segmentation.MultipleModelMethod.SELECT_FIRST, null);

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	for(Object[] step : steps){
		String name = TupleUtil.extractElement(step, 0, String.class);
		Estimator estimator = TupleUtil.extractElement(step, 1, Estimator.class);
		String predicate = TupleUtil.extractElement(step, 2, String.class);

		if(!(miningFunction).equals(estimator.getMiningFunction())){
			throw new IllegalArgumentException();
		}

		Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope);

		Model model = estimator.encodeModel(schema);

		Segment segment = new Segment(pmmlPredicate, model)
			.setId(name);

		segmentation.addSegments(segment);
	}

	MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(label))
		.setSegmentation(segmentation);

	return miningModel;
}
 
Example #19
Source File: TreeModelFlattener.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Iterator<Node> getChildren(Node node){
	Predicate predicate = node.getPredicate();

	if(!(predicate instanceof SimplePredicate)){
		return null;
	}

	SimplePredicate simplePredicate = (SimplePredicate)predicate;

	if(!hasOperator(simplePredicate, SimplePredicate.Operator.LESS_OR_EQUAL)){
		return null;
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		int endPos = 0;

		for(Node child : children){
			Predicate childPredicate = child.getPredicate();

			if(!hasFieldReference(childPredicate, simplePredicate.getField()) || !hasOperator(childPredicate, simplePredicate.getOperator())){
				break;
			}

			endPos++;
		}

		if(endPos > 0){
			return (children.subList(0, endPos)).iterator();
		}

		return null;
	}

	return null;
}
 
Example #20
Source File: TreeModelFlattener.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void exitNode(Node node){
	Predicate predicate = node.getPredicate();

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		List<Node> parentChildren = parentNode.getNodes();
		if(parentChildren.size() != 1){
			return;
		}

		boolean success = parentChildren.remove(node);
		if(!success){
			throw new IllegalArgumentException();
		} // End if

		if((MiningFunction.REGRESSION).equals(this.miningFunction)){
			parentNode.setScore(null);

			initScore(parentNode, node);
		} else

		if((MiningFunction.CLASSIFICATION).equals(this.miningFunction)){
			initScoreDistribution(parentNode, node);
		} else

		{
			throw new IllegalArgumentException();
		}
	}
}
 
Example #21
Source File: TreeModelCompactor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void exitNode(Node node){
	Predicate predicate = node.getPredicate();

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		if((MiningFunction.REGRESSION).equals(this.miningFunction)){
			parentNode.setScore(null);

			initScore(parentNode, node);
			replaceChildWithGrandchildren(parentNode, node);
		} else

		if((MiningFunction.CLASSIFICATION).equals(this.miningFunction)){

			// Replace intermediate nodes, but not terminal nodes
			if(node.hasNodes()){
				replaceChildWithGrandchildren(parentNode, node);
			}
		} else

		{
			throw new IllegalArgumentException();
		}
	}
}
 
Example #22
Source File: PredicateInternerTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void internSimpleSetPredicate(){
	FieldName name = FieldName.create("x");

	Predicate left = new SimpleSetPredicate(name, SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "1"));
	Predicate right = new SimpleSetPredicate(name, SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "\"1\""));

	checkTree(left, right);
}
 
Example #23
Source File: TreeModelCompactor.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void exitNode(Node node){
	Predicate predicate = node.getPredicate();

	if(predicate instanceof True){
		Node parentNode = getParentNode();

		if(parentNode == null){
			return;
		}

		initScore(parentNode, node);
		replaceChildWithGrandchildren(parentNode, node);
	}
}
 
Example #24
Source File: LeafNode.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@ValueConstructor
public LeafNode(@Property("score") Object score, @Property("predicate") Predicate predicate){
	super(score, predicate);
}
 
Example #25
Source File: PredicateInternerTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void checkTree(Predicate left, Predicate right){
	checkTree(new LeafNode(null, left), new LeafNode(null, right));
}
 
Example #26
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 #27
Source File: CountingBranchNode.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@ValueConstructor
public CountingBranchNode(@Property("score") Object score, @Property("predicate") Predicate predicate){
	super(score, predicate);
}
 
Example #28
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 #29
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private Boolean evaluate(Predicate predicate, Object... objects){
	Map<FieldName, ?> arguments = ModelEvaluatorTest.createArguments(objects);

	return evaluate(predicate, arguments);
}
 
Example #30
Source File: Tree.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<Object> selectValues(boolean indexAsValue, List<?> values, java.util.function.Predicate<Object> valueFilter, int cat_idx, boolean left){
	List<Object> result;

	if(left){
		result = new ArrayList<>();
	} else

	{
		result = new ArrayList<>(values);
	}

	int n = (this.cat_boundaries_[cat_idx + 1] - this.cat_boundaries_[cat_idx]);

	for(int i = 0; i < n; i++){

		for(int j = 0; j < 32; j++){
			int cat = (i * 32) + j;

			if(findInBitset(this.cat_threshold_, this.cat_boundaries_[cat_idx], n, cat)){
				Object value;

				if(indexAsValue){
					value = cat;
				} else

				{
					value = values.get(cat);
				} // End if

				if(left){
					result.add(value);
				} else

				{
					result.remove(value);
				}
			}
		}
	}

	result.removeIf(value -> !valueFilter.test(value));

	return result;
}