nope/modules/net-analyzer/registry/siemens.parser.ts

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-11-05 17:01:47 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-02-20 19:46:19
2020-12-01 12:05:35 +00:00
* @modify date 2020-11-25 07:36:29
2020-11-05 17:01:47 +00:00
* @desc [description]
*/
2020-12-01 12:05:35 +00:00
import * as handlebars from "handlebars";
import { IParsedLogic } from "../types/interface";
2020-11-05 17:01:47 +00:00
// declare const handlebars;
2020-12-01 12:05:35 +00:00
import { IParser } from "./default.parser";
2020-11-05 17:01:47 +00:00
export class SiemensParser implements IParser {
mainLoop(context: IParsedLogic): string {
const templates = {
2020-12-01 12:05:35 +00:00
ifHeaderTemplate: "{{#if isFirst}}IF{{/if}}{{#unless isFirst}}ELSIF{{/unless}}{{> testPrePlaces}}{{> testPostPlaces}}{{> testGuard}} THEN",
testPrePlaces: "{{#each inputs}} (\"{{placeTokenVar}}\"{{#if minTokens}} - \"{{minTokens}}\"{{/if}}) >= \"{{tokensToRemove}}\" AND{{/each}}",
testPostPlaces: "{{#each testOutputs}} (\"{{placeTokenVar}}\" + \"{{tokensToAdd}}\") <= {{maxTokens}}\" AND {{/each}}",
testGuard: " {{guard.external}}",
removeTokens: "{{#each inputs}}\"{{placeTokenVar}}\" := \"{{placeTokenVar}}\" - \"{{tokensToRemove}}\";{{/each}}",
addTokens: "{{#each outputs}}\"{{placeTokenVar}}\" := \"{{placeTokenVar}}\" + \"{{tokensToAdd}}\";{{/each}}"
};
2020-11-05 17:01:47 +00:00
for (const partial in templates) {
handlebars.registerPartial(partial, templates[partial]);
}
const functions: { [index: string]: (context: any) => string } = {
2020-12-01 12:05:35 +00:00
"plc.set": (context) => {
2020-11-05 17:01:47 +00:00
console.log(context);
2020-12-01 12:05:35 +00:00
return "";
2020-11-05 17:01:47 +00:00
}
2020-12-01 12:05:35 +00:00
};
2020-11-05 17:01:47 +00:00
// handlebars.registerHelper(require('handlebars-helpers/lib/array.js'))
2020-12-01 12:05:35 +00:00
handlebars.registerHelper("getFunctionCode", (context, name, paramers, dynParams) => {
console.log("FUNC", context, name, paramers, dynParams);
});
2020-11-05 17:01:47 +00:00
2020-12-01 12:05:35 +00:00
const mainTemplate = "" +
2020-11-05 17:01:47 +00:00
`
ORGANIZATION_BLOCK "Main"
TITLE = "Main Program Sweep (Cycle)"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
BEGIN
{{#each transitions}}
// Test Transtion "{{{label}}}"
{{> ifHeaderTemplate}}
{{#if inputs}}
// Removing Tokens from Pre-Places.
{{> removeTokens}}
{{/if}}
{{#if outputs}}
// Adding Tokens to the Post-Places.
{{> addTokens}}
{{/if}}
{{#if funcName}}
// Execute the desired Function
{{#getFunctionCode funcName params dynamicParameters}}{{/getFunctionCode}}
{{/if}}
{{#if isLast}}
END_IF;
{{/if}}
{{/each}}
END_ORGANIZATION_BLOCK
2020-12-01 12:05:35 +00:00
`;
2020-11-05 17:01:47 +00:00
// handlebars.registerHelper("joinList", helpers_1.joinListHelper);
const render = handlebars.compile(mainTemplate);
2020-12-01 12:05:35 +00:00
return "";
2020-11-05 17:01:47 +00:00
}
variableDefinition(context: IParsedLogic): string {
2020-12-01 12:05:35 +00:00
const template = "" +
2020-11-05 17:01:47 +00:00
`
DATA_BLOCK "VARS_DB"
{ S7_Optimized_Access := 'FALSE' }
VERSION : 0.1
NON_RETAIN
VAR
{{#each variables}}
{{name}} : {{type}};
{{/each}}
END_VAR
BEGIN
{{#some variables "v=>(v.hasValue)"}}
{{#joinList this "\n" "v=>(v.hasValue)"}}
{{name}} := {{value}};
{{/joinList}}
{{/some}}
END_DATA_BLOCK
2020-12-01 12:05:35 +00:00
`;
2020-11-05 17:01:47 +00:00
const render = handlebars.compile(template);
return render(context);
}
instanceCreation(context: IParsedLogic): string {
throw new Error("Method not implemented.");
}
}