1 6 7 package SOFA.Connector.ECG.SGenerator; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 import SOFA.Connector.ECG.VariableExpander; 14 15 20 public class SGenerator implements SOFA.Connector.ECG.ECGPluginInterface { 21 22 protected java.util.LinkedList connectors; 23 24 protected javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory; 25 protected javax.xml.parsers.DocumentBuilder documentBuilder; 26 27 protected Document configDoc; 28 29 protected java.util.HashMap props; 30 31 protected SOFA.Connector.EEG.EEGenerator eeGenerator; 32 33 34 public SGenerator(SOFA.Connector.EEG.EEGenerator eeGenerator, java.util.HashMap props) throws SOFA.Connector.ECG.ECGPluginException { 35 this.props=props; 36 this.eeGenerator=eeGenerator; 37 38 try { 39 documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance(); 40 documentBuilder=documentBuilderFactory.newDocumentBuilder(); 41 42 String confFile=(String )props.get("configuration"); 43 if (confFile==null) { 44 confFile="SGenerator/conf.xml"; 45 } 46 confFile=SOFA.Connector.ECG.ECGenerator.getECGConfigRoot()+"/"+confFile; 47 configDoc=documentBuilder.parse(new java.io.File (confFile)); 48 49 connectors=new java.util.LinkedList (); 50 51 Element elParent=configDoc.getDocumentElement(); 52 Node elPlugins, elPlugin, elUnit, elElement, elProp; 53 Element elc, elu, ele, elp; 54 java.util.Iterator iter; 55 int i; 56 57 System.out.println(" Loading connectors specification:"); 58 elPlugins=elParent.getFirstChild(); 59 while (elPlugins!=null) { 60 if (elPlugins.getNodeType()==Node.ELEMENT_NODE && elPlugins.getNodeName().equals("Connectors")) { 61 elPlugin=elPlugins.getFirstChild(); 62 while (elPlugin!=null) { 63 if (elPlugin.getNodeType()==Node.ELEMENT_NODE && elPlugin.getNodeName().equals("Connector")) { 64 elc=(Element )elPlugin; 65 66 if (!(elc.hasAttribute("name") && elc.hasAttribute("type") && elc.hasAttribute("impl"))) { 67 throw new SOFA.Connector.ECG.ECGPluginException("Missing attributes in connector declaration"); 68 } 69 70 ConnectorArchitecture ca=new ConnectorArchitecture(); 71 ca.type=elc.getAttribute("type"); 72 ca.name=elc.getAttribute("name"); 73 ca.implementation=elc.getAttribute("impl"); 74 75 System.out.println(" Connector '"+ca.name+"' ("+ca.type+")"); 76 77 java.util.LinkedList units=new java.util.LinkedList (); 78 79 elUnit=elPlugin.getFirstChild(); 80 while (elUnit!=null) { 81 if (elUnit.getNodeType()==Node.ELEMENT_NODE && elUnit.getNodeName().equals("unit")) { 82 elu=(Element )elUnit; 83 84 if (!elu.hasAttribute("name")) { 85 throw new SOFA.Connector.ECG.ECGPluginException("Missing 'name' attributes in unit declaration"); 86 } 87 88 ConnectorUnit cu=new ConnectorUnit(); 89 cu.unitName=elu.getAttribute("name"); 90 if (elu.hasAttribute("provides")) { 91 cu.provides=elu.getAttribute("provides"); 92 } 93 if (elu.hasAttribute("requires")) { 94 cu.requires=elu.getAttribute("requires"); 95 } 96 97 System.out.println(" Unit '"+cu.unitName+"'"); 98 99 java.util.LinkedList elements=new java.util.LinkedList (); 100 java.util.LinkedList pars=new java.util.LinkedList (); 101 102 elElement=elUnit.getFirstChild(); 103 while (elElement!=null) { 104 if (elElement.getNodeType()==Node.ELEMENT_NODE) { 105 ele=(Element )elElement; 106 if (ele.getNodeName().equals("element")) { 107 108 if (!(ele.hasAttribute("name") && ele.hasAttribute("type"))) { 109 throw new SOFA.Connector.ECG.ECGPluginException("Missing attributes in element declaration"); 110 } 111 112 ConnectorElement ce=new ConnectorElement(); 113 ce.instanceName=ele.getAttribute("name"); 114 ce.type=ele.getAttribute("type"); 115 ce.required=!ele.getAttribute("required").equals("no"); 116 117 System.out.println(" Element '"+ce.instanceName+"' ("+ce.type+")"); 118 119 java.util.LinkedList eProps=new java.util.LinkedList (); 120 121 elProp=elElement.getFirstChild(); 122 while (elProp!=null) { 123 if (elProp.getNodeType()==Node.ELEMENT_NODE && elProp.getNodeName().equals("property")) { 124 elp=(Element )elProp; 125 126 if (elp.hasAttribute("name") && elp.hasAttribute("value")) { 127 eProps.add(new SOFA.Connector.Property(elp.getAttribute("name"),elp.getAttribute("value"))); 128 } 129 } 130 elProp=elProp.getNextSibling(); 131 } 132 133 ce.props=new SOFA.Connector.Property[eProps.size()]; 134 iter=eProps.iterator(); i=0; 135 while (iter.hasNext()) { 136 ce.props[i]=(SOFA.Connector.Property)iter.next(); 137 i++; 138 } 139 elements.add(ce); 140 } else if (ele.getNodeName().equals("param") && ele.hasAttribute("name") && ele.hasAttribute("value")) { 141 pars.add(new SOFA.Connector.Property(ele.getAttribute("name"),ele.getAttribute("value"))); 142 } 143 } 144 145 elElement=elElement.getNextSibling(); 146 } 147 148 cu.elements=new ConnectorElement[elements.size()]; 149 iter=elements.iterator(); 150 for (i=0;iter.hasNext();i++) { 151 cu.elements[i]=(ConnectorElement)iter.next(); 152 } 153 154 cu.params=new SOFA.Connector.Property[pars.size()]; 155 iter=pars.iterator(); 156 for (i=0;iter.hasNext();i++) { 157 cu.params[i]=(SOFA.Connector.Property)iter.next(); 158 } 159 160 units.add(cu); 161 } 162 elUnit=elUnit.getNextSibling(); 163 } 164 165 ca.units=new ConnectorUnit[units.size()]; 166 iter=units.iterator(); i=0; 167 while (iter.hasNext()) { 168 ca.units[i]=(ConnectorUnit)iter.next(); 169 i++; 170 } 171 172 connectors.add(ca); 173 } 174 elPlugin=elPlugin.getNextSibling(); 175 } 176 } 177 elPlugins=elPlugins.getNextSibling(); 178 } 179 180 } catch (Exception e) { 181 throw new SOFA.Connector.ECG.ECGPluginException("Can't parse configuration",e); 182 } 183 } 184 185 public SOFA.Connector.ECG.ConnectorTechnologyDescriptor[] canGenerate(SOFA.Connector.ECG.ConnectorQuery query) { 186 java.util.LinkedList cTDescr=new java.util.LinkedList (); java.util.Iterator conIter=connectors.iterator(); 188 189 while (conIter.hasNext()) { 190 ConnectorArchitecture ca=(ConnectorArchitecture)conIter.next(); 191 connectorProcessing: { 192 try { 193 if (query.type.equals(ca.type)) { 194 SOFA.Connector.ECG.ConnectorTechnologyDescriptor ctd=new SOFA.Connector.ECG.ConnectorTechnologyDescriptor(); 195 ctd.name=ca.name; 196 ctd.implementation=getName()+":"+ca.name; 197 ctd.props=new SOFA.Connector.Property[0]; 198 199 SOFA.Connector.ECG.VariableExpander expander=new SOFA.Connector.ECG.VariableExpander(); 200 ConnectorUnit unit=null; 201 int j, k; 202 203 for (j=0;j<ca.units.length;j++) { 204 ConnectorUnit cu=ca.units[j]; 205 206 for (k=0;k<query.provides.length;k++) { 207 if (query.provides[k].unit.equals(cu.unitName)) { 208 if (cu.provides!=null) { 209 String varName=VariableExpander.extractVariable(cu.provides); 210 if (varName!=null) { 211 expander.addSubst(varName,query.provides[k].iface); 212 } else if (!query.provides[k].iface.equals(cu.provides)) { 213 break connectorProcessing; 214 } 215 } 216 } 217 } 218 219 for (k=0;k<query.requires.length;k++) { 220 if (query.requires[k].unit.equals(cu.unitName)) { 221 if (cu.requires!=null) { 222 String varName=VariableExpander.extractVariable(cu.requires); 223 if (varName!=null) { 224 expander.addSubst(varName,query.requires[k].iface); 225 } else if (!query.requires[k].iface.equals(cu.requires)) { 226 break connectorProcessing; 227 } 228 } 229 } 230 } 231 232 if (cu.unitName.equals(query.unit)) { 233 unit=cu; 234 } 235 } 236 if (unit==null) { 237 break connectorProcessing; 238 } 239 240 for (j=0;j<query.props.length;j++) { 241 try { 242 expander.addSubst(query.props[j].name,query.props[j].value); 243 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 244 } 245 } 246 247 java.util.LinkedList elsTD=new java.util.LinkedList (); 249 for (j=0;j<unit.elements.length;j++) { 250 ConnectorElement ce=unit.elements[j]; 251 SOFA.Connector.EEG.ElementQuery eQuery=new SOFA.Connector.EEG.ElementQuery(); 252 eQuery.instanceName=ce.instanceName; 253 eQuery.type=ce.type; 254 eQuery.props=new SOFA.Connector.Property[ce.props.length]; 255 for (k=0;k<ce.props.length;k++) { 256 eQuery.props[k]=new SOFA.Connector.Property(ce.props[k].name,expander.expand(ce.props[k].value)); 257 } 258 259 elsTD.add(eeGenerator.canGenerate(eQuery)); 260 } 261 262 ctd.elementTechnologies=new SOFA.Connector.EEG.ElementsTechnologyDescriptor[elsTD.size()]; 263 java.util.Iterator iter=elsTD.iterator(); j=0; 264 while (iter.hasNext()) { 265 ctd.elementTechnologies[j]=(SOFA.Connector.EEG.ElementsTechnologyDescriptor)iter.next(); 266 j++; 267 } 268 cTDescr.add(ctd); 269 } 270 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 271 System.out.println("Can't use connector '"+ca.name+"' ("+ca.type+"):"); 272 System.out.println(e.getMessage()); 273 System.out.println(e.getCause()); 274 e.printStackTrace(); 275 } 276 } 277 } 278 279 int i; 280 SOFA.Connector.ECG.ConnectorTechnologyDescriptor[] ret=new SOFA.Connector.ECG.ConnectorTechnologyDescriptor[cTDescr.size()]; 281 java.util.Iterator iter=cTDescr.iterator(); i=0; 282 while (iter.hasNext()) { 283 ret[i]=(SOFA.Connector.ECG.ConnectorTechnologyDescriptor)iter.next(); 284 i++; 285 } 286 287 return ret; 288 } 289 290 public SOFA.Connector.ECG.ConnectorOutputDescriptor generate(SOFA.Connector.ECG.ConnectorInputDescriptor cDescr) throws SOFA.Connector.ECG.ECGPluginException { 291 String impl; 292 int i, j, k; 293 294 i=cDescr.implementation.indexOf(':'); 295 try { 296 if (i==-1 || !cDescr.implementation.substring(0,i).equals(getName())) { 297 throw new SOFA.Connector.ECG.ECGPluginException("Malformed implementation specification."); 298 } 299 impl=cDescr.implementation.substring(i+1); 300 } catch (Exception e) { 301 throw new SOFA.Connector.ECG.ECGPluginException("Malformed implementation specification."); 302 } 303 304 java.util.Iterator conIter=connectors.iterator(); 305 306 while (conIter.hasNext()) { 307 ConnectorArchitecture ca=(ConnectorArchitecture)conIter.next(); 308 if (impl.equals(ca.name)) { 309 SOFA.Connector.ECG.VariableExpander expander=new SOFA.Connector.ECG.VariableExpander(); 310 ConnectorUnit unit=null; 311 312 for (j=0;j<ca.units.length;j++) { 313 ConnectorUnit cu=ca.units[j]; 314 for (k=0;k<cDescr.provides.length;k++) { 315 if (cDescr.provides[k].unit.equals(cu.unitName)) { 316 if (cu.provides!=null) { 317 String varName=VariableExpander.extractVariable(cu.provides); 318 if (varName!=null) { 319 try { 320 expander.addSubst(varName,cDescr.provides[k].iface); 321 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 322 throw new SOFA.Connector.ECG.ECGPluginException("Can't assign interfaces to elements.",e); 323 } 324 } else if (!cDescr.provides[k].iface.equals(cu.provides)) { 325 throw new SOFA.Connector.ECG.ECGPluginException("Connector is incompatible"); 326 } 327 } 328 } 329 } 330 331 for (k=0;k<cDescr.requires.length;k++) { 332 if (cDescr.requires[k].unit.equals(cu.unitName)) { 333 if (cu.requires!=null) { 334 String varName=VariableExpander.extractVariable(cu.requires); 335 if (varName!=null) { 336 try { 337 expander.addSubst(varName,cDescr.requires[k].iface); 338 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 339 throw new SOFA.Connector.ECG.ECGPluginException("Can't assign interfaces to elements.",e); 340 } 341 } else if (!cDescr.requires[k].iface.equals(cu.requires)) { 342 throw new SOFA.Connector.ECG.ECGPluginException("Connector is incompatible"); 343 } 344 } 345 } 346 } 347 348 if (cu.unitName.equals(cDescr.unit)) { 349 unit=cu; 350 } 351 } 352 if (unit==null) { 353 throw new SOFA.Connector.ECG.ECGPluginException("Can't find unit '"+cDescr.unit+"' in connector architecture."); 354 } 355 356 for (j=0;j<cDescr.props.length;j++) { 357 try { 358 expander.addSubst(cDescr.props[j].name,cDescr.props[j].value); 359 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 360 } 361 } 362 363 SOFA.Connector.EEG.ElementInputDescriptor eDes=null; 364 java.util.LinkedList eODes=new java.util.LinkedList (); try { 366 for (j=0;j<unit.elements.length;j++) { 367 ConnectorElement ce=unit.elements[j]; 368 369 boolean elementFound=false; 370 for (k=0;k<cDescr.elements.length;k++) { 371 eDes=cDescr.elements[k]; 372 if (eDes.instanceName.equals(ce.instanceName)) { 373 elementFound=true; 374 java.util.LinkedList eProps=new java.util.LinkedList (); 375 for (i=0;i<ce.props.length;i++) { 376 try { 377 eProps.add(new SOFA.Connector.Property(ce.props[i].name,expander.expand(ce.props[i].value))); 378 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 379 } 380 } 381 382 try { 383 eProps.add(new SOFA.Connector.Property("resolverFile",expander.expand("{resolverFile}"))); 384 } catch (SOFA.Connector.ECG.VariableExpanderException e) { 385 } 386 387 SOFA.Connector.Property[] oldEProps=eDes.props; 388 eDes.props=new SOFA.Connector.Property[eProps.size()+oldEProps.length]; 389 for (i=0;i<oldEProps.length;i++) { 390 eDes.props[i]=oldEProps[i]; 391 } 392 java.util.Iterator iter=eProps.iterator(); 393 while (iter.hasNext()) { 394 eDes.props[i]=(SOFA.Connector.Property)iter.next(); 395 i++; 396 } 397 398 eODes.add(eeGenerator.generate(eDes)); 399 } 400 } 401 402 if (!elementFound && ce.required) { 403 throw new SOFA.Connector.ECG.ECGPluginException("Can't find element '"+ce.instanceName+"' in input descriptor."); 404 } 405 } 406 } catch (SOFA.Connector.EEG.EEGeneratorException e) { 407 throw new SOFA.Connector.ECG.ECGPluginException("Can't generate element '"+eDes.instanceName+"'.",e); 408 } 409 410 SOFA.Connector.ECG.ConnectorOutputDescriptor ret=new SOFA.Connector.ECG.ConnectorOutputDescriptor(); 411 412 ret.name=cDescr.instanceName; 413 ret.unit=cDescr.unit; 414 ret.impl=ca.implementation; 415 416 ret.params=new SOFA.Connector.Property[unit.params.length+cDescr.props.length]; 417 try { 418 for (j=0;j<unit.params.length;j++) { 419 ret.params[j]=new SOFA.Connector.Property(unit.params[j].name,expander.expand(unit.params[j].value)); 420 } 421 } catch (SOFA.Connector.ECG.VariableExpanderUnresolvedNameException e) { 422 throw new SOFA.Connector.ECG.ECGPluginException("Can't expand variable '"+unit.params[j].name+"'.",e); 423 } 424 for (j=0;j<cDescr.props.length;j++) { 425 ret.params[j+unit.params.length]=cDescr.props[j]; 426 } 427 428 java.util.Iterator iter=eODes.iterator(); 429 ret.elements=new SOFA.Connector.EEG.ElementOutputDescriptor[eODes.size()]; 430 for (j=0;iter.hasNext();j++) { 431 ret.elements[j]=(SOFA.Connector.EEG.ElementOutputDescriptor)iter.next(); 432 } 433 434 return ret; 435 } 436 } 437 throw new SOFA.Connector.ECG.ECGPluginException("Connector implementation '"+cDescr.implementation+"' not found."); 438 } 439 440 public String getName() { 441 return "SGenerator"; 442 } 443 444 } 445 | Popular Tags |