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

286 lines
7.5 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:42:06
2020-12-01 12:05:35 +00:00
* @modify date 2020-11-25 17:29:25
2020-11-05 17:01:47 +00:00
* @desc [description]
*/
import * as handlebars from "handlebars";
import { updateIds } from "../../ZISS-Network/helpers/idUpdater";
import { ILogicNetwork } from "../../ZISS-Network/type/ILogicNetwork";
import { defineFlows } from "../../ZISS-Petri-Net/src/wrappers/loader";
2020-12-01 12:05:35 +00:00
import { IArcExpression } from "../../ZISS-Petri-Net/types/IArcAnnotation";
2020-11-05 17:01:47 +00:00
import { analyzeLogic } from "../src/analyze.logic";
2020-12-01 12:05:35 +00:00
import {
IParseableLogic,
IParsedLogic,
ITransitionDescription
} from "../types/interface";
2020-11-05 17:01:47 +00:00
export type THelperFunction = (data: IParsedLogic) => string;
export interface IParser {
mainLoop(context: IParsedLogic): string;
variableDefinition(context: IParsedLogic): string;
instanceCreation(context: IParsedLogic): string;
}
export class AbstractParser {
public variableDefinition(context: IParsedLogic): string {
throw new Error("Method not implemented.");
}
// Array, containing all Elements which should be replaced.
protected _replace: {
regex: RegExp;
replacer: string;
}[] = [];
/**
* Function, which will be used to adapt the Path.
* @param value
*/
protected _adaptGuard(
guard: string,
simplifiedGuard: string,
OriginalScope: {
[index: string]: {
source: string;
schema: IJsonSchema;
};
},
simplifiedScope: {
[index: string]: {
source: string;
schema: IJsonSchema;
};
},
mappingOriginalToSimplified: Map<string, string>,
mappingSimplifiedToOriginal: Map<string, string>,
mappingSimplifiedToAdapted: Map<string, string>,
mappingAdaptedToSimplified: Map<string, string>,
mappingVariableToRoot: Map<string, string>
): string {
throw Error("Please Create a Valid Instance");
}
/**
* Function, which will be used to adapt a Variable Name.
* @param value The original Variable Name
*/
protected _adaptVariableName(
variable: string,
simplifiedName: string,
path: string
): string {
throw Error("Please Create a Valid Instance");
}
/**
* Function, which will be used to generate Function Code.
* @param taskData
*/
protected _getFunctionTriggerCode(taskData): string {
throw Error("Please Create a Valid Instance");
}
protected _getVariableName(taskData): string {
throw Error("Please Create a Valid Instance");
}
protected _templates: {
[index: string]: string;
} = {};
linkTemplates(...args) {
for (const partial in this._templates) {
handlebars.registerPartial(partial, this._templates[partial]);
}
}
/**
* Helper Function, to Generate the desired Logic.
* @param _network
*/
parseLogic(_network: ILogicNetwork) {
let network = deepClone(_network);
// Reference to the Own Object.
const _this = this;
// Update the Ids.
network = updateIds(network);
// Define the Flow of the Network.
network = defineFlows(network);
const context = analyzeLogic(network, {
padSizeNames: true,
// The Adapted Values shouldn't be stored in the Network-Object
adaptGuard: (...args) => _this._adaptGuard(...args),
adaptVariableName: (...args) => _this._adaptVariableName(...args)
});
return context;
}
/**
* Function to Get the Transitions of the Network, Sorted by there Priority
* @param network The Network Describing the Logic.
*/
2020-12-01 12:05:35 +00:00
getTransitionDescriptions(
network: ILogicNetwork,
sort: "priority" | "id" = "priority"
): ITransitionDescription[] {
2020-11-05 17:01:47 +00:00
const context = this.parseLogic(network);
const transitions = Array.from(context.transitions.values());
transitions.sort(dynamicSort(sort));
return transitions;
}
protected _convertInput(definition: {
// Amount of Tokens that will be consumed (Result must be greater the minTokens)
consumed: IArcExpression;
placeId: string;
type:
2020-12-01 12:05:35 +00:00
| "expression"
| "inhibitor"
| "multiexpression"
| "test"
| "value"
| "variable";
2020-11-05 17:01:47 +00:00
minTokens: number;
}): {
placeId: string;
minTokens: number;
tokensToRemove: number;
} {
2020-12-01 12:05:35 +00:00
throw Error("Not implemented");
2020-11-05 17:01:47 +00:00
}
protected _convertOutput(definition: {
// Amount of Tokens that will be produced
produced: IArcExpression;
placeId: string;
type:
2020-12-01 12:05:35 +00:00
| "expression"
| "inhibitor"
| "multiexpression"
| "test"
| "value"
| "variable";
2020-11-05 17:01:47 +00:00
maxTokens: number;
}): {
// Amount of Tokens that will be produced
placeId: string;
maxTokens: number;
tokensToAdd: number;
2020-12-01 12:05:35 +00:00
} {
throw Error("Not implemented");
}
2020-11-05 17:01:47 +00:00
/**
2020-12-01 12:05:35 +00:00
*
* @param network
2020-11-05 17:01:47 +00:00
*/
getParseableLogic(network: ILogicNetwork) {
const _this = this;
2020-12-01 12:05:35 +00:00
const context = this.parseLogic(network);
2020-11-05 17:01:47 +00:00
const ret: IParseableLogic = {
initialMarking: [],
places: context.places,
transitions: [],
transitionsSortedByPriority: [],
variables: context.variables,
subscribedVariables: context.subscribedVariables,
2020-12-01 12:05:35 +00:00
actions: []
2020-11-05 17:01:47 +00:00
};
2020-12-01 12:05:35 +00:00
const transtionsSortedById = this.getTransitionDescriptions(network, "id");
// Iterate over the
const _createTrans = (
idx: number,
trans: ITransitionDescription,
length: number
) => {
2020-11-05 17:01:47 +00:00
return {
externalGuard: trans.adaptedExternalGuard,
dynamicParams: trans.simplifiedDynamicParams,
id: trans.id,
2020-12-01 12:05:35 +00:00
consumed: Array.from(trans.inputs.consumed.values()).map((item) =>
_this._convertInput(item)
),
2020-11-05 17:01:47 +00:00
avoided: Array.from(trans.inputs.prevents.values()),
2020-12-01 12:05:35 +00:00
required: Array.from(trans.inputs.required.values()).map((item) =>
_this._convertInput(item)
),
2020-11-05 17:01:47 +00:00
isAction: trans.isAction,
label: trans.label,
logicalGuard: trans.logicalGuard,
2020-12-01 12:05:35 +00:00
produced: Array.from(trans.outputs.values()).map((item) =>
_this._convertOutput(item)
),
2020-11-05 17:01:47 +00:00
priority: trans.priority,
scope: trans.simplifiedScope,
taskID: trans.taskID,
triggers: trans.triggers,
functionName: trans.functionName,
mode: trans.mode,
params: trans.params,
isFirst: idx === 0,
isLast: length - 1 === idx,
maxTime: trans.maxTime,
minTime: trans.minTime,
useMaxTime: trans.useMaxTime,
useTimes: trans.useTimes,
locks: trans.locks,
releases: trans.releases,
2020-12-01 12:05:35 +00:00
idx
};
};
2020-11-05 17:01:47 +00:00
for (const [idx, trans] of transtionsSortedById.entries()) {
const _trans = _createTrans(idx, trans, transtionsSortedById.length);
ret.transitions.push(_trans);
if (trans.isAction) {
ret.actions.push(deepClone(_trans));
}
}
2020-12-01 12:05:35 +00:00
const transtionsSortedByPriority = this.getTransitionDescriptions(
network,
"priority"
);
2020-11-05 17:01:47 +00:00
for (const [idx, trans] of transtionsSortedByPriority.entries()) {
2020-12-01 12:05:35 +00:00
const _trans = _createTrans(
idx,
trans,
transtionsSortedByPriority.length
);
2020-11-05 17:01:47 +00:00
ret.transitionsSortedByPriority.push(_trans);
}
if (ret.actions.length > 0) {
ret.actions[0].isFirst = true;
ret.actions[ret.actions.length - 1].isLast = true;
}
return ret;
}
/**
* Function, which will generate the MainLoop.
* @param network
*/
mainLoop(network: ILogicNetwork): string {
throw new Error("Method not implemented.");
}
instanceCreation(context: IParsedLogic): string {
throw new Error("Method not implemented.");
}
}