aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/GraphStore.ts
blob: 301b4d8614da7e51e7b6556ecb8aa67f5e43aef2 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { makeAutoObservable, observable } from 'mobx';

import type EditorStore from '../editor/EditorStore';
import type {
  RelationMetadata,
  SemanticsSuccessResult,
} from '../xtext/xtextServiceResults';

export type Visibility = 'all' | 'must' | 'none';

export function getDefaultVisibility(
  metadata: RelationMetadata | undefined,
): Visibility {
  if (metadata === undefined || metadata.arity <= 0 || metadata.arity > 2) {
    return 'none';
  }
  const { detail } = metadata;
  switch (detail.type) {
    case 'class':
    case 'reference':
    case 'opposite':
      return 'all';
    case 'predicate':
      return detail.error ? 'must' : 'none';
    default:
      return 'none';
  }
}

export function isVisibilityAllowed(
  metadata: RelationMetadata | undefined,
  visibility: Visibility,
): boolean {
  if (metadata === undefined || metadata.arity <= 0 || metadata.arity > 2) {
    return visibility === 'none';
  }
  const { detail } = metadata;
  if (detail.type === 'predicate' && detail.error) {
    // We can't display may matches of error predicates,
    // because they have none by definition.
    return visibility !== 'all';
  }
  return true;
}

const TYPE_HASH_HEX_PREFFIX = '_';

export default class GraphStore {
  semantics: SemanticsSuccessResult = {
    nodes: [],
    relations: [],
    partialInterpretation: {},
  };

  relationMetadata = new Map<string, RelationMetadata>();

  visibility = new Map<string, Visibility>();

  abbreviate = true;

  scopes = false;

  selectedSymbol: RelationMetadata | undefined;

  hexTypeHashes: string[] = [];

  private typeHashesMap = new Map<string, number>();

  constructor(
    private readonly editorStore: EditorStore,
    private readonly nameOverride?: string,
  ) {
    makeAutoObservable<GraphStore, 'editorStore'>(this, {
      editorStore: false,
      semantics: observable.ref,
    });
  }

  getVisibility(relation: string): Visibility {
    const visibilityOverride = this.visibility.get(relation);
    if (visibilityOverride !== undefined) {
      return visibilityOverride;
    }
    return this.getDefaultVisibility(relation);
  }

  getDefaultVisibility(relation: string): Visibility {
    const metadata = this.relationMetadata.get(relation);
    return getDefaultVisibility(metadata);
  }

  isVisibilityAllowed(relation: string, visibility: Visibility): boolean {
    const metadata = this.relationMetadata.get(relation);
    return isVisibilityAllowed(metadata, visibility);
  }

  setVisibility(relation: string, visibility: Visibility): void {
    const metadata = this.relationMetadata.get(relation);
    if (metadata === undefined || !isVisibilityAllowed(metadata, visibility)) {
      return;
    }
    const defaultVisiblity = getDefaultVisibility(metadata);
    if (defaultVisiblity === visibility) {
      this.visibility.delete(relation);
    } else {
      this.visibility.set(relation, visibility);
    }
  }

  cycleVisibility(relation: string): void {
    const metadata = this.relationMetadata.get(relation);
    if (metadata === undefined) {
      return;
    }
    switch (this.getVisibility(relation)) {
      case 'none':
        if (isVisibilityAllowed(metadata, 'must')) {
          this.setVisibility(relation, 'must');
        }
        break;
      case 'must':
        {
          const next = isVisibilityAllowed(metadata, 'all') ? 'all' : 'none';
          this.setVisibility(relation, next);
        }
        break;
      default:
        this.setVisibility(relation, 'none');
        break;
    }
  }

  hideAll(): void {
    this.relationMetadata.forEach((metadata, name) => {
      if (getDefaultVisibility(metadata) === 'none') {
        this.visibility.delete(name);
      } else {
        this.visibility.set(name, 'none');
      }
    });
  }

  resetFilter(): void {
    this.visibility.clear();
  }

  getName({ name, simpleName }: { name: string; simpleName: string }): string {
    return this.abbreviate ? simpleName : name;
  }

  toggleAbbrevaite(): void {
    this.abbreviate = !this.abbreviate;
  }

  toggleScopes(): void {
    this.scopes = !this.scopes;
  }

  setSelectedSymbol(option: RelationMetadata | undefined): void {
    if (option === undefined) {
      this.selectedSymbol = undefined;
      return;
    }
    const metadata = this.relationMetadata.get(option.name);
    if (metadata !== undefined) {
      this.selectedSymbol = metadata;
    } else {
      this.selectedSymbol = undefined;
    }
  }

  setSemantics(semantics: SemanticsSuccessResult) {
    this.semantics = semantics;
    this.relationMetadata.clear();
    this.semantics.relations.forEach((metadata) => {
      this.relationMetadata.set(metadata.name, metadata);
    });
    const toRemove = new Set<string>();
    this.visibility.forEach((value, key) => {
      if (
        !this.isVisibilityAllowed(key, value) ||
        this.getDefaultVisibility(key) === value
      ) {
        toRemove.add(key);
      }
    });
    toRemove.forEach((key) => {
      this.visibility.delete(key);
    });
    this.setSelectedSymbol(this.selectedSymbol);
    this.updateTypeHashes();
  }

  /**
   * Maintains a list of past and current color codes to avoid flashing
   * when the graph view updates.
   *
   * As long as the previously used colors are still in in `typeHashesMap`,
   * the view will not flash while Graphviz is recomputing, because we'll
   * keep emitting styles for the colors.
   */
  private updateTypeHashes(): void {
    this.semantics.nodes.forEach(({ typeHash }) => {
      if (
        typeHash !== undefined &&
        typeHash.startsWith(TYPE_HASH_HEX_PREFFIX)
      ) {
        const key = typeHash.substring(TYPE_HASH_HEX_PREFFIX.length);
        this.typeHashesMap.set(key, 0);
      }
    });
    this.hexTypeHashes = Array.from(this.typeHashesMap.keys());
    this.hexTypeHashes.forEach((typeHash) => {
      const age = this.typeHashesMap.get(typeHash);
      if (age !== undefined && age < 10) {
        this.typeHashesMap.set(typeHash, age + 1);
      } else {
        this.typeHashesMap.delete(typeHash);
      }
    });
  }

  get colorNodes(): boolean {
    return this.editorStore.colorIdentifiers;
  }

  get name(): string {
    return this.nameOverride ?? this.editorStore.simpleNameOrFallback;
  }
}