From 85e0e53e3b9bf5222404f2f55762c27f3c5366eb Mon Sep 17 00:00:00 2001 From: nmacedo Date: Fri, 21 Mar 2014 14:06:29 +0000 Subject: [PATCH] atl (unique) lazy calls close #177 #173 --- .../models/hsm2nhsm/NHM_example.xmi | 1 + .../haslab/echo/engine/OCLTranslator.java | 5 +- .../echo/engine/alloy/EAlloyRelation.java | 2 +- .../engine/alloy/EAlloyTransformation.java | 23 ++++++---- .../haslab/echo/engine/alloy/OCL2Alloy2.java | 46 ++++++++++++++----- .../echo/engine/ast/EEngineRelation.java | 5 +- .../engine/ast/EEngineTransformation.java | 2 +- .../echo/engine/kodkod/EKodkodRelation.java | 2 +- .../src/pt/uminho/haslab/mde/OCLUtil.java | 5 +- .../atl/EATLTransformation.java | 12 +++-- 10 files changed, 69 insertions(+), 34 deletions(-) diff --git a/examples/pt.uminho.haslab.echo.examples/models/hsm2nhsm/NHM_example.xmi b/examples/pt.uminho.haslab.echo.examples/models/hsm2nhsm/NHM_example.xmi index 38399df..3a5fff0 100644 --- a/examples/pt.uminho.haslab.echo.examples/models/hsm2nhsm/NHM_example.xmi +++ b/examples/pt.uminho.haslab.echo.examples/models/hsm2nhsm/NHM_example.xmi @@ -7,4 +7,5 @@ xsi:schemaLocation="NHSM /pt.uminho.haslab.echo.examples/metamodels/hsm2nhsm/NHSM.ecore" name="machine"> + diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/OCLTranslator.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/OCLTranslator.java index 93593bb..1513f70 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/OCLTranslator.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/OCLTranslator.java @@ -6,6 +6,7 @@ import org.eclipse.qvtd.pivot.qvtrelation.RelationCallExp; import org.eclipse.qvtd.pivot.qvttemplate.ObjectTemplateExp; import org.eclipse.qvtd.pivot.qvttemplate.PropertyTemplateItem; + import pt.uminho.haslab.echo.*; import pt.uminho.haslab.echo.engine.ast.*; import pt.uminho.haslab.mde.MDEManager; @@ -456,13 +457,13 @@ IFormula translateFormula(RelationCallExp expr) throws EchoError { } // tries to call referred relation - IFormula res = ((ITContext) context).getCallerRel().transformation + IFormula res = (IFormula) ((ITContext) context).getCallerRel().transformation .callRelation(rel,((ITContext) context),params); // if it doesn't exist, process it if (res == null) { ((ITContext) context).getCallerRel().newRelation(rel); - res = ((ITContext) context).getCallerRel().transformation + res = (IFormula) ((ITContext) context).getCallerRel().transformation .callRelation(rel, ((ITContext) context), params); } diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyRelation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyRelation.java index c6cb91a..c78213b 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyRelation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyRelation.java @@ -113,7 +113,7 @@ protected AlloyExpression addNonTopRel(List eModelDomain } @Override - public void newRelation(EQVTRelation rel) throws EchoError { + public void newRelation(ERelation rel) throws EchoError { new EAlloyRelation(this, rel); } diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyTransformation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyTransformation.java index 7e61fc0..2dbe9ef 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyTransformation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/EAlloyTransformation.java @@ -21,6 +21,7 @@ import pt.uminho.haslab.echo.engine.ast.IDecl; import pt.uminho.haslab.echo.engine.ast.IExpression; import pt.uminho.haslab.echo.engine.ast.IFormula; +import pt.uminho.haslab.echo.engine.ast.INode; import pt.uminho.haslab.mde.transformation.EDependency; import pt.uminho.haslab.mde.transformation.EModelDomain; import pt.uminho.haslab.mde.transformation.EModelParameter; @@ -234,7 +235,7 @@ protected void addTopRelationConstraint(EEngineRelation relation) /** {@inheritDoc} */ @Override - public AlloyFormula callRelation(ERelation n, ITContext context, + public INode callRelation(ERelation n, ITContext context, List params) { if (subRelationFields == null) return null; @@ -251,15 +252,19 @@ public AlloyFormula callRelation(ERelation n, ITContext context, Expr exp = f.call(vars); IExpression expp = new AlloyExpression(exp); - + INode form = null; // applies the relation parameters to the relation function - IExpression insig = params.get(params.size() - 1); - params.remove(insig); - for (IExpression param : params) - expp = param.join(expp); - IFormula form = insig.in(expp); - - return (AlloyFormula) form; + if (params.size() == 1) { + form = params.get(0).join(expp); + } + else { + IExpression insig = params.get(params.size() - 1); + params.remove(insig); + for (IExpression param : params) + expp = param.join(expp); + form = insig.in(expp); + } + return form; } @Override diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/OCL2Alloy2.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/OCL2Alloy2.java index 21d0d06..a869986 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/OCL2Alloy2.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/alloy/OCL2Alloy2.java @@ -7,7 +7,9 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.ocl.examples.pivot.OCLExpression; +import pt.uminho.haslab.echo.EchoError; import pt.uminho.haslab.echo.EchoOptionsSetup; import pt.uminho.haslab.echo.EchoReporter; import pt.uminho.haslab.echo.ErrorParser; @@ -23,7 +25,11 @@ import pt.uminho.haslab.echo.engine.ast.INode; import pt.uminho.haslab.mde.MDEManager; import pt.uminho.haslab.mde.model.EMetamodel; +import pt.uminho.haslab.mde.transformation.atl.EATLRelation; +import pt.uminho.haslab.mde.transformation.atl.EATLTransformation; +import pt.uminho.haslab.mde.transformation.qvt.EQVTRelation; +import java.util.ArrayList; import java.util.List; public class OCL2Alloy2 { @@ -34,8 +40,7 @@ public OCL2Alloy2(ITContext context) { this.context = context; } - public INode oclExprToAlloy(EObject expr) throws ErrorTransform, - ErrorAlloy, ErrorUnsupported, ErrorParser { + public INode oclExprToAlloy(EObject expr) throws EchoError { if (expr.eClass().getName().equals("OperatorCallExp") || expr.eClass().getName().equals("OperationCallExp") || expr.eClass().getName().equals("CollectionOperationCallExp")) { @@ -74,8 +79,7 @@ IFormula oclExprToAlloyBooleanLit(EObject expr) { return Constants.FALSE(); } - INode oclExprToAlloyAttribute(EObject expr) throws ErrorTransform, ErrorAlloy, - ErrorUnsupported, ErrorParser { + INode oclExprToAlloyAttribute(EObject expr) throws EchoError { INode res = null; EStructuralFeature source = expr.eClass().getEStructuralFeature( "source"); @@ -108,8 +112,7 @@ INode oclExprToAlloyAttribute(EObject expr) throws ErrorTransform, ErrorAlloy, return res; } - INode oclExprToAlloyBinding(EObject expr) throws ErrorTransform, ErrorAlloy, - ErrorUnsupported, ErrorParser { + INode oclExprToAlloyBinding(EObject expr) throws EchoError { INode res = null; EStructuralFeature value = expr.eClass().getEStructuralFeature("value"); @@ -133,7 +136,7 @@ INode oclExprToAlloyBinding(EObject expr) throws ErrorTransform, ErrorAlloy, "propertyName"); IExpression aux = propertyToField((String) expr.eGet(oname), var); - if (valmodel != null && !varmodel.equals(valmodel)) { + if (valmodel != null && !varmodel.equals(valmodel) && val instanceof IExpression) { EchoReporter.getInstance().debug(" *** Call implicit trace! "+context.getCallerRel().transformation.callAllRelation(context, (IExpression) val)); val = context.getCallerRel().transformation.callAllRelation(context, (IExpression) val); } @@ -158,8 +161,7 @@ INode oclExprToAlloyBinding(EObject expr) throws ErrorTransform, ErrorAlloy, return res; } - INode oclExprToAlloyOperationCall(EObject expr) throws ErrorTransform, ErrorAlloy, - ErrorUnsupported, ErrorParser { + INode oclExprToAlloyOperationCall(EObject expr) throws EchoError { INode res = null; EStructuralFeature source = expr.eClass().getEStructuralFeature( "source"); @@ -236,6 +238,29 @@ else if (operatorname.equals("-")) .minus((IIntExpression) oclExprToAlloy(argumentso.get(0))); else if (operatorname.equals("allInstances")) res = src; + else if (((EATLTransformation) context.getCallerRel().transformation.transformation).getRelation(operatorname) != null) { + EATLRelation rel = ((EATLTransformation) context.getCallerRel().transformation.transformation).getRelation(operatorname); + + // translates variable parameters + List params = new ArrayList(); + for (EObject arg : argumentso) { + IExpression param = (IExpression) oclExprToAlloy(arg); + params.add(param); + } + + // tries to call referred relation + res = ((ITContext) context).getCallerRel().transformation + .callRelation(rel,((ITContext) context),params); + + // if it doesn't exist, process it + if (res == null) { + ((ITContext) context).getCallerRel().newRelation(rel); + res = ((ITContext) context).getCallerRel().transformation + .callRelation(rel, ((ITContext) context), params); + } + context.setCurrentModel(null); + EchoReporter.getInstance().debug("Call rule result: "+res); + } else throw new ErrorUnsupported("OCL operation not supported: " + expr.toString() + "."); @@ -281,8 +306,7 @@ IExpression propertyToField(String propn, IExpression var) return exp; } - public IFormula translateExpressions(List lex) throws ErrorAlloy, - ErrorTransform, ErrorUnsupported, ErrorParser { + public IFormula translateExpressions(List lex) throws EchoError { IFormula expr = Constants.TRUE(); for (Object ex : lex) { expr = expr.and((IFormula) oclExprToAlloy((EObject) ex)); diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineRelation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineRelation.java index 8059778..9b99fa4 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineRelation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineRelation.java @@ -8,7 +8,6 @@ import pt.uminho.haslab.mde.transformation.EDependency; import pt.uminho.haslab.mde.transformation.EModelDomain; import pt.uminho.haslab.mde.transformation.ERelation; -import pt.uminho.haslab.mde.transformation.qvt.EQVTRelation; import java.util.*; @@ -262,7 +261,7 @@ private IFormula calculateConstraint() throws EchoError { List params = new ArrayList<>(); for (IDecl d : rootVar2engineDecl.values()) params.add(d.variable()); - targetFormula = targetFormula.and(transformation.callRelation(relation, context, params)); + targetFormula = targetFormula.and((IFormula) transformation.callRelation(relation, context, params)); } if (targetVar2engineDecl.size() == 1) @@ -415,7 +414,7 @@ private IExpression addRelationField() throws EchoError { */ abstract protected IFormula simplify(IFormula formula) throws ErrorUnsupported; - public abstract void newRelation(EQVTRelation rel) throws EchoError; + public abstract void newRelation(ERelation rel) throws EchoError; public enum Mode { TOP_QUANTIFIER, diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineTransformation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineTransformation.java index cb8ead0..6d708a5 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineTransformation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/ast/EEngineTransformation.java @@ -146,7 +146,7 @@ protected abstract void addTopRelationConstraint(EEngineRelation rel) * @return the expression resulting from calling * rel over params */ - public abstract IFormula callRelation(ERelation rel, ITContext context, + public abstract INode callRelation(ERelation rel, ITContext context, List params); public abstract IExpression callAllRelation(ITContext context, diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/kodkod/EKodkodRelation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/kodkod/EKodkodRelation.java index bc270a2..19bfec9 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/kodkod/EKodkodRelation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/echo/engine/kodkod/EKodkodRelation.java @@ -70,7 +70,7 @@ protected KodkodExpression addNonTopRel(List eModelDomai /** {@inheritDoc} */ @Override - public void newRelation(EQVTRelation rel) throws EchoError { + public void newRelation(ERelation rel) throws EchoError { new EKodkodRelation(this, rel); } diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/OCLUtil.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/OCLUtil.java index 9887ec4..060604d 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/OCLUtil.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/OCLUtil.java @@ -112,8 +112,9 @@ public static Map variablesOCLExpression (final EObject exp, f if (exp.eClass().getName().equals("VariableExp")) { final EStructuralFeature var = exp.eClass().getEStructuralFeature("referredVariable"); final EObject x = (EObject) exp.eGet(var); - if (vars.get(x) == null) - vars.put(EVariable.getVariable(x),null); + if (!x.eGet(x.eClass().getEStructuralFeature("varName")).equals("thisModule")) + if (vars.get(x) == null) + vars.put(EVariable.getVariable(x),null); }/* else if (exp instanceof ObjectTemplateExp) { VariableDeclaration x = ((ObjectTemplateExp) exp).getBindsTo(); diff --git a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/transformation/atl/EATLTransformation.java b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/transformation/atl/EATLTransformation.java index 116c7da..98336a6 100644 --- a/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/transformation/atl/EATLTransformation.java +++ b/plugins/pt.uminho.haslab.echo/src/pt/uminho/haslab/mde/transformation/atl/EATLTransformation.java @@ -30,7 +30,7 @@ public class EATLTransformation extends ETransformation { private Map modelParams; - private List relations; + private HashMap relations; private EObject transformation; public static Map metamodeluris = new HashMap<>(); @@ -49,7 +49,7 @@ protected void process(EObject module) throws ErrorUnsupported, ErrorParser { this.transformation = module; if (modelParams == null) modelParams = new HashMap<>(); - if (relations == null) relations = new ArrayList<>(); + if (relations == null) relations = new HashMap<>(); if (!module.eClass().getName().equals("Module")) throw new ErrorParser("Bad atl"); @@ -71,7 +71,7 @@ protected void process(EObject module) throws ErrorUnsupported, ErrorParser { EStructuralFeature outmdls = module.eClass().getEStructuralFeature("outModels"); EList objs = (EList) module.eGet(elements); for (EObject x : objs) - relations.add(new EATLRelation(this,x)); + relations.put((String) x.eGet(x.eClass().getEStructuralFeature("name")),new EATLRelation(this,x)); objs = (EList) module.eGet(inmdls); for (EObject x : objs) modelParams.put((String) x.eGet(x.eClass().getEStructuralFeature("name")), new EATLModelParameter(x,this)); @@ -88,7 +88,11 @@ public List getModelParams() { @Override public List getRelations() { - return relations; + return new ArrayList(relations.values()); + } + + public EATLRelation getRelation(String name) { + return relations.get(name); } @Override