cache.service.ts 2.01 KB
Newer Older
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
import {EventEmitter, Injectable} from '@angular/core';
import {ApiService, RawData} from './api.service';

@Injectable({
  providedIn: 'root'
})
export class CacheService {
  public rawValues: number[];
  public rawIndices: string[];
  public windows: number[][];
  public hashFunctions;
  public tables;
  public windowSimilarity;

  public windowSize = 20;
  public nrOfTables = 10;
  public hashSize = 10;

  public onQuery: EventEmitter<void> = new EventEmitter<void>();
  public onNewTables: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();


  public initialized: Promise<void>;

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

  async initialize(): Promise<void> {
    await this.getRawData();
    await this.getWindows();
    await this.createTables();
  }

  async reset(): Promise<void> {
    this.windowSimilarity = undefined;
    await this.getWindows();
    await this.createTables();
  }

  async getRawData(): Promise<void> {
    const rawData: RawData = await this.api.readFile();
    this.rawIndices = rawData.index;
    this.rawValues = rawData.values;
  }

  async getWindows(): Promise<void> {
    this.windows = await this.api.createWindows(this.rawValues, this.parameters);
    this.onNewWindows.emit();
  }

  async createTables(): Promise<void> {
    this.tables = await this.api.createTables(this.windows, this.parameters);
    this.onNewTables.emit();
  }

  async getSimilarWindows(window): Promise<any> {
    this.windowSimilarity = await this.api.getSimilarWindows(window, this.tables);
    this.onQuery.emit();
    return this.windowSimilarity;
  }

  async updateTables(labeldata): Promise<void> {
    this.tables = await this.api.updateTables(this.windows, labeldata, this.tables, this.parameters);
    this.onNewTables.emit();
  }

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