📄 scenario.java
字号:
/******************************************************************************* ** BonnMotion - a mobility scenario generation and analysis tool ** ** Copyright (C) 2002, 2003 University of Bonn ** ** ** ** This program is free software; you can redistribute it and/or modify ** ** it under the terms of the GNU General Public License as published by ** ** the Free Software Foundation; either version 2 of the License, or ** ** (at your option) any later version. ** ** ** ** This program is distributed in the hope that it will be useful, ** ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** ** GNU General Public License for more details. ** ** ** ** You should have received a copy of the GNU General Public License ** ** along with this program; if not, write to the Free Software ** ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ** *******************************************************************************/package edu.bonn.cs.iv.bonnmotion;import java.io.*;import java.util.*;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;/** Base class for creating new scenarios. */public class Scenario extends App { /** Mobile nodes. */ protected MobileNode[] node; /** Area x length [m]. */ protected double x = 200.0; /** Area y length [m]. */ protected double y = 200.0; /** Duration of scenario [s]. */ protected double duration = 600.0; /** Length of initial time span which is to be cut off after scenario generation [s]. */ protected double ignore = 3600.0; /** Random seed to initialise RNG. */ protected long randomSeed = System.currentTimeMillis(); // this is what the java.util.Random constructor does without parameter, too protected Random rand; /** * Returns random double form the RandomSeed. * @return double */ protected double randomNextDouble() { return rand.nextDouble(); } /** * Returns random Gaussian form the RandomSeed * @return double */ protected double randomNextGaussian() { return rand.nextGaussian(); } public Scenario() {} public Scenario(int nodes, double x, double y, double duration, double ignore, long randomSeed) { node = new MobileNode[nodes]; this.x = x; this.y = y; this.duration = duration; this.ignore = ignore; rand = new Random(this.randomSeed = randomSeed); } public Scenario(String basename) throws FileNotFoundException, IOException { read(basename); } protected boolean parseArg(char key, String val) { switch (key) { case 'd' : // "duration" duration = Double.parseDouble(val); return true; case 'i' : // "ignore" (Einschwingphase) ignore = Double.parseDouble(val); return true; case 'n' : // "nodes" node = new MobileNode[Integer.parseInt(val)]; return true; case 'x' : // "x" x = Double.parseDouble(val); return true; case 'y' : // "y" y = Double.parseDouble(val); return true; case 'R' : // "R" randomSeed = Long.parseLong(val); return true; default : return super.parseArg(key, val); } } protected boolean parseArg(String key, String val) { if (key.equals("ignore") ) { ignore = Double.parseDouble(val); return true; } else if ( key.equals("randomSeed") ) { randomSeed = Long.parseLong(val); return true; } else if ( key.equals("x") ) { x = Double.parseDouble(val); return true; } else if ( key.equals("y") ) { y = Double.parseDouble(val); return true; } else if ( key.equals("duration") ) { duration = Double.parseDouble(val); return true; } else if ( key.equals("nn") ) { node = new MobileNode[Integer.parseInt(val)]; return true; } else return false; } /** Called by subclasses before they generate node movements. */ protected void preGeneration() { duration += ignore; rand = new Random(randomSeed); } /** Called by subclasses after they generate node movements. */ protected void postGeneration() { if (ignore < 600.0) // this is a somewhat arbitrary value :) System.out.println("warning: setting the initial phase to be cut off to be too short may result in very weird scenarios"); if (ignore > 0.0) cut(ignore, duration); } /** Extract a certain time span from the scenario. */ public void cut(double begin, double end) { if ((begin >= 0.0) && (end <= duration) && (begin < end)) { for (int i = 0; i < node.length; i++) node[i].cut(begin, end); duration = end - begin; } } /** * @see edu.bonn.cs.iv.bonnmotion.App#go(String[]) */ public void go ( String[] _args ) { String paramFile = _args[0]; String[] args = new String[_args.length - 1]; System.arraycopy(_args, 1, args, 0, args.length); if (paramFile != null) { try { paramFromFile(paramFile, true); } catch (Exception e) { App.exceptionHandler( "Could not open parameter file", e ); } } parse(args); if ( node == null ) { System.out.println("Please define the number of nodes.");// printHelp(); System.exit(0); } } public static void printHelp() { App.printHelp(); System.out.println("Scenario:"); System.out.println("\t-d <scenario duration>"); System.out.println("\t-i <number of seconds to skip>"); System.out.println("\t-n <number of nodes>"); System.out.println("\t-x <width of simulation area>"); System.out.println("\t-y <height of simulation area>"); System.out.println("\t-R <random seed>"); } public double getDuration() { return duration; } public MobileNode[] getNode() { MobileNode[] r = new MobileNode[node.length]; System.arraycopy(node, 0, r, 0, node.length); return r; } public MobileNode getNode(int n) { if (node[n] == null) node[n] = new MobileNode(); return node[n]; } public double getX() { return x; } public double getY() { return y; } public int nodeCount() { return node.length; } /** * Does the same job as paramFronFile but w/o showing warnings. * @see Scenario#paramFromFile(String _fn) * @param _fn Filename * @param _args arguments list * @throws FileNotFoundException * @throws IOException */ public void paramFromFile(String _fn) throws FileNotFoundException, IOException { paramFromFile( _fn, false ); } /** * Reads arguments from specific file. Then processes * the command line arguments (overrides arguments from file).<br> * This Method must be implemented in every subclass. * @param _fn Filename * @param _warn if warnings should be shown during parsing * @throws FileNotFoundException * @throws IOException */ public void paramFromFile(String _fn, boolean _warn) throws FileNotFoundException, IOException { String line; BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( _fn ) ) ); while ( (line=in.readLine()) != null ) { StringTokenizer st = new StringTokenizer(line, "="); String key = st.nextToken(); String value = st.nextToken(); if (! parseArg(key, value) && (_warn) ) System.out.println("warning: unknown key \"" + key + "\" while parsing arguments from \"" + _fn + "\""); } in.close(); } /** * Reads the base information of a scenario from a * file. * It is typically invoked by application to re-read the processing * scenario from a generated file. * @param basename Basename of the scenario */ public void read(String basename) throws FileNotFoundException, IOException { String line; paramFromFile(basename+".params"); BufferedReader in = new BufferedReader( new InputStreamReader( new GZIPInputStream(new FileInputStream(basename + ".movements.gz")))); int i = 0; while ((line = in.readLine()) != null) { node[i] = new MobileNode(); StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { double time = Double.parseDouble(st.nextToken()); Position pos = new Position( Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())); if (!node[i].add(time, pos)) throw new RuntimeException("Error while adding waypoint."); } i++; } in.close(); } public void setDuration(double _duration) { duration = _duration; } public void write( String _name ) throws FileNotFoundException, IOException { write(_name, null); } /** * Writes the generated scenario and the scenario * parameters to files. * @param basename Basename of the output files */ public void write(String basename, String[] params) throws FileNotFoundException, IOException { PrintWriter info = new PrintWriter(new FileOutputStream(basename + ".params")); if ( params != null ) for (int i = 0; i < params.length; i++) info.println(params[i]); info.println( "ignore=" + ignore ); info.println( "randomSeed=" + randomSeed ); info.println( "x=" + x ); info.println( "y=" + y ); info.println( "duration=" + duration ); info.println( "nn=" + node.length ); info.close(); PrintWriter movements = new PrintWriter( new GZIPOutputStream(new FileOutputStream(basename + ".movements.gz"))); for (int i = 0; i < node.length; i++) movements.println(node[i].movementString()); movements.close(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -