ModelDescriptionBuilder.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation;
  2. import java.io.StringWriter;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import javax.xml.transform.OutputKeys;
  7. import javax.xml.transform.Transformer;
  8. import javax.xml.transform.TransformerFactory;
  9. import javax.xml.transform.dom.DOMSource;
  10. import javax.xml.transform.stream.StreamResult;
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Element;
  13. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVCausality;
  14. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVType;
  15. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.ScalarVariable;
  16. public class ModelDescriptionBuilder {
  17. private DocumentBuilderFactory docFactory;
  18. private DocumentBuilder docBuilder;
  19. private Document doc;
  20. private Element rootElement;
  21. private Element modelVariables;
  22. private Element outputs;
  23. private int valueReferenceCounter = 1;
  24. public ModelDescriptionBuilder() throws ParserConfigurationException {
  25. docFactory = DocumentBuilderFactory.newInstance();
  26. docBuilder = docFactory.newDocumentBuilder();
  27. doc = docBuilder.newDocument();
  28. doc.setXmlStandalone(true);
  29. }
  30. public void CreateTemplate(String modelName, String guid) throws ParserConfigurationException {
  31. rootElement = doc.createElement("fmiModelDescription");
  32. doc.appendChild(rootElement);
  33. rootElement.setAttribute("fmiVersion", "2.0");
  34. rootElement.setAttribute("modelName", modelName);
  35. rootElement.setAttribute("guid", guid);
  36. rootElement.setAttribute("variableNamingConvention", "flat");
  37. modelVariables = doc.createElement("ModelVariables");
  38. rootElement.appendChild(modelVariables);
  39. outputs = doc.createElement("Outputs");
  40. rootElement.appendChild(outputs);
  41. }
  42. public String getNextValueReference() {
  43. return Integer.toString(valueReferenceCounter++);
  44. }
  45. public void addScalarVariable(ScalarVariable var) {
  46. Element sv = doc.createElement("ScalarVariable");
  47. modelVariables.appendChild(sv);
  48. if (var.getName() != null)
  49. sv.setAttribute("name", var.getName());
  50. if (var.getValueReference() != null)
  51. sv.setAttribute("valueReference", var.getValueReference());
  52. if (var.getDescription() != null)
  53. sv.setAttribute("description", var.getDescription());
  54. if (var.getVariability() != null)
  55. sv.setAttribute("variability", var.getVariability().name());
  56. if (var.getCausality() != null) {
  57. sv.setAttribute("causality", var.getCausality().name());
  58. if (var.getCausality() == SVCausality.output) {
  59. outputs.appendChild(createUnknown(Integer.toString(modelVariables.getChildNodes().getLength())));
  60. }
  61. }
  62. if (var.getType() != null) {
  63. Element svType = getSvType(var.getType());
  64. sv.appendChild(svType);
  65. if (var.getStart() != null) {
  66. svType.setAttribute("start", var.getStart());
  67. }
  68. }
  69. }
  70. private Element createUnknown(String index) {
  71. Element e = doc.createElement("Unknown");
  72. e.setAttribute("index", index);
  73. return e;
  74. }
  75. private Element getSvType(SVType type) {
  76. return doc.createElement(type.name());
  77. }
  78. @Override
  79. public String toString() {
  80. try {
  81. StringWriter sw = new StringWriter();
  82. TransformerFactory tf = TransformerFactory.newInstance();
  83. Transformer transformer = tf.newTransformer();
  84. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  85. transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  86. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  87. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  88. transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
  89. transformer.transform(new DOMSource(doc), new StreamResult(sw));
  90. return sw.toString();
  91. } catch (Exception ex) {
  92. throw new RuntimeException("Error converting to String", ex);
  93. }
  94. }
  95. }