/* Copyright (c) 2009 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.. */ package org.clothocad.tool.spectacles.eugeneimportexport; import eugene.Device; import eugene.Part; import eugene.eugeneLexer; import eugene.eugeneParser; import eugene.fileDialogFrame; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.List; import javax.swing.JOptionPane; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; //import org.clothocad.tool.spectacles.Spectacles; import org.clothocad.tool.spectacles.ui.frames.WorkspaceFrame; import org.clothocad.tool.spectacles.ui.scenes.WorkspaceScene; /** * EugeneImporter loads Eugene files into Spectacles. * @author Joanna * @author Richard */ public class EugeneImporter { /** * Creates a new EugeneImporter. * @param frame the parent WorkspaceFrame. */ public EugeneImporter(WorkspaceFrame frame) { _wsFrame = frame; } /** * Imports Eugene code from a file that the user selects from a file dialog. * @throws java.io.IOException */ public void runImportFromFile() throws IOException { // Open dialog box to choose Eugene file to import File eugFile = null; fileDialogFrame fdFrame = new fileDialogFrame(); fdFrame.display(); eugFile = fdFrame.getFile(); if (eugFile == null) { //System.err.println("No file found; Eugene cannot run"); return; } String fileName = eugFile.getName(); if (_wsFrame.getCurrentFilePath().equals(fileName)) { JOptionPane.showMessageDialog(_wsFrame, "Error, file of same name (" + fileName + ") is already imported", "Error", JOptionPane.ERROR_MESSAGE); return; } if (runImport(eugFile)) { _wsFrame.setModified(false); } } /** * Imports Eugene code from the notepad window. * @param notepadText the text from the notepad window. * @throws java.io.IOException */ public void runImportFromNotepad(String notepadText) throws IOException { // Transfer content of notepad to a temporary Eugene file String newFileName = System.getProperty("java.io.tmpdir") + "/tmpIn.eug"; File eugFile = new File(newFileName); Writer output = new BufferedWriter(new FileWriter(eugFile)); output.write(notepadText); output.close(); if (runImport(eugFile)) { _wsFrame.setModified(_wsFrame.getNotepad().isModified()); } eugFile.delete(); } /** * Performs the actual Eugene import given a file. Import consists of preprocessing * the include files, running the Eugene lexer and parser, storing the data * structure generated by Eugene into a Manager, and populating the WorkspaceFrame. * This method is called by both <code>runImportFromFile()</code> and * <code>runImportFromNotepad()</code>. * @param eugFile the file containing the Eugene code to import. * @return <code>true</code> if the import is successful, * and <code>false</code> otherwise. * @throws java.io.IOException */ public boolean runImport(File eugFile) throws IOException { return runImport(eugFile, false); } public boolean runImport(File eugFile, boolean stealth) throws IOException { // Preprocess includes in eugFile eugene.Run.tmpPath = System.getProperty("java.io.tmpdir"); eugene.Run.includePath = org.clothocad.tool.spectacles.Spectacles.getHeaderFilesLocation(); //System.out.println(eugene.Run.tmpPath); //System.out.println(eugene.Run.includePath); File newFile = eugene.Run.includeFiles(eugFile); // Run Eugene lexer and parser eugeneLexer lex = new eugeneLexer(new ANTLRFileStream(newFile.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lex); _parser = new eugeneParser(tokens); try { _parser.prog(); } catch (IllegalArgumentException iae) { if (!_parser.ruleAssertionViolations.isEmpty()) { String assertionMessage = "One or more rule assertions have been violated:"; for (String deviceName : _parser.ruleAssertionViolations.keySet()) { assertionMessage += "\nIn device " + deviceName + ":"; List<String> rules = _parser.ruleAssertionViolations.get(deviceName); for (String ruleName : rules) { assertionMessage += " " + ruleName; } } JOptionPane.showMessageDialog(_wsFrame, assertionMessage, "Warning - Assertions", JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(_wsFrame, "Eugene parser error: " + iae.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } return false; } catch (RecognitionException ex) { JOptionPane.showMessageDialog(_wsFrame, "Eugene parser error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return false; } finally { if (!_parser.ruleNoteViolations.isEmpty()) { String noteMessage = "One or more rule notes have been violated:"; for (String deviceName : _parser.ruleNoteViolations.keySet()) { noteMessage += "\nIn device " + deviceName + ":"; List<String> rules = _parser.ruleNoteViolations.get(deviceName); for (String ruleName : rules) { noteMessage += " " + ruleName; } } JOptionPane.showMessageDialog(_wsFrame, noteMessage, "Warning - Notes", JOptionPane.WARNING_MESSAGE); } } // Import Eugene data structure into Manager String fileName = eugFile.getName(); newFile.delete(); if (stealth) { return true; } if (_parser.deviceDeclarations.size() > 250) { throw new IOException("Maximum number of devices allowed is 250.\nFor sending large quantities of data to the algorithm manager,\nuse the 'Import to algorithm manager' option under the File menu."); } Manager newManager = new Manager(fileName, _parser.propertyDefinitions, _parser.partDefinitions, _parser.primitiveDeclarations, _parser.partDeclarations, _parser.deviceDeclarations, _parser.ruleDeclarations, _parser.ruleAssertions, _parser.ruleNotes); // Import information into Manager _wsFrame.clearWorkspace(); _wsFrame.setCurrentFilePath(fileName); _wsFrame.setManager(newManager); for (String aDeviceName : _parser.deviceDeclarations.keySet()) { importAndWrapDevice(aDeviceName, newManager); } _wsFrame.showDeviceDialog(); _wsFrame.getActiveWorkspaceScene().showContents(); return true; } /*import device: * get device name * if device is already wrapped * do nothing? * else * get the device * create a new tab * populate scenes with parts * if part * get the part * put it in a wrapper * add it to the scene * if device * if not already wrapped * import and wrap device * get the device * put in new wrapper * add it to the scene * return */ /** * Imports a Device. This includes creating a new tab for the Device in the * Spectacles WorkspaceFrame, placing it in a DevicePartWrapper, placing the * wrapper in the DeviceTracker, and importing all items listed in its parts * list. If the Device has already been imported, the method will return the * DevicePartWrapper containing the already-imported device. This method will * also recursively import nested Devices. * @param aDeviceName the name of the Device to be imported. * @param thisManager the Manager that stores the currently imported information. */ private void importAndWrapDevice(String aDeviceName, Manager thisManager) { if (thisManager.containsWrappedDevice(aDeviceName)) { // if it's already wrapped return; // do nothing } Device aDevice = _parser.deviceDeclarations.get(aDeviceName); _wsFrame.newTab(aDevice); // otherwise create and populate new tab WorkspaceScene wsScene = _wsFrame.getActiveWorkspaceScene(); wsScene.hideContents(); for (String aComponentName : aDevice.components) { if (_parser.partDeclarations.containsKey(aComponentName)) { // if it's a part Part aPart = _parser.partDeclarations.get(aComponentName); DevicePartWrapper wrapper = new DevicePartWrapper(aPart); //thisManager.addWrappedComponent(wrapper); wsScene.insertHiddenNode(wrapper); } else if (_parser.deviceDeclarations.containsKey(aComponentName)) { // if it's a device if (!thisManager.containsWrappedDevice(aComponentName)) { // if device is not already wrapped importAndWrapDevice(aComponentName, thisManager); wsScene.hideContents(); } Device aNestedDevice = thisManager.getAllWrappedDevices().get(aComponentName).getDevice(); WorkspaceScene nestedDeviceScene = thisManager.getAllWrappedDevices().get(aComponentName).getWorkspaceScene(); DevicePartWrapper wrapper = new DevicePartWrapper(aNestedDevice, nestedDeviceScene); wsScene.insertHiddenNode(wrapper); } } _wsFrame.closeTab(_wsFrame.getMainJTPTabCount()-1); //wsScene.validate(); return; } /** * Imports Eugene code from a file and generates output for sending to the * Algorithm Manager without loading anything in WorkspaceFrame. * @throws java.io.IOException */ public String runImportAlgorithm() throws IOException { // Open dialog box to choose Eugene file to import File eugFile = null; fileDialogFrame fdFrame = new fileDialogFrame(); fdFrame.display(); eugFile = fdFrame.getFile(); if (eugFile == null) { //System.err.println("No file found; Eugene cannot run"); return "="; } // Preprocess includes in eugFile eugene.Run.tmpPath = System.getProperty("java.io.tmpdir"); eugene.Run.includePath = org.clothocad.tool.spectacles.Spectacles.getHeaderFilesLocation(); //System.out.println(eugene.Run.tmpPath); //System.out.println(eugene.Run.includePath); File newFile = eugene.Run.includeFiles(eugFile); // Run Eugene lexer and parser eugeneLexer lex = new eugeneLexer(new ANTLRFileStream(newFile.getAbsolutePath())); CommonTokenStream tokens = new CommonTokenStream(lex); _parser = new eugeneParser(tokens); try { _parser.prog(); } catch (IllegalArgumentException iae) { if (!_parser.ruleAssertionViolations.isEmpty()) { String assertionMessage = "One or more rule assertions have been violated:"; for (String deviceName : _parser.ruleAssertionViolations.keySet()) { assertionMessage += "\nIn device " + deviceName + ":"; List<String> rules = _parser.ruleAssertionViolations.get(deviceName); for (String ruleName : rules) { assertionMessage += " " + ruleName; } } JOptionPane.showMessageDialog(_wsFrame, assertionMessage, "Warning - Assertions", JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(_wsFrame, "Eugene parser error: " + iae.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } return "="; } catch (RecognitionException ex) { JOptionPane.showMessageDialog(_wsFrame, "Eugene parser error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return "="; } finally { if (!_parser.ruleNoteViolations.isEmpty()) { String noteMessage = "One or more rule notes have been violated:"; for (String deviceName : _parser.ruleNoteViolations.keySet()) { noteMessage += "\nIn device " + deviceName + ":"; List<String> rules = _parser.ruleNoteViolations.get(deviceName); for (String ruleName : rules) { noteMessage += " " + ruleName; } } JOptionPane.showMessageDialog(_wsFrame, noteMessage, "Warning - Notes", JOptionPane.WARNING_MESSAGE); } } String names = ""; for (String aDeviceName : _parser.deviceDeclarations.keySet()) { names += getNames(aDeviceName).substring(1) + "\n"; } return names; } private String getNames(String deviceName) { String names = ""; Device device = _parser.deviceDeclarations.get(deviceName); if (device.components.size() == 0) { return "_"; } for (String componentName : device.components) { if (_parser.partDeclarations.containsKey(componentName)) { names += "." + componentName; } else if (_parser.deviceDeclarations.containsKey(componentName)) { names += "\n" + getNames(componentName).substring(1); } } return names; } private WorkspaceFrame _wsFrame; private eugeneParser _parser; }