This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 251
/
Attribute.java
1534 lines (1340 loc) · 46.6 KB
/
Attribute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Attribute.java
* Copyright (C) 1999-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.core;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
/**
* Class for handling an attribute. Once an attribute has been created, it can't
* be changed.
* <p>
*
* The following attribute types are supported:
* <ul>
* <li>numeric: <br/>
* This type of attribute represents a floating-point number.</li>
* <li>nominal: <br/>
* This type of attribute represents a fixed set of nominal values.</li>
* <li>string: <br/>
* This type of attribute represents a dynamically expanding set of nominal
* values. Usually used in text classification.</li>
* <li>date: <br/>
* This type of attribute represents a date, internally represented as
* floating-point number storing the milliseconds since January 1, 1970,
* 00:00:00 GMT. The string representation of the date must be <a
* href="http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html"
* target="_blank"> ISO-8601</a> compliant, the default is
* <code>yyyy-MM-dd'T'HH:mm:ss</code>.</li>
* <li>relational: <br/>
* This type of attribute can contain other attributes and is, e.g., used for
* representing Multi-Instance data. (Multi-Instance data consists of a nominal
* attribute containing the bag-id, then a relational attribute with all the
* attributes of the bag, and finally the class attribute.)</li>
* </ul>
*
* Typical usage (code from the main() method of this class):
* <p>
*
* <code>
* ... <br>
*
* // Create numeric attributes "length" and "weight" <br>
* Attribute length = new Attribute("length"); <br>
* Attribute weight = new Attribute("weight"); <br><br>
*
* // Create list to hold nominal values "first", "second", "third" <br>
* List<String> my_nominal_values = new ArrayList<String>(3); <br>
* my_nominal_values.add("first"); <br>
* my_nominal_values.add("second"); <br>
* my_nominal_values.add("third"); <br><br>
*
* // Create nominal attribute "position" <br>
* Attribute position = new Attribute("position", my_nominal_values);<br>
*
* ... <br>
* </code>
* <p>
*
* @author Eibe Frank (eibe@cs.waikato.ac.nz)
* @version $Revision$
*/
public class Attribute implements Copyable, Serializable, RevisionHandler {
/** for serialization */
static final long serialVersionUID = -742180568732916383L;
/** Constant set for numeric attributes. */
public static final int NUMERIC = 0;
/** Constant set for nominal attributes. */
public static final int NOMINAL = 1;
/** Constant set for attributes with string values. */
public static final int STRING = 2;
/** Constant set for attributes with date values. */
public static final int DATE = 3;
/** Constant set for relation-valued attributes. */
public static final int RELATIONAL = 4;
/** Constant set for symbolic attributes. */
public static final int ORDERING_SYMBOLIC = 0;
/** Constant set for ordered attributes. */
public static final int ORDERING_ORDERED = 1;
/** Constant set for modulo-ordered attributes. */
public static final int ORDERING_MODULO = 2;
/** The keyword used to denote the start of an arff attribute declaration */
public final static String ARFF_ATTRIBUTE = "@attribute";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_INTEGER = "integer";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_REAL = "real";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_NUMERIC = "numeric";
/** The keyword used to denote a string attribute */
public final static String ARFF_ATTRIBUTE_STRING = "string";
/** The keyword used to denote a date attribute */
public final static String ARFF_ATTRIBUTE_DATE = "date";
/** The keyword used to denote a relation-valued attribute */
public final static String ARFF_ATTRIBUTE_RELATIONAL = "relational";
/** The keyword used to denote the end of the declaration of a subrelation */
public final static String ARFF_END_SUBRELATION = "@end";
/** Strings longer than this will be stored compressed. */
protected static final int STRING_COMPRESS_THRESHOLD = 200;
/** The attribute's name. */
protected final/* @ spec_public non_null @ */String m_Name;
/** The attribute's type. */
protected/* @ spec_public @ */int m_Type;
/*
* @ invariant m_Type == NUMERIC || m_Type == DATE || m_Type == STRING ||
* m_Type == NOMINAL || m_Type == RELATIONAL;
*/
/** The attribute info (null for numeric attributes) */
protected AttributeInfo m_AttributeInfo;
/** The attribute's index. */
protected/* @ spec_public @ */int m_Index = -1;
/** The attribute's weight. */
protected double m_Weight = 1.0;
/** The meta data for the attribute. */
protected AttributeMetaInfo m_AttributeMetaInfo;
/**
* Constructor for a numeric attribute.
*
* @param attributeName the name for the attribute
*/
// @ requires attributeName != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName) {
this(attributeName, (ProtectedProperties)null);
}
/**
* Constructor for a numeric attribute, where metadata is supplied.
*
* @param attributeName the name for the attribute
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires metadata != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, ProtectedProperties metadata) {
m_Name = attributeName;
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for a numeric or string attribute. Provides an alternative
* way for creating string attributes.
*
* @param attributeName the name for the attribute
* @param createStringAttribute if true, a string attribute will be created, otherwise a numeric one.
*/
// @ requires attributeName != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, boolean createStringAttribute) {
this(attributeName, createStringAttribute, (ProtectedProperties)null);
}
/**
* Constructor for a numeric or string attribute, where metadata is supplied. Provides an alternative
* way for creating string attributes.
*
* @param attributeName the name for the attribute
* @param createStringAttribute if true, a string attribute will be created, otherwise a numeric one.
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires metadata != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, boolean createStringAttribute, ProtectedProperties metadata) {
m_Name = attributeName;
if (createStringAttribute) {
m_AttributeInfo = new NominalAttributeInfo((List<String>)null, attributeName);
m_Type = STRING;
}
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for a date attribute.
*
* @param attributeName the name for the attribute
* @param dateFormat a string suitable for use with SimpleDateFormatter for
* parsing dates.
*/
// @ requires attributeName != null;
// @ requires dateFormat != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, String dateFormat) {
this(attributeName, dateFormat, (ProtectedProperties)null);
}
/**
* Constructor for a date attribute, where metadata is supplied.
*
* @param attributeName the name for the attribute
* @param dateFormat a string suitable for use with SimpleDateFormatter for
* parsing dates.
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires dateFormat != null;
// @ requires metadata != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, String dateFormat,
ProtectedProperties metadata) {
m_Name = attributeName;
m_Type = DATE;
m_AttributeInfo = new DateAttributeInfo(dateFormat);
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for nominal attributes and string attributes. If a null vector
* of attribute values is passed to the method, the attribute is assumed to be
* a string.
*
* @param attributeName the name for the attribute
* @param attributeValues a vector of strings denoting the attribute values.
* Null if the attribute is a string attribute.
*/
// @ requires attributeName != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, List<String> attributeValues) {
this(attributeName, attributeValues, (ProtectedProperties)null);
}
/**
* Constructor for nominal attributes and string attributes, where metadata is
* supplied. If a null vector of attribute values is passed to the method, the
* attribute is assumed to be a string.
*
* @param attributeName the name for the attribute
* @param attributeValues a vector of strings denoting the attribute values.
* Null if the attribute is a string attribute.
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires metadata != null;
/*
* @ ensures m_Name == attributeName; ensures m_Index == -1; ensures
* attributeValues == null && m_Type == STRING || attributeValues != null &&
* m_Type == NOMINAL && m_Values.size() == attributeValues.size(); signals
* (IllegalArgumentException ex) (* if duplicate strings in attributeValues
* *);
*/
public Attribute(String attributeName, List<String> attributeValues,
ProtectedProperties metadata) {
m_Name = attributeName;
m_AttributeInfo = new NominalAttributeInfo(attributeValues, attributeName);
if (attributeValues == null) {
m_Type = STRING;
} else {
m_Type = NOMINAL;
}
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for relation-valued attributes.
*
* @param attributeName the name for the attribute
* @param header an Instances object specifying the header of the relation.
*/
public Attribute(String attributeName, Instances header) {
this(attributeName, header, (ProtectedProperties)null);
}
/**
* Constructor for relation-valued attributes.
*
* @param attributeName the name for the attribute
* @param header an Instances object specifying the header of the relation.
* @param metadata the attribute's properties
*/
public Attribute(String attributeName, Instances header,
ProtectedProperties metadata) {
if (header.numInstances() > 0) {
throw new IllegalArgumentException("Header for relation-valued "
+ "attribute should not contain " + "any instances");
}
m_Name = attributeName;
m_Type = RELATIONAL;
m_AttributeInfo = new RelationalAttributeInfo(header);
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Produces a shallow copy of this attribute.
*
* @return a copy of this attribute with the same index
*/
// @ also ensures \result instanceof Attribute;
@Override
public/* @ pure non_null @ */Object copy() {
return copy(m_Name);
}
/**
* Returns an enumeration of all the attribute's values if the attribute is
* nominal, string, or relation-valued, null otherwise.
*
* @return enumeration of all the attribute's values
*/
public final/* @ pure @ */Enumeration<Object> enumerateValues() {
if (isNominal() || isString()) {
final Enumeration<Object> ee =
new WekaEnumeration<Object>(((NominalAttributeInfo)m_AttributeInfo).m_Values);
return new Enumeration<Object>() {
@Override
public boolean hasMoreElements() {
return ee.hasMoreElements();
}
@Override
public Object nextElement() {
Object oo = ee.nextElement();
if (oo instanceof SerializedObject) {
return ((SerializedObject) oo).getObject();
} else {
return oo;
}
}
};
}
return null;
}
/**
* Tests if given attribute is equal to this attribute. Attribute indices are ignored in the comparison.
*
* @param other the Object to be compared to this attribute
* @return true if the given attribute is equal to this attribute
*/
@Override
public final/* @ pure @ */boolean equals(Object other) {
return (equalsMsg(other) == null);
}
/**
* Returns a hash code for this attribute based on its name.
*
* @return the hash code
*/
@Override
public final/* @ pure @ */int hashCode() {
return name().hashCode();
}
/**
* Tests if given attribute is equal to this attribute. If they're not the
* same a message detailing why they differ will be returned, otherwise null.
* Attribute indices are ignored in the comparison.
*
* @param other the Object to be compared to this attribute
* @return null if the given attribute is equal to this attribute
*/
public final String equalsMsg(Object other) {
if (other == null) {
return "Comparing with null object";
}
if (!(other.getClass().equals(this.getClass()))) {
return "Object has wrong class";
}
Attribute att = (Attribute) other;
if (!m_Name.equals(att.m_Name)) {
return "Names differ: " + m_Name + " != " + att.m_Name;
}
if (isNominal() && att.isNominal()) {
if (((NominalAttributeInfo)m_AttributeInfo).m_Values.size() !=
((NominalAttributeInfo)att.m_AttributeInfo).m_Values.size()) {
return "Different number of labels: " + ((NominalAttributeInfo)m_AttributeInfo).m_Values.size() + " != "
+ ((NominalAttributeInfo)att.m_AttributeInfo).m_Values.size();
}
for (int i = 0; i < ((NominalAttributeInfo)m_AttributeInfo).m_Values.size(); i++) {
if (!((NominalAttributeInfo)m_AttributeInfo).m_Values.get(i).
equals(((NominalAttributeInfo)att.m_AttributeInfo).m_Values.get(i))) {
return "Labels differ at position " + (i + 1) + ": "
+ ((NominalAttributeInfo)m_AttributeInfo).m_Values.get(i) + " != " +
((NominalAttributeInfo)att.m_AttributeInfo).m_Values.get(i);
}
}
return null;
}
if (isRelationValued() && att.isRelationValued()) {
return ((RelationalAttributeInfo)m_AttributeInfo).m_Header.equalHeadersMsg(((RelationalAttributeInfo)att.m_AttributeInfo).m_Header);
}
if ((type() != att.type())) {
return "Types differ: " + typeToString(this) + " != " + typeToString(att);
}
return null;
}
/**
* Returns a string representation of the attribute type.
*
* @param att the attribute to return the type string for
* @return the string representation of the attribute type
*/
public static String typeToString(Attribute att) {
return typeToString(att.type());
}
/**
* Returns a string representation of the attribute type.
*
* @param type the type of the attribute
* @return the string representation of the attribute type
*/
public static String typeToString(int type) {
String result;
switch (type) {
case NUMERIC:
result = "numeric";
break;
case NOMINAL:
result = "nominal";
break;
case STRING:
result = "string";
break;
case DATE:
result = "date";
break;
case RELATIONAL:
result = "relational";
break;
default:
result = "unknown(" + type + ")";
}
return result;
}
/**
* Returns a short string representation of the attribute type.
*
* @param att the attribute to return the type string for
* @return the string representation of the attribute type
*/
public static String typeToStringShort(Attribute att) {
return typeToStringShort(att.type());
}
/**
* Returns a short string representation of the attribute type.
*
* @param type the type of the attribute
* @return the string representation of the attribute type
*/
public static String typeToStringShort(int type) {
String result;
switch (type) {
case NUMERIC:
result = "Num";
break;
case NOMINAL:
result = "Nom";
break;
case STRING:
result = "Str";
break;
case DATE:
result = "Dat";
break;
case RELATIONAL:
result = "Rel";
break;
default:
result = "???";
}
return result;
}
/**
* Returns the index of this attribute.
*
* @return the index of this attribute
*/
// @ ensures \result == m_Index;
public final/* @ pure @ */int index() {
return m_Index;
}
/**
* Returns the index of a given attribute value. (The index of the first
* occurence of this value.)
*
* @param value the value for which the index is to be returned
* @return the index of the given attribute value if attribute is nominal or a
* string, -1 if it is not or the value can't be found
*/
public final int indexOfValue(String value) {
if (!isNominal() && !isString()) {
return -1;
}
Object store = value;
if (value.length() > STRING_COMPRESS_THRESHOLD) {
try {
store = new SerializedObject(value, true);
} catch (Exception ex) {
System.err.println("Couldn't compress string attribute value -"
+ " searching uncompressed.");
}
}
Integer val = ((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.get(store);
if (val == null) {
return -1;
} else {
return val.intValue();
}
}
/**
* Test if the attribute is nominal.
*
* @return true if the attribute is nominal
*/
// @ ensures \result <==> (m_Type == NOMINAL);
public final/* @ pure @ */boolean isNominal() {
return (m_Type == NOMINAL);
}
/**
* Tests if the attribute is numeric.
*
* @return true if the attribute is numeric
*/
// @ ensures \result <==> ((m_Type == NUMERIC) || (m_Type == DATE));
public final/* @ pure @ */boolean isNumeric() {
return ((m_Type == NUMERIC) || (m_Type == DATE));
}
/**
* Tests if the attribute is relation valued.
*
* @return true if the attribute is relation valued
*/
// @ ensures \result <==> (m_Type == RELATIONAL);
public final/* @ pure @ */boolean isRelationValued() {
return (m_Type == RELATIONAL);
}
/**
* Tests if the attribute is a string.
*
* @return true if the attribute is a string
*/
// @ ensures \result <==> (m_Type == STRING);
public final/* @ pure @ */boolean isString() {
return (m_Type == STRING);
}
/**
* Tests if the attribute is a date type.
*
* @return true if the attribute is a date type
*/
// @ ensures \result <==> (m_Type == DATE);
public final/* @ pure @ */boolean isDate() {
return (m_Type == DATE);
}
/**
* Returns the attribute's name.
*
* @return the attribute's name as a string
*/
// @ ensures \result == m_Name;
public final/* @ pure @ */String name() {
return m_Name;
}
/**
* Returns the number of attribute values. Returns 0 for attributes that are
* not either nominal, string, or relation-valued.
*
* @return the number of attribute values
*/
public final/* @ pure @ */int numValues() {
if (!isNominal() && !isString() && !isRelationValued()) {
return 0;
} else {
return ((NominalAttributeInfo)m_AttributeInfo).m_Values.size();
}
}
/**
* Returns a description of this attribute in ARFF format. Quotes strings if
* they contain whitespace characters, or if they are a question mark.
*
* @return a description of this attribute as a string
*/
@Override
public final String toString() {
StringBuffer text = new StringBuffer();
text.append(ARFF_ATTRIBUTE).append(" ").append(Utils.quote(m_Name))
.append(" ");
switch (m_Type) {
case NOMINAL:
text.append('{');
Enumeration<Object> enu = enumerateValues();
while (enu.hasMoreElements()) {
text.append(Utils.quote((String) enu.nextElement()));
if (enu.hasMoreElements()) {
text.append(',');
}
}
text.append('}');
text.append((weight() != 1.0) ? " {" + weight() + "}" : "");
break;
case NUMERIC:
text.append(ARFF_ATTRIBUTE_NUMERIC);
text.append((weight() != 1.0) ? " {" + weight() + "}" : "");
break;
case STRING:
text.append(ARFF_ATTRIBUTE_STRING);
text.append((weight() != 1.0) ? " {" + weight() + "}" : "");
break;
case DATE:
text.append(ARFF_ATTRIBUTE_DATE).append(" ")
.append(Utils.quote(((DateAttributeInfo)m_AttributeInfo).m_DateFormat.toPattern()));
text.append((weight() != 1.0) ? " {" + weight() + "}" : "");
break;
case RELATIONAL:
text.append(ARFF_ATTRIBUTE_RELATIONAL);
text.append((weight() != 1.0) ? " {" + weight() + "}" : "");
text.append("\n");
Enumeration<Attribute> enm = ((RelationalAttributeInfo)m_AttributeInfo).m_Header.enumerateAttributes();
while (enm.hasMoreElements()) {
text.append(enm.nextElement()).append("\n");
}
text.append(ARFF_END_SUBRELATION).append(" ").append(Utils.quote(m_Name));
break;
default:
text.append("UNKNOWN");
break;
}
return text.toString();
}
/**
* Returns the attribute's type as an integer.
*
* @return the attribute's type.
*/
// @ ensures \result == m_Type;
public final/* @ pure @ */int type() {
return m_Type;
}
/**
* Returns the Date format pattern in case this attribute is of type DATE,
* otherwise an empty string.
*
* @return the date format pattern
* @see java.text.SimpleDateFormat
*/
public final String getDateFormat() {
if (isDate()) {
return ((DateAttributeInfo)m_AttributeInfo).m_DateFormat.toPattern();
} else {
return "";
}
}
/**
* Returns a value of a nominal or string attribute. Returns an empty string
* if the attribute is neither a string nor a nominal attribute.
*
* @param valIndex the value's index
* @return the attribute's value as a string
*/
public final/* @ non_null pure @ */String value(int valIndex) {
if (!isNominal() && !isString()) {
return "";
} else {
Object val = ((NominalAttributeInfo)m_AttributeInfo).m_Values.get(valIndex);
// If we're storing strings compressed, uncompress it.
if (val instanceof SerializedObject) {
val = ((SerializedObject) val).getObject();
}
return (String) val;
}
}
/**
* Returns the header info for a relation-valued attribute, null if the
* attribute is not relation-valued.
*
* @return the attribute's value as an Instances object
*/
public final/* @ non_null pure @ */Instances relation() {
if (!isRelationValued()) {
return null;
} else {
return ((RelationalAttributeInfo)m_AttributeInfo).m_Header;
}
}
/**
* Returns a value of a relation-valued attribute. Returns null if the
* attribute is not relation-valued.
*
* @param valIndex the value's index
* @return the attribute's value as an Instances object
*/
public final/* @ non_null pure @ */Instances relation(int valIndex) {
if (!isRelationValued()) {
return null;
} else {
return (Instances) ((RelationalAttributeInfo)m_AttributeInfo).m_Values.get(valIndex);
}
}
/**
* Constructor for a numeric attribute with a particular index.
*
* @param attributeName the name for the attribute
* @param index the attribute's index
*/
// @ requires attributeName != null;
// @ requires index >= 0;
// @ ensures m_Name == attributeName;
// @ ensures m_Index == index;
public Attribute(String attributeName, int index) {
this(attributeName);
m_Index = index;
}
/**
* Constructor for date attributes with a particular index.
*
* @param attributeName the name for the attribute
* @param dateFormat a string suitable for use with SimpleDateFormatter for
* parsing dates. Null for a default format string.
* @param index the attribute's index
*/
// @ requires attributeName != null;
// @ requires index >= 0;
// @ ensures m_Name == attributeName;
// @ ensures m_Index == index;
public Attribute(String attributeName, String dateFormat, int index) {
this(attributeName, dateFormat);
m_Index = index;
}
/**
* Constructor for nominal attributes and string attributes with a particular
* index. If a null vector of attribute values is passed to the method, the
* attribute is assumed to be a string.
*
* @param attributeName the name for the attribute
* @param attributeValues a vector of strings denoting the attribute values.
* Null if the attribute is a string attribute.
* @param index the attribute's index
*/
// @ requires attributeName != null;
// @ requires index >= 0;
// @ ensures m_Name == attributeName;
// @ ensures m_Index == index;
public Attribute(String attributeName, List<String> attributeValues, int index) {
this(attributeName, attributeValues);
m_Index = index;
}
/**
* Constructor for a relation-valued attribute with a particular index.
*
* @param attributeName the name for the attribute
* @param header the header information for this attribute
* @param index the attribute's index
*/
// @ requires attributeName != null;
// @ requires index >= 0;
// @ ensures m_Name == attributeName;
// @ ensures m_Index == index;
public Attribute(String attributeName, Instances header, int index) {
this(attributeName, header);
m_Index = index;
}
/**
* Adds a string value to the list of valid strings for attributes of type
* STRING and returns the index of the string.
*
* @param value The string value to add
* @return the index assigned to the string, or -1 if the attribute is not of
* type Attribute.STRING
*/
/*
* @ requires value != null; ensures isString() && 0 <= \result && \result <
* m_Values.size() || ! isString() && \result == -1;
*/
public int addStringValue(String value) {
if (!isString()) {
return -1;
}
Object store = value;
if (value.length() > STRING_COMPRESS_THRESHOLD) {
try {
store = new SerializedObject(value, true);
} catch (Exception ex) {
System.err.println("Couldn't compress string attribute value -"
+ " storing uncompressed.");
}
}
Integer index = ((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.get(store);
if (index != null) {
return index.intValue();
} else {
int intIndex = ((NominalAttributeInfo)m_AttributeInfo).m_Values.size();
((NominalAttributeInfo)m_AttributeInfo).m_Values.add(store);
((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.put(store, new Integer(intIndex));
return intIndex;
}
}
/**
* Clear the map and list of values and set them to contain just the supplied
* value
*
* @param value the current (and only) value of this String attribute. If null
* then just the map is cleared.
*/
public void setStringValue(String value) {
if (!isString()) {
return;
}
((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.clear();
((NominalAttributeInfo)m_AttributeInfo).m_Values.clear();
if (value != null) {
addStringValue(value);
}
}
/**
* Adds a string value to the list of valid strings for attributes of type
* STRING and returns the index of the string. This method is more efficient
* than addStringValue(String) for long strings.
*
* @param src The Attribute containing the string value to add.
* @param index the index of the string value in the source attribute.
* @return the index assigned to the string, or -1 if the attribute is not of
* type Attribute.STRING
*/
/*
* @ requires src != null; requires 0 <= index && index < src.m_Values.size();
* ensures isString() && 0 <= \result && \result < m_Values.size() || !
* isString() && \result == -1;
*/
public int addStringValue(Attribute src, int index) {
if (!isString()) {
return -1;
}
Object store = ((NominalAttributeInfo)src.m_AttributeInfo).m_Values.get(index);
Integer oldIndex = ((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.get(store);
if (oldIndex != null) {
return oldIndex.intValue();
} else {
int intIndex = ((NominalAttributeInfo)m_AttributeInfo).m_Values.size();
((NominalAttributeInfo)m_AttributeInfo).m_Values.add(store);
((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.put(store, new Integer(intIndex));
return intIndex;
}
}
/**
* Adds a relation to a relation-valued attribute.
*
* @param value The value to add
* @return the index assigned to the value, or -1 if the attribute is not of
* type Attribute.RELATIONAL
*/
public int addRelation(Instances value) {
if (!isRelationValued()) {
return -1;
}
if (!((RelationalAttributeInfo)m_AttributeInfo).m_Header.equalHeaders(value)) {
throw new IllegalArgumentException("Incompatible value for "
+ "relation-valued attribute.\n" + ((RelationalAttributeInfo)m_AttributeInfo).m_Header.equalHeadersMsg(value));
}
Integer index = ((NominalAttributeInfo)m_AttributeInfo).m_Hashtable.get(value);
if (index != null) {
return index.intValue();
} else {
int intIndex = ((NominalAttributeInfo)m_AttributeInfo).m_Values.size();
((NominalAttributeInfo)m_AttributeInfo).m_Values.add(value);