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