ModelDescription.xtend 3.5 KB

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