123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- package model;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import com.google.gson.annotations.Expose;
- import interfaces.LocalMode;
- import interfaces.TimelineDependent;
- import ui.controller.IndexTranslator;
- import utility.Vector2Float;
- /**
- * The class HolonSwitch represents a Switch, which can be turned on and off.
- *
- * @author Gruppe14
- *
- */
- public class HolonSwitch extends AbstractCanvasObject implements TimelineDependent {
- /**
- * The class HolonSwitch represents an Object in the system, that has the
- * capacity of manipulate the electricity flow. The switch can be manage
- * automatically through a graph or direct manually.
- *
- */
- /*
- * manual state True, if this wire is working (capable of carrying electricity),
- * else false
- */
- @Expose
- boolean manualActive;
- /*
- * active state True, if this wire is working (capable of carrying electricity),
- * else false
- */
- @Expose
- private boolean autoActive;
- /*
- * true if switch has to be used manually
- */
- @Expose
- boolean manualMode;
- @Expose
- int localPeriod;
- @Expose
- boolean localPeriodActive;
- /*
- * Energy at each point of the graph with 50 predefined points. At the
- * beginning, it starts with all values at energy
- */
- boolean[] activeAt;
- // Points on the UnitGraph
- LinkedList<Vector2Float> graphPoints = new LinkedList<>();
- /**
- * Create a new HolonSwitch with the default name ("Switch"), a default value of
- * automatic handle and active status.
- *
- * @param objName String
- */
- public HolonSwitch(String objName) {
- super(objName);
- setUseLocalPeriod(false);
- activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
- setManualState(true);
- setAutoState(true);
- setManualMode(false);
- setGraphPoints(new LinkedList<Vector2Float>());
- initGraphPoints();
- sampleGraph();
- }
- /**
- * Create a copy of an existing HolonSwitch.
- *
- * @param obj the Object to copy
- */
- public HolonSwitch(AbstractCanvasObject obj) {
- super(obj);
- HolonSwitch copyObj = (HolonSwitch) obj;
- setLocalPeriod(copyObj.getLocalPeriod());
- setUseLocalPeriod(copyObj.isUsingLocalPeriod());
- activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
- super.setName(obj.getName());
- setManualState(copyObj.getManualState());
- setAutoState(true);
- setGraphPoints(new LinkedList<Vector2Float>());
- for (Vector2Float p : copyObj.getGraphPoints()) {
- this.graphPoints.add(new Vector2Float(p.getX(), p.getY()));
- }
- sampleGraph();
- setManualMode(copyObj.getManualMode());
- }
- @Override
- public void initForReflection() {
- super.initForReflection();
- this.graphPoints = new LinkedList<>();
- this.reset();
- }
- /**
- * Calculates the state of the Switch.
- */
- public void switchState() {
- if (!manualMode) {
- setManualMode(true);
- }
- if (this.manualActive == true) {
- setImage("/Images/switch-off.png");
- } else {
- setImage("/Images/switch-on.png");
- }
- this.manualActive = !manualActive;
- }
- public static String getSwitchClosedImage() {
- return "/Images/switch-on.png";
- }
- public static String getSwitchOpenImage() {
- return "/Images/switch-off.png";
- }
- /**
- * Getter for the status of the Switch at a given timestep.
- *
- * @param timeStep state at given iteration.
- * @return state value (true if closed/active, false if open)
- */
- public boolean getState(int timeStep) {
- if (manualMode) {
- return this.manualActive;
- } else {
- return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
- }
- }
- /**
- * Change the state of the Switch to manual.
- *
- * @param state the State
- */
- public void setManualState(boolean state) {
- this.manualActive = state;
- setImage();
- }
- /**
- * Set the state of the Switch to automatic.
- *
- * @param state the State
- */
- public void setAutoState(boolean state) {
- this.autoActive = state;
- setImage();
- }
- /**
- * Set Image of the Switch.
- */
- private void setImage() {
- if (manualMode) {
- if (!this.manualActive) {
- setImage("/Images/switch-off.png");
- } else {
- setImage("/Images/switch-on.png");
- }
- } else {
- if (!this.autoActive) {
- setImage("/Images/switch-off.png");
- } else {
- setImage("/Images/switch-on.png");
- }
- }
- }
- /**
- * For automatic use only (through the graph).
- *
- * @return the Graph Points
- */
- public LinkedList<Vector2Float> getGraphPoints() {
- return graphPoints;
- }
- /**
- * Set the values of the switch in the graph (auto. mode only).
- *
- * @param linkedList the Graph points
- */
- public void setGraphPoints(LinkedList<Vector2Float> linkedList) {
- this.graphPoints = linkedList;
- }
- /**
- * Initialize the Graph as a closed Switch.
- */
- private void initGraphPoints() {
- graphPoints.clear();
- graphPoints.add(new Vector2Float(0, 1));
- graphPoints.add(new Vector2Float(1, 1));
- }
- /**
- * Returns the ManualState.
- *
- * @return boolean Manual State
- */
- public boolean getManualState() {
- return this.manualActive;
- }
- /**
- * Returns the autoActive.
- *
- * @return boolean autoActive
- */
- public boolean getAutoActive() {
- return autoActive;
- }
- /**
- * Set the overall value of the Switch (manual mode).
- *
- * @param mode the mode (boolean)
- */
- public void setManualMode(boolean mode) {
- manualMode = mode;
- }
- /**
- * Get manualmode state.
- *
- * @return boolean manual mode state
- */
- public boolean getManualMode() {
- return manualMode;
- }
- // interfaces.GraphEditable
- @Override
- public GraphType getGraphType() {
- return GraphType.boolGraph;
- }
- @Override
- public LinkedList<Vector2Float> getStateGraph() {
- return graphPoints;
- }
- @Override
- public void reset() {
- initGraphPoints();
- sampleGraph();
- }
- @Override
- public void sampleGraph() {
- activeAt = sampleGraph(100);
- }
- /**
- * Generate out of the Graph Points a array of boolean that represent the
- * Curve("on or off"-Graph) at each sample position. The Values are in the Range
- * [0,1].
- *
- * @param sampleLength amount of samplePositions. The positions are equidistant
- * on the Range[0,1].
- * @return the boolean array of samplepoints.
- */
- private boolean[] sampleGraph(int sampleLength) {
- ListIterator<Vector2Float> iter = this.graphPoints.listIterator();
- Vector2Float before = iter.next();
- Vector2Float after = iter.next();
- boolean[] activeTriggerPos = new boolean[sampleLength];
- for (int i = 0; i < sampleLength; i++) {
- double graphX = (double) i / (double) (sampleLength - 1); // from 0.0 to 1.0
- if (graphX > after.x) {
- before = after;
- after = iter.next();
- }
- activeTriggerPos[i] = (before.getY() >= 0.5);
- }
- return activeTriggerPos;
- }
- // interfaces.LocalMode
- @Override
- public void setLocalPeriod(int period) {
- localPeriod = period;
- }
- @Override
- public int getLocalPeriod() {
- return localPeriod;
- }
- @Override
- public boolean isUsingLocalPeriod() {
- return localPeriodActive;
- }
- @Override
- public void setUseLocalPeriod(boolean state) {
- this.localPeriodActive = state;
- }
- public String toString() {
- return name + "[ID:" + getId() + "]";
- }
- }
|