1   /*
2    *  Copyright (c) 1998-2004, 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.4 2004/07/21 17:10:05 akshay 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  
20  import org.jdom.Element;
21  
22  import gate.util.GateException;
23  
24  /**
25   * Stores data describing a dataset.
26   */
27  
28  public class DatasetDefintion implements Serializable{
29  
30    public DatasetDefintion(Element domElement) throws GateException{
31      if(!domElement.getName().equals("DATASET")) throw new GateException(
32      "Dataset defintion element is \"" + domElement.getName() +
33      "\" instead of \"DATASET\"!");
34  
35      //find instance the type
36      Element anElement = domElement.getChild("INSTANCE-TYPE");
37      if(anElement != null) instanceType = anElement.getTextTrim();
38      else throw new GateException(
39        "Required element \"INSTANCE-TYPE\" not present!");
40  
41      //find the attributes
42      int attrIndex = 0;
43      attributes = new ArrayList();
44      Iterator childrenIter = domElement.getChildren("ATTRIBUTE").iterator();
45      while(childrenIter.hasNext()){
46        Element child = (Element)childrenIter.next();
47        Attribute attribute = new Attribute(child);
48        if(attribute.isClass()){
49          if(classAttribute != null) throw new GateException(
50            "Attribute \""+ attribute.getName() +
51            "\" marked as class attribute but the class is already known to be\""+
52            classAttribute.getName() + "\"!");
53          classAttribute = attribute;
54          classIndex = attrIndex;
55        }
56        attributes.add(attribute);
57        attrIndex ++;
58      }
59  
60      if(classAttribute == null) throw new GateException(
61        "No class attribute defined!");
62    }
63  
64    public DatasetDefintion(){
65      attributes = new ArrayList();
66      classAttribute = null;
67      classIndex = -1;
68      instanceType = null;
69    }
70  
71  
72    public String toString(){
73      StringBuffer res = new StringBuffer();
74      res.append("Instance type: " + instanceType + "\n");
75      Iterator attrIter = attributes.iterator();
76      while(attrIter.hasNext()){
77        res.append("Attribute:" + attrIter.next().toString() + "\n");
78      }
79      return res.toString();
80    }
81  
82  
83    public java.util.List getAttributes() {
84      return attributes;
85    }
86  
87    public Attribute getClassAttribute(){
88      return classAttribute;
89    }
90    public String getInstanceType() {
91      return instanceType;
92    }
93  
94    public int getClassIndex() {
95      return classIndex;
96    }
97    public void setClassAttribute(Attribute classAttribute) {
98      this.classAttribute = classAttribute;
99    }
100   public void setClassIndex(int classIndex) {
101     this.classIndex = classIndex;
102   }
103   public void setInstanceType(String instanceType) {
104     this.instanceType = instanceType;
105   }
106 
107   protected java.util.List attributes;
108   protected Attribute classAttribute = null;
109   protected String instanceType;
110 
111   protected int classIndex;
112 }