123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package be.uantwerpen.ansymo.semanticadaptation.cg.cpp
- import java.io.File
- import java.util.zip.ZipFile
- import java.util.Enumeration
- import java.util.zip.ZipEntry
- import org.w3c.dom.Document
- import javax.xml.parsers.DocumentBuilderFactory
- import javax.xml.parsers.DocumentBuilder
- import javax.xml.xpath.XPathExpression
- import javax.xml.xpath.XPathFactory
- import javax.xml.xpath.XPath
- import javax.xml.soap.Node
- import javax.xml.xpath.XPathConstants
- import org.w3c.dom.NodeList
- import java.util.LinkedHashMap
- import javax.lang.model.element.Element
- class ModelDescription {
- private final Document md;
- private final String name;
- private var LinkedHashMap<String, Pair<String, Integer>> svDefs = newLinkedHashMap();
- private var LinkedHashMap<String, ScalarVariable> svs = newLinkedHashMap();
- new(String name, File path) {
- this.name = name;
- var ZipFile fmu = new ZipFile(path);
- var Enumeration<? extends ZipEntry> entries = fmu.entries();
- var boolean entryFound = false;
- var ZipEntry locatedEntry;
- while (!entryFound && entries.hasMoreElements()) {
- var ZipEntry entry = entries.nextElement();
- if (entry.name.equalsIgnoreCase("modelDescription.xml"))
- locatedEntry = entry;
- }
- var DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
- var DocumentBuilder builder = fac.newDocumentBuilder();
- val is = fmu.getInputStream(locatedEntry)
- this.md = builder.parse(is);
- is.close();
- calcScalars();
- }
- private def calcScalars() {
- val XPathFactory xPathfactory = XPathFactory.newInstance();
- val XPath xpath = xPathfactory.newXPath();
- val XPathExpression expr = xpath.compile("/fmiModelDescription/ModelVariables/ScalarVariable");
- val NodeList nl = expr.evaluate(this.md, XPathConstants.NODESET) as NodeList;
- for (var int i = 0; i < nl.length; i++) {
- val node = nl.item(i);
- val nodeName = node.attributes.getNamedItem("name").nodeValue;
- val valueRef = node.attributes.getNamedItem("valueReference").nodeValue;
- val name = this.name + nodeName;
- val define = name.toUpperCase;
- this.svDefs.put(name, define -> Integer.parseInt(valueRef));
- val sv = ScalarVariable.Create().setCausality(
- SVCausality.valueOf(node.attributes.getNamedItem("causality").nodeValue)).setName(nodeName).setOwner(
- this.name).setValueReference(valueRef);
- for (var j = 0; j < node.childNodes.length; j++) {
- val subNode = node.childNodes.item(j);
- if (subNode.nodeType == Node.ELEMENT_NODE) {
- val startAttribute = subNode.attributes.getNamedItem("start");
- if (startAttribute !== null) {
- sv.start = startAttribute.nodeValue;
- }
- sv.type = SVType.valueOf(subNode.nodeName);
- this.svs.put(nodeName, sv);
- }
- }
- }
- }
- public def getName() {
- return this.name;
- }
- public def getSvDef() {
- return this.svDefs;
- }
- public def getSv() { return this.svs; }
- }
|