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

📄 basicarithmeticoperationgenerator.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.generator;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.example.Example;import edu.udo.cs.yale.example.ExampleTable;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.example.DataRow;/** This class is a very simple implementation of a FeatureGenerator. It has two numerical input *  attributes and one output attribute. Depending on the mode specified in the constructor the *  result will be the sum, difference, product or quotient. The four modes are numered from 0 to 3 *  int this order. * *  @author simon, ingo *  @version $Id: BasicArithmeticOperationGenerator.java,v 2.5 2003/09/04 14:39:49 fischer Exp $ */public class BasicArithmeticOperationGenerator extends FeatureGenerator {      private static final Attribute[] INPUT_ATTR = { new Attribute(Ontology.NUMERICAL, Ontology.SINGLE_VALUE),						    new Attribute(Ontology.NUMERICAL, Ontology.SINGLE_VALUE) };    private static final int[] VALUE_TYPES = {Ontology.NUMERICAL,    Ontology.NUMERICAL};    private static final int[] BLOCK_TYPES = {Ontology.SINGLE_VALUE, Ontology.SINGLE_VALUE};    private static final int[] RESULT_VALUE_TYPE = { Ontology.NUMERICAL };    private static final int[] RESULT_BLOCK_TYPE = { Ontology.SINGLE_VALUE };    public static final String[] FUNCTION_NAMES = { "+", "-", "*", "/" };    private int mode;    private boolean bounded;    public BasicArithmeticOperationGenerator() {    }    public BasicArithmeticOperationGenerator(int mode) {	this(mode, false);    }    public BasicArithmeticOperationGenerator(int mode, boolean bounded) {	this.mode = mode;	this.bounded = bounded;    }    public FeatureGenerator newInstance() {	return new BasicArithmeticOperationGenerator(mode, bounded);    }    public Attribute[] getInputAttributes() {	return INPUT_ATTR;    }    public Attribute[] getOutputAttributes(ExampleTable input) {	Attribute a1 = getArgument(0);	Attribute a2 = getArgument(1);        if ((mode == 0) ||	    (mode == 2) ||	    !a1.getConstructionDescription().equals(a2.getConstructionDescription())) { 	    String op = FUNCTION_NAMES[mode];	    Attribute ao = new Attribute(Ontology.NUMERICAL, Ontology.SINGLE_VALUE,					 op, new Attribute[] { a1, a2 }, true,					 input.getNextFreeBlockNr(),					 null);	    return new Attribute[] { ao };	}        else {	    LogService.logMessage("Identical attribute descriptions. No attribute is constructed.", 				  LogService.WARNING);	    return new Attribute[] {};        }    }    public void generate(DataRow data) throws GenerationException {	try {  	    Attribute a0 = getArgument(0);  	    Attribute a1 = getArgument(1);	    double o1 = data.get(a0);	    double o2 = data.get(a1);	    double r = 0;	    switch (mode) {	    case 0: r = o1 + o2; break;	    case 1: r = o1 - o2; break;	    case 2: r = o1 * o2; break;	    case 3: r = o1 / o2; break;	    }	    if (bounded) {                if (r >= Double.MAX_VALUE) {		    LogService.logMessage(this.toString()+": cannot operate on arguments " + a0 + " and " + a1 +					  ": result is out of bounds!", LogService.WARNING);                    r = Double.MAX_VALUE;		} else if ((0 < r) && (r < Double.MIN_VALUE)) {		    LogService.logMessage(this.toString()+": cannot operate on arguments " + a0 + " and " + a1 +					  ": result is out of bounds!", LogService.WARNING);		    r = Double.MIN_VALUE;		} else if ((0 > r) && (r > -Double.MIN_VALUE)) {		    LogService.logMessage(this.toString()+": cannot operate on arguments " + a0 + " and " + a1 +					  ": result is out of bounds!", LogService.WARNING);		    r = - Double.MIN_VALUE;  		} else if (r <= -Double.MAX_VALUE) {		    LogService.logMessage(this.toString()+": cannot operate on arguments " + a0 + " and " + a1 +					  ": result is out of bounds!", LogService.WARNING);                    r = -Double.MAX_VALUE;		} else if (Double.isNaN(r)) {		    LogService.logMessage(this.toString()+": cannot operate on arguments " + a0 + " and " + a1 +					  ": result is out of bounds!", LogService.WARNING);		    r = 0;		}	    } else {		if ((Double.isInfinite(r)) || (Double.isNaN(r))) {		    throw new GenerationException(this.toString() + ": cannot operate on arguments "						  + a0 + " (" + o1 + ") and "						  + a1 + " (" + o2 + ") = "+r);		}	    }	    if (resultAttributes[0] != null)		data.set(resultAttributes[0], r);	} catch (ArrayIndexOutOfBoundsException ex) {  	    throw new GenerationException("a1:" + getArgument(0) + " a2: " + getArgument(1));  	}    }    public void setFunction(String name) {	for (int i = 0; i < FUNCTION_NAMES.length; i++) {	    if (FUNCTION_NAMES[i].equals(name)) {		this.mode = i;		return;	    }	}	LogService.logMessage("Illegal function name '"+name+"' for "+getClass().getName()+".", 			      LogService.ERROR);    }    public String getFunction() {	return FUNCTION_NAMES[mode];    }    public String toString() {	String s = "Basic arithmetic operation (";	if ((resultAttributes != null) && 	    (resultAttributes[0] != null))	    s += resultAttributes[0].getName()+":=";	if (argumentsSet()) s+=getArgument(0).getName() + " ";	switch (mode) {	  case 0: s += "+"; break;	  case 1: s += "-"; break;	  case 2: s += "*"; break;	  case 3: s += "/"; break;	}	if (argumentsSet()) s+=" " + getArgument(1).getName();	s += ")";	return s;    }    public boolean equals(Object o) {	return (super.equals(o) && (this.mode == ((BasicArithmeticOperationGenerator)o).mode));    }}

⌨️ 快捷键说明

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