001 package org.LiveGraph.demoDataSource; 002 003 import java.io.IOException; 004 005 import org.LiveGraph.LiveGraph; 006 import org.LiveGraph.dataFile.common.PipeClosedByReaderException; 007 import org.LiveGraph.dataFile.write.DataStreamWriter; 008 009 import com.softnetConsult.utils.sys.SystemTools; 010 011 012 /** 013 * This is a demo showing how to use LiveGraph in memory stream mode, i.e. how to feed data to LiveGraph 014 * without through a memory stream, without using a file on the hard disk. 015 * 016 * <p> 017 * <strong>LiveGraph</strong> 018 * (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>). 019 * </p> 020 * <p>Copyright (c) 2007-2008 by G. Paperin.</p> 021 * <p>File: LiveGraphMemoryStreamDemo.java</p> 022 * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or 023 * without modification, are permitted provided that the following terms and conditions are met: 024 * </p> 025 * <p style="font-size:smaller;">1. Redistributions of source code must retain the above 026 * acknowledgement of the LiveGraph project and its web-site, the above copyright notice, 027 * this list of conditions and the following disclaimer.<br /> 028 * 2. Redistributions in binary form must reproduce the above acknowledgement of the 029 * LiveGraph project and its web-site, the above copyright notice, this list of conditions 030 * and the following disclaimer in the documentation and/or other materials provided with 031 * the distribution.<br /> 032 * 3. All advertising materials mentioning features or use of this software or any derived 033 * software must display the following acknowledgement:<br /> 034 * <em>This product includes software developed by the LiveGraph project and its 035 * contributors.<br />(http://www.live-graph.org)</em><br /> 036 * 4. All advertising materials distributed in form of HTML pages or any other technology 037 * permitting active hyper-links that mention features or use of this software or any 038 * derived software must display the acknowledgment specified in condition 3 of this 039 * agreement, and in addition, include a visible and working hyper-link to the LiveGraph 040 * homepage (http://www.live-graph.org). 041 * </p> 042 * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY 043 * OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 044 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 045 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 046 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 047 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 048 * </p> 049 * 050 * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>) 051 * @version {@value org.LiveGraph.LiveGraph#version} 052 * 053 */ 054 public class LiveGraphMemoryStreamDemo { 055 056 public static final int SLEEP_MEAN = 100; 057 public static final int SLEEP_SCATTER = 100; 058 public static final int BURST_MEAN = 10; 059 public static final int BURST_SCATTER = 5; 060 public static final int MAX_DATASETS = 100000; 061 062 public void exec() throws IOException { 063 064 // Print a welcome message: 065 System.out.println("Welcome to the LiveGraph memory mode demo."); 066 067 // Start LiveGraph: 068 LiveGraph lg = LiveGraph.application(); 069 lg.execStandalone(); 070 071 // Turn LiveGraph into memory mode: 072 DataStreamWriter out = lg.updateInvoker().startMemoryStreamMode(); 073 if (null == out) { 074 System.out.println("Could not switch LiveGraph into memory stream mode."); 075 lg.disposeGUIAndExit(); 076 return; 077 } 078 079 080 // Set a values separator: 081 out.setSeparator(";"); 082 083 // Add a file description line: 084 out.writeFileInfo("LiveGraph demo file."); 085 086 // Set-up the data series: 087 out.addDataSeries("Time"); 088 out.addDataSeries("Dataset number"); 089 out.addDataSeries("Burst number"); 090 out.addDataSeries("Random value"); 091 092 // Loop until enough datasets a written: 093 int datasetNumber = 0; 094 int burstNumber = 0; 095 long startMillis = System.currentTimeMillis(); 096 while (MAX_DATASETS > datasetNumber) { 097 098 // Status message: 099 System.out.println("Datasets sent through memory so far: " + datasetNumber + ". " 100 + "Now sending burst " + burstNumber + "..."); 101 102 // Write a few datasets to the file: 103 int burstSize = (int) Math.max(BURST_MEAN + Math.random() * 2 * BURST_SCATTER - BURST_SCATTER, 0.); 104 for (int b = 0; b < burstSize && MAX_DATASETS > datasetNumber; b++) { 105 106 // Set-up the data values: 107 out.setDataValue(System.currentTimeMillis() - startMillis); 108 out.setDataValue(datasetNumber); 109 out.setDataValue(burstNumber); 110 out.setDataValue(Math.random()); 111 112 // Write dataset to disk: 113 out.writeDataSet(); 114 115 // If LiveGraph's main window was closed by user, we can finish the demo: 116 if (out.hadIOException()) { 117 if (out.getIOException() instanceof PipeClosedByReaderException) { 118 System.out.println("LiveGraph window closed. No reason for more data. Finishing."); 119 out.close(); 120 System.out.println("Demo finished. Cheers."); 121 return; 122 } 123 } 124 125 // Check for any other IOErrors and display: 126 if (out.hadIOException()) { 127 out.getIOException().printStackTrace(); 128 out.resetIOException(); 129 } 130 131 datasetNumber++; 132 } 133 burstNumber++; 134 135 136 // Pause: 137 long sleep = (long) Math.max(SLEEP_MEAN + Math.random() * 2 * SLEEP_SCATTER - SLEEP_SCATTER, 1.); 138 SystemTools.sleep(sleep); 139 } 140 141 // Finish: 142 out.close(); 143 lg.disposeGUIAndExit(); 144 System.out.println("Demo finished. Cheers."); 145 } 146 147 public static void main(String[] unusedArgs) { 148 try { 149 (new LiveGraphMemoryStreamDemo()).exec(); 150 } catch (Exception e) { 151 e.printStackTrace(); 152 } 153 } 154 155 } // public class LiveGraphMemoryStreamDemo