ModelDescription.xtend 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp
  2. import java.io.File
  3. import java.util.zip.ZipFile
  4. import java.util.Enumeration
  5. import java.util.zip.ZipEntry
  6. import org.w3c.dom.Document
  7. import javax.xml.parsers.DocumentBuilderFactory
  8. import javax.xml.parsers.DocumentBuilder
  9. import javax.xml.xpath.XPathExpression
  10. import javax.xml.xpath.XPathFactory
  11. import javax.xml.xpath.XPath
  12. import javax.xml.soap.Node
  13. import javax.xml.xpath.XPathConstants
  14. import org.w3c.dom.NodeList
  15. import java.util.LinkedHashMap
  16. import javax.lang.model.element.Element
  17. class ModelDescription {
  18. private final Document md;
  19. private final String name;
  20. private var LinkedHashMap<String, Pair<String, Integer>> svDefs = newLinkedHashMap();
  21. private var LinkedHashMap<String, ScalarVariable> svs = newLinkedHashMap();
  22. new(String name, File path) {
  23. this.name = name;
  24. var ZipFile fmu = new ZipFile(path);
  25. var Enumeration<? extends ZipEntry> entries = fmu.entries();
  26. var boolean entryFound = false;
  27. var ZipEntry locatedEntry;
  28. while (!entryFound && entries.hasMoreElements()) {
  29. var ZipEntry entry = entries.nextElement();
  30. if (entry.name.equalsIgnoreCase("modelDescription.xml"))
  31. locatedEntry = entry;
  32. }
  33. var DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
  34. var DocumentBuilder builder = fac.newDocumentBuilder();
  35. val is = fmu.getInputStream(locatedEntry)
  36. this.md = builder.parse(is);
  37. is.close();
  38. calcScalars();
  39. }
  40. private def calcScalars() {
  41. val XPathFactory xPathfactory = XPathFactory.newInstance();
  42. val XPath xpath = xPathfactory.newXPath();
  43. val XPathExpression expr = xpath.compile("/fmiModelDescription/ModelVariables/ScalarVariable");
  44. val NodeList nl = expr.evaluate(this.md, XPathConstants.NODESET) as NodeList;
  45. for (var int i = 0; i < nl.length; i++) {
  46. val node = nl.item(i);
  47. val nodeName = node.attributes.getNamedItem("name").nodeValue;
  48. val valueRef = node.attributes.getNamedItem("valueReference").nodeValue;
  49. val name = this.name + nodeName;
  50. val define = name.toUpperCase;
  51. this.svDefs.put(name, define -> Integer.parseInt(valueRef));
  52. val sv = ScalarVariable.Create().setCausality(
  53. SVCausality.valueOf(node.attributes.getNamedItem("causality").nodeValue)).setName(nodeName).setOwner(
  54. this.name).setValueReference(valueRef);
  55. for (var j = 0; j < node.childNodes.length; j++) {
  56. val subNode = node.childNodes.item(j);
  57. if (subNode.nodeType == Node.ELEMENT_NODE) {
  58. val startAttribute = subNode.attributes.getNamedItem("start");
  59. if (startAttribute !== null) {
  60. sv.start = startAttribute.nodeValue;
  61. }
  62. sv.type = SVType.valueOf(subNode.nodeName);
  63. this.svs.put(nodeName, sv);
  64. }
  65. }
  66. }
  67. }
  68. public def getName() {
  69. return this.name;
  70. }
  71. public def getSvDef() {
  72. return this.svDefs;
  73. }
  74. public def getSv() { return this.svs; }
  75. }