📄 attributedatasource.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.tools.att;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.tools.Tools;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.xml.XML2ObjectParser;import edu.udo.cs.yale.tools.xml.XMLException;import java.io.File;import java.io.FileReader;import java.io.Reader;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import java.util.LinkedList;import java.util.StringTokenizer;import java.util.ArrayList;import java.util.Iterator;import org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.*;/** Reference to source of an attribute, i.e. file, column number (token number). * Statics methods of this class can be used to parse an attribute description file. * * @version $Id: AttributeDataSource.java,v 2.5 2003/08/28 10:59:54 fischer Exp $ */public class AttributeDataSource { public static final String[] KNOWN_TYPES = { "attribute", "label", "id", "weight", "batch", "cluster" }; private File file; private int column; private Attribute attribute; private String attributeType; public AttributeDataSource(Attribute attribute, File file, int column, String attributeType) { this.attribute = attribute; this.file = file; this.column = column; this.attributeType = attributeType; } public Attribute getAttribute() { return attribute; } public int getColumn() { return column; } public File getFile() { return file; } public void setType(String type) { this.attributeType = type; } public String getType() { return attributeType; } public void setSource(File file, int column) { this.file = file; this.column = column; } public void writeXML(PrintWriter out, File defaultSource) throws IOException { out.println(" <"+attributeType); out.println(" name =\""+attribute.getName()+"\""); if (!getFile().equals(defaultSource)) { out.println(" sourcefile =\""+getFile().getAbsolutePath()+"\""); } out.println(" sourcecol =\""+(getColumn()+1)+"\""); out.println(" valuetype =\""+Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(attribute.getValueType())+"\""); out.println(" blocktype =\""+Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(attribute.getBlockType())+"\""); if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NOMINAL)) { out.print(" classes =\""); Iterator i = attribute.getValuesAsString().iterator(); boolean first = true; while (i.hasNext()) { if (!first) out.print(" "); out.print(i.next()); first = false; } out.println("\""); } String unit = attribute.unitToString(); if (unit.length() > 0) { out.print(" unit =\""+unit+"\""); } out.println(" />"); } /** Returns a list of {@link AttributeDataSource}s read from the file. */ public static AttributeDataSources createAttributeDataSources(File attributeDescriptionFile, boolean sourceColRequired) throws XMLException, ParserConfigurationException, SAXException, IOException { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(attributeDescriptionFile); Element attributeSet = document.getDocumentElement(); if (!attributeSet.getTagName().equals("attributeset")) { throw new XMLException("Outer tag of attribute description file must be <attributeset>"); } File defaultSource = null; if (attributeSet.getAttribute("default_source") != null) { defaultSource = Tools.getFile(attributeDescriptionFile.getParentFile(), attributeSet.getAttribute("default_source")); } List attributeDataSources = new LinkedList(); NodeList attributes = attributeSet.getChildNodes(); for (int i = 0; i < attributes.getLength(); i++) { Node node = attributes.item(i); if (node instanceof Element) { Element attributeTag = (Element)node; int blockType = Ontology.BLOCK_TYPE; String type = attributeTag.getTagName(); String name = attributeTag.getAttribute("name"); if (!name.equals("attribute")) blockType = Ontology.SINGLE_VALUE; String unit = attributeTag.getAttribute("unit"); String file = null; Attr fileAttr = attributeTag.getAttributeNode("sourcefile"); if (fileAttr != null) file = fileAttr.getValue(); int firstSourceCol = -1; Attr sourcecolAttr = attributeTag.getAttributeNode("sourcecol"); if (sourcecolAttr != null) { if (sourcecolAttr.getValue().equals("none")) { firstSourceCol = -1; } else { try { firstSourceCol = Integer.parseInt(sourcecolAttr.getValue()) - 1; } catch (NumberFormatException e) { throw new XMLException("Attribute sourcecol must be 'none' or an integer (was: '"+sourcecolAttr.getValue()+"')!"); } } } int lastSourceCol = -1; Attr sourceEndAttr = attributeTag.getAttributeNode("sourcecol_end"); if (sourceEndAttr != null) { try { lastSourceCol = Integer.parseInt(sourceEndAttr.getValue()) - 1; } catch (NumberFormatException e) { throw new XMLException("Attribute sourcecol_end must be 'none' or an integer (was: '"+sourceEndAttr.getValue()+"')!"); } } int valueType = Ontology.VALUE_TYPE; Attr valueTypeAttr = attributeTag.getAttributeNode("valuetype"); if (valueTypeAttr != null) { try { valueType = Integer.parseInt(valueTypeAttr.getValue()); } catch (NumberFormatException e) { valueType = Ontology.ATTRIBUTE_VALUE_TYPE.mapName(valueTypeAttr.getValue()); if (valueType < 0) throw new XMLException("valuetype must be an index number or a legal value type name (was: '"+valueTypeAttr.getValue()+"')"); } } Attr blockTypeAttr = attributeTag.getAttributeNode("blocktype"); if (blockTypeAttr != null) { try { blockType = Integer.parseInt(blockTypeAttr.getValue()); } catch (NumberFormatException e) { blockType = Ontology.ATTRIBUTE_BLOCK_TYPE.mapName(blockTypeAttr.getValue()); if (blockType < 0) throw new XMLException("blocktype must be an index number or a legal block type name (was: '"+blockTypeAttr.getValue()+"')"); } } int blockNumber = -1; Attr blockNumberAttr = attributeTag.getAttributeNode("blocknumber"); if (blockNumberAttr != null) { try { blockNumber = Integer.parseInt(blockNumberAttr.getValue()); } catch (NumberFormatException e) { throw new XMLException("Illegal blocknumber for attribute '"+name+"': " + blockNumberAttr.getValue()); } } List classList = null; Attr classesAttr = attributeTag.getAttributeNode("classes"); if (classesAttr != null) { classList = new LinkedList(); StringTokenizer tokenizer = new StringTokenizer(classesAttr.getValue()); while (tokenizer.hasMoreTokens()) { classList.add(tokenizer.nextToken()); } } if (lastSourceCol == -1) lastSourceCol = firstSourceCol; if (sourceColRequired) { if (firstSourceCol < 0) throw new XMLException("sourcecol not defined for "+type+" '"+name+"'!"); if (lastSourceCol < firstSourceCol) throw new XMLException("sourcecol < sourcecol_end must hold."); } for (int col = firstSourceCol; col <= lastSourceCol; col++) { int thisBlockType = blockType; if ((col == firstSourceCol) && (blockType == Ontology.VALUE_SERIES)) thisBlockType = Ontology.VALUE_SERIES_START; if ((col == lastSourceCol) && (blockType == Ontology.VALUE_SERIES)) thisBlockType = Ontology.VALUE_SERIES_END; String theName = name; if (lastSourceCol > firstSourceCol) { theName = name + "_"+(col+1); } Attribute attribute = new Attribute(theName, valueType, thisBlockType, blockNumber, unit); if (attribute.isNominal() && (classList != null)) { Iterator c = classList.iterator(); while (c.hasNext()) { attribute.mapString((String)c.next()); } } attributeDataSources.add(new AttributeDataSource(attribute, (file != null) ? Tools.getFile(attributeDescriptionFile.getParentFile(), file) : defaultSource, col, type)); } } } return new AttributeDataSources(attributeDataSources, defaultSource); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -