Commit 3b57364d authored by Florian Hölzl's avatar Florian Hölzl
Browse files

code generation for composite

refs 121
parent f3fabf3b
......@@ -13,6 +13,7 @@ Require-Bundle: org.fortiss.af3.expression;bundle-version="2.1.0";visibility:=re
Export-Package: org.fortiss.af3.component,
org.fortiss.af3.component.constraint,
org.fortiss.af3.component.generator.c,
org.fortiss.af3.component.generator.imperative,
org.fortiss.af3.component.model,
org.fortiss.af3.component.model.behavior,
org.fortiss.af3.component.model.behavior.code,
......
......@@ -149,7 +149,7 @@
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.component.generator.c.ComponentTransformation">
transformationProvider="org.fortiss.af3.component.generator.imperative.CodeSpecificationTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.component.model.Component">
......@@ -157,7 +157,20 @@
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.generator.common.model.c.CSourcePackage">
objectClass="org.fortiss.af3.expression.model.generator.Program">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.component.generator.imperative.CompositeComponentTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.component.model.Component">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.expression.model.generator.Program">
</objectClass>
</target>
</transformationProvider>
......
......@@ -17,9 +17,14 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.af3.component.generator.c;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCSourcePackage;
import static org.fortiss.af3.generator.common.utils.SourceUtils.mergeSourcePackageInto;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.getRootElement;
import static org.fortiss.tooling.kernel.utils.TransformationUtils.createTransformedObjectFor;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.ComponentArchitecture;
import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.tooling.kernel.extension.ITransformationProvider;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
......@@ -32,7 +37,7 @@ import org.fortiss.tooling.kernel.extension.exception.TransformationFailedExcept
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
* @ConQAT.Rating YELLOW Hash: 790891F86E6B92A3999BCD97A3A65113
*/
public class ComponentArchitectureTransformation implements ITransformationProvider {
......@@ -59,7 +64,17 @@ public class ComponentArchitectureTransformation implements ITransformationProvi
@Override
public Object transform(Object source, ITransformationContext context)
throws TransformationFailedException {
return createTransformedObjectFor(((ComponentArchitecture)source).getTopComponent(),
getTargetClass(), context);
CSourcePackage result = createCSourcePackage();
Component sourceComponent = ((ComponentArchitecture)source).getTopComponent();
CSourcePackage compSource =
createTransformedObjectFor(sourceComponent, CSourcePackage.class, context);
mergeSourcePackageInto(compSource, result);
DataDictionary dict = getRootElement(sourceComponent, DataDictionary.class);
CSourcePackage dictSource = createTransformedObjectFor(dict, CSourcePackage.class, context);
mergeSourcePackageInto(dictSource, result);
return result;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.component.generator.imperative;
import static org.conqat.lib.commons.reflect.ReflectionUtils.pickInstanceOf;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.returns;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.sequence;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.behavior.code.CodeSpecification;
import org.fortiss.af3.expression.model.terms.imperative.StatementSequence;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
/**
* A {@link ComponentTransformationBase} for {@link CodeSpecification}s.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class CodeSpecificationTransformation extends ComponentTransformationBase {
/** {@inheritDoc} */
@Override
protected StatementSequence createInitialization() {
return sequence(returns(null));
}
/** {@inheritDoc} */
@Override
protected StatementSequence createPerformStep() {
return sequence(returns(null));
}
/** {@inheritDoc} */
@Override
protected boolean canTransformComponent(Component source, ITransformationContext context) {
return pickInstanceOf(CodeSpecification.class, source.getSpecificationsList()) != null;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.component.generator.imperative;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.assignment;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.boolConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.boolType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.sequence;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.var;
import static org.fortiss.af3.expression.utils.GeneratorModelElementFactory.createProgram;
import static org.fortiss.af3.expression.utils.GeneratorModelElementFactory.createProgramFunction;
import static org.fortiss.af3.expression.utils.GeneratorModelElementFactory.createVariable;
import static org.fortiss.tooling.kernel.utils.IdentifierUtils.getUniqueIdentifier;
import java.util.ArrayList;
import java.util.List;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.InputPort;
import org.fortiss.af3.component.model.OutputPort;
import org.fortiss.af3.component.model.Port;
import org.fortiss.af3.expression.model.generator.Program;
import org.fortiss.af3.expression.model.generator.ProgramFunction;
import org.fortiss.af3.expression.model.terms.imperative.IStatementTerm;
import org.fortiss.af3.expression.model.terms.imperative.StatementSequence;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.tooling.kernel.extension.ITransformationProvider;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
import org.fortiss.tooling.kernel.extension.exception.TransformationFailedException;
/**
* {@link ITransformationProvider} for the transformation from {@link Component} to
* {@link CSourcePackage}.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public abstract class ComponentTransformationBase implements ITransformationProvider {
/** The prefix of the initialization function. */
public static final String INITIALIZE_FUNCTION_PREFIX = "init_";
/** The prefix of the perform step function. */
public static final String PERFORM_STEP_FUNCTION_PREFIX = "perform_step_";
/** The prefix of the clear inputs function. */
public static final String CLEAR_INPUTS_FUNCTION_PREFIX = "clear_inputs_";
/** The prefix of the clear outputs function. */
public static final String CLEAR_OUTPUTS_FUNCTION_PREFIX = "clear_outputs_";
/** The prefix of the NoVal port variables. */
public static final String NOVAL_PORT_VARIABLE_PREFIX = "noval_";
/** {@inheritDoc} */
@Override
public Class<?> getTargetClass() {
return Program.class;
}
/** {@inheritDoc} */
@Override
public boolean
canHandleChainTransformation(Class<?> sourceClass, ITransformationContext context) {
return Component.class.isAssignableFrom(sourceClass);
}
/** {@inheritDoc} */
@Override
public boolean canTransform(Object source, ITransformationContext context) {
return source instanceof Component && canTransformComponent((Component)source, context);
}
/** Returns whether the given component can actually be transformed by this transformation. */
protected abstract boolean canTransformComponent(Component source,
ITransformationContext context);
/** {@inheritDoc} */
@Override
public Object transform(Object source, ITransformationContext context)
throws TransformationFailedException {
Component sourceComponent = (Component)source;
Program result = createProgram(getUniqueIdentifier(sourceComponent));
createInterface(sourceComponent, result);
createBehavior(sourceComponent, result);
return result;
}
/** Creates the behavior implementation for the given component. */
private void createBehavior(Component sourceComponent, Program result) {
String initName = INITIALIZE_FUNCTION_PREFIX + getUniqueIdentifier(sourceComponent);
ProgramFunction init = createProgramFunction(true, initName, null);
init.setDefinition(createInitialization());
result.getFunctionsList().add(init);
String performStepName =
PERFORM_STEP_FUNCTION_PREFIX + getUniqueIdentifier(sourceComponent);
ProgramFunction performStep = createProgramFunction(true, performStepName, null);
performStep.setDefinition(createPerformStep());
result.getFunctionsList().add(performStep);
}
/** Creates the implementation for the initialization function. */
protected abstract StatementSequence createInitialization();
/** Creates the implementation for the perform-step function. */
protected abstract StatementSequence createPerformStep();
/** Creates the interface implementation for the given component. */
private void createInterface(Component component, Program result) {
List<IStatementTerm> clearFunctionBody = new ArrayList<IStatementTerm>();
for(InputPort inport : component.getInputPorts()) {
createPortInterface(result, inport, clearFunctionBody);
}
ProgramFunction clear =
createProgramFunction(true, CLEAR_INPUTS_FUNCTION_PREFIX +
getUniqueIdentifier(component), null);
clear.setDefinition(sequence(clearFunctionBody));
result.getFunctionsList().add(clear);
clearFunctionBody.clear();
for(OutputPort outport : component.getOutputPorts()) {
createPortInterface(result, outport, clearFunctionBody);
}
clear =
createProgramFunction(true, CLEAR_OUTPUTS_FUNCTION_PREFIX +
getUniqueIdentifier(component), null);
clear.setDefinition(sequence(clearFunctionBody));
result.getFunctionsList().add(clear);
}
/** Creates the variables for the given port and the clear function call. */
private void createPortInterface(Program result, Port port,
List<IStatementTerm> clearFunctionBody) {
String portID = getUniqueIdentifier(port);
result.getVariablesList().add(createVariable(portID, port.getVariableType(), true));
String noValVar = NOVAL_PORT_VARIABLE_PREFIX + portID;
result.getVariablesList().add(createVariable(noValVar, boolType(), true));
clearFunctionBody.add(assignment(var(noValVar), boolConst(true)));
}
}
......@@ -15,61 +15,53 @@ $Id$
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.component.generator.c;
package org.fortiss.af3.component.generator.imperative;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCSourcePackage;
import static org.fortiss.af3.generator.common.utils.SourceUtils.mergeSourcePackageInto;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.getRootElement;
import static org.fortiss.tooling.kernel.utils.TransformationUtils.createTransformedObjectFor;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.returns;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.sequence;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.tooling.kernel.extension.ITransformationProvider;
import org.fortiss.af3.component.model.behavior.code.CodeSpecification;
import org.fortiss.af3.expression.model.generator.Program;
import org.fortiss.af3.expression.model.terms.imperative.StatementSequence;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
import org.fortiss.tooling.kernel.extension.exception.TransformationFailedException;
import org.fortiss.tooling.kernel.utils.TransformationUtils;
/**
* {@link ITransformationProvider} for the transformation from {@link Component} to
* {@link CSourcePackage}.
* A {@link ComponentTransformationBase} for {@link CodeSpecification}s.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class ComponentTransformation implements ITransformationProvider {
public class CompositeComponentTransformation extends ComponentTransformationBase {
/** {@inheritDoc} */
@Override
public Class<?> getTargetClass() {
return CSourcePackage.class;
protected StatementSequence createInitialization() {
return sequence(returns(null));
}
/** {@inheritDoc} */
@Override
public boolean
canHandleChainTransformation(Class<?> sourceClass, ITransformationContext context) {
return Component.class.isAssignableFrom(sourceClass);
protected StatementSequence createPerformStep() {
return sequence(returns(null));
}
/** {@inheritDoc} */
@Override
public boolean canTransform(Object source, ITransformationContext context) {
return source instanceof Component;
protected boolean canTransformComponent(Component source, ITransformationContext context) {
return !source.getSubComponents().isEmpty() && canTransformSubComponents(source, context);
}
/** {@inheritDoc} */
@Override
public Object transform(Object source, ITransformationContext context)
throws TransformationFailedException {
Component sourceComponent = (Component)source;
CSourcePackage cPack = createCSourcePackage();
DataDictionary dict = getRootElement(sourceComponent, DataDictionary.class);
CSourcePackage dictSource = createTransformedObjectFor(dict, CSourcePackage.class, context);
mergeSourcePackageInto(dictSource, cPack);
return cPack;
/** Tests whether sub-component can be transformed. */
private boolean canTransformSubComponents(Component source, ITransformationContext context) {
for(Component sub : source.getSubComponents()) {
if(!TransformationUtils.canTransform(sub, Program.class, context)) {
return false;
}
}
return true;
}
}
......@@ -13,6 +13,7 @@ Require-Bundle: org.fortiss.af3.generator.common;bundle-version="2.1.0";visibili
Export-Package: org.fortiss.af3.expression,
org.fortiss.af3.expression.examples,
org.fortiss.af3.expression.generator.c,
org.fortiss.af3.expression.generator.imperative,
org.fortiss.af3.expression.language,
org.fortiss.af3.expression.language.compose,
org.fortiss.af3.expression.language.constraint,
......@@ -21,6 +22,9 @@ Export-Package: org.fortiss.af3.expression,
org.fortiss.af3.expression.model.definitions,
org.fortiss.af3.expression.model.definitions.impl,
org.fortiss.af3.expression.model.definitions.util,
org.fortiss.af3.expression.model.generator,
org.fortiss.af3.expression.model.generator.impl,
org.fortiss.af3.expression.model.generator.util,
org.fortiss.af3.expression.model.impl,
org.fortiss.af3.expression.model.parser,
org.fortiss.af3.expression.model.terms,
......
......@@ -17,12 +17,16 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.af3.expression.generator.c;
import static org.fortiss.af3.generator.common.model.c.CModifier.EXTERN;
import static org.fortiss.af3.generator.common.model.c.CModifier.NONE;
import static org.fortiss.af3.generator.common.model.c.CModifier.STATIC;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCEnumerationDeclaration;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCEnumerationMember;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCFunctionDeclaration;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCFunctionDefinition;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCSourcePackage;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCVariable;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCVariableDeclaration;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createGeneratedHeaderInclude;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createHeaderFile;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createImplementationFile;
......@@ -37,6 +41,7 @@ import org.fortiss.af3.expression.model.definitions.FunctionParameter;
import org.fortiss.af3.expression.model.definitions.TypeDefinition;
import org.fortiss.af3.expression.model.generator.Program;
import org.fortiss.af3.expression.model.generator.ProgramFunction;
import org.fortiss.af3.expression.model.generator.ProgramVariable;
import org.fortiss.af3.generator.common.model.c.CFunctionDeclaration;
import org.fortiss.af3.generator.common.model.c.CFunctionDefinition;
import org.fortiss.af3.generator.common.model.c.CHeaderFile;
......@@ -120,6 +125,13 @@ public class ProgramTransformation implements ITransformationProvider {
for(Program sub : p.getSubProgramsList()) {
implFile.getImportsList().add(createInclude(sub.getName(), false));
}
for(ProgramVariable var : p.getVariablesList()) {
CModifier mod = var.isExported() ? NONE : STATIC;
implFile.getDeclarationsList().add(
createCVariableDeclaration(mod,
createCVariable(var.getVar().getIdentifier(), var.getType())));
}
for(ProgramFunction fun : p.getFunctionsList()) {
implFile.getDefinitionsList().add(functionMap.get(fun).getDefinition());
}
......@@ -130,16 +142,23 @@ public class ProgramTransformation implements ITransformationProvider {
/** Creates the header source code. */
private CHeaderFile createHeader(Program p,
Map<ProgramFunction, CFunctionDeclaration> functionMap) {
CHeaderFile h = createHeaderFile(p.getName());
CHeaderFile hFile = createHeaderFile(p.getName());
for(TypeDefinition td : p.getTypesList()) {
if(td instanceof Enumeration) {
addCEnumeration((Enumeration)td, h);
addCEnumeration((Enumeration)td, hFile);
}
}
for(ProgramVariable var : p.getVariablesList()) {
if(var.isExported()) {
hFile.getDeclarationsList().add(
createCVariableDeclaration(EXTERN,
createCVariable(var.getVar().getIdentifier(), var.getType())));
}
}
for(ProgramFunction fun : p.getFunctionsList()) {
h.getDeclarationsList().add(functionMap.get(fun));
hFile.getDeclarationsList().add(functionMap.get(fun));
}
return h;
return hFile;
}
/** Adds the C enumeration for the given enumeration to the given header file. */
......
......@@ -35,7 +35,7 @@ import org.fortiss.af3.project.model.typesystem.IType;
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 98B58222403131E9A1E3B2BE5979840C
* @ConQAT.Rating YELLOW Hash: ABACD4BD483BC17DF8E34E3968E6DD07
*/
public class GeneratorModelElementFactory {
/** Creates a program with the given name. */
......@@ -55,7 +55,7 @@ public class GeneratorModelElementFactory {
ProgramVariable v = GeneratorFactory.eINSTANCE.createProgramVariable();
v.setExported(export);
v.setVariable(var(name));
v.setType(type);
v.setType(copy(type));
return v;
}
......
......@@ -108,7 +108,7 @@ public class CLanguageConstructTextGenerator {
}
buf.append(functionSignatureToString(def, mod));
buf.append("{\n");
buf.append(def.getBody().toString());
buf.append(def.getBody() == null ? "// nothing to do here\n" : def.getBody().toString());
buf.append("}\n");
return buf.toString();
......
......@@ -29,6 +29,7 @@ import org.fortiss.af3.generator.common.model.c.CInclude;
import org.fortiss.af3.generator.common.model.c.CModifier;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.af3.generator.common.model.c.CVariable;
import org.fortiss.af3.generator.common.model.c.CVariableDeclaration;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationDeclaration;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationMember;
import org.fortiss.af3.generator.common.model.c.types.TypesFactory;
......@@ -170,4 +171,13 @@ public class CLanguageModelElementFactory {
cvar.setType(copy(type));
return cvar;
}
/** Creates a {@link CVariableDeclaration} with the given modifier and variable. */
public static CVariableDeclaration createCVariableDeclaration(CModifier mod, CVariable var) {
CVariableDeclaration cvar =
AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCVariableDeclaration();
cvar.setModifier(mod);
cvar.setVariable(var);
return cvar;
}
}
......@@ -10,8 +10,12 @@ Bundle-ActivationPolicy: lazy
Bundle-Activator: org.fortiss.af3.mode.AF3ModeActivator
Require-Bundle: org.fortiss.af3.component;bundle-version="2.1.0";visibility:=reexport
Export-Package: org.fortiss.af3.mode,
org.fortiss.af3.mode.constraint,
org.fortiss.af3.mode.generator.c,
org.fortiss.af3.mode.generator.imperative,
org.fortiss.af3.mode.model,
org.fortiss.af3.mode.model.impl,
org.fortiss.af3.mode.model.util,
org.fortiss.af3.mode.simulator,
org.fortiss.af3.mode.simulator.transform,
org.fortiss.af3.mode.utils
......@@ -59,6 +59,19 @@
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.mode.generator.imperative.ModeAutomatonTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.component.model.Component">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.expression.model.generator.Program">
</objectClass>