Skip to content

Commit

Permalink
Merge pull request #714 from SteKoe/fix/one-selector
Browse files Browse the repository at this point in the history
fix: some expressions
  • Loading branch information
SteKoe authored Aug 14, 2024
2 parents 64fa2fc + 1e0cee0 commit 0621e03
Show file tree
Hide file tree
Showing 56 changed files with 236 additions and 320 deletions.
4 changes: 0 additions & 4 deletions book.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/node.js/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions examples/node.js/package-lock.json

This file was deleted.

12 changes: 0 additions & 12 deletions examples/node.js/package.json

This file was deleted.

37 changes: 0 additions & 37 deletions examples/ts/package-lock.json

This file was deleted.

15 changes: 0 additions & 15 deletions examples/ts/package.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/ts/src/index.ts

This file was deleted.

29 changes: 0 additions & 29 deletions examples/ts/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion examples/ts/tslint.json

This file was deleted.

25 changes: 25 additions & 0 deletions lib/components/expressions/BodyBasedExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {OclExecutionContext} from "../OclExecutionContext";
import {Expression} from "./Expression";
import {SourceBasedExpression} from "./SourceBasedExpression";

export abstract class BodyBasedExpression extends SourceBasedExpression {
private body: Expression;

setBody(body: Expression): any {
this.body = body;
}

getBody(): Expression {
return this.body;
}

_evaluateBodyAndSource(visitor: OclExecutionContext, localVariables?: any): { source: any, body: any } {
const body = this.getSource()
.evaluate(visitor, localVariables);

const source = this.getBody()
.evaluate(visitor, localVariables);

return {source, body};
}
}
48 changes: 1 addition & 47 deletions lib/components/expressions/Expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OclExecutionContext } from '../OclExecutionContext';
import {OclExecutionContext} from '../OclExecutionContext';

