Skip to content

Commit

Permalink
Merge pull request #711 from SteKoe/fix/709
Browse files Browse the repository at this point in the history
fix: #709 attempt to fix omitted "self" in expressions
  • Loading branch information
SteKoe authored Aug 13, 2024
2 parents e735747 + b42888b commit 64fa2fc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
35 changes: 23 additions & 12 deletions lib/components/expressions/VariableExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OclExecutionContext } from '../OclExecutionContext';
import {OclExecutionContext} from '../OclExecutionContext';

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

/**
* Resolve variables. Simple values are returned as is (e.g. self.age: number), collections are aggregated.
Expand All @@ -19,20 +19,31 @@ export class VariableExpression extends SourceBasedExpression {

evaluate(visitor: OclExecutionContext, localVariables?: any): any {
let obj;
const _variables = localVariables;

const objectToEvaluate = visitor.getObjectToEvaluate();
let _variables = {
...localVariables,
};

const source = this.getVariable();
const parts = source.split('.');
let parts = source.split('.');

const type = visitor.getRegisteredType(source);
if (type) {
return type;
}

if (parts[0] !== 'self' && Object.prototype.hasOwnProperty.call(objectToEvaluate, [parts[0]])) {
_variables = {
...localVariables,
self: localVariables?.self ?? localVariables
};
parts = ['self', ...parts]
}

if (parts[0] === 'self') {
parts.shift();
obj = (_variables && _variables['self']) || visitor.getObjectToEvaluate();
} else if (_variables === undefined) {
const type = visitor.getRegisteredType(source);
if (type) {
return type;
} else {
obj = visitor.getObjectToEvaluate();
}
obj = (_variables && _variables['self']) || objectToEvaluate;
} else {
obj = _variables;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/components/expressions/collection/ForAllExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export class ForAllExpression extends IteratorExpression {
const body = this.getBody();

if (!iterators || iterators.length === 0) {
return false;
return !collection.some(c => {
return body.evaluate(visitor, {['self']: c}) === false;
});
} else if (iterators.length === 1) {
return !collection.some(c => {
const iterator = this.getIterators()[0];
Expand Down
7 changes: 5 additions & 2 deletions test/expressions/collection/forAll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ describe('Collection->forAll', () => {
FixtureFactory.createPerson('B', 2),
FixtureFactory.createPerson('C', 4),
FixtureFactory.createPerson('D', 8),
FixtureFactory.createPerson('E', 10)
FixtureFactory.createPerson('E', 10),
];

const oclExpression = 'context Person inv: self.children->forAll(age < 10)';
let oclExpression = 'context Person inv: self.children->forAll(age <= 10)';
expectOclRuleValidatesToTrue(oclExpression, mother);

oclExpression = 'context Person inv: self.children->forAll(age < 10)';
expectOclRuleValidatesToFalse(oclExpression, mother);
});

Expand Down
2 changes: 1 addition & 1 deletion test/expressions/collection/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('Collection->select ', () => {
const mother = FixtureFactory.createPerson('Hilde', 50);

it('should parse select', () => {
const oclExpression = 'context Object inv: self.a->select(a | a < 2)->size = 1';
const oclExpression = 'context Object inv: self.a->select(b | b < 2)->size = 1';
expectOclRuleValidatesToTrue(oclExpression, {a: [1, 4]});
});

Expand Down
6 changes: 6 additions & 0 deletions test/expressions/variable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ describe('VariableExpression', () => {
const oclExpression = 'context Object inv: self.a = 12';
expectOclRuleValidatesToTrue(oclExpression, obj);
});

it('returns simple value without self', () => {
const obj = {a: 12};
const oclExpression = 'context Object inv: a = 12';
expectOclRuleValidatesToTrue(oclExpression, obj);
});
});

0 comments on commit 64fa2fc

Please sign in to comment.