⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractexampleset.java

📁 著名的开源仿真软件yale
💻 JAVA
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002, 2003 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,  *          Katharina Morik, Oliver Ritthoff *      Artificial Intelligence Unit *      Computer Science Department *      University of Dortmund *      44221 Dortmund,  Germany *  email: yale@ls8.cs.uni-dortmund.de *  web:   http://yale.cs.uni-dortmund.de/ * *  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 2 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, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *  USA. */package edu.udo.cs.yale.example;import edu.udo.cs.yale.tools.Tools;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.operator.OperatorException;import edu.udo.cs.yale.operator.FatalException;import edu.udo.cs.yale.operator.ResultObjectAdapter;import edu.udo.cs.yale.operator.Saveable;import edu.udo.cs.yale.gui.SwingTools;import java.util.Map;import java.util.HashMap;import java.util.Collection;import java.util.Iterator;import java.io.File;import java.io.IOException;import java.io.FileWriter;import java.io.PrintWriter;import java.awt.Component;import javax.swing.JLabel;/** Implements wrapper methods of abstract example set.  *  Implements all ResultObject methods.<br>  * *  Apart from the interface methods the implementing classes must have a public single  *  argument clone constructor. This constructor is invoked by reflection from the clone method. *  Do not forget to call the superclass method.  * *  @version $Id: AbstractExampleSet.java,v 2.15 2003/08/24 22:42:33 mierswa Exp $ */public abstract class AbstractExampleSet extends ResultObjectAdapter implements ExampleSet, Saveable {    /** Zero argument constructor for subclasses. */    public AbstractExampleSet() {    }    /** Clone constructor. Does nothing. */    public AbstractExampleSet(AbstractExampleSet exampleSet) {    }    public ExampleReader getExampleReader() {	return new SelectionExampleReader(getExampleTable().getDataReader(), this);    }    public void addAllAttributes(Collection c) {	Iterator i = c.iterator();	while (i.hasNext())	    addAttribute((Attribute)i.next());    }    public void removeAllAttributes() {	while (getNumberOfAttributes() > 0)	    removeAttribute(getAttribute(0));    }    public Attribute removeAttribute(int index) {	Attribute attr = getAttribute(index);	removeAttribute(attr);	return attr;    }    public void setAttributes(ExampleSet exampleSet) {	removeAllAttributes();	for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {	    addAttribute(exampleSet.getAttribute(i));	}    }    public Attribute getLabel() { return getSpecialAttribute("label"); }    public void setLabel(Attribute label) { setSpecialAttribute("label", label); }    public Attribute getPredictedLabel() { return getSpecialAttribute("prediction"); }    public void setPredictedLabel(Attribute predictedLabel) { setSpecialAttribute("prediction", 										  predictedLabel); }    public void clearPredictedLabel() { setSpecialAttribute("prediction", null); }    public Attribute getWeight() { return getSpecialAttribute("weight"); }    public void setWeight(Attribute weight) { setSpecialAttribute("weight", weight); }    public Attribute getCluster() { return getSpecialAttribute("cluster"); }    public void setCluster(Attribute cluster) { setSpecialAttribute("cluster", cluster); }    public Attribute getIdAttribute() { return getSpecialAttribute("id"); }    public void setIdAttribute(Attribute idAttribute) { setSpecialAttribute("id", idAttribute); }    public Attribute createPredictedLabel() throws OperatorException {	Attribute label = getLabel();	if (label == null) {	    throw new FatalException("Cannot create predicted label (no label used). If you want to label unlabelled data, use a <label> tag in the attribute description file with sourcecol=\"none\"!");	}	Attribute predictedLabel = new Attribute(label, "prediction");	getExampleTable().addAttribute(predictedLabel);	setPredictedLabel(predictedLabel);	return predictedLabel;     }    public Attribute createWeightAttribute() {	Attribute weight = getWeight();	if (weight != null) {	    LogService.logMessage("ExampleSet.createWeightAttribute(): Overwriting old weight attribute!",				  LogService.WARNING);	}	weight = new Attribute(Attribute.createName("weight"), 			       Ontology.REAL, Ontology.SINGLE_VALUE, Attribute.UNDEFINED_BLOCK_NR,			       null);	getExampleTable().addAttribute(weight);	setWeight(weight);	return weight;    }    public Attribute createClusterAttribute() {	Attribute cluster = getCluster();	if (cluster != null) {	    LogService.logMessage("ExampleSet.createClusterAttribute(): Overwriting old cluster attribute!",				  LogService.WARNING);	}	cluster = new Attribute(Attribute.createName("cluster"), 				Ontology.CLUSTER,				Ontology.SINGLE_VALUE,				Attribute.UNDEFINED_BLOCK_NR,				null);	getExampleTable().addAttribute(cluster);	setCluster(cluster);	return cluster;    }    // -------------------- Visualisation and toString() methods --------------------    public String toString() {        StringBuffer str = new StringBuffer(Tools.classNameWOPackage(this.getClass())+": ");	str.append(getSize() + " examples\n");	str.append("       attributes = {");	if (getNumberOfAttributes() >= 100) {	    str.append("..."+getNumberOfAttributes()+" attributes...");	} else {	    for (int i = 0; i < getNumberOfAttributes(); i++) {		Attribute att = getAttribute(i);		str.append((i == 0 ? "" : ",\n") +			   "         " + att.toString());		if (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(att.getBlockType(), Ontology.VALUE_SERIES_START)) {		    int end = getBlockEndIndex(i);		    str.append(",..., "+(end-i-1)+" attributes,...");		    i = end-1;		}	    }	}	str.append("}\n");	if (getIdAttribute() != null)    str.append("\n       id = " +getIdAttribute());	if (getLabel() != null)          str.append("\n       label = " +getLabel());	if (getPredictedLabel() != null) str.append("\n       predicted label = " + getPredictedLabel());	if (getWeight() != null)         str.append("\n       weight = " + getWeight());	if (getCluster() != null)        str.append("\n       cluster = " + getCluster());        return str.toString();    }    /** Returns a html description of the example set. */    protected String toHTML() {	StringBuffer buffer = new StringBuffer("");	buffer.append("<h1>"+edu.udo.cs.yale.tools.Tools.classNameWOPackage(this.getClass())+"</h1>");	buffer.append("<b>Number of examples:</b> "+getSize()+"<br>");	buffer.append("<b>Number of attributes:</b> "+getNumberOfAttributes()+"<br>");	buffer.append("<table bgcolor=\"#E3D8C3\" border=\"1\">");	buffer.append("<tr bgcolor=\"#ccccff\"><th>Index</th><th>Name</th><th>Generated from</th><th>Unit</th><th>Type</th><th>Blocktype</th><th>Blocknr.</th><th>Values</th></tr>");	buffer.append("<tr bgcolor=\"#C3B8A3\"><td colspan=\"8\" border=\"0\"><b>Regular attributes</b></td></tr>");	for (int i = 0; i < getNumberOfAttributes(); i++) {	    buffer.append(getAttribute(i).toHTML());	    if (i > 1000) { 		buffer.append("<tr><td colspan=\"8\">...</td></tr>");		break;	    }	    	}	specialAttributeToHTML(getIdAttribute(), "ID", buffer);	specialAttributeToHTML(getLabel(), "Label", buffer);	specialAttributeToHTML(getPredictedLabel(), "Prediction", buffer);	specialAttributeToHTML(getWeight(), "Weight", buffer);	specialAttributeToHTML(getCluster(), "Cluster", buffer);	buffer.append("</table>");	return buffer.toString();    }    private static void specialAttributeToHTML(Attribute attribute, String name, StringBuffer buffer) {	if (attribute != null) {	    buffer.append("<tr bgcolor=\"#C3B8A3\"><td colspan=\"8\" border=\"0\"><b>"+name+"</b></td></tr>");	    buffer.append(attribute.toHTML());	}    }    /** Returns a html label. */    public Component getVisualisationComponent() {	JLabel label = new JLabel("<html>" + toHTML() + "</html>");	label.setBorder(javax.swing.BorderFactory.createEmptyBorder(11,11,11,11));	label.setFont(label.getFont().deriveFont(java.awt.Font.PLAIN));	return label;    }    // -------------------- misc --------------------    public int getBlockEndIndex(int startindex) {	Attribute startAtt = getAttribute(startindex);	if (!Ontology.ATTRIBUTE_BLOCK_TYPE.isA(startAtt.getBlockType(), Ontology.VALUE_SERIES_START)) {	    throw new RuntimeException("Attribute  "+startAtt + " is not start of a value series!");	}	for (int i = startindex+1; i < getNumberOfAttributes(); i++) {	    Attribute attribute = getAttribute(i);	    if (attribute.getBlockNr() != startAtt.getBlockNr()) 		throw new RuntimeException("Data block ");	    if (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(attribute.getBlockType(), Ontology.VALUE_SERIES_END)) {		return i;	    }	}	throw new RuntimeException("Value series not terminated!");    }    public void save(File file) throws IOException {	File attFile = new File(file.getAbsolutePath()+".xml");	writeToFile(file, attFile);    }    public void writeToFile(File dataFile, File attFile) throws IOException {	PrintWriter out = new PrintWriter(new FileWriter(dataFile));	ExampleReader reader = getExampleReader();	while (reader.hasNext()) {	    out.println(reader.next().toString());	}	out.close();	if (attFile != null) { writeAttributeFile(dataFile, attFile, false); }    }    public void writeToFileSparse(int format, File dataFile, File attFile) throws IOException {	PrintWriter out = new PrintWriter(new FileWriter(dataFile));	ExampleReader reader = getExampleReader();	while (reader.hasNext()) {	    out.println(reader.next().toSparseString(format));	}	out.close();	if (attFile != null) { writeAttributeFile(dataFile, attFile, true); }    }    private void writeAttributeFile(File dataFile, File attFile, boolean sparse) throws IOException {	PrintWriter aout = new PrintWriter(new FileWriter(attFile));	aout.println("<attributeset default_source=\"" + dataFile.getAbsolutePath() + "\">");		int sourcecol = 1;		Attribute id = getIdAttribute();	if (id != null) {	    writeAttributeData("id", id, sourcecol, aout, sparse);	    if (!sparse) sourcecol++;	}		for (int i = 0; i < getNumberOfAttributes(); i++) {	    writeAttributeData("attribute", getAttribute(i), sourcecol, aout, sparse);	    sourcecol++;	}		// write label	Attribute label = getLabel();	if (label != null) {	    writeAttributeData("label", label, sourcecol, aout, sparse);	    if (!sparse) sourcecol++;	}	// write weight	Attribute weight = getWeight();	if (weight != null) {	    writeAttributeData("weight", weight, sourcecol, aout, sparse);	    if (!sparse) sourcecol++;	}	// write id	Attribute idAttribute = getIdAttribute();	if (weight != null) {	    writeAttributeData("id", idAttribute, sourcecol, aout, sparse);	    if (!sparse) sourcecol++;	}	aout.println("</attributeset>");	aout.close();    }    /** Writes the data of this attribute in the given stream. */    private static void writeAttributeData(String tag,					   Attribute attribute, 					   int sourcecol, 					   PrintWriter aout,					   boolean sparse) {	aout.println("  <"+tag);	aout.println("    name         = \"" + attribute.getName() + "\"");	if (!sparse || tag.equals("attribute")) {	    aout.println("    sourcecol    = \"" + sourcecol + "\"");	}	aout.println("    valuetype    = \"" + Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(attribute.getValueType()) + "\"");	if (attribute.isNominal()) {	    aout.print("    classes      = \"");	    for (int i = 0; i < attribute.getNumberOfClasses(); i++) {		if (i != 0) aout.print(" ");		aout.print(attribute.mapIndex(i+Attribute.FIRST_CLASS_INDEX));	    }	    aout.println("\"");	}	aout.println("    blocktype    = \"" + Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(attribute.getBlockType()) + "\"");	aout.println("    blocknumber  = \"" + attribute.getBlockNr() + "\"");	aout.println("    unit         = \"" + attribute.unitToString() + "\"");	aout.println("  />");    }     /** Returns true, if  all attributes including labels and other special attributes are equal. */    public boolean equals(Object o) {	if (!(o instanceof ExampleSet)) {	    return false;	}	ExampleSet es = (ExampleSet)o;	if (es.getNumberOfAttributes() != this.getNumberOfAttributes()) return false;	for (int i = 0; i < this.getNumberOfAttributes(); i++) {	    if (!equals(this.getAttribute(i), es.getAttribute(i))) return false;	}	if (!equals(this.getLabel(), es.getLabel())) return false;	if (!equals(this.getPredictedLabel(), es.getPredictedLabel())) return false;	if (!equals(this.getWeight(), es.getWeight())) return false;	if (!equals(this.getCluster(), es.getCluster())) return false;	return true;    }    /** Returns true iff both attributes are null or <code>a1.equals(a2)</code> returns true. */    private static boolean equals(Attribute a1, Attribute a2) {	if ((a1 == null) && (a2 == null)) return true;	if ((a1 == null) || (a2 == null)) return false;	return a1.equals(a2);    }    /** Clones the example set by invoking a single argument clone constructor. */    public Object clone() {	try {	    Class clazz = getClass();	    java.lang.reflect.Constructor cloneConstructor = clazz.getConstructor(new Class[] { clazz });	    return cloneConstructor.newInstance(new Object[] { this } );	} catch (NoSuchMethodException e) {	    throw new RuntimeException("'"+getClass().getName()+"' does not implement clone constructor!");	} catch (java.lang.reflect.InvocationTargetException e) {	    throw new RuntimeException("Cannot clone "+getClass().getName()+": "+e+				       ". Target: "+e.getCause()+". Cause: "+e.getTargetException()+".");	} catch (Throwable throwable) {	    throw new RuntimeException("Cannot clone "+getClass().getName()+": "+throwable);	}     }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -