state.service.ts 4.41 KB
Newer Older
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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
import {EventEmitter, Injectable} from '@angular/core';
import {ApiService, LshData, RawData, TableInfoData} from './api.service';

@Injectable({
  providedIn: 'root'
})
export class StateService {
  public loadingProgress: number = 0;

  private _rawData: RawData;
  private _lshData: LshData;
  private _tableInfo: TableInfoData;
  private _queryWindow: number[];
  private _table: {[bucket: string]: number[]};

  private _currentTab: number;
  private _labels = {};
  private _sliderValue;
  private _parameters: number[];

  public windowSize = 120;
  public nrOfTables = 5;
  public hashSize = 5;
  public stepSize = 200;
  public querySelectionMode = true;

  public onNewData: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();
  public onNewQuery: EventEmitter<void> = new EventEmitter<void>();
  public onNewTable: EventEmitter<void> = new EventEmitter<void>();
  public onNewTableInfo: EventEmitter<void> = new EventEmitter<void>();

  public onNewLabels: EventEmitter<void> = new EventEmitter<void>();
  public onNewTab: EventEmitter<void> = new EventEmitter<void>();
  public onNewSlider: EventEmitter<number> = new EventEmitter<number>();

  constructor(private api: ApiService) {
     this.initialize();
  }

  async initialize(): Promise<void> {
    this.loadingProgress = 0;
    await this.getRawData();
    this.loadingProgress = 50;
    await this.createWindows();
    this.loadingProgress = 100;
  }

  async reset(): Promise<void> {
    this.loadingProgress = 50;
    await this.createWindows();
    this.loadingProgress = 100;
  }

  async getRawData(): Promise<void> {
    this.rawData = await this.api.readFile();
  }

  async createWindows(): Promise<void> {
    await this.api.createWindows(this.parameters);
    this.onNewWindows.emit();
  }

  async lshInitial(): Promise<void> {
    this.lshData = await this.api.lshInitial(this._queryWindow);
    this.createTable();
  }

  async update(): Promise<void> {
    this.lshData = await this.api.lshUpdate(this._queryWindow, [], this._parameters);
    this.createTable();
  }

  async getTableInfo(): Promise<TableInfoData> {
    this.tableInfo = await this.api.getTableInfo(Object.values(this._table));
    return this.tableInfo;
  }

  async getQueryWindow(windowIndex: number | {[index: number]: boolean}): Promise<number[]> {
    this.queryWindow = await this.api.getQueryWindow(windowIndex);
    return this._queryWindow;
  }

  async getWindow(indices: number[]): Promise<number[][]> {
    return await this.api.getWindowByIndices(indices);
  }

  public createTable() {
    const indices: number[] = this.lshData.distances.map((x) => x > 500 ? 100 : Math.floor(x / 5));
    const table = {};
    this.lshData.candidates.forEach((candidate, index) => {
      if (table[indices[index]] === undefined)
      {
        table[indices[index]] = [];
      }
      table[indices[index]].push(candidate);
    });
    this.table = table;
    this.getTableInfo();
  }

  public set rawData(v: RawData) {
    this._rawData = v;
    this.onNewData.emit();
  }

  public get rawData(): RawData {
    return this._rawData;
  }

  public set lshData(v: LshData) {
    this._lshData = v;
  }

  public get lshData(): LshData {
    return this._lshData;
  }

  public set tableInfo(v: TableInfoData) {
    this._tableInfo = v;
    this.onNewTableInfo.emit();
  }

  public get tableInfo(): TableInfoData {
    return this._tableInfo;
  }

  public set table(v: {[bucket: string]: number[]}) {
    this._table = v;
    this.onNewTable.emit();
  }

  public get table(): {[bucket: string]: number[]} {
    return this._table;
  }

  public set labels(v) {
    this._labels = v;
    this.onNewLabels.emit();
  }

  public get labels() {
    return this._labels;
  }

  public set currentTab(v: number) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

  public get currentTab(): number {
    return this._currentTab;
  }

  public set sliderValue(v: number) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

  public get sliderValue(): number {
    return this._sliderValue;
  }

  public set queryWindow(v: number[]) {
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

  public get queryWindow(): number[] {
    return this._queryWindow;
  }

  public get parameters(): {[parameter: string]: number} {
    return {
      windowsize: this.windowSize,
      hashsize: this.hashSize,
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
    };
  }
}