001    package org.LiveGraph.demoDataSource;
002    
003    import org.LiveGraph.dataFile.write.DataStreamWriter;
004    import org.LiveGraph.dataFile.write.DataStreamWriterFactory;
005    
006    import com.softnetConsult.utils.sys.SystemTools;
007    
008    
009    /**
010     * This class is used for generating a demo data file for demonstration and
011     * testing purposes.
012     * It creates a data file and fills it with data. This happens in bursts
013     * of {@value #MIN_BURST} to {@value #MAX_BURST} datasets at a time.
014     * Between the bursts the execution is paused for {@value #MIN_SLEEP}
015     * to {@value #MAX_SLEEP} milliseconds. This way a data generating simulation
016     * is emulated. The program stops after {@value #MAX_DATASETS} datasets have
017     * been written.
018     *  
019     * 
020     * <p style="font-size:smaller;">This product includes software developed by the
021     *    <strong>LiveGraph</strong> project and its contributors.<br />
022     *    (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>)<br />
023     *    Copyright (c) 2007-2008 G. Paperin.<br />
024     *    All rights reserved.
025     * </p>
026     * <p style="font-size:smaller;">File: LiveGraphDemo.java</p> 
027     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
028     *    without modification, are permitted provided that the following terms and conditions are met:
029     * </p>
030     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
031     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
032     *    this list of conditions and the following disclaimer.<br />
033     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
034     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
035     *    and the following disclaimer in the documentation and/or other materials provided with
036     *    the distribution.<br />
037     *    3. All advertising materials mentioning features or use of this software or any derived
038     *    software must display the following acknowledgement:<br />
039     *    <em>This product includes software developed by the LiveGraph project and its
040     *    contributors.<br />(http://www.live-graph.org)</em><br />
041     *    4. All advertising materials distributed in form of HTML pages or any other technology
042     *    permitting active hyper-links that mention features or use of this software or any
043     *    derived software must display the acknowledgment specified in condition 3 of this
044     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
045     *    homepage (http://www.live-graph.org).
046     * </p>
047     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
048     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
049     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
050     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
051     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
052     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
053     * </p>
054     * 
055     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
056     * @version {@value org.LiveGraph.LiveGraph#version}
057     */
058    public class LiveGraphDemo {
059    
060    public static final String DEMO_DIR = System.getProperty("user.dir");
061    
062    public static final int MIN_SLEEP = 0;
063    public static final int MAX_SLEEP = 1;
064    public static final int MIN_BURST = 1;
065    public static final int MAX_BURST = 10;
066    public static final int MAX_DATASETS = 1000000;
067    
068    public void exec() {
069            
070            // Print a welcome message:
071            System.out.println("Welcome to the LiveGraph demo.");
072            
073            // Setup a data writer object:
074            DataStreamWriter out = DataStreamWriterFactory.createDataWriter(DEMO_DIR, "LiveGraphDemo");
075            
076            // Set a values separator:
077            out.setSeparator(";");
078            
079            // Add a file description line:
080            out.writeFileInfo("LiveGraph demo file.");
081            
082            // Set-up the data series:
083            out.addDataSeries("Time");
084            out.addDataSeries("Dataset number");
085            out.addDataSeries("Burst number");
086            out.addDataSeries("Random value");
087            
088            // Loop until enough datasets a written:
089            int datasetNumber = 0;
090            int burstNumber = 0;
091            long startMillis = System.currentTimeMillis();
092            while (MAX_DATASETS > datasetNumber) {
093            
094                    // Status message:
095                    System.out.println("Datasets written so far: " + datasetNumber + ". "
096                                                     + "Now writing burst " + burstNumber + "...");
097                    
098                    // Write a few datasets to the file:
099                    int burstSize = (int) (MIN_BURST + (Math.random() * (double) (MAX_BURST - MIN_BURST)));
100                    for (int b = 0; b < burstSize && MAX_DATASETS > datasetNumber; b++) {
101                            
102                            // Set-up the data values:
103                            out.setDataValue(System.currentTimeMillis() - startMillis);
104                            out.setDataValue(datasetNumber);
105                            out.setDataValue(burstNumber);
106                            out.setDataValue(Math.random());
107                            
108                            // Write dataset to disk:
109                            out.writeDataSet();
110                            
111                            // Check for IOErrors:                  
112                            if (out.hadIOException()) {
113                                    out.getIOException().printStackTrace();
114                                    out.resetIOException();
115                            }
116                            
117                            datasetNumber++;
118                    }
119                    burstNumber++;
120                    
121                    
122                    // Pause:
123                    Thread.yield();
124                    long sleep = (long) (MIN_SLEEP + (Math.random() * (double) (MAX_SLEEP - MIN_SLEEP)));
125                    SystemTools.sleep(sleep);
126                    Thread.yield();
127            }               
128            
129            // Finish:
130            out.close();
131            System.out.println("Demo finished. Cheers.");
132    }
133    
134    public static void main(String[] unusedArgs) {
135            (new LiveGraphDemo()).exec();   
136    }
137    
138    } // public class LiveGraphDemo