From 13b464db253566290be6a1063ad8e296288d3339 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 14 Sep 2023 03:05:28 +0200 Subject: feat: specify random seed for generation --- subprojects/frontend/src/editor/AnimatedButton.tsx | 2 +- subprojects/frontend/src/editor/EditorStore.ts | 8 +++---- subprojects/frontend/src/editor/GenerateButton.tsx | 8 ++++++- .../frontend/src/editor/GeneratedModelStore.ts | 4 ++-- .../frontend/src/xtext/ModelGenerationService.ts | 26 +++++++++++++++++++--- subprojects/frontend/src/xtext/UpdateService.ts | 7 +++--- subprojects/frontend/src/xtext/XtextClient.ts | 5 +++-- .../frontend/src/xtext/XtextWebSocketClient.ts | 2 +- 8 files changed, 45 insertions(+), 17 deletions(-) (limited to 'subprojects/frontend') diff --git a/subprojects/frontend/src/editor/AnimatedButton.tsx b/subprojects/frontend/src/editor/AnimatedButton.tsx index 24ec69be..606aabea 100644 --- a/subprojects/frontend/src/editor/AnimatedButton.tsx +++ b/subprojects/frontend/src/editor/AnimatedButton.tsx @@ -45,7 +45,7 @@ export default function AnimatedButton({ children, }: { 'aria-label'?: string; - onClick?: () => void; + onClick?: React.MouseEventHandler; color: 'error' | 'warning' | 'primary' | 'inherit'; disabled?: boolean; startIcon?: JSX.Element; diff --git a/subprojects/frontend/src/editor/EditorStore.ts b/subprojects/frontend/src/editor/EditorStore.ts index f9a9a7da..9508858d 100644 --- a/subprojects/frontend/src/editor/EditorStore.ts +++ b/subprojects/frontend/src/editor/EditorStore.ts @@ -313,14 +313,14 @@ export default class EditorStore { this.disposed = true; } - startModelGeneration(): void { + startModelGeneration(randomSeed?: number): void { this.client - ?.startModelGeneration() + ?.startModelGeneration(randomSeed) ?.catch((error) => log.error('Could not start model generation', error)); } - addGeneratedModel(uuid: string): void { - this.generatedModels.set(uuid, new GeneratedModelStore()); + addGeneratedModel(uuid: string, randomSeed: number): void { + this.generatedModels.set(uuid, new GeneratedModelStore(randomSeed)); this.selectGeneratedModel(uuid); } diff --git a/subprojects/frontend/src/editor/GenerateButton.tsx b/subprojects/frontend/src/editor/GenerateButton.tsx index b8dcd531..b6b1655a 100644 --- a/subprojects/frontend/src/editor/GenerateButton.tsx +++ b/subprojects/frontend/src/editor/GenerateButton.tsx @@ -98,7 +98,13 @@ const GenerateButton = observer(function GenerateButton({ disabled={!editorStore.opened} color={warningCount > 0 ? 'warning' : 'primary'} startIcon={} - onClick={() => editorStore.startModelGeneration()} + onClick={(event) => { + if (event.shiftKey) { + editorStore.startModelGeneration(1); + } else { + editorStore.startModelGeneration(); + } + }} > {summary === '' ? GENERATE_LABEL : `${GENERATE_LABEL} (${summary})`} diff --git a/subprojects/frontend/src/editor/GeneratedModelStore.ts b/subprojects/frontend/src/editor/GeneratedModelStore.ts index d0181eed..5088d603 100644 --- a/subprojects/frontend/src/editor/GeneratedModelStore.ts +++ b/subprojects/frontend/src/editor/GeneratedModelStore.ts @@ -18,9 +18,9 @@ export default class GeneratedModelStore { graph: GraphStore | undefined; - constructor() { + constructor(randomSeed: number) { const time = new Date().toLocaleTimeString(undefined, { hour12: false }); - this.title = `Generated at ${time}`; + this.title = `Generated at ${time} (${randomSeed})`; makeAutoObservable(this); } diff --git a/subprojects/frontend/src/xtext/ModelGenerationService.ts b/subprojects/frontend/src/xtext/ModelGenerationService.ts index 1e9f837a..29a70623 100644 --- a/subprojects/frontend/src/xtext/ModelGenerationService.ts +++ b/subprojects/frontend/src/xtext/ModelGenerationService.ts @@ -4,12 +4,18 @@ * SPDX-License-Identifier: EPL-2.0 */ +import type { Transaction } from '@codemirror/state'; + import type EditorStore from '../editor/EditorStore'; import type UpdateService from './UpdateService'; import { ModelGenerationResult } from './xtextServiceResults'; +const INITIAL_RANDOM_SEED = 1; + export default class ModelGenerationService { + private nextRandomSeed = INITIAL_RANDOM_SEED; + constructor( private readonly store: EditorStore, private readonly updateService: UpdateService, @@ -26,14 +32,24 @@ export default class ModelGenerationService { } } + onTransaction(transaction: Transaction): void { + if (transaction.docChanged) { + this.resetRandomSeed(); + } + } + onDisconnect(): void { this.store.modelGenerationCancelled(); + this.resetRandomSeed(); } - async start(): Promise { - const result = await this.updateService.startModelGeneration(); + async start(randomSeed?: number): Promise { + const randomSeedOrNext = randomSeed ?? this.nextRandomSeed; + this.nextRandomSeed = randomSeedOrNext + 1; + const result = + await this.updateService.startModelGeneration(randomSeedOrNext); if (!result.cancelled) { - this.store.addGeneratedModel(result.data.uuid); + this.store.addGeneratedModel(result.data.uuid, randomSeedOrNext); } } @@ -43,4 +59,8 @@ export default class ModelGenerationService { this.store.modelGenerationCancelled(); } } + + private resetRandomSeed() { + this.nextRandomSeed = INITIAL_RANDOM_SEED; + } } diff --git a/subprojects/frontend/src/xtext/UpdateService.ts b/subprojects/frontend/src/xtext/UpdateService.ts index d1246d5e..70e79764 100644 --- a/subprojects/frontend/src/xtext/UpdateService.ts +++ b/subprojects/frontend/src/xtext/UpdateService.ts @@ -343,9 +343,9 @@ export default class UpdateService { return { cancelled: false, data: parsedOccurrencesResult }; } - async startModelGeneration(): Promise< - CancellableResult - > { + async startModelGeneration( + randomSeed: number, + ): Promise> { try { await this.updateOrThrow(); } catch (error) { @@ -360,6 +360,7 @@ export default class UpdateService { serviceType: 'modelGeneration', requiredStateId: this.xtextStateId, start: true, + randomSeed, }); if (isConflictResult(data)) { return { cancelled: true }; diff --git a/subprojects/frontend/src/xtext/XtextClient.ts b/subprojects/frontend/src/xtext/XtextClient.ts index 4df4f57a..7486d737 100644 --- a/subprojects/frontend/src/xtext/XtextClient.ts +++ b/subprojects/frontend/src/xtext/XtextClient.ts @@ -99,6 +99,7 @@ export default class XtextClient { this.contentAssistService.onTransaction(transaction); this.updateService.onTransaction(transaction); this.occurrencesService.onTransaction(transaction); + this.modelGenerationService.onTransaction(transaction); } private onPush( @@ -150,8 +151,8 @@ export default class XtextClient { return this.contentAssistService.contentAssist(context); } - startModelGeneration(): Promise { - return this.modelGenerationService.start(); + startModelGeneration(randomSeed?: number): Promise { + return this.modelGenerationService.start(randomSeed); } cancelModelGeneration(): Promise { diff --git a/subprojects/frontend/src/xtext/XtextWebSocketClient.ts b/subprojects/frontend/src/xtext/XtextWebSocketClient.ts index bb84223c..280ac875 100644 --- a/subprojects/frontend/src/xtext/XtextWebSocketClient.ts +++ b/subprojects/frontend/src/xtext/XtextWebSocketClient.ts @@ -204,7 +204,7 @@ export default class XtextWebSocketClient { get state() { this.stateAtom.reportObserved(); - return this.interpreter.state; + return this.interpreter.getSnapshot(); } get opening(): boolean { -- cgit v1.2.3-54-g00ecf