77import org .eclipse .emf .ecore .EObject ;
88import org .eclipse .emf .ecore .EReference ;
99import org .eclipse .emf .ecore .EStructuralFeature ;
10+ import org .eclipse .ocl .examples .pivot .IteratorExp ;
1011
1112import pt .uminho .haslab .echo .EchoError ;
1213import pt .uminho .haslab .echo .EchoOptionsSetup ;
1617import pt .uminho .haslab .echo .engine .EchoHelper ;
1718import pt .uminho .haslab .echo .engine .ITContext ;
1819import pt .uminho .haslab .echo .engine .ast .Constants ;
20+ import pt .uminho .haslab .echo .engine .ast .IDecl ;
1921import pt .uminho .haslab .echo .engine .ast .IExpression ;
2022import pt .uminho .haslab .echo .engine .ast .IFormula ;
2123import pt .uminho .haslab .echo .engine .ast .IIntExpression ;
2224import pt .uminho .haslab .echo .engine .ast .INode ;
2325import pt .uminho .haslab .mde .MDEManager ;
2426import pt .uminho .haslab .mde .model .EMetamodel ;
27+ import pt .uminho .haslab .mde .model .EVariable ;
2528import pt .uminho .haslab .mde .transformation .atl .EATLRelation ;
2629import pt .uminho .haslab .mde .transformation .atl .EATLTransformation ;
2730
@@ -50,6 +53,8 @@ public INode translateAtlOcl(EObject expr) throws EchoError {
5053 return translateAtlOclBooleanLit (expr );
5154 } else if (expr .eClass ().getName ().equals ("Binding" )) {
5255 return translateAtlOclBinding (expr );
56+ } else if (expr .eClass ().getName ().equals ("IteratorExp" )) {
57+ return translateAtlOclIterator (expr );
5358 } else
5459 throw new ErrorUnsupported ("OCL expression not supported: " + expr
5560 + "." );
@@ -63,7 +68,9 @@ IExpression translateAtlOclVariable(EObject expr) {
6368 "varName" );
6469 String varname = (String ) vardecl .eGet (name );
6570 context .setCurrentModel (context .getVarModel (varname ));
66- return context .getVar (varname );
71+ IExpression res = context .getVar (varname );
72+ EchoReporter .getInstance ().debug ("Translated " +varname +" to " +res );
73+ return res ;
6774 }
6875
6976 IFormula translateAtlOclBooleanLit (EObject expr ) {
@@ -84,6 +91,7 @@ INode translateAtlOclAttribute(EObject expr) throws EchoError {
8491 context .setCurrentModel (null );
8592
8693 IExpression var = (IExpression ) translateAtlOcl (sourceo );
94+
8795 EStructuralFeature oname = expr .eClass ().getEStructuralFeature ("name" );
8896 IExpression aux = propertyToField ((String ) expr .eGet (oname ), var );
8997
@@ -152,8 +160,12 @@ INode translateAtlOclBinding(EObject expr) throws EchoError {
152160 String nameo = feature .getEType ().getName ();
153161 if (nameo .equals ("EBoolean" ))
154162 res = ((IFormula ) var .join (aux )).iff ((IFormula ) val );
155- else
156- res = var .join (aux ).eq ((IExpression ) val );
163+ else {
164+ if (((EObject ) expr .eGet (value )).eClass ().getName ().equals ("IteratorExp" ) && ((EObject ) expr .eGet (value )).eGet (((EObject ) expr .eGet (value )).eClass ().getEStructuralFeature ("name" )).equals ("any" ))
165+ res = var .join (aux ).in ((IExpression ) val ).and (var .join (aux ).one ());
166+ else
167+ res = var .join (aux ).eq ((IExpression ) val );
168+ }
157169 return res ;
158170 }
159171
@@ -178,8 +190,12 @@ else if (operatorname.equals("=")) {
178190 INode aux = translateAtlOcl (argumentso .get (0 ));
179191 if (src instanceof IFormula )
180192 res = ((IFormula ) src ).iff ((IFormula ) aux );
181- else
182- res = ((IExpression ) src ).eq ((IExpression ) aux );
193+ else {
194+ if (argumentso .get (0 ).eClass ().getName ().equals ("IteratorExp" ) && argumentso .get (0 ).eGet (argumentso .get (0 ).eClass ().getEStructuralFeature ("name" )).equals ("any" ))
195+ res = (((IExpression ) src ).in ((IExpression ) aux )).and (((IExpression ) src ).one ());
196+ else
197+ res = ((IExpression ) src ).eq ((IExpression ) aux );
198+ }
183199 } else if (operatorname .equals ("<>" )) {
184200 INode aux = translateAtlOcl (argumentso .get (0 ));
185201 EStructuralFeature type = argumentso .get (0 ).eClass ()
@@ -263,6 +279,86 @@ else if (((EATLTransformation) context.getCallerRel().transformation.transformat
263279
264280 return res ;
265281 }
282+
283+ INode translateAtlOclIterator (EObject expr ) throws EchoError {
284+ INode res = null ;
285+
286+ EStructuralFeature source = expr .eClass ().getEStructuralFeature (
287+ "source" );
288+ IExpression src = (IExpression ) translateAtlOcl ((EObject ) expr .eGet (source ));
289+ EStructuralFeature iterator = expr .eClass ().getEStructuralFeature ("iterators" );
290+ EVariable x = null ;
291+ try {
292+ x = EVariable .getVariable (((EList <EObject >) expr .eGet (iterator )).get (0 ),((AlloyExpression ) src ).EXPR .type ().toExpr ().toString ());
293+ } catch (Err e ) {
294+ // TODO Auto-generated catch block
295+ e .printStackTrace ();
296+ }
297+ IDecl d = context .getDecl (x , true );
298+
299+ // tries to determine owning model of the iterator variable
300+ for (String s : context .getVars ()) {
301+ IExpression var = context .getVar (s );
302+ if (src .hasVar (var ) && context .getVarModel (s ) != null )
303+ context .addVar (d , context .getVarModel (s ));
304+ else
305+ context .addVar (d );
306+ }
307+
308+ EStructuralFeature operat = expr .eClass ().getEStructuralFeature (
309+ "name" );
310+ String operatorname = (String ) expr .eGet (operat );
311+ EStructuralFeature arguments = expr .eClass ().getEStructuralFeature (
312+ "body" );
313+ INode body = translateAtlOcl ((EObject ) expr .eGet (arguments ));
314+ IFormula aux ;
315+
316+ if (operatorname .equals ("forAll" )) {
317+ aux = (IFormula ) body ;
318+ aux = ((d .variable ().in (src )).implies (aux ));
319+ res = aux .forAll (d );
320+ }
321+ else if (operatorname .equals ("exists" )) {
322+ aux = (IFormula ) body ;
323+ aux = ((d .variable ().in (src )).and (aux ));
324+ res = aux .forSome (d );
325+ }
326+ else if (operatorname .equals ("one" )) {
327+ aux = (IFormula ) body ;
328+ aux = ((d .variable ().in (src )).and (aux ));
329+ res = aux .forOne (d );
330+ }
331+ else if (operatorname .equals ("collect" )) {
332+ aux = d .variable ().in (src );
333+ res = src .join (aux .comprehension (d ,
334+ ((IExpression ) body ).oneOf ("2_" )));
335+ }
336+ else if (operatorname .equals ("select" )) {
337+ aux = (IFormula ) body ;
338+ aux = ((d .variable ().in (src )).and (aux ));
339+ res = aux .comprehension (d );
340+ }
341+ else if (operatorname .equals ("any" )) {
342+ aux = (IFormula ) body ;
343+ aux = ((d .variable ().in (src )).and (aux ));
344+ res = aux .comprehension (d );
345+ }
346+ else if (operatorname .equals ("reject" )) {
347+ aux = (IFormula ) body ;
348+ aux = ((d .variable ().in (src )).and (aux .not ()));
349+ res = aux .comprehension (d );
350+ }
351+ else if (operatorname .equals ("closure" )) {
352+ IDecl dd = ((IExpression ) body ).oneOf ("2_" );
353+ res = Constants .TRUE ().comprehension (d , dd );
354+ res = src .join (((IExpression ) res ).closure ());
355+ } else
356+ throw new ErrorUnsupported ("OCL iterator not supported: "
357+ + operatorname + "." );
358+ context .remove (d .name ());
359+
360+ return res ;
361+ }
266362
267363 // retrieves the Alloy field corresponding to an OCL property (attribute)
268364 IExpression propertyToField (String propn , IExpression var )
@@ -273,8 +369,8 @@ IExpression propertyToField(String propn, IExpression var)
273369 try {
274370 varsig = ((AlloyExpression ) var ).EXPR .type ().toExpr ().toString ();
275371 metamodelID = EchoHelper .getMetamodelIDfromLabel (varsig );
276- EMetamodel metamodel ;
277- metamodel = MDEManager . getInstance (). getMetamodelID ( metamodelID );
372+ EMetamodel metamodel = MDEManager . getInstance (). getMetamodelID ( metamodelID ) ;
373+
278374 EStructuralFeature feature = ((EClass ) metamodel .getEObject ()
279375 .getEClassifier (EchoHelper .getClassifierName (varsig )))
280376 .getEStructuralFeature (propn );
0 commit comments