export abstract class Expression {
private readonly type: string;
Expand All @@ -15,49 +15,3 @@ export abstract class Expression {
abstract evaluate(visitor: OclExecutionContext, localVariables?: any): any;
}

export abstract class SourceBasedExpression extends Expression {
private readonly source: any;

constructor(source) {
super();
this.source = source;
}

getSource(): Expression {
return this.source;
}
}

export abstract class BodyBasedExpression extends SourceBasedExpression {
private body: Expression;

setBody(body: Expression): any {
this.body = body;
}

getBody(): Expression {
return this.body;
}

_evaluateBodyAndSource(visitor: OclExecutionContext, localVariables?: any): { source: any, body: any } {
const body = this.getSource()
.evaluate(visitor, localVariables);

const source = this.getBody()
.evaluate(visitor, localVariables);

return {source, body};
}
}

export abstract class IteratorExpression extends BodyBasedExpression {
private iterators: Array<any>;

setIterators(iterators: Array<any>): void {
this.iterators = iterators;
}

getIterators(): Array<any> {
return this.iterators;
}
}
29 changes: 29 additions & 0 deletions lib/components/expressions/IteratorExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {OclExecutionContext} from "../OclExecutionContext";
import {Utils} from "../Utils";

import {BodyBasedExpression} from "./BodyBasedExpression";

export abstract class IteratorExpression extends BodyBasedExpression {
private iterators: Array<any>;

setIterators(iterators: Array<any>): void {
this.iterators = iterators;
}

getIterators(): Array<any> {
return this.iterators;
}

protected evaluateBody(visitor: OclExecutionContext, localVariables: any, item: any) {
const variables = {};
if (this.getIterators()) {
variables[this.getIterators()[0]] = item;
} else {
const variableName = Utils.getVariableName(this);
const varName = variableName.getVariable();
variables[varName] = varName === "self" ? item : (item[varName] ?? item);
}

return this.getBody().evaluate(visitor, {...localVariables, ...variables});
}
}
5 changes: 2 additions & 3 deletions lib/components/expressions/LeftRightBasedExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { OclExecutionContext } from '../OclExecutionContext';

import { Expression } from './Expression';
import {OclExecutionContext} from '../OclExecutionContext';
import {Expression} from './Expression';

export abstract class LeftRightBasedExpression extends Expression {
private readonly left: any;
Expand Down
3 changes: 2 additions & 1 deletion lib/components/expressions/NativeJsFunctionCallExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OclExecutionContext } from '../OclExecutionContext';

import { Expression, SourceBasedExpression } from './Expression';
import { Expression } from './Expression';
import {SourceBasedExpression} from "./SourceBasedExpression";

export class NativeJsFunctionCallExpression extends SourceBasedExpression {
private readonly fn: any;
Expand Down
3 changes: 2 additions & 1 deletion lib/components/expressions/OclIsKindOfExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OclExecutionContext } from '../OclExecutionContext';

import { BodyBasedExpression } from './Expression';

import {BodyBasedExpression} from "./BodyBasedExpression";

/**
* Checks if *self* is an instance of the class identified by the name
Expand Down
3 changes: 2 additions & 1 deletion lib/components/expressions/OclIsTypeOfExpression.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { OclExecutionContext } from '../OclExecutionContext';
import { Utils } from '../Utils';

import { BodyBasedExpression } from './Expression';

import {BodyBasedExpression} from "./BodyBasedExpression";

/**
* Checks if *self* is an instance of exact the class identified by the name
Expand Down
3 changes: 2 additions & 1 deletion lib/components/expressions/OclIsUndefinedExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OclExecutionContext } from '../OclExecutionContext';

import { SourceBasedExpression } from './Expression';

import {SourceBasedExpression} from "./SourceBasedExpression";

/**
* Checks if *self* is not defined or null
Expand Down
14 changes: 14 additions & 0 deletions lib/components/expressions/SourceBasedExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Expression} from "./Expression";

export abstract class SourceBasedExpression extends Expression {
private readonly source: any;

constructor(source) {
super();
this.source = source;
}

getSource(): Expression {
return this.source;
}
}
3 changes: 2 additions & 1 deletion lib/components/expressions/VariableExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {OclExecutionContext} from '../OclExecutionContext';

import {SourceBasedExpression} from './Expression';

import {SourceBasedExpression} from "./SourceBasedExpression";

/**
* Resolve variables. Simple values are returned as is (e.g. self.age: number), collections are aggregated.
Expand Down
17 changes: 3 additions & 14 deletions lib/components/expressions/collection/AnyExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IteratorExpression } from '../Expression';
import { OclExecutionContext } from '../../OclExecutionContext';
import { Utils } from '../../Utils';
import {OclExecutionContext} from '../../OclExecutionContext';
import {IteratorExpression} from "../IteratorExpression";

/**
* Returns the first element that validates the given expression.
Expand All @@ -14,17 +13,7 @@ export class AnyExpression extends IteratorExpression {
.evaluate(visitor, localVariables);

if (collection instanceof Array) {
return collection.find(c => {
const variables = {};
if (this.getIterators()) {
variables[this.getIterators()[0]] = c;
} else {
const variableName = Utils.getVariableName(this);
variables[variableName.getVariable()] = c[variableName.getVariable()] || c;
}

return this.getBody().evaluate(visitor, {...localVariables, ...variables});
});
return collection.find(c => this.evaluateBody(visitor, localVariables, c));
}

return;
Expand Down
2 changes: 1 addition & 1 deletion lib/components/expressions/collection/AppendExpression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BodyBasedExpression } from '../Expression';
import { OclExecutionContext } from '../../OclExecutionContext';
import {BodyBasedExpression} from "../BodyBasedExpression";

/**
* Appends the given element to the given collection and returns the extended collection.
Expand Down
2 changes: 1 addition & 1 deletion lib/components/expressions/collection/AsSetExpression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SourceBasedExpression } from '../Expression';
import { OclExecutionContext } from '../../OclExecutionContext';
import {SourceBasedExpression} from "../SourceBasedExpression";

/**
* Returns the given collection as set, containing unique entries.
Expand Down
2 changes: 1 addition & 1 deletion lib/components/expressions/collection/AtExpression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BodyBasedExpression } from '../Expression';
import { OclExecutionContext } from '../../OclExecutionContext';
import {BodyBasedExpression} from "../BodyBasedExpression";

/**
* Returns the element of the collection at index index.
Expand Down
Loading

0 comments on commit 0621e03

Please sign in to comment.