@lf-lang/reactor-ts
Version:
A reactor-oriented programming framework in TypeScript
130 lines • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FederatePortAction = exports.Dummy = exports.Shutdown = exports.Startup = exports.Action = exports.SchedulableAction = void 0;
const internal_1 = require("./internal");
const defaultMIT = internal_1.TimeValue.withUnits(1, internal_1.TimeUnit.nsec); // FIXME
class SchedulableAction {
}
exports.SchedulableAction = SchedulableAction;
/**
* An action denotes a self-scheduled event.
* An action, like an input, can cause reactions to be invoked.
* Whereas inputs are provided by other reactors, actions are scheduled
* by this reactor itself, either in response to some observed external
* event or as a delayed response to some input event. The action can be
* scheduled by a reactor by invoking the schedule function in a reaction
* or in an asynchronous callback that has been set up in a reaction.
*/
class Action extends internal_1.ScheduledTrigger {
origin;
minDelay;
minInterArrival = defaultMIT;
get() {
if (this.isPresent()) {
return this.value;
}
else {
return undefined;
}
}
asSchedulable(key) {
if (this._key === key) {
return this.scheduler;
}
throw Error("Invalid reference to container.");
}
getManager(key) {
if (this._key === key) {
return this.manager;
}
throw Error("Unable to grant access to manager.");
}
scheduler = new (class extends SchedulableAction {
action;
get() {
return this.action.get();
}
constructor(action) {
super();
this.action = action;
}
schedule(extraDelay, value, intendedTag) {
if (!(extraDelay instanceof internal_1.TimeValue)) {
extraDelay = internal_1.TimeValue.zero();
}
let tag = this.action.runtime.util.getCurrentTag();
const delay = this.action.minDelay.add(extraDelay);
tag = tag.getLaterTag(delay);
if (this.action.origin === internal_1.Origin.physical) {
tag = new internal_1.Tag((0, internal_1.getCurrentPhysicalTime)(), 0).getLaterTag(delay);
}
else if (this.action instanceof FederatePortAction) {
if (intendedTag === undefined) {
throw new Error("No intended tag given while attempting to schedule an event coming from another federate.");
}
if (!this.action.runtime.util.isLastTAGProvisional() &&
!intendedTag.isGreaterThan(this.action.runtime.util.getCurrentTag())) {
throw new Error(`Intended tag must be greater than current tag. Intended tag: ${String(intendedTag)}` +
` Current tag: ${String(this.action.runtime.util.getCurrentTag())}`);
}
if (this.action.runtime.util.isLastTAGProvisional() &&
!intendedTag.isGreaterThanOrEqualTo(this.action.runtime.util.getCurrentTag())) {
throw new Error("Intended tag must be greater than or equal to current tag" +
`, when the last TAG is provisional. Intended tag: ${String(intendedTag)}` +
` Current tag: ${String(this.action.runtime.util.getCurrentTag())}`);
}
internal_1.Log.debug(this, () => `Using intended tag from RTI, similar to schedule_at_tag(tag) with an intended tag: ${String(intendedTag)}`);
tag = intendedTag;
}
internal_1.Log.debug(this, () => `Scheduling ${this.action.origin} action
${String(this.action._getFullyQualifiedName())} with tag: ${tag}`);
this.action.runtime.schedule(new internal_1.TaggedEvent(this.action, tag, value));
}
})(this);
/**
* Construct a new action.
* @param __container__ The reactor containing this action.
* @param origin Optional. If physical, then the hardware clock on the local
* platform is used to determine the tag of the resulting event. If logical,
* the current logical time (plus one microstep) is used as the offset.
* @param minDelay Optional. Defaults to 0. Specifies the intrinsic delay of
* any events resulting from scheduling this action.
* @param minInterArrival Optional. Defaults to 1 nsec. Specifies the minimum
* intrinsic delay between to occurrences of this action.
*/
constructor(__container__, origin, minDelay = internal_1.TimeValue.secs(0), minInterArrival = defaultMIT) {
super(__container__);
this.origin = origin;
this.minDelay = minDelay;
}
toString() {
return this._getFullyQualifiedName();
}
}
exports.Action = Action;
class Startup extends Action {
// FIXME: this should not be a schedulable trigger, just a trigger
constructor(__parent__) {
super(__parent__, internal_1.Origin.logical);
}
}
exports.Startup = Startup;
class Shutdown extends Action {
constructor(__parent__) {
super(__parent__, internal_1.Origin.logical);
}
}
exports.Shutdown = Shutdown;
class Dummy extends Action {
constructor(__parent__) {
super(__parent__, internal_1.Origin.logical);
}
}
exports.Dummy = Dummy;
class FederatePortAction extends Action {
constructor(__parent__, origin, minDelay = internal_1.TimeValue.zero()) {
super(__parent__, origin, minDelay);
}
}
exports.FederatePortAction = FederatePortAction;
//# sourceMappingURL=action.js.map