KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > coordinates > CoordinatesCompiler


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
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: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.coordinates;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask;
30 import org.objectweb.deployment.scheduling.component.lib.AbstractConfigurationTask;
31 import org.objectweb.fractal.adl.ADLException;
32 import org.objectweb.fractal.adl.TaskMap;
33 import org.objectweb.fractal.adl.components.Component;
34 import org.objectweb.fractal.adl.components.ComponentContainer;
35 import org.objectweb.fractal.adl.components.ComponentPair;
36 import org.objectweb.fractal.adl.components.PrimitiveCompiler;
37 import org.objectweb.fractal.adl.coordinates.Coordinates;
38 import org.objectweb.fractal.adl.coordinates.CoordinatesContainer;
39 import org.objectweb.fractal.api.control.BindingController;
40
41 /**
42  * A {@link PrimitiveCompiler} to compile {@link Coordinates} nodes in
43  * definitions.
44  */

45
46 public class CoordinatesCompiler implements PrimitiveCompiler, BindingController {
47   
48   /**
49    * Name of the mandatory interface bound to the {@link CoordinatesBuilder}
50    * used by this compiler.
51    */

52   
53   public final static String JavaDoc BUILDER_BINDING = "builder";
54   /**
55    * The {@link CoordinatesBuilder} used by this compiler.
56    */

57   
58   private CoordinatesBuilder builder;
59   
60   // --------------------------------------------------------------------------
61
// Implementation of the BindingController interface
62
// --------------------------------------------------------------------------
63

64   public String JavaDoc[] listFc () {
65     return new String JavaDoc[] { BUILDER_BINDING };
66   }
67
68   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
69     if (BUILDER_BINDING.equals(clientItfName)) {
70       return builder;
71     }
72     return null;
73   }
74
75   public void bindFc (
76       final String JavaDoc clientItfName,
77       final Object JavaDoc serverItf)
78   {
79     if (BUILDER_BINDING.equals(clientItfName)) {
80       builder = (CoordinatesBuilder)serverItf;
81     }
82   }
83
84   public void unbindFc (final String JavaDoc clientItfName) {
85     if (BUILDER_BINDING.equals(clientItfName)) {
86       builder = null;
87     }
88   }
89   
90   // --------------------------------------------------------------------------
91
// Implementation of the Compiler interface
92
// --------------------------------------------------------------------------
93

94   public void compile (
95     List JavaDoc path,
96     ComponentContainer container,
97     TaskMap tasks,
98     Map JavaDoc context) throws ADLException
99   {
100     if (path.size() > 0 || !(container instanceof CoordinatesContainer)) {
101       return;
102     }
103     compile((CoordinatesContainer)container, container, tasks);
104   }
105   
106   private void compile (CoordinatesContainer c, ComponentContainer cc, TaskMap tasks) {
107     Coordinates[] coords = c.getCoordinatess();
108     Component[] comps = cc.getComponents();
109     for (int i = 0; i < coords.length; ++i) {
110       String JavaDoc name = coords[i].getName();
111       for (int j = 0; j < comps.length; ++j) {
112         if (comps[j].getName().equals(name)) {
113           double x0 = Double.parseDouble(coords[i].getX0());
114           double y0 = Double.parseDouble(coords[i].getY0());
115           double x1 = Double.parseDouble(coords[i].getX1());
116           double y1 = Double.parseDouble(coords[i].getY1());
117           int color = Integer.parseInt(coords[i].getColor());
118           InstanceProviderTask pt = (InstanceProviderTask)tasks.getTask("create", cc);
119           InstanceProviderTask ct = (InstanceProviderTask)tasks.getTask("create", comps[j]);
120           SetCoordinatesTask t = new SetCoordinatesTask(builder, x0, y0, x1, y1, color);
121           t.setParentInstanceProviderTask(pt);
122           t.setInstanceProviderTask(ct);
123           ComponentPair pair = new ComponentPair(cc, comps[j]);
124           t.addPreviousTask(tasks.getTask("add", pair));
125           tasks.addTask("coordinates", pair, t);
126           compile(coords[i], comps[j], tasks);
127         }
128       }
129     }
130   }
131   
132   // --------------------------------------------------------------------------
133
// Inner classes
134
// --------------------------------------------------------------------------
135

136   static class SetCoordinatesTask extends AbstractConfigurationTask {
137
138     private InstanceProviderTask parentInstanceProviderTask;
139     
140     private CoordinatesBuilder builder;
141     
142     private double x0, y0, x1, y1;
143     
144     private int color;
145     
146     public SetCoordinatesTask (
147       CoordinatesBuilder builder,
148       double x0,
149       double y0,
150       double x1,
151       double y1,
152       int color)
153     {
154       this.builder = builder;
155       this.x0 = x0;
156       this.y0 = y0;
157       this.x1 = x1;
158       this.y1 = y1;
159       this.color = color;
160     }
161     
162     public InstanceProviderTask getParentInstanceProviderTask () {
163       return parentInstanceProviderTask;
164     }
165
166     public void setParentInstanceProviderTask (InstanceProviderTask task) {
167       if (parentInstanceProviderTask != null) {
168         removePreviousTask(parentInstanceProviderTask);
169       }
170       parentInstanceProviderTask = task;
171       if (parentInstanceProviderTask != null) {
172         addPreviousTask(parentInstanceProviderTask);
173       }
174     }
175     
176     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
177       Object JavaDoc parent = getParentInstanceProviderTask().getResult();
178       Object JavaDoc component = getInstanceProviderTask().getResult();
179       builder.setCoordinates(parent, component, x0, y0, x1, y1, color, context);
180     }
181
182     public Object JavaDoc getResult() {
183       return null;
184     }
185
186     public void setResult (Object JavaDoc result) {
187     }
188   }
189 }
190
Popular Tags