
aima.core.learning.knowledge.FOLDataSetDomain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
package aima.core.learning.knowledge;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import aima.core.learning.framework.DataSetSpecification;
import aima.core.logic.fol.domain.FOLDomain;
/**
* @author Ciaran O'Reilly
*
*/
public class FOLDataSetDomain extends FOLDomain {
//
private static Pattern allowableCharactersRegEx = Pattern
.compile("[^a-zA-Z_$0-9]");
//
private DataSetSpecification dataSetSpecification;
private String trueGoalValue = null;
// Default example prefix, see pg679 of AIMA
private String examplePrefix = "X";
private List descriptionPredicateNames = new ArrayList();
private List descriptionDataSetNames = new ArrayList();
private Map dsToFOLNameMap = new HashMap();
//
// PUBLIC METHODS
//
public FOLDataSetDomain(DataSetSpecification dataSetSpecification,
String trueGoalValue) {
this.dataSetSpecification = dataSetSpecification;
this.trueGoalValue = trueGoalValue;
constructFOLDomain();
}
public String getDataSetTargetName() {
return dataSetSpecification.getTarget();
}
public String getGoalPredicateName() {
return getFOLName(dataSetSpecification.getTarget());
}
public String getTrueGoalValue() {
return trueGoalValue;
}
public List getDescriptionPredicateNames() {
return descriptionPredicateNames;
}
public List getDescriptionDataSetNames() {
return descriptionDataSetNames;
}
public boolean isMultivalued(String descriptiveDataSetName) {
List possibleValues = dataSetSpecification
.getPossibleAttributeValues(descriptiveDataSetName);
// If more than two possible values
// then is multivalued
if (possibleValues.size() > 2) {
return true;
}
// If one of the possible values for the attribute
// matches the true goal value then consider
// it not being multivalued.
for (String pv : possibleValues) {
if (trueGoalValue.equals(pv)) {
return false;
}
}
return true;
}
public String getExampleConstant(int egNo) {
String egConstant = examplePrefix + egNo;
addConstant(egConstant);
return egConstant;
}
public String getFOLName(String dsName) {
String folName = dsToFOLNameMap.get(dsName);
if (null == folName) {
folName = dsName;
if (!Character.isJavaIdentifierStart(dsName.charAt(0))) {
folName = "_" + dsName;
}
folName = allowableCharactersRegEx.matcher(folName).replaceAll("_");
dsToFOLNameMap.put(dsName, folName);
}
return folName;
}
//
// PRIVATE METHODS
//
private void constructFOLDomain() {
// Ensure the target predicate is included
addPredicate(getFOLName(dataSetSpecification.getTarget()));
// Create the descriptive predicates
for (String saName : dataSetSpecification.getNamesOfStringAttributes()) {
if (dataSetSpecification.getTarget().equals(saName)) {
// Don't add the target to the descriptive predicates
continue;
}
String folSAName = getFOLName(saName);
// Add a predicate for the attribute
addPredicate(folSAName);
descriptionPredicateNames.add(folSAName);
descriptionDataSetNames.add(saName);
List attributeValues = dataSetSpecification
.getPossibleAttributeValues(saName);
// If a multivalued attribute need to setup
// Constants for the different possible values
if (isMultivalued(saName)) {
for (String av : attributeValues) {
addConstant(getFOLName(av));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy