diff --git a/src/main/java/pulse/input/Range.java b/src/main/java/pulse/input/Range.java index d410683..166f986 100644 --- a/src/main/java/pulse/input/Range.java +++ b/src/main/java/pulse/input/Range.java @@ -208,7 +208,7 @@ public void optimisationVector(ParameterVector output, List flags) { @Override public void assign(ParameterVector params) throws SolverException { if(!params.validate()) - throw new SolverException("Parameter values not sensible"); + throw new SolverException("Parameter values not sensible: " + params); NumericProperty p = null; diff --git a/src/main/java/pulse/io/export/XMLConverter.java b/src/main/java/pulse/io/export/XMLConverter.java index cd2b3fe..73b91e4 100644 --- a/src/main/java/pulse/io/export/XMLConverter.java +++ b/src/main/java/pulse/io/export/XMLConverter.java @@ -35,196 +35,214 @@ * information XML file in the resource folder. * */ - public class XMLConverter { - private XMLConverter() { - } - - private static void toXML(NumericProperty np, Document doc, Element rootElement) { - Element property = doc.createElement(np.getClass().getSimpleName()); - rootElement.appendChild(property); - - Attr keyword = doc.createAttribute("keyword"); - keyword.setValue(np.getType().toString()); - property.setAttributeNode(keyword); - - Attr descriptor = doc.createAttribute("descriptor"); - descriptor.setValue(np.getDescriptor(false)); - property.setAttributeNode(descriptor); - - Attr abbreviation = doc.createAttribute("abbreviation"); - abbreviation.setValue(np.getAbbreviation(false)); - property.setAttributeNode(abbreviation); - - Attr value = doc.createAttribute("value"); - value.setValue(np.getValue().toString()); - property.setAttributeNode(value); - - Attr minimum = doc.createAttribute("minimum"); - minimum.setValue(np.getMinimum().toString()); - property.setAttributeNode(minimum); - - Attr maximum = doc.createAttribute("maximum"); - maximum.setValue(np.getMaximum().toString()); - property.setAttributeNode(maximum); - - Attr dim = doc.createAttribute("dimensionfactor"); - dim.setValue(np.getDimensionFactor().toString()); - property.setAttributeNode(dim); - - Attr autoAdj = doc.createAttribute("auto-adjustable"); - autoAdj.setValue(np.isAutoAdjustable() + ""); - property.setAttributeNode(autoAdj); - - Attr primitiveType = doc.createAttribute("primitive-type"); - primitiveType.setValue(np.getValue() instanceof Double ? "double" : "int"); - property.setAttributeNode(primitiveType); - - Attr defSearch = doc.createAttribute("default-search-variable"); - primitiveType.setValue(np.isDefaultSearchVariable() + ""); - property.setAttributeNode(defSearch); - - } - - /** - * Utility method that creates an {@code .xml} file listing all public final - * static instances of {@code NumericProperty} found in the - * {@code NumericProperty} class. - */ - - public static void writeXML() throws ParserConfigurationException, TransformerException { - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.newDocument(); - - Element rootElement = doc.createElement("NumericProperties"); - doc.appendChild(rootElement); - - List properties = new ArrayList<>(); - - int modifiers; - - /** - * Reads all final static {@code NumericProperty} constants in the - * {@code NumericProperty} class - */ - - for (Field field : NumericProperty.class.getDeclaredFields()) { - - modifiers = field.getModifiers(); - - // filter only public final static NumericProperties - if ((Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) - && field.getType().equals(NumericProperty.class)) { - - NumericProperty value = null; - try { - value = (NumericProperty) field.get(null); - } catch (IllegalArgumentException | IllegalAccessException e) { - System.out.println("Unable to access field: " + field); - e.printStackTrace(); - } - if (value != null) - properties.add(value); - - } - - } - - properties.stream().forEach(p -> XMLConverter.toXML(p, doc, rootElement)); - - // write the content into xml file - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - Transformer transformer = transformerFactory.newTransformer(); - transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - DOMSource source = new DOMSource(doc); - StreamResult result = new StreamResult(new File(NumericProperty.class.getSimpleName() + ".xml")); - transformer.transform(source, result); - - // Output to console for testing - StreamResult consoleResult = new StreamResult(System.out); - transformer.transform(source, consoleResult); - - } - - /** - * Utility method used to read {@code NumericProperty} constants from - * {@code xml} files. - * - * @param inputStream the input stream used to read data from. - * @return a list of {@code NumericProperty} objects with their attributes - * specified in the {@code xml} file. - */ - - public static List readXML(InputStream inputStream) - throws ParserConfigurationException, SAXException, IOException { - - List properties = new ArrayList<>(); - - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(inputStream); - - doc.getDocumentElement().normalize(); - NodeList nList = doc.getElementsByTagName(NumericProperty.class.getSimpleName()); - - for (int temp = 0; temp < nList.getLength(); temp++) { - Node nNode = nList.item(temp); - - if (nNode.getNodeType() == Node.ELEMENT_NODE) { - Element eElement = (Element) nNode; - NumericPropertyKeyword keyword = NumericPropertyKeyword.valueOf(eElement.getAttribute("keyword")); - boolean autoAdjustable = Boolean.valueOf(eElement.getAttribute("auto-adjustable")); - boolean discrete = Boolean.valueOf(eElement.getAttribute("discreet")); - String descriptor = eElement.getAttribute("descriptor"); - String abbreviation = eElement.getAttribute("abbreviation"); - boolean defSearch = Boolean.valueOf(eElement.getAttribute("default-search-variable")); - - Number value, minimum, maximum, dimensionFactor; - - if (eElement.getAttribute("primitive-type").equalsIgnoreCase("double")) { - value = Double.valueOf(eElement.getAttribute("value")); - minimum = Double.valueOf(eElement.getAttribute("minimum")); - maximum = Double.valueOf(eElement.getAttribute("maximum")); - dimensionFactor = Double.valueOf(eElement.getAttribute("dimensionfactor")); - } else { - value = Integer.valueOf(eElement.getAttribute("value")); - minimum = Integer.valueOf(eElement.getAttribute("minimum")); - maximum = Integer.valueOf(eElement.getAttribute("maximum")); - dimensionFactor = Integer.valueOf(eElement.getAttribute("dimensionfactor")); - } - - var np = new NumericProperty(keyword, value, minimum, maximum, dimensionFactor); - np.setDescriptor(descriptor); - np.setAbbreviation(abbreviation); - np.setAutoAdjustable(autoAdjustable); - np.setDiscrete(discrete); - np.setDefaultSearchVariable(defSearch); - properties.add(np); - } - } - - return properties; - - } - - /** - * The default XML file is specific in the 'messages.properties' text file in - * the {@code pulse.ui} package - * - * @return a list of default instances of {@code NumericProperty}. - */ - - public static List readDefaultXML() { - try { - return readXML(NumericProperty.class.getResourceAsStream(Messages.getString("NumericProperty.XMLFile"))); - } catch (ParserConfigurationException | SAXException | IOException e) { - System.err.println("Unable to read list of default numeric properties"); - e.printStackTrace(); - } - return null; - } - -} \ No newline at end of file + private XMLConverter() { + } + + private static void toXML(NumericProperty np, Document doc, Element rootElement) { + Element property = doc.createElement(np.getClass().getSimpleName()); + rootElement.appendChild(property); + + Attr keyword = doc.createAttribute("keyword"); + keyword.setValue(np.getType().toString()); + property.setAttributeNode(keyword); + + Attr descriptor = doc.createAttribute("descriptor"); + descriptor.setValue(np.getDescriptor(false)); + property.setAttributeNode(descriptor); + + Attr abbreviation = doc.createAttribute("abbreviation"); + abbreviation.setValue(np.getAbbreviation(false)); + property.setAttributeNode(abbreviation); + + Attr value = doc.createAttribute("value"); + value.setValue(np.getValue().toString()); + property.setAttributeNode(value); + + Attr minimum = doc.createAttribute("minimum"); + minimum.setValue(np.getMinimum().toString()); + property.setAttributeNode(minimum); + + Attr maximum = doc.createAttribute("maximum"); + maximum.setValue(np.getMaximum().toString()); + property.setAttributeNode(maximum); + + Attr dim = doc.createAttribute("dimensionfactor"); + dim.setValue(np.getDimensionFactor().toString()); + property.setAttributeNode(dim); + + Attr autoAdj = doc.createAttribute("visible"); + autoAdj.setValue(np.isVisibleByDefault() + ""); + property.setAttributeNode(autoAdj); + + Attr primitiveType = doc.createAttribute("primitive-type"); + primitiveType.setValue(np.getValue() instanceof Double ? "double" : "int"); + property.setAttributeNode(primitiveType); + + Attr defSearch = doc.createAttribute("default-search-variable"); + primitiveType.setValue(np.isDefaultSearchVariable() + ""); + property.setAttributeNode(defSearch); + + } + + /** + * Utility method that creates an {@code .xml} file listing all public final + * static instances of {@code NumericProperty} found in the + * {@code NumericProperty} class. + * + * @throws javax.xml.parsers.ParserConfigurationException + * @throws javax.xml.transform.TransformerException + */ + public static void writeXML() throws ParserConfigurationException, TransformerException { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.newDocument(); + + Element rootElement = doc.createElement("NumericProperties"); + doc.appendChild(rootElement); + + List properties = new ArrayList<>(); + + int modifiers; + + /** + * Reads all final static {@code NumericProperty} constants in the + * {@code NumericProperty} class + */ + for (Field field : NumericProperty.class.getDeclaredFields()) { + + modifiers = field.getModifiers(); + + // filter only public final static NumericProperties + if ((Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) + && field.getType().equals(NumericProperty.class)) { + + NumericProperty value = null; + try { + value = (NumericProperty) field.get(null); + } catch (IllegalArgumentException | IllegalAccessException e) { + System.out.println("Unable to access field: " + field); + e.printStackTrace(); + } + if (value != null) { + properties.add(value); + } + + } + + } + + properties.stream().forEach(p -> XMLConverter.toXML(p, doc, rootElement)); + + // write the content into xml file + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(new File(NumericProperty.class.getSimpleName() + ".xml")); + transformer.transform(source, result); + + // Output to console for testing + StreamResult consoleResult = new StreamResult(System.out); + transformer.transform(source, consoleResult); + + } + + /** + * Utility method used to read {@code NumericProperty} constants from + * {@code xml} files. + * + * @param inputStream the input stream used to read data from. + * @return a list of {@code NumericProperty} objects with their attributes + * specified in the {@code xml} file. + * @throws javax.xml.parsers.ParserConfigurationException + * @throws org.xml.sax.SAXException + * @throws java.io.IOException + */ + public static List readXML(InputStream inputStream) + throws ParserConfigurationException, SAXException, IOException { + + List properties = new ArrayList<>(); + + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputStream); + + doc.getDocumentElement().normalize(); + NodeList nList = doc.getElementsByTagName(NumericProperty.class.getSimpleName()); + + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + NumericPropertyKeyword keyword = NumericPropertyKeyword.valueOf(eElement.getAttribute("keyword")); + boolean visible = Boolean.valueOf(eElement.getAttribute("visible")); + boolean discrete = Boolean.valueOf(eElement.getAttribute("discreet")); + String descriptor = eElement.getAttribute("descriptor"); + String abbreviation = eElement.getAttribute("abbreviation"); + boolean defSearch = Boolean.valueOf(eElement.getAttribute("default-search-variable")); + + Number value, minimum, maximum, dimensionFactor; + + if (eElement.getAttribute("primitive-type").equalsIgnoreCase("double")) { + value = Double.valueOf(eElement.getAttribute("value")); + minimum = Double.valueOf(eElement.getAttribute("minimum")); + maximum = Double.valueOf(eElement.getAttribute("maximum")); + dimensionFactor = Double.valueOf(eElement.getAttribute("dimensionfactor")); + } else { + value = Integer.valueOf(eElement.getAttribute("value")); + minimum = Integer.valueOf(eElement.getAttribute("minimum")); + maximum = Integer.valueOf(eElement.getAttribute("maximum")); + dimensionFactor = Integer.valueOf(eElement.getAttribute("dimensionfactor")); + } + + NodeList excludeList = eElement.getElementsByTagName("excludes"); + + var np = new NumericProperty(keyword, value, minimum, maximum, dimensionFactor); + + if (excludeList.getLength() > 0) { + var excludeKeywords = ((Element) excludeList.item(0)).getElementsByTagName("keyword"); + NumericPropertyKeyword[] array = new NumericPropertyKeyword[excludeKeywords.getLength()]; + + for (int i = 0; i < excludeKeywords.getLength(); i++) { + String textValue = excludeKeywords.item(i).getChildNodes().item(0).getNodeValue(); + array[i] = NumericPropertyKeyword.valueOf(textValue); + } + + np.setExcludeKeywords(array); + + } + + np.setDescriptor(descriptor); + np.setAbbreviation(abbreviation); + np.setVisibleByDefault(visible); + np.setDiscrete(discrete); + np.setDefaultSearchVariable(defSearch); + properties.add(np); + } + } + + return properties; + + } + + /** + * The default XML file is specific in the 'messages.properties' text file + * in the {@code pulse.ui} package + * + * @return a list of default instances of {@code NumericProperty}. + */ + public static List readDefaultXML() { + try { + return readXML(NumericProperty.class.getResourceAsStream(Messages.getString("NumericProperty.XMLFile"))); + } catch (ParserConfigurationException | SAXException | IOException e) { + System.err.println("Unable to read list of default numeric properties"); + e.printStackTrace(); + } + return null; + } + +} diff --git a/src/main/java/pulse/ui/components/models/ResultListModel.java b/src/main/java/pulse/ui/components/models/ResultListModel.java deleted file mode 100644 index 42dc185..0000000 --- a/src/main/java/pulse/ui/components/models/ResultListModel.java +++ /dev/null @@ -1,69 +0,0 @@ -package pulse.ui.components.models; - -import static pulse.tasks.processing.ResultFormat.getInstance; -import static pulse.tasks.processing.ResultFormat.getMinimalArray; - -import java.util.ArrayList; -import java.util.List; - -import javax.swing.AbstractListModel; - -import pulse.properties.NumericPropertyKeyword; - -public class ResultListModel extends AbstractListModel { - - /** - * - */ - private static final long serialVersionUID = 1L; - private List elements = new ArrayList(); - - public ResultListModel() { - super(); - update(); - } - - public void update() { - elements.clear(); - elements.addAll(getInstance().getKeywords()); - } - - @Override - public int getSize() { - return elements.size(); - } - - @Override - public NumericPropertyKeyword getElementAt(int i) { - return elements.get(i); - } - - public void add(NumericPropertyKeyword key) { - elements.add(key); - var size = this.getSize(); - this.fireContentsChanged(this, size - 1, size); - } - - public void remove(NumericPropertyKeyword key) { - if (!elements.contains(key)) - return; - - for (var keyMin : getMinimalArray()) { - if (key == keyMin) - return; - } - var index = elements.indexOf(key); - elements.remove(key); - this.fireContentsChanged(this, index - 1, index); - - } - - public boolean contains(NumericPropertyKeyword key) { - return elements.contains(key); - } - - public List getData() { - return elements; - } - -} \ No newline at end of file