|
| 1 | +import { InMultiPort, InPort, OutMultiPort, OutPort, Parameter, Reactor, State, } from "../../core/internal"; |
| 2 | +import { SORActor } from "./actor"; |
| 3 | +import { SORPeer } from "./peer"; |
| 4 | +import { Message, SorBorder, omega } from "./sorutils"; |
| 5 | + |
| 6 | +export class SORRunner extends Reactor { |
| 7 | + protected sorActors: State<Reactor[]>; |
| 8 | + protected sorPeer: State<Reactor | undefined>; |
| 9 | + |
| 10 | + protected portToSORActors: OutPort<Message>; |
| 11 | + protected portToSORPeer: OutPort<Message>; |
| 12 | + // Unsure if this would work, let's just try...... |
| 13 | + protected portFromSORActor: InPort<Message>; |
| 14 | + protected portFromSORPeer: InPort<Message>; |
| 15 | + |
| 16 | + constructor(parent: Reactor, size: number, _randoms: number[][]) { |
| 17 | + super(parent, "SORRunner"); |
| 18 | + // These are in SorRunner; |
| 19 | + const s = size; |
| 20 | + // In the scala implementation a simple /2 was used. |
| 21 | + // In JS we might need to enforce some sort of guarantee as it was used to calculate position |
| 22 | + const part = Math.floor(s / 2); |
| 23 | + /** These are from Savina. They should be rather irrelevant, actually. */ |
| 24 | + this.sorActors = new State([]); |
| 25 | + this.sorPeer = new State(undefined); |
| 26 | + |
| 27 | + /** These are the actual messaging passing mechanism that are synonomous to that of Savina. */ |
| 28 | + // This creates a bunch of ports. |
| 29 | + this.portToSORActors = new OutPort(this); |
| 30 | + this.portToSORPeer = new OutPort(this); |
| 31 | + |
| 32 | + // SorRunner::boot() |
| 33 | + this.addMutation( |
| 34 | + [this.startup], |
| 35 | + [this.sorActors, this.sorPeer], |
| 36 | + function (this, sorActors, sorPeer) { |
| 37 | + const myBorder: Reactor[] = []; |
| 38 | + const randoms = _randoms; |
| 39 | + |
| 40 | + // In scala, (i <- 0 until s) is loop excluding s. |
| 41 | + const sorActorsValue = sorActors.get(); |
| 42 | + for (let i = 0; i < s; ++i) { |
| 43 | + let c = i % 2; |
| 44 | + for (let j = 0; j < part; ++j) { |
| 45 | + const pos = i * (part + 1) + j; |
| 46 | + c = 1 - c; |
| 47 | + // We modify them in bulk, then update the state. |
| 48 | + // Unlike in Scala we do not need to initialise the array here, JS supports sparse array. |
| 49 | + // I have absolutely no idea why these parametres are called as such...... |
| 50 | + sorActorsValue[pos] = this.getReactor()._uncheckedAddSibling( |
| 51 | + SORActor, |
| 52 | + pos, randoms[i][j], c, s, part + 1, omega, this.getReactor(), false |
| 53 | + ); |
| 54 | + // TODO: Make connections |
| 55 | + if (j === (part - 1)) { |
| 56 | + myBorder[i] = sorActorsValue[pos]; |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + } |
| 61 | + sorActors.set(sorActorsValue); |
| 62 | + |
| 63 | + const partialMatrix: number[][] = []; |
| 64 | + for (let i = 0; i < s; ++i) { |
| 65 | + for (let j = 0; j < s - part; ++j) { |
| 66 | + partialMatrix[i][j] = randoms[i][j + part]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const sorPeerValue = this.getReactor()._uncheckedAddSibling( |
| 71 | + SORPeer, |
| 72 | + s, part, partialMatrix, new SorBorder(myBorder), |
| 73 | + // A dirty hack. Maybe this will be removed as ports get added. |
| 74 | + this.getReactor() as SORRunner |
| 75 | + ); |
| 76 | + sorPeer.set(sorPeerValue); |
| 77 | + // TODO: Add connections. |
| 78 | + |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
0 commit comments