📄 streaminputsynapse.java
字号:
package org.joone.io;import java.util.*;import java.io.*;import org.joone.log.*;import org.joone.engine.*;import org.joone.util.*;import org.joone.net.NetCheck;import org.joone.inspection.Inspectable;import org.joone.inspection.implementations.InputsInspection;import org.joone.exception.JooneRuntimeException;public abstract class StreamInputSynapse extends Synapse implements InputSynapse, Inspectable { /** * Logger * */ private static final ILogger log = LoggerFactory.getLogger(StreamInputSynapse.class); //protected Vector column; //private boolean[] colList; private int firstRow = 1; private int lastRow = 0; private int firstCol = 0; private int lastCol = 0; private String advColumnsSel = ""; /** Flag indicating if the stream buffers the data of not. */ private boolean buffered = true; private char decimalPoint = '.'; private boolean StepCounter = true; protected transient int[] cols; protected transient Vector InputVector; protected transient int currentRow = 0; protected transient PatternTokenizer tokens; protected transient boolean EOF = false; private ConverterPlugIn plugIn; private int maxBufSize = 0; private static final long serialVersionUID = -3316265583083866079L; private transient int startFrom = 0; /** List of plug-in listeners (often input connectors). */ List plugInListeners = new ArrayList(); public StreamInputSynapse() { super(); } protected void backward(double[] pattern) { // Not used. } protected void forward(double[] pattern) { outs = pattern; } public synchronized Pattern fwdGet() { if (!isEnabled()) return null; if ((EOF) || (outs == null)) { try { if ( (EOF) && (getMonitor() != null )) { if (isStepCounter()) getMonitor().resetCycle(); } gotoFirstLine(); outs = new double[1]; // Remember that it already has been here } catch (Exception ioe) { log.error("Exception while executing the \"fwdGet\". Message is : " + ioe.getMessage()); if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"Exception while executing the \"fwdGet\" method. Message is : " + ioe.getMessage()); return zeroPattern(); } } if (currentRow - firstRow > (getMonitor().getNumOfPatterns() - 1)) { try { gotoFirstLine(); } catch (Exception ioe) { log.error("Exception while executing the \"fwdGet\". Message is : " + ioe.getMessage()); if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"Exception while attempting to access the first line. Message is : " + ioe.getMessage()); return zeroPattern(); } } if (isStepCounter()) { // Checks if the next step can be elaborated boolean cont = getMonitor().nextStep(); if (!cont) { /* If not, then creates and returns a zero pattern, * a pattern having count = -1 * this does mean that the net must stop */ reset(); return zeroPattern(); } } // Reads the next input pattern if (isBuffered()) { int actualRow = currentRow - firstRow + (startFrom==0 ? 0 : startFrom-1); if (getInputVector().size() == 0) readAll(); else { if ((currentRow == firstRow) && (getPlugIn() != null) && !skipNewCycle) { getPlugIn().setInputVector(getInputVector()); if (getPlugIn().newCycle()) { fireDataChanged(); } } } if (actualRow < InputVector.size()) { m_pattern = (Pattern) InputVector.elementAt(actualRow); } // Checks if EOF if ((lastRow > 0) && (currentRow >= lastRow)) EOF = true; if ((actualRow + 1) >= InputVector.size()) EOF = true; ++currentRow; } else { try { m_pattern = getStream(); } catch (Exception ioe) { String error = "Exception in "+getName()+". Message is : "; log.error(error + ioe.getMessage()); ioe.printStackTrace(); if ( getMonitor() != null ) new NetErrorManager(getMonitor(),error + ioe.getMessage()); return zeroPattern(); } } m_pattern.setCount(currentRow - firstRow); return m_pattern; } /** * Returns the decimal point accepted * (19/04/00 0.23.56) * @return char */ public char getDecimalPoint() { return decimalPoint; } /** * @return int */ public int getFirstRow() { return firstRow; } /** * @return java.util.Vector */ protected java.util.Vector getInputVector() { if (InputVector == null) InputVector = new Vector(); return InputVector; } /** * @return int */ public int getLastRow() { return lastRow; } protected Pattern getStream() throws java.io.IOException { int x = 0; EOF = (!tokens.nextLine()); if (!EOF) { if (cols == null) { setColList(); } if (cols == null) { return null; // In this case we cannot continue } // even though the output dimension might differ, we create a input pattern // of the size equaling the number of input columns. Plug-ins might adjust // the dimension and otherwise the layer will adjust its dimenstion. This is // beter than creating an input pattern of a size equal to the output dimenstion // in case the number of columns is smaller and fill the pattern with 0, for // the columns where no imput data is available for. inps = new double[cols.length]; for (x = 0; x < cols.length; ++x) inps[x] = tokens.getTokenAt(cols[x] - 1); // Calculates the new current line // and check the EOF ++currentRow; if ((lastRow > 0) && (currentRow > lastRow)) EOF = true; forward(inps); m_pattern = new Pattern(outs); m_pattern.setCount(currentRow - firstRow); return m_pattern; } else return null; } public void gotoFirstLine() throws java.io.IOException { this.gotoLine(firstRow); } /** * Point to the indicated line into the input stream */ public void gotoLine(int numLine) throws java.io.IOException { EOF = false; if (!isBuffered() || (getInputVector().size() == 0)) { PatternTokenizer tk = getTokens(); if (tk != null) { if (isBuffered()) tk.resetInput(); else // patched by Razvan Surdulescu initInputStream(); EOF = false; currentRow = 1; while ((currentRow < numLine) && (!EOF)) { if (tokens.nextLine()) ++currentRow; else EOF = true; } } } currentRow = numLine; if ((lastRow > 0) && (currentRow >= lastRow)) EOF = true; else EOF = false; } /** * Checks if the input synapse is buffered or not * * @return <code>true</code> in case the input stream is buffered, false otherwise. */ public boolean isBuffered() { return buffered; } /** * Returns if reached the EOF * (10/04/00 23.16.20) * @return boolean */ public boolean isEOF() { return EOF; } /** * Returns if this input layer is an active counter of the steps. * Warning: in a neural net there can be only one StepCounter element! * (10/04/00 23.23.26) * @return boolean */ public boolean isStepCounter() { if (getMonitor() != null) if (getMonitor().isSingleThreadMode()) return false; return StepCounter; } public int numColumns() { if (cols == null) setColList(); if (cols == null) return 0; else return cols.length; } // Read all input values and fullfills the buffer public void readAll() { Pattern ptn; getInputVector().removeAllElements(); try { gotoFirstLine(); ptn = getStream(); while (ptn != null) { InputVector.addElement(ptn); if (EOF) break; ptn = getStream(); } if (plugIn != null) { plugIn.setInputVector(InputVector); plugIn.convertPatterns(); } gotoFirstLine(); } catch (java.io.IOException ioe) { String error = "IOException in "+getName()+". Message is : "; log.warn(error + ioe.getMessage()); if ( getMonitor() != null ) new NetErrorManager(getMonitor(),error + ioe.getMessage()); } catch (NumberFormatException nfe) { String error = "IOException in "+getName()+". Message is : "; log.warn(error + nfe.getMessage()); if ( getMonitor() != null ) new NetErrorManager(getMonitor(),error + nfe.getMessage()); } } public synchronized void revPut(Pattern array) { // Not used. } /** * setArrays method. */ protected void setArrays(int rows, int cols) { } /** * Sets the buffer-mode for this input synapse. * * @param aNewBuffered <code>true</code> if the input should be buffered. * <code>false</code> if the input should not be buffered, the input will be * retrieved from the input source every cycle again. * <p><b>Whenever any converter plug in is added, the buffer-mode will be set * to <code>true</code>, regardless of the parameter's argument. </b></p> */ public void setBuffered(boolean aNewBuffered) { if(plugIn == null) { buffered = aNewBuffered; } else { // If a plugin exists, the input synapse must be buffered // to permit to the plugin to work correctly. buffered = true; } } /** * Sets the list of columns that must be returned as the pattern * Creation date: (18/10/2000 0.45.52) * @param cols java.util.Vector */ protected void setColList() { int i; if (getAdvancedColumnSelector().trim().length() > 0) { CSVParser parser = new CSVParser(getAdvancedColumnSelector().trim()); try { cols = parser.parseInt(); } catch (NumberFormatException nfe) { new NetErrorManager(getMonitor(), nfe.getMessage()); } } else { if ((getFirstCol() == 0) || (getLastCol() == 0)) return; cols = new int[getLastCol() - getFirstCol() + 1]; for (i=getFirstCol(); i <= getLastCol(); ++i) cols[i - getFirstCol()] = i; } } public void setDecimalPoint(char dp) { decimalPoint = dp; if (tokens != null) tokens.setDecimalPoint(dp); } protected void setDimensions(int rows, int cols) { } protected void setEOF(boolean newEOF) { EOF = newEOF; } /** * Inserire qui la descrizione del metodo. * Data di creazione: (11/04/00 1.22.28) * @param newFirstRow int */ public void setFirstRow(int newFirstRow) { myFirstRow = firstRow; if (firstRow != newFirstRow) { firstRow = newFirstRow; this.resetInput(); } } /** * Reset the input stream to read its content again */ public synchronized void resetInput() { restart(); tokens = null; notifyAll(); } private void restart() { getInputVector().removeAllElements(); EOF = false; cols = null; } /** * Inserire qui la descrizione del metodo. * Data di creazione: (11/04/00 1.22.34) * @param newLastRow int * */ public void setLastRow(int newLastRow) { myLastRow = lastRow; if (lastRow != newLastRow) { lastRow = newLastRow; this.resetInput(); } } /** * Adds a plug in to the stream input synapse for data preprocessing. If one * or more plug ins are already added to the this synapse, the plug in will * be added at the end of the list of plug ins. * * @param aNewPlugIn The new converter plug in to add (at the end of the list). * @return <code>true</code> when the plug in is added, <code>false</code> when
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -