Created
June 20, 2014 16:53
-
-
Save jirkapenzes/0bbc05a4c2c5019f2fc7 to your computer and use it in GitHub Desktop.
Simulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.cz.jpenzes.jsim.simulator; | |
public abstract class BaseEvent<TimeStamp extends Comparable<TimeStamp>> implements Event<TimeStamp> { | |
public abstract void execute(Simulator<TimeStamp> simulator); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.cz.jpenzes.jsim.simulator; | |
import com.google.common.base.Preconditions; | |
import main.cz.jpenzes.jsim.utils.collection.PriorityQueue; | |
import main.cz.jpenzes.jsim.utils.collection.impl.ImmutablePriorityQueue; | |
/** | |
* Author: Jirka Penzes | |
* Date: 21/12/13 19:37 | |
*/ | |
public class Calendar<TimeStamp extends Comparable<TimeStamp>> { | |
private final PriorityQueue<ScheduledEvent<TimeStamp>> priorityQueue; | |
private boolean empty; | |
public Calendar() { | |
priorityQueue = ImmutablePriorityQueue.createEmpty(); | |
} | |
private Calendar(PriorityQueue<ScheduledEvent<TimeStamp>> priorityQueue) { | |
this.priorityQueue = priorityQueue; | |
} | |
public Calendar<TimeStamp> schedule(Event event, TimeStamp timeStamp) { | |
Preconditions.checkNotNull(event, Messages.Errors.CALENDAR_001); | |
Preconditions.checkNotNull(timeStamp, Messages.Errors.CALENDAR_002); | |
return new Calendar(priorityQueue.push(new ScheduledEvent(event, timeStamp))); | |
} | |
public ScheduledEvent<TimeStamp> findNearest() { | |
Preconditions.checkState(priorityQueue.isEmpty(), Messages.Errors.CALENDAR_003); | |
return priorityQueue.peek(); | |
} | |
public Calendar<TimeStamp> remove(ScheduledEvent<TimeStamp> scheduledEvent) { | |
return null; | |
} | |
public boolean isEmpty() { | |
return empty; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.cz.jpenzes.jsim.simulator; | |
/** | |
* Author: Jirka Penzes | |
* Date: 21/12/13 19:43 | |
*/ | |
public interface Event<TimeStamp extends Comparable<TimeStamp>> { | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.cz.jpenzes.jsim.simulator; | |
/** | |
* Author: Jirka Penzes | |
* Date: 21/12/13 19:43 | |
*/ | |
public class ScheduledEvent<TimeStamp extends Comparable<TimeStamp>> implements Comparable<ScheduledEvent<TimeStamp>> { | |
private final Event event; | |
private final TimeStamp timeStamp; | |
public ScheduledEvent(Event event, TimeStamp timeStamp) { | |
this.event = event; | |
this.timeStamp = timeStamp; | |
} | |
public Event getEvent() { | |
return event; | |
} | |
public TimeStamp getTimeStamp() { | |
return timeStamp; | |
} | |
@Override | |
public int compareTo(ScheduledEvent<TimeStamp> other) { | |
return getTimeStamp().compareTo(other.getTimeStamp()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.cz.jpenzes.jsim.simulator; | |
/** | |
* Author: Jirka Penzes | |
* Date: 22/12/13 19:31 | |
*/ | |
public abstract class Simulator<TimeStamp extends Comparable<TimeStamp>> { | |
private Calendar<TimeStamp> calendar; | |
private TimeStamp localVirtualTime; | |
private EventInvoker<TimeStamp, Event<TimeStamp>> invoker; | |
protected Simulator(SimulatorSetup<TimeStamp> setup) { | |
this(new BaseEventInvoker(), setup); | |
} | |
protected Simulator(EventInvoker<TimeStamp, Event<TimeStamp>> invoker, SimulatorSetup<TimeStamp> setup) { | |
this.invoker = invoker; | |
} | |
public SimulationResult simulate(final SimulationModel<TimeStamp> model) { | |
prepareEnvironment(model); | |
while (!calendar.isEmpty()) { | |
ScheduledEvent<TimeStamp> scheduledEvent = calendar.findNearest(); | |
localVirtualTime = scheduledEvent.getTimeStamp(); | |
invoker.invoke(this, scheduledEvent.getEvent()); | |
calendar = calendar.remove(scheduledEvent); | |
} | |
return new SimulationResult(); | |
} | |
private void prepareEnvironment(final SimulationModel<TimeStamp> model) { | |
calendar = new Calendar<TimeStamp>(); | |
model.prepareInitialEvents(); | |
for (ScheduledEvent<TimeStamp> scheduledEvent : model.getInitialEvents()) { | |
calendar.schedule(scheduledEvent.getEvent(), scheduledEvent.getTimeStamp()); | |
} | |
} | |
public TimeStamp getLocalVirtualTime() { | |
return localVirtualTime; | |
} | |
public void scheduleAt(final TimeStamp time, final Event<TimeStamp> event) { | |
calendar.schedule(event, time); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment