ModelDescriptionBuilder.java 3.6 KB

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