aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/GraphStore.ts
blob: d9282326650d8522a26aaf4e9748d5a55a9ede1e (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
/*
 * 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;
}

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;

  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);
  }

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

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