[1] | 1 | package de.ugoe.cs.eventbench.markov;
|
---|
| 2 |
|
---|
| 3 | import java.util.Random;
|
---|
| 4 |
|
---|
| 5 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 6 |
|
---|
| 7 | public abstract class State {
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * {@link Event} associated with the state.
|
---|
| 11 | */
|
---|
| 12 | private Event<?> action;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Identifier of the state
|
---|
| 16 | */
|
---|
| 17 | private String id;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Random number generator used for state transmissions.
|
---|
| 21 | */
|
---|
| 22 | protected Random rand;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * State transmission function to be used by models.
|
---|
| 26 | * @return
|
---|
| 27 | */
|
---|
| 28 | public abstract State getNextState();
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Creates a new State object. The id should be unique.
|
---|
| 32 | * @param id identifier string of the state
|
---|
| 33 | */
|
---|
| 34 | protected State(String id, Event<?> action) {
|
---|
| 35 | this.id = id;
|
---|
| 36 | this.action = action;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Dummy method for history-less state implementations.
|
---|
| 41 | */
|
---|
| 42 | public void setHistoryObject() {}
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Defines a random number generator to be used by getNextState.
|
---|
| 46 | * @param r Random number generator
|
---|
| 47 | */
|
---|
| 48 | public void setRandom(Random r) {
|
---|
| 49 | rand = r;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Returns the id of the state.
|
---|
| 54 | * @return id of the state
|
---|
| 55 | */
|
---|
| 56 | public String getId() {
|
---|
| 57 | return id;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * The {@link Event} associated with this state.
|
---|
| 62 | * @return {@link Event} associated with this state
|
---|
| 63 | */
|
---|
| 64 | public Event<?> getAction() {
|
---|
| 65 | return action;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Two states are equal if their id string is equal.
|
---|
| 70 | */
|
---|
| 71 | @Override
|
---|
| 72 | public boolean equals(Object other) {
|
---|
| 73 | if( other==this ) {
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 | boolean isEqual = false;
|
---|
| 77 | if( other instanceof State ) {
|
---|
| 78 | isEqual = id.equals(((State) other).id);
|
---|
| 79 | }
|
---|
| 80 | return isEqual;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | /* (non-Javadoc)
|
---|
| 85 | * @see java.lang.Object#hashCode()
|
---|
| 86 | */
|
---|
| 87 | @Override
|
---|
| 88 | public int hashCode() {
|
---|
| 89 | int multiplier = 37;
|
---|
| 90 | int hash = 42;
|
---|
| 91 |
|
---|
| 92 | hash = multiplier*hash + id.hashCode();
|
---|
| 93 |
|
---|
| 94 | return hash;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|