11package aima .learning .knowledge ;
22
33import java .util .ArrayList ;
4+ import java .util .HashMap ;
45import java .util .List ;
6+ import java .util .Map ;
7+ import java .util .regex .Pattern ;
58
69import aima .learning .framework .DataSetSpecification ;
710import aima .logic .fol .domain .FOLDomain ;
1114 *
1215 */
1316public class FOLDataSetDomain extends FOLDomain {
17+ //
18+ private static Pattern allowableCharactersRegEx = Pattern .compile ("[^a-zA-Z_$0-9]" );
19+ //
1420 private DataSetSpecification dataSetSpecification ;
1521 private String trueGoalValue = null ;
1622 // Default example prefix, see pg679 of AIMA
1723 private String examplePrefix = "X" ;
1824 private List <String > descriptionPredicateNames = new ArrayList <String >();
25+ private List <String > descriptionDataSetNames = new ArrayList <String >();
26+ private Map <String , String > dsToFOLNameMap = new HashMap <String , String >();
1927
2028 //
2129 // PUBLIC METHODS
@@ -26,10 +34,14 @@ public FOLDataSetDomain(DataSetSpecification dataSetSpecification, String trueGo
2634 constructFOLDomain ();
2735 }
2836
29- public String getGoalPredicateName () {
37+ public String getDataSetTargetName () {
3038 return dataSetSpecification .getTarget ();
3139 }
3240
41+ public String getGoalPredicateName () {
42+ return getFOLName (dataSetSpecification .getTarget ());
43+ }
44+
3345 public String getTrueGoalValue () {
3446 return trueGoalValue ;
3547 }
@@ -38,8 +50,12 @@ public List<String> getDescriptionPredicateNames() {
3850 return descriptionPredicateNames ;
3951 }
4052
41- public boolean isMultivalued (String descriptivePredicateName ) {
42- List <String > possibleValues = dataSetSpecification .getPossibleAttributeValues (descriptivePredicateName );
53+ public List <String > getDescriptionDataSetNames () {
54+ return descriptionDataSetNames ;
55+ }
56+
57+ public boolean isMultivalued (String descriptiveDataSetName ) {
58+ List <String > possibleValues = dataSetSpecification .getPossibleAttributeValues (descriptiveDataSetName );
4359 // If more than two possible values
4460 // then is multivalued
4561 if (possibleValues .size () > 2 ) {
@@ -63,29 +79,45 @@ public String getExampleConstant(int egNo) {
6379 return egConstant ;
6480 }
6581
82+ public String getFOLName (String dsName ) {
83+ String folName = dsToFOLNameMap .get (dsName );
84+ if (null == folName ) {
85+ folName = dsName ;
86+ if (!Character .isJavaIdentifierStart (dsName .charAt (0 ))) {
87+ folName = "_" +dsName ;
88+ }
89+ folName = allowableCharactersRegEx .matcher (folName ).replaceAll ("_" );
90+ dsToFOLNameMap .put (dsName , folName );
91+ }
92+
93+ return folName ;
94+ }
95+
6696 //
6797 // PRIVATE METHODS
6898 //
6999 private void constructFOLDomain () {
70100 // Ensure the target predicate is included
71- addPredicate (dataSetSpecification .getTarget ());
101+ addPredicate (getFOLName ( dataSetSpecification .getTarget () ));
72102 // Create the descriptive predicates
73103 for (String saName : dataSetSpecification .getNamesOfStringAttributes ()) {
74104 if (dataSetSpecification .getTarget ().equals (saName )) {
75105 // Don't add the target to the descriptive predicates
76106 continue ;
77107 }
108+ String folSAName = getFOLName (saName );
78109 // Add a predicate for the attribute
79- addPredicate (saName );
110+ addPredicate (folSAName );
80111
81- descriptionPredicateNames .add (saName );
112+ descriptionPredicateNames .add (folSAName );
113+ descriptionDataSetNames .add (saName );
82114
83115 List <String > attributeValues = dataSetSpecification .getPossibleAttributeValues (saName );
84116 // If a multivalued attribute need to setup
85117 // Constants for the different possible values
86118 if (isMultivalued (saName )) {
87119 for (String av : attributeValues ) {
88- addConstant (av );
120+ addConstant (getFOLName ( av ) );
89121 }
90122 }
91123 }
0 commit comments