Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PF-49] - Update data field init type #38

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions src/lib/export/export-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export class ExportUtils {

protected xmlConstructor = document.implementation.createDocument(null, 'document', null);

public exportTag(doc: Element, name: string, value: string | I18nString | I18nWithDynamic, force = false, attributes?: Array<{ key: string, value: string }>): void {
public exportTag(doc: Element, name: string, value: string | I18nString | I18nWithDynamic, force = false, attributes?: Array<{
key: string,
value: string
}>): void {
if ((typeof value === 'string' && value !== '') ||
(value instanceof I18nString && value.value !== undefined && value.value !== null && value.value !== '')) {
const tag = this.xmlConstructor.createElement(name);
Expand Down Expand Up @@ -44,24 +47,34 @@ export class ExportUtils {
}
}

public exportExpression(doc: Element, name: string, value: Expression | Array<Expression> | undefined) {
if (value !== undefined) {
if (!Array.isArray(value)) {
this.exportTag(doc, name, value.expression, false, value.dynamic ? [{
key: 'dynamic',
value: value.dynamic.toString()
}] : undefined);
} else if (value.length > 0) {
const exportInits = this.xmlConstructor.createElement('inits');
value.forEach(init => {
this.exportTag(exportInits, name, init.expression, false, init.dynamic ? [{
key: 'dynamic',
value: init.dynamic.toString()
}] : undefined);
});
doc.appendChild(exportInits);
}
public exportExpression(doc: Element, name: string, value: Expression | undefined) {
if (value === undefined) {
return;
}
this.exportTag(doc, name, value.expression, false, value.dynamic ? [{
key: 'dynamic',
value: value.dynamic.toString()
}] : undefined);
}

public exportI18nWithDynamic(doc: Element, name: string, value: I18nWithDynamic | undefined) {
if (value === undefined) {
return;
}
const attributes = [];
if (value.dynamic) {
attributes.push({
key: 'dynamic',
value: value.dynamic.toString()
})
}
if (value.name) {
attributes.push({
key: 'name',
value: value.name
})
}
this.exportTag(doc, name, value.value, false, attributes);
}

public exportActions<T>(element: Element, event: Event<T>, phase: string): void {
Expand Down
11 changes: 2 additions & 9 deletions src/lib/export/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,11 @@ export class ExportService {
});
exportData.appendChild(validations);
}
this._exportUtils.exportExpression(exportData, 'init', data.init);
this._exportUtils.exportI18nWithDynamic(exportData, 'init', data.init);
if (data.inits.length > 0) {
const inits = this.xmlConstructor.createElement('inits');
data.inits.forEach(init => {
let attr;
if (init.dynamic) {
attr = [{
key: 'dynamic',
value: String(init.dynamic)
}];
}
this._exportUtils.exportTag(inits, 'init', init.expression, false, attr);
this._exportUtils.exportI18nWithDynamic(inits, 'init', init);
});
exportData.appendChild(inits);
}
Expand Down
42 changes: 29 additions & 13 deletions src/lib/import/import-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export class ImportUtils {
}

public tagAttribute(xmlTag: Element | null, attribute: string): string {
if (!xmlTag)
if (!xmlTag) {
return '';
}
let attr;
for (let i = 0; i < xmlTag.attributes.length; i++) {
if (xmlTag.attributes.item(i)?.name === attribute) {
Expand Down Expand Up @@ -215,7 +216,9 @@ export class ImportUtils {
}

public checkVariability(model: PetriNet, arc: Arc, reference: string | undefined): void {
if (!reference) return;
if (!reference) {
return;
}
let ref: Place | DataVariable | undefined = model.getPlace(reference);
if (ref) {
this.attachReference(arc, ref);
Expand All @@ -228,7 +231,7 @@ export class ImportUtils {
}

public attachReference(arc: Arc, reference: Place | DataVariable): void {
const weight = reference instanceof Place ? reference.marking : parseInt(reference.init?.expression ?? '' as string, 10);
const weight = reference instanceof Place ? reference.marking : parseInt(reference.init?.value ?? '' as string, 10);

if (isNaN(weight)) {
throw new Error('Not a number. Cannot change the value of arc weight.');
Expand Down Expand Up @@ -301,8 +304,9 @@ export class ImportUtils {

public parseDataLayout(xmlLayout: Element | null): DataLayout {
const layout = new DataLayout();
if (!xmlLayout)
if (!xmlLayout) {
return layout;
}
layout.x = this.parseNumberValue(xmlLayout, 'x') ?? 0;
layout.y = this.parseNumberValue(xmlLayout, 'y') ?? 0;
layout.rows = this.parseNumberValue(xmlLayout, 'rows') ?? 0;
Expand Down Expand Up @@ -376,29 +380,38 @@ export class ImportUtils {
}
}

public resolveInits(xmlData: Element): Array<Expression> {
const inits: Array<Expression> = [];
public resolveInits(xmlData: Element): Array<I18nWithDynamic> {
const inits: Array<I18nWithDynamic> = [];
if (this.checkLengthAndNodes(xmlData, 'inits')) {
for (const value of Array.from(xmlData.getElementsByTagName('inits')[0]?.getElementsByTagName('init'))) {
const dynamic = this.tagAttribute(value, 'dynamic');
inits.push(new Expression(value.textContent ?? '', dynamic === '' ? undefined : dynamic === 'true'));
const init = this.resolveInitValue(value);
if (!init) {
continue;
}
inits.push(init);
}
}
return inits;
}

public resolveInit(xmlData: Element): Expression | undefined {
public resolveInit(xmlData: Element): I18nWithDynamic | undefined {
let elementValue;
for (const value of Array.from(xmlData.getElementsByTagName('init'))) {
if (!value.parentElement?.tagName || value.parentElement.tagName !== 'data') {
continue;
}
elementValue = value;
}
if (!elementValue)
return this.resolveInitValue(elementValue);
}

public resolveInitValue(elementValue: Element | undefined): I18nWithDynamic | undefined {
if (!elementValue) {
return undefined;
}
const dynamic = this.tagAttribute(elementValue, 'dynamic');
return new Expression(elementValue.textContent ?? '', dynamic === '' ? undefined : dynamic === 'true');
const name = this.tagAttribute(elementValue, 'name');
return new I18nWithDynamic(elementValue.textContent ?? '', name, dynamic === '' ? undefined : dynamic === 'true');
}

public checkLengthAndNodes(element: Element, name: string) {
Expand All @@ -412,16 +425,19 @@ export class ImportUtils {
data.component = new Component('currency');
}
const xmlCur = xmlData.getElementsByTagName('format')?.item(0)?.getElementsByTagName('currency').item(0);
if (!xmlCur) return;
if (!xmlCur) {
return;
}
data.component.properties.push(new Property('locale', this.tagValue(xmlCur, 'locale')));
data.component.properties.push(new Property('code', this.tagValue(xmlCur, 'code') !== '' ? this.tagValue(xmlCur, 'code') : 'EUR'));
data.component.properties.push(new Property('fractionSize', (this.parseNumberValue(xmlCur, 'fractionSize') !== undefined ? this.parseNumberValue(xmlCur, 'fractionSize') : 2)?.toString() ?? ''));
}
}

public parseNumberValue(element: Element | null, name: string): number | undefined {
if (!element)
if (!element) {
return undefined;
}
const value = parseInt(this.tagValue(element, name), 10);
return isNaN(value) ? undefined : value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class ImportService {
data.immediate = this.importUtils.tagAttribute(xmlData, 'immediate') === 'true';
data.encryption = this.importUtils.parseEncryption(xmlData);
data.init = this.importUtils.resolveInit(xmlData);
this.importUtils.resolveInits(xmlData).forEach(i => data.inits.push(i));
data.inits = this.importUtils.resolveInits(xmlData);
data.length = this.importUtils.parseNumberValue(xmlData, 'length');
data.component = this.importUtils.parseViewAndComponent(xmlData);
this.importUtils.resolveFormat(xmlData, data);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/model/data-variable/data-type.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export enum DataType {
BUTTON = 'button',
TASK_REF = 'taskRef',
CASE_REF = 'caseRef',
FILTER = 'filter'
FILTER = 'filter',
I18N = 'i18n'
}
13 changes: 7 additions & 6 deletions src/lib/model/data-variable/data-variable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {I18nString} from '../i18n/i18n-string';
import {I18nWithDynamic} from "../i18n/i18n-with-dynamic";
import {Component} from './component';
import {DataEventSource} from './data-event-source';
import {DataType} from './data-type.enum';
Expand All @@ -14,8 +15,8 @@ export class DataVariable extends DataEventSource {
private _options: Array<Option>;
private _optionsInit?: Expression;
private _validations: Array<Validation>;
private _init?: Expression;
private _inits: Array<Expression>;
private _init?: I18nWithDynamic;
private _inits: Array<I18nWithDynamic>;
private _component?: Component;
private _type: DataType;
private _immediate: boolean;
Expand Down Expand Up @@ -96,19 +97,19 @@ export class DataVariable extends DataEventSource {
this._validations = value;
}

get init(): Expression | undefined {
get init(): I18nWithDynamic | undefined {
return this._init;
}

set init(value: Expression | undefined) {
set init(value: I18nWithDynamic | undefined) {
this._init = value;
}

get inits(): Array<Expression> {
get inits(): Array<I18nWithDynamic> {
return this._inits;
}

set inits(value: Array<Expression>) {
set inits(value: Array<I18nWithDynamic>) {
this._inits = value;
}

Expand Down
8 changes: 3 additions & 5 deletions src/lib/model/i18n/i18n-with-dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {I18nString} from './i18n-string';
export class I18nWithDynamic extends I18nString {
private _dynamic: boolean;

constructor(value: string, dynamic = false) {
super(value);
constructor(value: string, name?: string, dynamic = false) {
super(value, name);
this._dynamic = dynamic;
}

Expand All @@ -17,8 +17,6 @@ export class I18nWithDynamic extends I18nString {
}

public clone(): I18nWithDynamic {
const cloned = new I18nWithDynamic(this.value, this._dynamic);
cloned.name = this.name;
return cloned;
return new I18nWithDynamic(this.value, this.name, this._dynamic);
}
}
11 changes: 11 additions & 0 deletions src/lib/model/petrinet/event.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {Action} from './action';
import {EventPhase} from './event-phase.enum';
import {I18nString} from "../i18n/i18n-string";

export abstract class Event<T> {
private _type: T;
private _id: string;
private _preActions: Array<Action>;
private _postActions: Array<Action>;
private _message: I18nString;

protected constructor(type: T, id: string) {
this._type = type;
this._id = id;
this._preActions = [];
this._postActions = [];
this._message = new I18nString('');
}

get type(): T {
Expand All @@ -38,6 +41,14 @@ export abstract class Event<T> {
return this._postActions;
}

get message(): I18nString {
return this._message;
}

set message(value: I18nString) {
this._message = value;
}

public addAction(action: Action, phase: EventPhase): void {
if (!action) {
throw new Error('Action is undefined');
Expand Down
12 changes: 1 addition & 11 deletions src/lib/model/role/role-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import {RoleEventType} from './role-event-type.enum';

export class RoleEvent extends Event<RoleEventType> {
private _title: I18nString;
private _message: I18nString;

constructor(type: RoleEventType, id: string) {
super(type, id);
this._title = new I18nString('');
this._message = new I18nString('');
}

get title(): I18nString {
Expand All @@ -20,18 +18,10 @@ export class RoleEvent extends Event<RoleEventType> {
this._title = value;
}

get message(): I18nString {
return this._message;
}

set message(value: I18nString) {
this._message = value;
}

public clone(): RoleEvent {
const cloned = new RoleEvent(this.type, this.id);
cloned.message = this.message?.clone();
cloned._title = this._title?.clone();
cloned._message = this._message?.clone();
this.preActions.forEach(item => cloned.preActions.push(item.clone()));
this.postActions.forEach(item => cloned.postActions.push(item.clone()));
return cloned;
Expand Down
12 changes: 1 addition & 11 deletions src/lib/model/transition/transition-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import {TransitionEventType} from './transition-event-type.enum';

export class TransitionEvent extends Event<TransitionEventType> {
private _title: I18nString;
private _message: I18nString;

constructor(type: TransitionEventType, id: string) {
super(type, id);
this._title = new I18nString('');
this._message = new I18nString('');
}

get title(): I18nString {
Expand All @@ -20,17 +18,9 @@ export class TransitionEvent extends Event<TransitionEventType> {
this._title = value;
}

get message(): I18nString {
return this._message;
}

set message(value: I18nString) {
this._message = value;
}

public clone(): TransitionEvent {
const cloned = new TransitionEvent(this.type, this.id);
cloned._message = this._message?.clone();
cloned.message = this.message?.clone();
cloned._title = this._title?.clone();
this.preActions.forEach(item => cloned.preActions.push(item.clone()));
this.postActions.forEach(item => cloned.postActions.push(item.clone()));
Expand Down
Loading