📄 nominalattrinfo.java
字号:
+ str +" is not a valid attribute value for a fixed set"
+" "+name()+".\nPossible values are: ");
display_attr_values();
Error.err("fatal_error");
}
else if(suppress) {
//make the value unknown
val = Globals.UNKNOWN_NOMINAL_VAL;
}
else {
//add a new value to the array
String[] newValues = new String[values.length+1];
for(int i=0;i<values.length;i++)newValues[i]=values[i];
values = null;
values = newValues;
newValues = null;
values[values.length-1]=str;
System.out.print("Values : "+values[values.length-1]+" ");
}
}
}
set_nominal_val(av,val);
}
/** Sets the integer representation of the given AttrValue.
* @param av The AttrValue storing the new value.
* @param val The new value to be stored.
*/
public void set_nominal_val(AttrValue av, int val) {
if(av.type == AttrInfo.unknown)
av.type = AttrInfo.nominal;
else if(av.type != nominal)
Error.err("NominalAttrInfo::set_nominal_val: "
+"cannot assign a nominal value to a "
+attr_type_to_string(av.type)+" AttrValue-->fatal_error");
av.intVal = val; //host_to_net(val + NOMINAL_OFFSET);
}
/** Checks if this attribute's possible value set is fixed.
* @return TRUE if the value set is fixed, FALSE otherwise.
*/
public boolean is_fixed() {
return fixedValueSet;
}
/** Returns number of possible values that the attribute has.
* @return The number of possible values.
*/
public int num_values() {
return values.length;// + 1;
}
/** Returns the ith attribute value stored in this NominalAttrInfo's
* possible value set.
* @return A String value representing the value stored in the specified
* position of the possible values set.
* @param i The index value of the value queried.
*/
public String get_value(int i) {
// if (i == Globals.UNKNOWN_NOMINAL_VAL)
// return Globals.UNKNOWN_VAL_STR;
if (i == 0) return ""; //ADDED TO COMPENSTATE FOR UNKNOWN VALUE -JL
return values[i - Globals.FIRST_NOMINAL_VAL]; //USING NEW GLOBAL VALUE -JL
}
/** Checks if this AttrInfo subclass can be cast to a NominalAttrInfo class.
* @return Always returns TRUE for the NominalAttrInfo class.
*/
public boolean can_cast_to_nominal() {
return true;
}
/** Casts this AttrInfo subclass to a NominalAttrInfo class.
* @return A reference to this NominalAttrInfo object.
*/
public NominalAttrInfo cast_to_nominal() {
return this;
}
/** Reads in an attribute value from the specified BufferedReader. Attribute
* value to be read assumed to match AttrInfo. Although this function may
* potentially modify this NominalAttrInfo because it may add a new nominal
* value if fixedValueSet is FALSE. If the ignore flag is set, we read
* specially so that real values can be properly ignored. Then we always set
* an unknown value.
* @return The attribute value read.
* @param stream The BufferedReader containing the file to be read.
* @param isTest TRUE if this value is for a test Instance, FALSE otherwise.
* @param f The FileSchema for the file being read from.
*/
public AttrValue read_attr_value(BufferedReader stream, boolean isTest,
FileSchema f) {
AttrValue av = new AttrValue();
set_unknown(av);
if(get_ignore()) {
String dummy = f.read_word_on_same_line(stream, true, true);
//set_unknown(av);
}
else {
String str = f.read_word_on_same_line(stream, true,true);
set_nominal_string(av, str, isTest);
}
return av;
}
/** Reads in an attribute value from the specified StreamTokenizer. Attribute
* value to be read assumed to match AttrInfo. Although this function may
* potentially modify this NominalAttrInfo because it may add a new nominal
* value if fixedValueSet is FALSE. If the ignore flag is set, we read
* specially so that real values can be properly ignored. Then we always set
* an unknown value.
* @return The attribute value read.
* @param stream The StreamTokenizer containing the file to be read.
* @param isTest TRUE if this value is for a test Instance, FALSE otherwise.
* @param f The FileSchema for the file being read from.
*/
public AttrValue read_attr_value(StreamTokenizer stream, boolean isTest, FileSchema f) {
AttrValue av = new AttrValue();
//set_unknown(av);
if(get_ignore()) {
// String dummy = f.read_word_on_same_line(stream, true, true);
set_unknown(av);
}
else {
//String str = stream.sval; //CHANGED FOR ZOO DATASET -JL
String str = null;
if (stream.ttype == StreamTokenizer.TT_WORD) str = stream.sval;
else if (stream.ttype == StreamTokenizer.TT_NUMBER)
str = Double.toString(stream.nval);
else if (stream.ttype == (int)'?')
str = "?"; //FIX ERROR WITH READING '?' -JL
if (str.indexOf((int)'.') > -1)
str = str.substring(0,str.indexOf((int)'.'));
set_nominal_string(av, str, isTest);
}
return av;
}
/** Returns TRUE if given AttrValues are equivalent. Assumes that given
* AttrValues are both of the type described by the NominalAttrInfo object
* calling this function.
* @return TRUE if the AttrValues are equal, FALSE otherwise.
* @param val1 An AttrValue to be compared.
* @param val2 An AttrValue to be compared.
*/
public boolean equal_value(AttrValue val1, AttrValue val2) {
// ATTRDBG(check_valid_attr_value_type(val1); check_valid_attr_value_type(val2));
return (val1.intVal == val2.intVal);
}
/** Displays the attribute values in names file format. ProtectChars forces
* quoting of special characters (now only periods).
* @param stream Writer to which the display will be written.
* @param protectChars True if "\" and "." are to be protected by "\"
* characters, false otherwise.
*/
public void display_attr_values(Writer stream,
boolean protectChars) {
try{
// if the attribute is marked "ignore" say so here
if(get_ignore())
stream.write("ignore_attribute.\n");
// if the attribute is non-fixed, display "discrete" here
else if(!is_fixed())
stream.write("discrete.\n");
else {
int numValues = num_values();
for (int i = 0; i < numValues; i++) {
if (protectChars)
stream.write(protect_chars(get_value(Globals.FIRST_CATEGORY_VAL+i)));
else
stream.write(get_value(Globals.FIRST_CATEGORY_VAL+i));
if (i != numValues - 1)
stream.write(", ");
else
stream.write(".\n");
}
}
}catch(IOException e){e.printStackTrace();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -