PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ptolemy/backtrack/ui/BacktrackTransformer.java

https://github.com/ptII/ptII
Java | 337 lines | 153 code | 29 blank | 155 comment | 35 complexity | 68d91a299a16d9dd60b799ebf5fc6407 MD5 | raw file
  1. /* Model transformer for backtracking.
  2. Copyright (c) 2005-2011 The Regents of the University of California.
  3. All rights reserved.
  4. Permission is hereby granted, without written agreement and without
  5. license or royalty fees, to use, copy, modify, and distribute this
  6. software and its documentation for any purpose, provided that the above
  7. copyright notice and the following two paragraphs appear in all copies
  8. of this software.
  9. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  10. FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  11. ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  12. THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  13. SUCH DAMAGE.
  14. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  17. PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  18. CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
  19. ENHANCEMENTS, OR MODIFICATIONS.
  20. PT_COPYRIGHT_VERSION_2
  21. COPYRIGHTENDKEY
  22. */
  23. package ptolemy.backtrack.ui;
  24. import java.io.InputStreamReader;
  25. import java.io.Reader;
  26. import java.io.StringWriter;
  27. import java.net.URL;
  28. import java.util.Iterator;
  29. import java.util.LinkedList;
  30. import java.util.List;
  31. import java.util.Stack;
  32. import ptolemy.kernel.util.IllegalActionException;
  33. import ptolemy.kernel.util.NamedObj;
  34. import ptolemy.moml.filter.MoMLFilterSimple;
  35. import ptolemy.moml.MoMLParser;
  36. ///////////////////////////////////////////////////////////////////
  37. //// BacktrackTransformer
  38. /**
  39. Model transformer for backtracking. This class parses the MoML of the given
  40. model, and applies a renaming filter to the MoML. The actors with their
  41. backtracking versions will be replaced. Actors without existing backtracking
  42. versions will remain unchanged. Other entities in the model, such as
  43. relations between ports, are kept. The resulting model supports backtracking
  44. for the transformed actors.
  45. @author Thomas Feng
  46. @version $Id$
  47. @since Ptolemy II 5.1
  48. @Pt.ProposedRating Red (tfeng)
  49. @Pt.AcceptedRating Red (tfeng)
  50. */
  51. public class BacktrackTransformer {
  52. /** Transform a model by replacing the actors with existing backtracking
  53. * versions, and return the resulting model.
  54. *
  55. * Each transformed actor has a "_decorate" attribute. It adds a small
  56. * backtracking decoration to the actor's icon.
  57. *
  58. * @param model The model to be transformed.
  59. * @return The transformed model.
  60. * @exception IllegalActionException If the parser fails to parse the
  61. * transformed model.
  62. */
  63. public static NamedObj transformModel(NamedObj model)
  64. throws IllegalActionException {
  65. StringWriter moml = new StringWriter();
  66. try {
  67. model.exportMoML(moml, 0);
  68. MoMLParser parser = new MoMLParser();
  69. RenameClassMoMLFilter filter = new RenameClassMoMLFilter();
  70. MoMLParser.addMoMLFilter(filter);
  71. NamedObj topLevel = parser.parse(null, moml.toString());
  72. MoMLParser.getMoMLFilters().remove(filter);
  73. Iterator entitiesToRename = filter.entitiesChanged();
  74. while (entitiesToRename.hasNext()) {
  75. NamedObj entity = (NamedObj) entitiesToRename.next();
  76. // Add a little visual effect to the transformed entity.
  77. String imageMoML = "<property name=\"_decorate\" "
  78. + "class=\"ptolemy.data.expr.FileParameter\" "
  79. + "value=\"$CLASSPATH/ptolemy/backtrack/manual/ptolemy/"
  80. + "actor/lib/BacktrackIconSmall.gif\">\n"
  81. + "</property>";
  82. parser.setContext(entity);
  83. try {
  84. parser.parse(null, imageMoML);
  85. MoMLParser.setModified(true);
  86. } catch (Exception ex) {
  87. throw new IllegalActionException("Unable to parse\n"
  88. + imageMoML);
  89. }
  90. /* Do not modify the actor names any more.
  91. // Add "(B)" to the end of the entity's name.
  92. String oldName = entity.getName();
  93. try {
  94. entity.setName(oldName + " (B)");
  95. } catch (NameDuplicationException ex1) {
  96. int i = 0;
  97. while (true) {
  98. try {
  99. entity.setName(oldName + " (B" + i + ")");
  100. break;
  101. } catch (NameDuplicationException ex2) {
  102. // Ignore; try another name.
  103. }
  104. i++;
  105. }
  106. }*/
  107. }
  108. return topLevel;
  109. } catch (Exception e) {
  110. throw new IllegalActionException(e.toString());
  111. }
  112. }
  113. ///////////////////////////////////////////////////////////////////
  114. //// RenameClassMoMLFilter
  115. /**
  116. The MoML filter that renames the actor classes in the model, if there are
  117. backtracking versions for them. No change is done on the actor classes
  118. that do not have backtracking versions.
  119. @author Thomas Feng
  120. @version $Id$
  121. @since Ptolemy II 5.1
  122. @Pt.ProposedRating Red (tfeng)
  123. @Pt.AcceptedRating Red (tfeng)
  124. */
  125. private static class RenameClassMoMLFilter extends MoMLFilterSimple {
  126. /** Return the entities that are changed during the last XML parsing.
  127. *
  128. * @return The entities that are changed.
  129. */
  130. public Iterator<NamedObj> entitiesChanged() {
  131. return _entitiesChanged.iterator();
  132. }
  133. /** Filter the value of the attribute. If the attribute corresponds to
  134. * a Ptolemy actor with a backtracking version, its class name is
  135. * changed to the class name of its backtracking version.
  136. *
  137. * @param container The container of the attribute.
  138. * @param element The XML element.
  139. * @param attributeName The attribute name.
  140. * @param attributeValue The attribute value.
  141. * @param xmlFile The file currently being parsed.
  142. */
  143. public String filterAttributeValue(NamedObj container, String element,
  144. String attributeName, String attributeValue, String xmlFile) {
  145. if (attributeValue == null) {
  146. return null;
  147. }
  148. if (attributeName.equals("class")) {
  149. if (element.equals("entity")) {
  150. String classAfterChange = _newClassName(attributeValue);
  151. if (classAfterChange != null) {
  152. MoMLParser.setModified(true);
  153. String classBeforeChange = attributeValue;
  154. _classStack.push(classBeforeChange);
  155. return classAfterChange;
  156. } else {
  157. _classStack.push(null);
  158. return attributeValue;
  159. }
  160. } else if (element.equals("property")) {
  161. if (_classStack.size() > 0 && _classStack.peek() == null) {
  162. _classStack.push(null);
  163. return attributeValue;
  164. }
  165. String newClassName = _newClassName(attributeValue);
  166. if (newClassName != null) {
  167. _classStack.push(attributeValue);
  168. return newClassName;
  169. } else {
  170. _classStack.push(null);
  171. return attributeValue;
  172. }
  173. } else {
  174. return attributeValue;
  175. }
  176. } else {
  177. return attributeValue;
  178. }
  179. }
  180. /** Further process the XML element when it is closed with an end tag.
  181. * If the element corresponds to a Ptolemy actor that has been changed
  182. * to its backtracking version, the MoML description of the original
  183. * actor's icon is copied to the new model, because the backtracking
  184. * version does not have an icon associated with it.
  185. *
  186. * @param container The container of the element.
  187. * @param elementName The XML element to be closed.
  188. * @param currentCharData The character data, which appears
  189. * only in the doc and configure elements
  190. * @param xmlFile The file currently being parsed.
  191. * @exception IllegalActionException If the MoML of the original
  192. * actor's icon cannot be read and inserted into the new model.
  193. */
  194. public void filterEndElement(NamedObj container, String elementName,
  195. StringBuffer currentCharData, String xmlFile)
  196. throws IllegalActionException {
  197. if ((elementName.equals("entity") || elementName.equals("property"))
  198. && container != null && container.getClassName() != null) {
  199. if (_classStack.peek() != null
  200. && container.getClassName().equals(
  201. _newClassName(_classStack.peek()))) {
  202. // Copy the original icon to the MoML.
  203. _copyIcon(container);
  204. // Add "(B)" to the actor's name later.
  205. _entitiesChanged.add(container);
  206. }
  207. _classStack.pop();
  208. }
  209. }
  210. /** The prefix to the automatically generated backtracking version of
  211. * actors.
  212. */
  213. public static final String AUTOMATIC_PREFIX = "ptolemy.backtrack.automatic";
  214. /** The prefix to the manually written backtracking version of actors.
  215. */
  216. public static final String MANUAL_PREFIX = "ptolemy.backtrack.manual";
  217. /** Test whether a class with the given name can be found.
  218. *
  219. * @param className The name of the class.
  220. * @return true if the class is found; false, otherwise.
  221. */
  222. private static boolean _classExists(String className) {
  223. try {
  224. Class.forName(className);
  225. return true;
  226. } catch (ClassNotFoundException e) {
  227. return false;
  228. }
  229. }
  230. /** Copy the icon of the last modified class (of a Ptolemy actor) to the
  231. * MoML within the container's context.
  232. *
  233. * @param container The container.
  234. * @exception IllegalActionException If the parsing is not successful.
  235. */
  236. private void _copyIcon(NamedObj container)
  237. throws IllegalActionException {
  238. String iconFileName = (_classStack.peek()).replace('.', '/')
  239. + "Icon.xml";
  240. URL iconFile = getClass().getClassLoader()
  241. .getResource(iconFileName);
  242. if (iconFile != null) {
  243. try {
  244. Reader reader = new InputStreamReader(iconFile.openStream());
  245. _parse(reader, iconFileName, container);
  246. reader.close();
  247. } catch (Exception e) {
  248. throw new IllegalActionException(e.toString());
  249. }
  250. }
  251. }
  252. /** Get the new name for class to be changed to its backtracking
  253. * version. If the class has a manually written backtracking version,
  254. * the name of that backtracking version will be returned; if there is
  255. * no manually written backtracking version for it, but there is an
  256. * automatically generated backtracking version, the automatically
  257. * name of the generated version will be returned; otherwise, null will
  258. * be returned.
  259. *
  260. * @param oldClassName The name of the class before change.
  261. * @return The new class name, or null.
  262. */
  263. private static String _newClassName(String oldClassName) {
  264. String automaticClass = AUTOMATIC_PREFIX + "." + oldClassName;
  265. String manualClass = MANUAL_PREFIX + "." + oldClassName;
  266. if (_classExists(manualClass)) {
  267. return manualClass;
  268. } else if (_classExists(automaticClass)) {
  269. return automaticClass;
  270. } else {
  271. return null;
  272. }
  273. }
  274. /** Parse the content in the reader within the context of the container.
  275. *
  276. * @param reader The reader to be read from.
  277. * @param container The context of the parsing.
  278. * @return The NamedObj returned by the parser.
  279. * @exception Exception If the parsing is not successful.
  280. */
  281. private NamedObj _parse(Reader reader, String systemID,
  282. NamedObj container) throws Exception {
  283. if (_parser == null) {
  284. _parser = new MoMLParser();
  285. }
  286. _parser.setContext(container);
  287. NamedObj result = _parser.parse(null, systemID, reader);
  288. MoMLParser.setModified(true);
  289. return result;
  290. }
  291. /** The stack of the name of the classes that have been changed.
  292. */
  293. private Stack<String> _classStack = new Stack<String>();
  294. /** The list of entities changed during the parsing.
  295. */
  296. private List<NamedObj> _entitiesChanged = new LinkedList<NamedObj>();
  297. /** The parser to parse extra content (e.g., icon MoML).
  298. */
  299. private MoMLParser _parser = null;
  300. }
  301. }