org.jpmml.manager.AssociationModelManager Maven / Gradle / Ivy
/*
* Copyright (c) 2013 University of Tartu
*/
package org.jpmml.manager;
import java.util.*;
import org.dmg.pmml.*;
import com.google.common.collect.*;
import static com.google.common.base.Preconditions.*;
public class AssociationModelManager extends ModelManager implements HasEntityRegistry {
private AssociationModel associationModel = null;
public AssociationModelManager(){
}
public AssociationModelManager(PMML pmml){
this(pmml, find(pmml.getContent(), AssociationModel.class));
}
public AssociationModelManager(PMML pmml, AssociationModel associationModel){
super(pmml);
this.associationModel = associationModel;
}
@Override
public String getSummary(){
return "Association rules model";
}
public FieldName getActiveField(){
List activeFields = getActiveFields();
if(activeFields.size() < 1){
throw new InvalidFeatureException("No active fields", getMiningSchema());
} else
if(activeFields.size() > 1){
throw new InvalidFeatureException("Too many active fields", getMiningSchema());
}
return activeFields.get(0);
}
/**
* @return null
Always.
*/
@Override
public Target getTarget(FieldName name){
return null;
}
@Override
public AssociationModel getModel(){
checkState(this.associationModel != null);
return this.associationModel;
}
/**
* @see #getModel()
*/
public AssociationModel createModel(Double minimumSupport, Double minimumConfidence){
checkState(this.associationModel == null);
this.associationModel = new AssociationModel(new MiningSchema(), MiningFunctionType.ASSOCIATION_RULES, 0, minimumSupport, minimumConfidence, 0, 0, 0);
getModels().add(this.associationModel);
return this.associationModel;
}
/**
* @return A bidirectional map between {@link Item#getId Item identifiers} and {@link Item instances}.
*/
public BiMap getItemRegistry(){
BiMap result = HashBiMap.create();
List- items = getItems();
for(Item item : items){
result.put(item.getId(), item);
}
return result;
}
/**
* @return A bidirectional map between {@link Itemset#getId() Itemset identifiers} and {@link Itemset instances}.
*/
public BiMap
getItemsetRegistry(){
BiMap result = HashBiMap.create();
List itemsets = getItemsets();
for(Itemset itemset : itemsets){
result.put(itemset.getId(), itemset);
}
return result;
}
@Override
public BiMap getEntityRegistry(){
BiMap result = HashBiMap.create();
List associationRules = getAssociationRules();
EntityUtil.putAll(associationRules, result);
return result;
}
public List- getItems(){
AssociationModel associationModel = getModel();
return associationModel.getItems();
}
public List
getItemsets(){
AssociationModel associationModel = getModel();
return associationModel.getItemsets();
}
public List getAssociationRules(){
AssociationModel associationModel = getModel();
return associationModel.getAssociationRules();
}
}