001    /*
002     * Created on Apr 13, 2005
003     *
004     */
005    package aima.learning.framework;
006    
007    /**
008     * @author Ravi Mohan
009     * 
010     */
011    import java.util.ArrayList;
012    import java.util.Iterator;
013    import java.util.List;
014    
015    public class DataSetSpecification {
016            List<AttributeSpecification> attributeSpecifications;
017    
018            private String targetAttribute;
019    
020            public DataSetSpecification() {
021                    this.attributeSpecifications = new ArrayList<AttributeSpecification>();
022            }
023    
024            public boolean isValid(List<String> uncheckedAttributes) {
025                    if (attributeSpecifications.size() != uncheckedAttributes.size()) {
026                            throw new RuntimeException("size mismatch specsize = "
027                                            + attributeSpecifications.size() + " attrbutes size = "
028                                            + uncheckedAttributes.size());
029                    }
030                    Iterator<AttributeSpecification> attributeSpecIter = attributeSpecifications
031                                    .iterator();
032                    Iterator<String> valueIter = uncheckedAttributes.iterator();
033                    while (valueIter.hasNext() && attributeSpecIter.hasNext()) {
034                            if (!(attributeSpecIter.next().isValid(valueIter.next()))) {
035                                    return false;
036                            }
037                    }
038                    return true;
039            }
040    
041            /**
042             * @return Returns the targetAttribute.
043             */
044            public String getTarget() {
045                    return targetAttribute;
046            }
047    
048            public List<String> getPossibleAttributeValues(String attributeName) {
049                    for (AttributeSpecification as : attributeSpecifications) {
050                            if (as.getAttributeName().equals(attributeName)) {
051                                    return ((StringAttributeSpecification) as)
052                                                    .possibleAttributeValues();
053                            }
054                    }
055                    throw new RuntimeException("No such attribute" + attributeName);
056            }
057    
058            public List<String> getAttributeNames() {
059                    List<String> names = new ArrayList<String>();
060                    for (AttributeSpecification as : attributeSpecifications) {
061                            names.add(as.getAttributeName());
062                    }
063                    return names;
064            }
065    
066            public void defineStringAttribute(String name, String[] attributeValues) {
067                    attributeSpecifications.add(new StringAttributeSpecification(name,
068                                    attributeValues));
069                    setTarget(name);// target defaults to last column added
070            }
071    
072            /**
073             * @param target
074             *            The targetAttribute to set.
075             */
076            public void setTarget(String target) {
077                    this.targetAttribute = target;
078            }
079    
080            public AttributeSpecification getAttributeSpecFor(String name) {
081                    for (AttributeSpecification spec : attributeSpecifications) {
082                            if (spec.getAttributeName().equals(name)) {
083                                    return spec;
084                            }
085                    }
086                    throw new RuntimeException("no attribute spec for  " + name);
087            }
088    
089            public void defineNumericAttribute(String name) {
090                    attributeSpecifications.add(new NumericAttributeSpecification(name));
091            }
092    
093            public List<String> getNamesOfStringAttributes() {
094                    List<String> names = new ArrayList<String>();
095                    for (AttributeSpecification spec : attributeSpecifications) {
096                            if (spec instanceof StringAttributeSpecification) {
097                                    names.add(spec.getAttributeName());
098                            }
099                    }
100                    return names;
101            }
102    
103    }