org.jpmml.evaluator.association.Association Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmml-evaluator Show documentation
Show all versions of pmml-evaluator Show documentation
JPMML class model evaluator
/*
* Copyright (c) 2013 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see .
*/
package org.jpmml.evaluator.association;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.dmg.pmml.MiningFunction;
import org.dmg.pmml.OutputField;
import org.dmg.pmml.association.AssociationRule;
import org.jpmml.evaluator.AbstractComputable;
import org.jpmml.evaluator.HasEntityRegistry;
import org.jpmml.evaluator.HasRuleValues;
import org.jpmml.model.ToStringHelper;
/**
* @see MiningFunction#ASSOCIATION_RULES
*/
abstract
public class Association extends AbstractComputable implements HasEntityRegistry, HasRuleValues {
private List associationRules = null;
private BitSet antecedentFlags = null;
private BitSet consequentFlags = null;
protected Association(List associationRules, BitSet antecedentFlags, BitSet consequentFlags){
setAssociationRules(associationRules);
setAntecedentFlags(antecedentFlags);
setConsequentFlags(consequentFlags);
}
/**
* @throws UnsupportedOperationException Always.
*/
@Override
public Object getResult(){
throw new UnsupportedOperationException();
}
@Override
protected ToStringHelper toStringHelper(){
ToStringHelper helper = super.toStringHelper()
.add("antecedentFlags", getAntecedentFlags())
.add("consequentFlags", getConsequentFlags());
return helper;
}
@Override
public List getRuleValues(OutputField.Algorithm algorithm){
List associationRules = getAssociationRules();
BitSet flags;
switch(algorithm){
// "A rule is selected if its antecedent itemset is a subset of the input itemset"
case RECOMMENDATION:
flags = getAntecedentFlags();
break;
// "A rule is selected if its antecedent itemset is a subset of the input itemset, and its consequent itemset is not a subset of the input itemset"
case EXCLUSIVE_RECOMMENDATION:
flags = (BitSet)getAntecedentFlags().clone();
flags.andNot(getConsequentFlags());
break;
// "A rule is selected if its antecedent and consequent itemsets are included in the input itemset"
case RULE_ASSOCIATION:
flags = (BitSet)getAntecedentFlags().clone();
flags.and(getConsequentFlags());
break;
default:
throw new IllegalArgumentException();
}
int cardinality = flags.cardinality();
if(cardinality == 0){
return Collections.emptyList();
}
List result = new ArrayList<>(cardinality);
for(int i = flags.nextSetBit(0); i > -1; i = flags.nextSetBit(i + 1)){
AssociationRule associationRule = associationRules.get(i);
result.add(associationRule);
}
return result;
}
public List getAssociationRules(){
return this.associationRules;
}
private void setAssociationRules(List associationRules){
this.associationRules = Objects.requireNonNull(associationRules);
}
public BitSet getAntecedentFlags(){
return this.antecedentFlags;
}
private void setAntecedentFlags(BitSet antecedentFlags){
this.antecedentFlags = Objects.requireNonNull(antecedentFlags);
}
public BitSet getConsequentFlags(){
return this.consequentFlags;
}
private void setConsequentFlags(BitSet consequentFlags){
this.consequentFlags = Objects.requireNonNull(consequentFlags);
}
}