1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 19/11/2002
10   *
11   *  $Id: DatasetDefintion.java,v 1.6 2005/10/18 11:52:16 nirajaswani Exp $
12   *
13   */
14  package gate.creole.ml;
15  
16  import java.io.Serializable;
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.jdom.Element;
22  
23  import gate.util.GateException;
24  
25  /**
26   * Stores data describing a dataset.
27   */
28  
29  public class DatasetDefintion implements Serializable{
30  
31    public DatasetDefintion(Element domElement) throws GateException{
32      if(!domElement.getName().equals("DATASET")) throw new GateException(
33      "Dataset defintion element is \"" + domElement.getName() +
34      "\" instead of \"DATASET\"!");
35  
36      //find instance the type
37      Element anElement = domElement.getChild("INSTANCE-TYPE");
38      if(anElement != null) instanceType = anElement.getTextTrim();
39      else throw new GateException(
40        "Required element \"INSTANCE-TYPE\" not present!");
41  
42      //find the attributes
43      int attrIndex = 0;
44      attributes = new ArrayList();
45      Iterator childrenIter = domElement.getChildren("ATTRIBUTE").iterator();
46      while(childrenIter.hasNext()){
47        Element child = (Element)childrenIter.next();
48        Attribute attribute = new Attribute(child);
49        if(attribute.isClass()){
50          if(classAttribute != null) throw new GateException(
51            "Attribute \""+ attribute.getName() +
52            "\" marked as class attribute but the class is already known to be\""+
53            classAttribute.getName() + "\"!");
54          classAttribute = attribute;
55          classIndex = attrIndex;
56        }
57        attributes.add(attribute);
58        attrIndex ++;
59      }
60    
61    // parsing ATTRIBUTELIST to support range
62    List attributeList = domElement.getChildren("ATTRIBUTELIST");
63    if(attributeList != null) {
64      Iterator childrenSerieIter = attributeList.iterator();
65      while(childrenSerieIter.hasNext()){
66        Element child = (Element)childrenSerieIter.next();
67        List attributelist = Attribute.parseSeries(child);
68        attributes.addAll(attributelist);
69        attrIndex += attributelist.size();
70      }
71    }
72    
73      if(classAttribute == null) throw new GateException(
74        "No class attribute defined!");
75    }
76  
77    public DatasetDefintion(){
78      attributes = new ArrayList();
79      classAttribute = null;
80      classIndex = -1;
81      instanceType = null;
82    }
83  
84  
85    public String toString(){
86      StringBuffer res = new StringBuffer();
87      res.append("Instance type: " + instanceType + "\n");
88      Iterator attrIter = attributes.iterator();
89      while(attrIter.hasNext()){
90        res.append("Attribute:" + attrIter.next().toString() + "\n");
91      }
92      return res.toString();
93    }
94  
95  
96    public java.util.List getAttributes() {
97      return attributes;
98    }
99  
100   public Attribute getClassAttribute(){
101     return classAttribute;
102   }
103   public String getInstanceType() {
104     return instanceType;
105   }
106 
107   public int getClassIndex() {
108     return classIndex;
109   }
110   public void setClassAttribute(Attribute classAttribute) {
111     this.classAttribute = classAttribute;
112   }
113   public void setClassIndex(int classIndex) {
114     this.classIndex = classIndex;
115   }
116   public void setInstanceType(String instanceType) {
117     this.instanceType = instanceType;
118   }
119 
120   protected java.util.List attributes;
121   protected Attribute classAttribute = null;
122   protected String instanceType;
123 
124   protected int classIndex;
125 }