-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(interpreter): Refactored interpreter service
- Loading branch information
Showing
4 changed files
with
166 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
|
||
agent person 10 { | ||
const x = random(0, width()); | ||
const y = random(0, height()); | ||
|
||
property close = filter(agents(person) => p => p.x >= 0 and p.x <= width()); | ||
property closest = min(close => c => c.x); | ||
|
||
property color = true; | ||
|
||
property coloured = closest.color otherwise false; | ||
agent person 50 { | ||
const speed = 2; | ||
property angle: random(0, 2 * pi()) = angle + choice(-0.1, 0.1); | ||
|
||
property shouldStay = prob(0.5); | ||
|
||
property xNew: 0 = (x + speed * cos(angle)) % width(); | ||
property yNew: 0 = (y + speed * sin(angle)) % height(); | ||
|
||
property x: random(50, width() - 50) = if shouldStay then x else xNew; | ||
property y: random(50, height() - 50) = if shouldStay then y else yNew; | ||
|
||
const distance = 20; | ||
|
||
property people = agents(person); | ||
property closePeople = filter(people => p => sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)) <= distance); | ||
property closeInfected = filter(closePeople => c => c.infected == true); | ||
|
||
const timespan = 200; | ||
property remaining: timespan = if infected then remaining - 1 else timespan; | ||
|
||
property shouldInfect = prob(0.4); | ||
property infected: prob(0.5) = (infected and remaining > 0) or (count(closeInfected) > 0 and shouldInfect); | ||
|
||
property coloured: false = infected; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { EMPTY, Observable, Subject, interval, of, startWith, switchMap, takeUntil } from "rxjs"; | ||
|
||
class Runner { | ||
|
||
private dataSubject: Subject<number> = new Subject(); | ||
private stopSubject: Subject<boolean> = new Subject(); | ||
|
||
private isPaused = false; | ||
private counter = 0; | ||
|
||
get(steps: number, delay: number): Observable<number> { | ||
return this.dataSubject.asObservable().pipe( | ||
startWith(null), | ||
takeUntil(this.stopSubject), | ||
switchMap(() => interval(delay).pipe(takeUntil(this.stopSubject))), | ||
takeUntil(this.stopSubject), | ||
switchMap(() => { | ||
if (!this.isPaused) { | ||
this.counter++; | ||
if (this.counter <= steps) { | ||
return of(this.counter); | ||
} else { | ||
this.stop(); | ||
return EMPTY; | ||
} | ||
} else { | ||
return EMPTY; | ||
} | ||
}) | ||
); | ||
} | ||
|
||
start() { | ||
this.stop(); | ||
this.dataSubject.next(0); | ||
} | ||
|
||
stop() { | ||
this.isPaused = false; | ||
this.counter = 0; | ||
this.stopSubject.next(true); | ||
} | ||
|
||
pause() { | ||
this.isPaused = true; | ||
} | ||
|
||
resume() { | ||
this.isPaused = false; | ||
} | ||
|
||
step() { | ||
if (this.isPaused) { | ||
this.dataSubject.next(++this.counter); | ||
} | ||
} | ||
} |
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