📄 simulation.java
字号:
Collection moteXML = mote.getConfigXML(); if (moteXML != null) { element.addContent(moteXML); } config.add(element); } return config; } /** * Sets the current simulation config depending on the given XML elements. * * @see #getConfigXML() * @param configXML * Config XML elements * @param visAvailable * True if simulation is allowed to show visualizers while loading * the given config * @return True if simulation config set successfully * @throws Exception * If configuration could not be loaded */ public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) throws Exception { // Parse elements for (Element element : configXML) { // Title if (element.getName().equals("title")) { title = element.getText(); } // Delay time if (element.getName().equals("delaytime")) { delayTime = Integer.parseInt(element.getText()); } // Tick time if (element.getName().equals("ticktime")) { tickTime = Integer.parseInt(element.getText()); } // Random seed if (element.getName().equals("randomseed")) { randomSeed = Long.parseLong(element.getText()); delayMotesRandom.setSeed(randomSeed); } // Max mote startup delay if (element.getName().equals("motedelay")) { maxMoteStartupDelay = Integer.parseInt(element.getText()); } // Radio medium if (element.getName().equals("radiomedium")) { String radioMediumClassName = element.getText().trim(); Class<? extends RadioMedium> radioMediumClass = myGUI.tryLoadClass( this, RadioMedium.class, radioMediumClassName); if (radioMediumClass != null) { // Create radio medium specified in config try { currentRadioMedium = RadioMedium.generateRadioMedium( radioMediumClass, this); } catch (Exception e) { currentRadioMedium = null; logger.warn("Could not load radio medium class: " + radioMediumClassName); } } // Show configure simulation dialog boolean createdOK = false; if (visAvailable) { createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), this); } else { createdOK = true; } if (!createdOK) { logger.debug("Simulation not created, aborting"); throw new Exception("Load aborted by user"); } // Check if radio medium specific config should be applied if (radioMediumClassName .equals(currentRadioMedium.getClass().getName())) { currentRadioMedium.setConfigXML(element.getChildren(), visAvailable); } else { logger .info("Radio Medium changed - ignoring radio medium specific config"); } } // Mote type if (element.getName().equals("motetype")) { String moteTypeClassName = element.getText().trim(); Class<? extends MoteType> moteTypeClass = myGUI.tryLoadClass(this, MoteType.class, moteTypeClassName); if (moteTypeClass == null) { logger.fatal("Could not load mote type class: " + moteTypeClassName); return false; } MoteType moteType = moteTypeClass.getConstructor((Class[]) null) .newInstance(); boolean createdOK = moteType.setConfigXML(this, element.getChildren(), visAvailable); if (createdOK) { addMoteType(moteType); } else { logger .fatal("Mote type was not created: " + element.getText().trim()); throw new Exception("All mote types were not recreated"); } } // Mote if (element.getName().equals("mote")) { Class<? extends Mote> moteClass = myGUI.tryLoadClass(this, Mote.class, element.getText().trim()); Mote mote = moteClass.getConstructor((Class[]) null).newInstance((Object[]) null); if (mote.setConfigXML(this, element.getChildren(), visAvailable)) { addMote(mote); } else { logger.fatal("Mote was not created: " + element.getText().trim()); throw new Exception("All motes were not recreated"); } } } setChanged(); notifyObservers(this); return true; } /** * Removes a mote from this simulation * * @param mote * Mote to remove */ public void removeMote(Mote mote) { if (isRunning()) { stopSimulation(); motes.remove(mote); startSimulation(); } else { motes.remove(mote); } myGUI.closeMotePlugins(mote); currentRadioMedium.unregisterMote(mote, this); this.setChanged(); this.notifyObservers(this); } /** * Adds a mote to this simulation * * @param mote * Mote to add */ public void addMote(Mote mote) { if (isRunning()) { stopSimulation(); motes.add(mote); startSimulation(); } else { motes.add(mote); } if (maxMoteStartupDelay > 0 && mote.getInterfaces().getClock() != null) { mote.getInterfaces().getClock().setDrift(-delayMotesRandom.nextInt(maxMoteStartupDelay)); } currentRadioMedium.registerMote(mote, this); this.setChanged(); this.notifyObservers(this); } /** * Get a mote from this simulation. * * @param pos * Internal list position of mote * @return Mote */ public Mote getMote(int pos) { return motes.get(pos); } /** * Returns number of motes in this simulation. * * @return Number of motes */ public int getMotesCount() { return motes.size(); } /** * Returns all mote types in simulation. * * @return All mote types */ public Vector<MoteType> getMoteTypes() { return moteTypes; } /** * Returns mote type with given identifier. * * @param identifier * Mote type identifier * @return Mote type or null if not found */ public MoteType getMoteType(String identifier) { for (MoteType moteType : getMoteTypes()) { if (moteType.getIdentifier().equals(identifier)) { return moteType; } } return null; } /** * Adds given mote type to simulation. * * @param newMoteType Mote type */ public void addMoteType(MoteType newMoteType) { moteTypes.add(newMoteType); this.setChanged(); this.notifyObservers(this); } /** * Set delay time to delayTime. When all motes have been ticked, the * simulation waits for this time before ticking again. * * @param delayTime * New delay time (ms) */ public void setDelayTime(int delayTime) { this.delayTime = delayTime; scheduleEvent(delayEvent, currentSimulationTime); this.setChanged(); this.notifyObservers(this); } /** * Returns current delay time. * * @return Delay time (ms) */ public int getDelayTime() { return delayTime; } /** * Set simulation time to simulationTime. * * @param simulationTime * New simulation time (ms) */ public void setSimulationTime(int simulationTime) { currentSimulationTime = simulationTime; this.setChanged(); this.notifyObservers(this); } /** * Returns current simulation time. * * @return Simulation time (ms) */ public int getSimulationTime() { return currentSimulationTime; } /** * Set tick time to tickTime. The tick time is the simulated time every tick * takes. When all motes have been ticked, current simulation time is * increased with tickTime. Default tick time is 1 ms. * * @see #getTickTime() * @see #getTickTimeInSeconds() * @param tickTime * New tick time (ms) */ public void setTickTime(int tickTime) { this.tickTime = tickTime; this.setChanged(); this.notifyObservers(this); } /** * Changes radio medium of this simulation to the given. * * @param radioMedium * New radio medium */ public void setRadioMedium(RadioMedium radioMedium) { // Remove current radio medium from observing motes if (currentRadioMedium != null) { for (int i = 0; i < motes.size(); i++) { currentRadioMedium.unregisterMote(motes.get(i), this); } } // Change current radio medium to new one if (radioMedium == null) { logger.fatal("Radio medium could not be created!"); return; } this.currentRadioMedium = radioMedium; // Add all current motes to the new radio medium for (int i = 0; i < motes.size(); i++) { currentRadioMedium.registerMote(motes.get(i), this); } } /** * Get currently used radio medium. * * @return Currently used radio medium */ public RadioMedium getRadioMedium() { return currentRadioMedium; } /** * Get current tick time (ms). * * @see #setTickTime(int) * @return Current tick time (ms) */ public int getTickTime() { return tickTime; } /** * Get current tick time (seconds). * * @see #setTickTime(int) * @return Current tick time (seconds) */ public double getTickTimeInSeconds() { return (tickTime) / 1000.0; } /** * Return true is simulation is running. * * @return True if simulation is running */ public boolean isRunning() { return isRunning && thread != null; } /** * Get current simulation title (short description). * * @return Title */ public String getTitle() { return title; } /** * Set simulation title. * * @param title * New title */ public void setTitle(String title) { this.title = title; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -