Skip to content

Commit

Permalink
fix and test simulation command
Browse files Browse the repository at this point in the history
  • Loading branch information
robmeth committed Nov 26, 2024
1 parent c539a61 commit 8f30782
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/ttsl-cli/src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ program
.argument('date', `date to be simulated on`)
.argument('data', `csv-File needed for the simulation`)
.argument('targets', `list of targets to be simulated`)
.argument('<paths...>', `list of TTSL-Files or Directories`)
.argument('path', `TTSL-Files or Directories`)
.description('simulate tax and transfers')
.action(simulate);

Expand Down
8 changes: 5 additions & 3 deletions packages/ttsl-cli/src/cli/simulate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createTTSLServices } from '@ttsl/lang';
import { URI } from 'langium';
import { NodeFileSystem } from 'langium/node';

export const simulate = async (date: string, data: string, targets: string, fsPaths: string[], ): Promise<void> => {
export const simulate = async (date: string, data: string, targets: string, fsPaths: string ): Promise<void> => {
const services = (await createTTSLServices(NodeFileSystem)).TTSL;


let uri = URI.file(fsPaths)
const runner = services.runtime.Runner
runner.runFunction("./helpers/simulate.ttsl", "simulate", [date, data, targets])
runner.runSimulation(uri.toString(), "simulate", [date, data, targets])
}
Empty file.
3 changes: 3 additions & 0 deletions packages/ttsl-cli/tests/resources/commands/main.ttsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tests.commands.simulation

function f(a: Int, b: String){}
2 changes: 1 addition & 1 deletion packages/ttsl-lang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"clean": "shx rm -rf dist lib *.tsbuildinfo",
"langium:generate": "langium generate",
"langium:watch": "langium generate --watch",
"build": "tsc -b tsconfig.src.json && shx cp -r src/resources/ lib/",
"build": "tsc -b tsconfig.src.json",
"build:clean": "npm run clean && npm run build",
"watch": "tsc -b tsconfig.src.json --watch"
},
Expand Down
75 changes: 62 additions & 13 deletions packages/ttsl-lang/src/language/runtime/ttsl-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export class TTSLRunner {
return this.pythonServer.isStarted;
}

async runFunction(documentUri: string, nodePath: string, params: string[]=[]) {
async runFunction(documentUri: string, nodePath: string) {
const uri = URI.parse(documentUri);
const document = this.langiumDocuments.getDocument(uri);
const document = await this.langiumDocuments.getOrCreateDocument(uri);
if (!document) {
this.messaging.showErrorMessage('Could not find document.');
return;
Expand Down Expand Up @@ -115,6 +115,57 @@ export class TTSLRunner {
await this.executeFunction(functExecutionId, document, funct.name);
}

async runSimulation(documentUri: string, nodePath: string, params: string[]=[]) {
const uri = URI.parse(documentUri);
const document = await this.langiumDocuments.getOrCreateDocument(uri);
if (!document) {
this.messaging.showErrorMessage('Could not find document.');
return;
}

const functExecutionId = crypto.randomUUID();

const start = Date.now();
const progress = await this.messaging.showProgress('TTSL Runner', 'Starting...');
this.logger.info(`[${functExecutionId}] Running simulation in ${documentUri}.`);

const disposables = [
this.pythonServer.addMessageCallback('placeholder_type', (message) => {
if (message.id === functExecutionId) {
progress.report(`Computed ${message.data.name}`);
}
}),

this.pythonServer.addMessageCallback('runtime_error', (message) => {
if (message.id === functExecutionId) {
progress?.done();
disposables.forEach((it) => {
it.dispose();
});
this.messaging.showErrorMessage('An error occurred during function execution.');
}
progress.done();
disposables.forEach((it) => {
it.dispose();
});
}),

this.pythonServer.addMessageCallback('runtime_progress', (message) => {
if (message.id === functExecutionId) {
progress.done();
const timeElapsed = Date.now() - start;
this.logger.info(
`[${functExecutionId}] Finished running function simulation in ${timeElapsed}ms.`,
);
disposables.forEach((it) => {
it.dispose();
});
}
}),
];
await this.executeFunction(functExecutionId, document, "simulate");
}

async printValue(documentUri: string, nodePath: string) {
const uri = URI.parse(documentUri);
const document = this.langiumDocuments.getDocument(uri);
Expand Down Expand Up @@ -482,15 +533,15 @@ export class TTSLRunner {
for (const generatedDocument of generatedDocuments) {
const fsPath = URI.parse(generatedDocument.uri).fsPath;
const workspaceRelativeFilePath = path.relative(rootGenerationDir, path.dirname(fsPath));
const sdsFileName = path.basename(fsPath);
const sdsNoExtFilename =
path.extname(sdsFileName).length > 0
? sdsFileName.substring(0, sdsFileName.length - path.extname(sdsFileName).length)
const ttslFileName = path.basename(fsPath);
const ttslNoExtFilename =
path.extname(ttslFileName).length > 0
? ttslFileName.substring(0, ttslFileName.length - path.extname(ttslFileName).length)
: /* c8 ignore next */
sdsFileName;
ttslFileName;
// Put code in map for further use in the extension (e.g. to remap errors)
lastGeneratedSources.set(
path.join(workspaceRelativeFilePath, sdsFileName).replaceAll('\\', '/'),
path.join(workspaceRelativeFilePath, ttslFileName).replaceAll('\\', '/'),
generatedDocument.getText(),
);
// Check for sourcemaps after they are already added to the function context
Expand All @@ -504,16 +555,14 @@ export class TTSLRunner {
codeMap[modulePath] = {};
}
// Put code in object for runner
codeMap[modulePath]![sdsNoExtFilename] = generatedDocument.getText();
codeMap[modulePath]![ttslNoExtFilename] = generatedDocument.getText();
}
return [codeMap, lastGeneratedSources];
}

public getMainModuleName(functDocument: LangiumDocument): string {
if (functDocument.uri.fsPath.endsWith('.sds')) {
return this.generator.sanitizeModuleNameForPython(path.basename(functDocument.uri.fsPath, '.sds'));
} else if (functDocument.uri.fsPath.endsWith('.sdsdev')) {
return this.generator.sanitizeModuleNameForPython(path.basename(functDocument.uri.fsPath, '.sdsdev'));
if (functDocument.uri.fsPath.endsWith('.ttsl')) {
return this.generator.sanitizeModuleNameForPython(path.basename(functDocument.uri.fsPath, '.ttsl'));
} else {
return this.generator.sanitizeModuleNameForPython(path.basename(functDocument.uri.fsPath));
}
Expand Down

0 comments on commit 8f30782

Please sign in to comment.