Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Online Facility Location #226

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/benchmark/FacilityLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class Accumulator extends Reactor {

toNextAccumulator = new OutPort<ConfirmExitMsg>(this);

constructor(parent: Reactor) {
constructor(parent: Quadrant) {
super(parent);

this.addReaction(
Expand All @@ -308,15 +308,17 @@ export class Accumulator extends Reactor {
this.fromSecondQuadrant,
this.fromThirdQuadrant,
this.fromFourthQuadrant,
this.writable(this.toNextAccumulator)
this.writable(this.toNextAccumulator),
parent.writable(parent.toAccumulator)
],
function (
this,
fromFirstQuadrant,
fromSecondQuadrant,
fromThirdQuadrant,
fromFourthQuadrant,
toNextAccumulator
toNextAccumulator,
parentToAccumulator
) {
// Reaction.
if (
Expand Down Expand Up @@ -362,6 +364,14 @@ export class Accumulator extends Reactor {
numQuadrantReactors + 1 // Add one for the quadrant reactor itself.
)
);
parentToAccumulator.set(
new ConfirmExitMsg(
numFacilities + 1, // Add one for the facility itself.
// (A quadrant with four children is considered as one facility in Akka-version implementation.)
numCustomers,
numQuadrantReactors + 1 // Add one for the quadrant reactor itself.
)
);
} else {
console.log(
"Accumulator Error: some input ConfirmExitMsg's are undefined."
Expand Down Expand Up @@ -621,12 +631,9 @@ export class Quadrant extends Reactor {
);

// console.log(`Children boundaries: ${childrenBoundaries.get()[0]}, ${childrenBoundaries.get()[1]}, ${childrenBoundaries.get()[2]}, ${childrenBoundaries.get()[3]}`)
const accumulator = new Accumulator(thisReactor);
const toAccumulatorOfQuadrant = (
toAccumulator as unknown as WritablePort<Msg>
).getPort();
const accumulator = new Accumulator(thisReactor as Quadrant);
// Connect Accumulator's output to Quadrant's output.
this.connect(accumulator.toNextAccumulator, toAccumulatorOfQuadrant);
// this.connect(accumulator.toNextAccumulator, toAccumulatorOfQuadrant);

const firstChild = new Quadrant(
thisReactor,
Expand Down
59 changes: 59 additions & 0 deletions src/core/reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export abstract class Reactor extends Component {
*/
private readonly _keyChain = new Map<Component, symbol>();

// This is the keychain for creation, i.e. if Reactor R's mutation created reactor B,
// then R is B's creator, even if they are siblings. R should have access to B,
// at least semantically......?
private readonly _creatorKeyChain = new Map<Component, symbol>();

/**
* This graph has in it all the dependencies implied by this container's
* ports, reactions, and connections.
Expand Down Expand Up @@ -387,6 +392,9 @@ export abstract class Reactor extends Component {
return owner._getKey(component, this._keyChain.get(owner));
}
}
return component
.getContainer()
._getKey(component, this._creatorKeyChain.get(component.getContainer()));
}

/**
Expand Down Expand Up @@ -467,6 +475,20 @@ export abstract class Reactor extends Component {
public delete(reactor: Reactor): void {
reactor._delete();
}

public addChild<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
return this.reactor._addChild(constructor, ...args);
}

public addSibling<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
return this.reactor._addSibling(constructor, ...args);
}
};

/**
Expand Down Expand Up @@ -1555,6 +1577,33 @@ export abstract class Reactor extends Component {
toString(): string {
return this._getFullyQualifiedName();
}

protected _addChild<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
const newReactor = new constructor(this, ...args);
return newReactor;
}

protected _addSibling<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
if (this._getContainer() == null) {
throw new Error(
`Reactor ${this} does not have a parent. Sibling is not well-defined.`
);
}
if (this._getContainer() === this) {
throw new Error(
`Reactor ${this} is self-contained. Adding sibling creates logical issue.`
);
}
const newReactor = this._getContainer()._addChild(constructor, ...args);
this._creatorKeyChain.set(newReactor, newReactor._key);
return newReactor;
}
}

/*
Expand Down Expand Up @@ -1795,6 +1844,16 @@ export interface MutationSandbox extends ReactionSandbox {

getReactor: () => Reactor; // Container

addChild: <R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
) => R;

addSibling: <R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
) => R;

// FIXME:
// forkJoin(constructor: new () => Reactor, ): void;
}
Expand Down