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

📄 visadoutputsynapse.java

📁 拥有图形界面的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.joone.edit.visad;import org.joone.engine.*;import org.joone.edit.*;import java.io.*;import java.awt.*;import java.util.*;import javax.swing.*;import java.rmi.RemoteException;import org.joone.engine.learning.ComparingElement;import visad.*;import visad.java2d.*;import visad.util.*;import org.joone.log.*;/** * <P>This component implements the chart interface and allows data obtained via the use of ChartHandles to be plotted onto a graph.</P> * <P>The VisADOutputSynapse uses the visad display library, more details on this can be obtained from http://www.visad.org.</P> * <P>When the property show is set to true a JFrame is displayed, this will display a chart of any previously obtained data or no chart if * the data has not yet been obtained.  When the network starts the component collects the data in internal buffers and at the end of the * network run will display the data in a chart.  Serie lines are coloured according to the choice made in the specific ChartHandle object.</P> * <P>The displayed component allows the user to change the X and Y range of data that should be displayed, this allows ther user to zoom in ans out of the data range.</P> * <P>The user can also decided whether to display the data as points and whatthe point size can be.</P> * <P> Multi serie plots can be done by the use of ChartHandles.  Mono plots can be done by connecting this to a normal Layer.</P> *<P> This component will throw an network error if it runs out of memory, the network will then stop running.</P> */public class VisADOutputSynapse implements org.joone.util.NotSerialize, java.io.Serializable, NeuralNetListener, ChartInterface {        private static final long serialVersionUID = 7501932794591309201L;    private static final ILogger log = LoggerFactory.getLogger( VisADOutputSynapse.class );    private boolean show;    private Monitor monitor;    private String name = "";    private String title = "VisAD Chart";    private boolean resizable = true;    private int maxXaxis = 1000;    private double maxYaxis = 1.0;    private int serie = 1;    private boolean outputFull;    private transient javax.swing.JFrame iFrame;    private boolean enabled = true;        private transient ChartingHandle def_handle = new ChartingHandle();        private transient JLabel status_label = new JLabel("Waiting for network to run!");    private transient JPanel pane;    private transient JPanel ControlPane = new JPanel();    private transient Hashtable ChartBuffers = new Hashtable();    private transient Hashtable DataRefs = new Hashtable();    private transient Hashtable DataColors = new Hashtable();    private transient Hashtable flat_fields = new Hashtable();        // Declare variables    // The quantities to be displayed in x- and y-axis    private transient RealType time, height;        // The function height = f(time), represented by ( time -> height )    private transient FunctionType func_time_height;        // Our Data values for time are represented by the set    private transient visad.Set time_set;        // The Data class FlatField, which will hold time and height data.    // time data are implicitely given by the Set time_set    private transient FlatField vals_ff;        // The DataReference from the data to display    private transient DataReferenceImpl data_ref;        // The 2D display, and its the maps    private transient DisplayImplJ2D display;    private transient GMCWidget control_widg;    private transient ScalarMap timeMap, heightMap;    private transient RangeWidget timeWidget;    private transient RangeWidget heightWidget;        private transient boolean visadinit = false;    private transient int current_sample_index = 0;        /**     * Initialises the VisAd components.  Sets up the internal frame.     * @param max_samples The initial number of samples that will be plotted.     * @param msg The initial message that will be displayed in the status panel.     */    private void initVisAd(int max_samples, String msg) {        try {                        if (getMonitor() != null)                getMonitor().addNeuralNetListener(this);   // Ensure we get Net Events                        status_label.setText(msg);            // Create the quantities            // Use RealType(String name)            time = RealType.getRealType("Sample");            height = RealType.getRealType("Value");                        // Create a FunctionType, that is the class which represents our function            // This is the MathType ( time -> height )            // Use FunctionType(MathType domain, MathType range)            func_time_height = new FunctionType(time, height);                        // Create the time_set, with 5 integer values, ranging from 0 to 4.            // That means, that there should be 5 values for height.            // Use Integer1DSet(MathType type, int length)            time_set = new Integer1DSet(time,max_samples);                        // Create Display and its maps            // A 2D display            display = new DisplayImplJ2D("display1");            display.setAutoAspect(true);            // Get display's graphic mode control and draw scales            GraphicsModeControl dispGMC = (GraphicsModeControl) display.getGraphicsModeControl();            control_widg = new GMCWidget(dispGMC);            dispGMC.setScaleEnable(true);                        // Create the ScalarMaps: quantity time is to be displayed along x-axis            // and height along y-axis            // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)            timeMap = new ScalarMap( time, Display.XAxis );            heightMap = new ScalarMap( height, Display.YAxis );                        // Add maps to display            display.addMap( timeMap );            display.addMap( heightMap );                        // Scale heightMap. This will scale the y-axis, because heightMap has DisplayRealType YAXIS            timeMap.setRange( 0.0, maxXaxis);            heightMap.setRange( 0.0, maxYaxis);                        timeWidget = new RangeWidget( timeMap );            heightWidget = new RangeWidget( heightMap );                        if ( (iFrame != null) ) {                iFrame.getContentPane().removeAll();                pane = new JPanel();                ControlPane = new JPanel();                pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));                pane.setAlignmentY(JPanel.TOP_ALIGNMENT);                pane.setAlignmentX(JPanel.LEFT_ALIGNMENT);                pane.add(display.getComponent());                ((JPanel)display.getComponent()).setMinimumSize(new Dimension(500,250));                ((JPanel)display.getComponent()).setMaximumSize(new Dimension(1000,500));                iFrame.getContentPane().setLayout(new BorderLayout());                iFrame.getContentPane().add(pane,BorderLayout.CENTER);                iFrame.getContentPane().add(timeWidget,BorderLayout.NORTH);                ControlPane.setLayout(new GridLayout(2,1));                ControlPane.add(heightWidget);                ControlPane.add(control_widg);                iFrame.getContentPane().add(ControlPane,BorderLayout.SOUTH);                                // Set window size and make it visible                iFrame.setSize(740, 450);                //iFrame.setVisible(true);            }        }        catch(VisADException ex) {            log.error(ex.toString());            if ( getMonitor() != null )                new NetErrorManager(getMonitor(),"VisADException while attempting to initialise VisAd. Message is : " + ex.getMessage());        }                catch(RemoteException ex) {            if ( getMonitor() != null )                new NetErrorManager(getMonitor(),"Remote exception while attempting to initialise VisAd. Message is : " + ex.getMessage());        }        catch(Exception ex) {            log.error(ex.toString());            if ( getMonitor() != null )                new NetErrorManager(getMonitor(),"Exception while attempting to initialise VisAd. Message is : " + ex.getMessage());        }    }        /**     * Plots the VisAd chart in the internal frame.     */    private void PlotVisAd() {        Enumeration myEnum = ChartBuffers.elements();        // Loop through all the shared buffers ...        while(myEnum.hasMoreElements()) {            try {                SharedBuffer buffer = ((SharedBuffer)myEnum.nextElement());                ChartingHandle handle = buffer.getHandle();                double [][] arr = buffer.get();                //double [][] copy = new double [1][getMaxXaxis()];                // for ( int i=0;i<arr.length;i++) changed because it causes ArrayOutOfBoundException                // The for() loop must be limited to the smallest array size                int max = (int)timeMap.getRange()[1]; //getMaxXaxis();                if (max > arr.length)                    max = arr.length;                if ( max > 0)   // Check that we have data to plot!                {                    // We set the buffer's dimension equal to the biggest value                    double [][] copy = new double [1][(int)timeMap.getRange()[1]]; //max > getMaxXaxis() ? max : getMaxXaxis()];                    for ( int i=0;i < max;i++)                        copy[0][i]=arr[i][0];                    ((FlatField)flat_fields.get(handle)).setSamples(copy);                    ((DataReferenceImpl)DataRefs.get(handle)).setData((FlatField)flat_fields.get(handle));                    display.addReference((DataReferenceImpl)DataRefs.get(handle),(ConstantMap [])DataColors.get(handle));                }                else {                    log.error("No data to plot!");                    if ( getMonitor() != null )                        new NetErrorManager(getMonitor(),"Warning while attempting to plot VisAd chart. No data to plot!");                }            }            catch(VisADException ex) {                log.error(ex.toString());                if ( getMonitor() != null )                    new NetErrorManager(getMonitor(),"VisADException while attempting to plot VisAd chart. Message is : " + ex.getMessage());            }            catch(RemoteException ex) {                if ( getMonitor() != null )                    new NetErrorManager(getMonitor(),"Remote Exception while attempting to plot VisAd chart. Message is : " + ex.getMessage());            }            catch(Exception ex) {                log.error(ex.toString());                if ( getMonitor() != null )                    new NetErrorManager(getMonitor(),"Exception while attempting to plot VisAd chart. Message is : " + ex.getMessage());            }        }    }        /** Creates new form ChartOutputSynapse */    public VisADOutputSynapse() {        initComponents();        // Setup a default handle for mono plots.        def_handle.setSerie(getSerie());        def_handle.setBlueColor(230);        def_handle.setGreenColor(0);        def_handle.setRedColor(0);        def_handle.setName("Serie "+getSerie());    }        /** This method is called from within the constructor to initialize the form.  The method initialises     * the default handle for mono plots and calls the initVisAd method to construct the main frame.     */    private void initComponents() {        iFrame = new javax.swing.JFrame();        iFrame.setTitle(getTitle());        iFrame.setResizable(isResizable());        iFrame.addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(java.awt.event.WindowEvent evt) {                exitForm(evt);            }        });        iFrame.pack();        ChartBuffers = new Hashtable();        DataRefs = new Hashtable();        DataColors = new Hashtable();        flat_fields = new Hashtable();        ControlPane = new JPanel();        def_handle = new ChartingHandle();        status_label = new JLabel("Waiting for network to run!");        initVisAd(getMaxXaxis() , "Waiting...");        visadinit = true;    }        /** Exits the form.     * @param evt The window event.     */    private void exitForm(java.awt.event.WindowEvent evt) {        setShow(false);    }        /** Sets the Monitor object of this VisADOutputSyanpse.     * @param newMonitor org.joone.engine.Monitor     */    public void setMonitor(Monitor newMonitor) {        monitor = newMonitor;        if (monitor != null)            monitor.addNeuralNetListener(this);    }        /** Method to plot a serie of a pattern in multi-serie mode     * This method is used by a ChartingHandle to plot a specific serie     * @param pattern The input pattern     * @param handle The ChartingHandle class that describes the serie to plot     */    public void fwdPut(Pattern pattern, ChartingHandle handle) {        SharedBuffer sb = null;        if (isEnabled() && (pattern.getCount() > -1))  {            if ( ChartBuffers.size() > 0 )                sb = (SharedBuffer)ChartBuffers.get(handle);            else                sb = null;            if ( sb == null ) {                try {                    sb = new SharedBuffer();                    sb.setHandle(handle);                    ChartBuffers.put(handle,sb);                    time_set = new Integer1DSet(time,(int)timeMap.getRange()[1]);                    flat_fields.put(handle,new FlatField( func_time_height, time_set));                    ConstantMap[] lineCMap =                    {   new ConstantMap( (float)handle.getRedColor()/255, Display.Red),                        new ConstantMap( (float)handle.getGreenColor()/255, Display.Green),                        new ConstantMap( (float)handle.getBlueColor()/255, Display.Blue),                        new ConstantMap( 1.00f, Display.LineWidth)                    };                    DataColors.put(handle,lineCMap);                    DataRefs.put(handle,new DataReferenceImpl(handle.getName()));                }                catch(VisADException ex) {                    log.error(ex.toString());                    if ( getMonitor() != null )                        new NetErrorManager(getMonitor(),"VisADException while attempting to define VisAd compononent in fwdPut(handle,pattern) method. Message is : " + ex.getMessage());                }                catch(Exception ex) {                    log.error(ex.toString());                    if ( getMonitor() != null )                        new NetErrorManager(getMonitor(),"Exception while attempting to define VisAd component in fwdPut(handle,pattern) method. Message is : " + ex.getMessage());                }                catch(java.lang.OutOfMemoryError err) {                    log.error(err.toString());                    if ( getMonitor() != null )                        new NetErrorManager(getMonitor(),"Out of memory error while attempting to define VisAd component in fwdPut(handle,pattern) method. Message is : " + err.getMessage());                }            }            sb.put(pattern.getArray()[handle.getSerie()-1],pattern.getCount());        }    }    

⌨️ 快捷键说明

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