View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2007 STMicroelectronics
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   * Contact: fractal@objectweb.org
20   *
21   * Author:Matthieu Leclercq
22   */
23  
24  package org.objectweb.fractal.cecilia.composite.c.components;
25  
26  import static org.objectweb.fractal.api.type.TypeFactory.OPTIONAL;
27  
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.objectweb.fractal.adl.ADLException;
32  import org.objectweb.fractal.adl.ComponentVisitor;
33  import org.objectweb.fractal.adl.Node;
34  import org.objectweb.fractal.adl.components.ComponentContainer;
35  import org.objectweb.fractal.api.Component;
36  import org.objectweb.fractal.cecilia.adl.AbstractDefinitionTask;
37  import org.objectweb.fractal.cecilia.adl.SourceCodeProvider;
38  import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
39  import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
40  import org.objectweb.fractal.task.core.TaskException;
41  import org.objectweb.fractal.task.core.primitive.annotations.ClientInterface;
42  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
43  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
44  import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
45  
46  /**
47   * Builds the definition of a composite component.
48   */
49  public class ComponentDefinitionVisitor extends AbstractTaskFactoryUser
50      implements
51        ComponentVisitor {
52  
53    // ---------------------------------------------------------------------------
54    // Implementation of the ComponentVisitor interface
55    // ---------------------------------------------------------------------------
56  
57    /**
58     * Visits {@link ComponentContainer} nodes and creates a task that writes the
59     * source code containing the definition of the component data structure.
60     */
61    public Component visit(final List<Node> path,
62        final ComponentContainer container, final Map<Object, Object> context)
63        throws ADLException, TaskException {
64      return taskFactoryItf.newPrimitiveTask(new ComponentDefinitionTask(),
65          container);
66    }
67  
68    // ---------------------------------------------------------------------------
69    // Task classes
70    // ---------------------------------------------------------------------------
71  
72    /**
73     * Builds source code for the definition of the component data structure for a
74     * default composite component. This task provides the produced source code.
75     */
76    @TaskParameters("componentNode")
77    @ServerInterfaces(@ServerInterface(name = "component-definition-provider", signature = SourceCodeProvider.class, record = "role:componentDefinition, id:%", parameters = "componentNode"))
78    public static class ComponentDefinitionTask extends AbstractDefinitionTask {
79  
80      // -------------------------------------------------------------------------
81      // Task client interfaces
82      // -------------------------------------------------------------------------
83  
84      /** Source code to be included for interface definitions (optional). */
85      @ClientInterface(name = "interface-definition-provider", contingency = OPTIONAL, record = "role:interfaceDefinition, id:%", parameters = "componentNode")
86      public SourceCodeProvider interfaceDefinitionsProviderItf;
87  
88      // -------------------------------------------------------------------------
89      // Implementation of abstract methods of AbstractDefinitionTask
90      // -------------------------------------------------------------------------
91      @Override
92      protected String processSourceCode() throws Exception {
93        final CodeWriter cw = new CodeWriter("Composite Definition Builder");
94        cw.append("#define IN_CECILIA_MECHANISM").endl();
95        cw.append("#include <fractal/api/defaultComposite.h>").endl();
96  
97        if (interfaceDefinitionsProviderItf != null)
98          cw.appendln(interfaceDefinitionsProviderItf.getSourceCode()).endl();
99  
100       cw.endl();
101       return cw.toString();
102     }
103   }
104 }