~hokiegeek/biologist-web

ref: eb01fd11848c890f7301568a617ac7cd259c55d5 biologist-web/src/app/lab.service.ts -rw-r--r-- 5.7 KiB
eb01fd11HokieGeek Finally fixed the annoying box shadow 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { environment } from '../environments/environment';

import { Cell } from './cell';

const maxGenerationsPerExperiment = 500;
// const maxGenerationsPerPoll = 25
const server = environment.biologistUrl;

class Dimensions {
    width: number;
    height: number;
}

class Location {
    X: number;
    Y: number;
}

class CreateAnalysisResponse {
    ID: string;
    dims: Dimensions;
}

class AnalysisUpdate {
    ID: string;
    Dims: Dimensions;
    Status: string;
    Generation: number;
    Living: Location[];
}

class AnalysisUpdateResponse {
    ID: string;
    Updates: AnalysisUpdate[];
}

export class Generation {
    num: number;
    status: string;
    living: Cell[];

    constructor(n, s, l) {
        this.num = n;
        this.status = s;
        this.living = l;
    }
}

const pollRateMs = 1000;
const maxGenerationsPerPoll = 25;

export class Experiment {
    id: string;
    rows: number;
    columns: number;
    initialDensity: number;
    autoStart: boolean;
    seed: Generation;
    generations: { [gen: number]: Generation } = {};
    maxGenerations: number;
    private lowGeneration = 0;
    private highGeneration = 0;
    private isPolling = true;
    private poller;

    constructor(private lab: Lab, rows: number, columns: number, density: number, autoStart: boolean, maxGenerations: number) {
        this.rows = rows;
        this.columns = columns;
        this.initialDensity = density;
        this.autoStart = autoStart;
        this.maxGenerations = maxGenerations;
    }

    private create() {
        this.lab.createExperiment(this.columns, this.rows, this.seed.living)
            .subscribe(response => {
                    // console.log(">> ANALYZE CALLBACK", response)
                    this.id = response.ID;

                    this.lab.control(this.id, 0);
                    this.poller = setInterval(() => {
                            if (this.isPolling) {
                                this.lab.poll(this.id, this.highGeneration, maxGenerationsPerPoll)
                                    .subscribe(updates => {
                                            for (const update of updates.Updates) {
                                                // console.log(">> POLL CALLBACK", update)
                                                if (update.Generation < this.lowGeneration) {
                                                    this.lowGeneration = update.Generation;
                                                }
                                                if (update.Generation > this.highGeneration) {
                                                    this.highGeneration = update.Generation;
                                                }
                                                this.generations[update.Generation] =
                                                    new Generation(update.Generation, update.Status, update.Living);
                                            }
                                        }
                                    );
                            }
                        }, pollRateMs
                        );
                }
            );
    }

    start() {
        if (this.id == null) {
            this.create();
        }
        this.isPolling = true;
    }

    stop() {
        this.isPolling = false;
    }

    incinerate() {
        this.stop();
        clearInterval(this.poller);
        this.lab.removeExperiment(this);
        this.lab.control(this.id, 2);
    }

    numGenerations(): number {
        return Object.keys(this.generations).length;
    }

    get(num: number): Generation {
        // console.log("get: ", num)
        if (num in this.generations) {
            return this.generations[num];
        } else if (this.numGenerations() === 0 && this.seed != null) {
            return this.seed;
        } else {
            console.log('oh oh: get(): ', num, this.generations);
            return null;
        }
        // TODO if num is outside the boundaries (lowGen, highGen), poll for gen 'num' as well as a buffer around it
        // -> what about the boundaries? should I poll for the space between highGen and num+buffer? What if that is huge?
        // return this.generations.
    }
}

@Injectable()
export class Lab {
    experiments: Experiment[] = [];

    constructor(private http: HttpClient) { }

    newExperiment(rows: number, cols: number, density: number, autostart: boolean) {
        this.experiments.push(new Experiment(this, rows, cols, density, autostart, maxGenerationsPerExperiment));
    }

    removeExperiment(experiment: Experiment) {
        const index: number = this.experiments.indexOf(experiment);
        if (index > -1) {
            this.experiments.splice(index, 1);
            // this.lab.control(this.id, 1)
        }
    }

    createExperiment(width, height, seed): Observable<CreateAnalysisResponse> {
        const req = {Dims: {Width: width, Height: height}, Pattern: 0, Seed: seed};
        return this.http.post<CreateAnalysisResponse>(server + '/analyze', JSON.stringify(req));
        // .map((res: Response) => res.json())
        // .catch()
    }

    poll(id, startingGen, maxGen): Observable<AnalysisUpdateResponse> {
        const req = {'ID': id, 'StartingGeneration': startingGen, 'NumMaxGenerations': maxGen};
        return this.http.post<AnalysisUpdateResponse>(server + '/poll', JSON.stringify(req));
        // .map((res: Response) => res.json())
    }

    control(id, order) {
        // console.log(">> CONTROL")
        this.http.post(server + '/control', JSON.stringify({'ID': id, 'Order': order})).subscribe(blah => console.log('blah', blah));
    }
}