2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Clement DAVID <clement.david@scilab.org>
4 * Copyright (C) 2011-2011 - Scilab Enterprises - Clement DAVID
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 package org.scilab.modules.xcos.modelica;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.io.StringWriter;
23 import java.nio.charset.Charset;
24 import java.util.logging.Logger;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import javax.swing.SwingUtilities;
29 import javax.xml.XMLConstants;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBElement;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34 import javax.xml.bind.Unmarshaller;
35 import javax.xml.stream.FactoryConfigurationError;
36 import javax.xml.validation.Schema;
37 import javax.xml.validation.SchemaFactory;
39 import org.scilab.modules.commons.ScilabConstants;
40 import org.scilab.modules.graph.utils.ScilabExported;
41 import org.scilab.modules.xcos.modelica.model.Model;
42 import org.scilab.modules.xcos.utils.XcosConstants;
43 import org.xml.sax.SAXException;
46 * Main class for modelica manipulation.
48 public final class Modelica {
49 private static final String LATIN1_ENCODING = "ISO-8859-1";
50 private static final String MODEL_CLASS_PACKAGE = "org.scilab.modules.xcos.modelica.model";
51 private static final String SCHEMA_FILENAME = "/Modelica.xsd";
53 private static Modelica instance;
55 private Marshaller marshaller;
56 private Unmarshaller unmarshaller;
59 * Default constructor.
62 final String schemaPath = ScilabConstants.SCI.getAbsolutePath() + XcosConstants.XCOS_ETC + SCHEMA_FILENAME;
64 JAXBContext jaxbContext;
66 jaxbContext = JAXBContext.newInstance(MODEL_CLASS_PACKAGE);
67 marshaller = jaxbContext.createMarshaller();
68 unmarshaller = jaxbContext.createUnmarshaller();
70 Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File(schemaPath));
72 marshaller.setSchema(schema);
73 unmarshaller.setSchema(schema);
76 * Customize the file to be handled by the xml2modelica and
79 marshaller.setProperty(Marshaller.JAXB_ENCODING, LATIN1_ENCODING);
81 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
82 } catch (JAXBException e) {
83 throw new RuntimeException(e);
84 } catch (SAXException e) {
85 Logger.getLogger(Modelica.class.getName()).severe(e.toString());
90 * @return the instance
92 public static Modelica getInstance() {
93 if (instance == null) {
94 instance = new Modelica();
100 * Setup a new modelica settings UI
103 * the initialisation file.
107 @ScilabExported(module = "xcos", filename = "Modelica.giws.xml")
108 public static void load(final String init, final String relation) {
109 SwingUtilities.invokeLater(new Runnable() {
112 ModelicaController.showDialog(new File(init), new File(relation));
122 * @return the model element
123 * @throws JAXBException
126 @SuppressWarnings("unchecked")
127 public Model load(File file) throws JAXBException {
128 InputStreamReader reader;
130 reader = new InputStreamReader(new FileInputStream(file), Charset.forName(LATIN1_ENCODING));
131 } catch (FileNotFoundException e) {
132 Logger.getLogger(Modelica.class.getName()).severe(e.toString());
135 return ((JAXBElement<Model>) unmarshaller.unmarshal(reader)).getValue();
139 * Save the model into the file
142 * the root of the model to save
145 * @throws JAXBException
148 public void save(Model root, File file) throws JAXBException {
150 final StringWriter strw = new StringWriter();
151 marshaller.marshal(new org.scilab.modules.xcos.modelica.model.ObjectFactory().createModel(unmerge(root)), strw);
154 * Customize the file to be handled by the xml2modelica tool
156 final StringBuffer buffer = strw.getBuffer();
157 final String newline = System.getProperty("line.separator");
159 Pattern pat = Pattern.compile("(/\\w*>)(<[\\w/])");
160 Matcher m = pat.matcher(buffer);
162 final int index = m.end(1);
163 buffer.insert(index, newline);
166 buffer.append(newline);
168 new FileOutputStream(file).write(strw.toString().getBytes());
169 } catch (FactoryConfigurationError e) {
170 Logger.getLogger(Modelica.class.getName()).severe(e.toString());
171 } catch (IOException e) {
172 Logger.getLogger(Modelica.class.getName()).severe(e.toString());
177 * Merge the models into a single shared model
180 * the initialization model
181 * @param relationModel
183 * @return a new model
185 public Model merge(Model initModel, Model relationModel) {
186 // merge the relation model into the init model
188 initModel.setIdentifiers(relationModel.getIdentifiers());
189 initModel.setModelInfo(relationModel.getModelInfo());
191 initModel.setExplicitRelations(relationModel.getExplicitRelations());
192 initModel.setImplicitRelations(relationModel.getImplicitRelations());
194 initModel.setOutputs(relationModel.getOutputs());
203 * @return the unmerged model
205 public Model unmerge(Model initModel) {
206 final Model model = new Model();
211 model.setName(initModel.getName());
213 model.setElements(initModel.getElements());
214 model.setEquations(initModel.getEquations());
215 model.setWhenClauses(initModel.getWhenClauses());