Search in sources :

Example 1 with ClassParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter in project elki by elki-project.

the class SettingsVisualization method makeVisualization.

@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
    SettingsResult sr = task.getResult();
    Collection<TrackedParameter> settings = sr.getSettings();
    Element layer = plot.svgElement(SVGConstants.SVG_G_TAG);
    // FIXME: use CSSClass and StyleLibrary
    int i = 0;
    Object last = null;
    for (TrackedParameter setting : settings) {
        if (setting.getOwner() != last && setting.getOwner() != null) {
            String name;
            try {
                if (setting.getOwner() instanceof Class) {
                    name = ((Class<?>) setting.getOwner()).getName();
                } else {
                    name = setting.getOwner().getClass().getName();
                }
                if (ClassParameter.class.isInstance(setting.getOwner())) {
                    name = ((ClassParameter<?>) setting.getOwner()).getValue().getName();
                }
            } catch (NullPointerException e) {
                name = "[null]";
            }
            Element object = plot.svgText(0, i + 0.7, name);
            object.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6; font-weight: bold");
            layer.appendChild(object);
            i++;
            last = setting.getOwner();
        }
        // get name and value
        String name = setting.getParameter().getOptionID().getName();
        String value = "[unset]";
        try {
            if (setting.getParameter().isDefined()) {
                value = setting.getParameter().getValueAsString();
            }
        } catch (NullPointerException e) {
            value = "[null]";
        }
        Element label = plot.svgText(0, i + 0.7, name);
        label.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6");
        layer.appendChild(label);
        Element vale = plot.svgText(7.5, i + 0.7, value);
        vale.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, "font-size: 0.6");
        layer.appendChild(vale);
        // only advance once, since we want these two to be in the same line.
        i++;
    }
    int cols = Math.max(30, (int) (i * height / width));
    int rows = i;
    final double margin = context.getStyleLibrary().getSize(StyleLibrary.MARGIN);
    final String transform = SVGUtil.makeMarginTransform(width, height, cols, rows, margin / StyleLibrary.SCALE);
    SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
    return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
Also used : Element(org.w3c.dom.Element) SettingsResult(de.lmu.ifi.dbs.elki.result.SettingsResult) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) StaticVisualizationInstance(de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)

Example 2 with ClassParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter in project elki by elki-project.

the class TextWriter method writeSettingsResult.

private void writeSettingsResult(StreamFactory streamOpener, List<SettingsResult> rs) throws IOException {
    if (rs.isEmpty()) {
        return;
    }
    SettingsResult r = rs.get(0);
    PrintStream outStream = streamOpener.openStream(getFilename(r, r.getShortName()));
    TextWriterStream out = new TextWriterStream(outStream, writers, fallback);
    // Write settings preamble
    out.commentPrintLn("Settings:");
    if (rs != null) {
        for (SettingsResult settings : rs) {
            Object last = null;
            for (TrackedParameter setting : settings.getSettings()) {
                if (setting.getOwner() != last && setting.getOwner() != null) {
                    if (last != null) {
                        out.commentPrintLn("");
                    }
                    String name;
                    try {
                        if (setting.getOwner() instanceof Class) {
                            name = ((Class<?>) setting.getOwner()).getName();
                        } else {
                            name = setting.getOwner().getClass().getName();
                        }
                        if (ClassParameter.class.isInstance(setting.getOwner())) {
                            name = ((ClassParameter<?>) setting.getOwner()).getValue().getName();
                        }
                    } catch (NullPointerException e) {
                        name = "[null]";
                    }
                    out.commentPrintLn(name);
                    last = setting.getOwner();
                }
                String name = setting.getParameter().getOptionID().getName();
                String value = "[unset]";
                try {
                    if (setting.getParameter().isDefined()) {
                        value = setting.getParameter().getValueAsString();
                    }
                } catch (NullPointerException e) {
                    value = "[null]";
                }
                out.commentPrintLn(SerializedParameterization.OPTION_PREFIX + name + " " + value);
            }
        }
    }
    out.flush();
    streamOpener.closeStream(outStream);
}
Also used : PrintStream(java.io.PrintStream) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)

Example 3 with ClassParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter in project elki by elki-project.

the class DocumentParameters method makeByOptOverviewHTML.

private static Document makeByOptOverviewHTML(Map<OptionID, List<Pair<Parameter<?>, Class<?>>>> byopt) throws IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        throw new IOException(e1);
    }
    DOMImplementation impl = builder.getDOMImplementation();
    Document htmldoc = impl.createDocument(HTMLUtil.HTML_NAMESPACE, HTMLUtil.HTML_HTML_TAG, null);
    // head
    Element head = htmldoc.createElement(HTMLUtil.HTML_HEAD_TAG);
    htmldoc.getDocumentElement().appendChild(head);
    // body
    Element body = htmldoc.createElement(HTMLUtil.HTML_BODY_TAG);
    htmldoc.getDocumentElement().appendChild(body);
    // modification warnings
    head.appendChild(htmldoc.createComment(MODIFICATION_WARNING));
    body.appendChild(htmldoc.createComment(MODIFICATION_WARNING));
    // meta with charset information
    {
        Element meta = htmldoc.createElement(HTMLUtil.HTML_META_TAG);
        meta.setAttribute(HTMLUtil.HTML_HTTP_EQUIV_ATTRIBUTE, HTMLUtil.HTML_HTTP_EQUIV_CONTENT_TYPE);
        meta.setAttribute(HTMLUtil.HTML_CONTENT_ATTRIBUTE, HTMLUtil.CONTENT_TYPE_HTML_UTF8);
        head.appendChild(meta);
    }
    // stylesheet
    {
        Element css = htmldoc.createElement(HTMLUtil.HTML_LINK_TAG);
        css.setAttribute(HTMLUtil.HTML_REL_ATTRIBUTE, HTMLUtil.HTML_REL_STYLESHEET);
        css.setAttribute(HTMLUtil.HTML_TYPE_ATTRIBUTE, HTMLUtil.CONTENT_TYPE_CSS);
        css.setAttribute(HTMLUtil.HTML_HREF_ATTRIBUTE, CSSFILE);
        head.appendChild(css);
    }
    // title
    {
        Element title = htmldoc.createElement(HTMLUtil.HTML_TITLE_TAG);
        title.setTextContent("Command line parameter overview - by option");
        head.appendChild(title);
    }
    // Heading
    {
        Element h1 = htmldoc.createElement(HTMLUtil.HTML_H1_TAG);
        h1.setTextContent("ELKI command line parameter overview:");
        body.appendChild(h1);
    }
    // Main definition list
    Element maindl = htmldoc.createElement(HTMLUtil.HTML_DL_TAG);
    body.appendChild(maindl);
    final Comparator<OptionID> osort = new SortByOption();
    final Comparator<Class<?>> csort = new ELKIServiceScanner.ClassSorter();
    Comparator<Pair<Parameter<?>, Class<?>>> psort = new Comparator<Pair<Parameter<?>, Class<?>>>() {

        @Override
        public int compare(Pair<Parameter<?>, Class<?>> o1, Pair<Parameter<?>, Class<?>> o2) {
            int c = osort.compare(o1.first.getOptionID(), o2.first.getOptionID());
            return (c != 0) ? c : csort.compare(o1.second, o2.second);
        }
    };
    List<OptionID> opts = new ArrayList<>(byopt.keySet());
    Collections.sort(opts, osort);
    for (OptionID oid : opts) {
        final Parameter<?> firstopt = byopt.get(oid).get(0).getFirst();
        // DT = definition term
        Element optdt = htmldoc.createElement(HTMLUtil.HTML_DT_TAG);
        // Anchor for references
        {
            Element optan = htmldoc.createElement(HTMLUtil.HTML_A_TAG);
            optan.setAttribute(HTMLUtil.HTML_NAME_ATTRIBUTE, firstopt.getOptionID().getName());
            optdt.appendChild(optan);
        }
        // option name
        {
            Element elemtt = htmldoc.createElement(HTMLUtil.HTML_TT_TAG);
            elemtt.setTextContent(SerializedParameterization.OPTION_PREFIX + firstopt.getOptionID().getName() + " " + firstopt.getSyntax());
            optdt.appendChild(elemtt);
        }
        maindl.appendChild(optdt);
        // DD = definition description
        Element optdd = htmldoc.createElement(HTMLUtil.HTML_DD_TAG);
        {
            Element elemp = htmldoc.createElement(HTMLUtil.HTML_P_TAG);
            HTMLUtil.appendMultilineText(htmldoc, elemp, firstopt.getShortDescription());
            optdd.appendChild(elemp);
        }
        // class restriction?
        Class<?> superclass = getRestrictionClass(oid, firstopt, byopt);
        if (superclass != null) {
            appendClassRestriction(htmldoc, superclass, optdd);
        }
        // default value?
        appendDefaultValueIfSet(htmldoc, firstopt, optdd);
        // known values?
        if (superclass != null) {
            appendKnownImplementationsIfNonempty(htmldoc, superclass, optdd);
        }
        maindl.appendChild(optdd);
        // nested definition list for options
        Element classesul = htmldoc.createElement(HTMLUtil.HTML_UL_TAG);
        {
            Element p = htmldoc.createElement(HTMLUtil.HTML_P_TAG);
            p.appendChild(htmldoc.createTextNode(HEADER_PARAMETER_FOR));
            optdd.appendChild(p);
        }
        optdd.appendChild(classesul);
        List<Pair<Parameter<?>, Class<?>>> plist = byopt.get(oid);
        Collections.sort(plist, psort);
        for (Pair<Parameter<?>, Class<?>> clinst : plist) {
            // DT definition term: option name, in TT for typewriter optics
            Element classli = htmldoc.createElement(HTMLUtil.HTML_LI_TAG);
            // Link back to original class
            {
                Element classa = htmldoc.createElement(HTMLUtil.HTML_A_TAG);
                classa.setAttribute(HTMLUtil.HTML_HREF_ATTRIBUTE, linkForClassName(clinst.getSecond().getName()));
                classa.setTextContent(clinst.getSecond().getName());
                classli.appendChild(classa);
            }
            if (clinst.getFirst() instanceof ClassParameter<?> && firstopt instanceof ClassParameter<?>) {
                ClassParameter<?> cls = (ClassParameter<?>) clinst.getFirst();
                if (cls.getRestrictionClass() != null) {
                    // TODO: if it is null, it could still be different!
                    if (!cls.getRestrictionClass().equals(superclass)) {
                        appendClassRestriction(htmldoc, cls.getRestrictionClass(), classli);
                    }
                } else {
                    appendNoClassRestriction(htmldoc, classli);
                }
            }
            Parameter<?> param = clinst.getFirst();
            if (param.getDefaultValue() != null) {
                if (!param.getDefaultValue().equals(firstopt.getDefaultValue())) {
                    appendDefaultValueIfSet(htmldoc, param, classli);
                }
            } else if (firstopt.getDefaultValue() != null) {
                appendNoDefaultValue(htmldoc, classli);
            }
            classesul.appendChild(classli);
        }
    }
    return htmldoc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document) Comparator(java.util.Comparator) OptionID(de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Pair(de.lmu.ifi.dbs.elki.utilities.pairs.Pair) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RandomParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) Parameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter) ClassListParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)

Example 4 with ClassParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter in project elki by elki-project.

the class DocumentParameters method appendDefaultValueIfSet.

/**
 * Append string containing the default value.
 *
 * @param htmldoc Document
 * @param par Parameter
 * @param optdd HTML Element
 */
private static void appendDefaultValueIfSet(Document htmldoc, Parameter<?> par, Element optdd) {
    if (par.hasDefaultValue()) {
        Element p = htmldoc.createElement(HTMLUtil.HTML_P_TAG);
        p.appendChild(htmldoc.createTextNode(HEADER_DEFAULT_VALUE));
        if (par instanceof ClassParameter<?>) {
            appendDefaultClassLink(htmldoc, par, p);
        } else if (par instanceof RandomParameter && par.getDefaultValue() == RandomFactory.DEFAULT) {
            p.appendChild(htmldoc.createTextNode("use global random seed"));
        } else {
            p.appendChild(htmldoc.createTextNode(par.getDefaultValueAsString()));
        }
        optdd.appendChild(p);
    }
}
Also used : RandomParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter) Element(org.w3c.dom.Element) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)

Example 5 with ClassParameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter in project elki by elki-project.

the class DocumentParameters method makeByClassOverviewHTML.

private static Document makeByClassOverviewHTML(Map<Class<?>, List<Parameter<?>>> byclass) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        throw new RuntimeException(e1);
    }
    DOMImplementation impl = builder.getDOMImplementation();
    Document htmldoc = impl.createDocument(HTMLUtil.HTML_NAMESPACE, HTMLUtil.HTML_HTML_TAG, null);
    // head
    Element head = htmldoc.createElement(HTMLUtil.HTML_HEAD_TAG);
    htmldoc.getDocumentElement().appendChild(head);
    // body
    Element body = htmldoc.createElement(HTMLUtil.HTML_BODY_TAG);
    htmldoc.getDocumentElement().appendChild(body);
    // modification warnings
    head.appendChild(htmldoc.createComment(MODIFICATION_WARNING));
    body.appendChild(htmldoc.createComment(MODIFICATION_WARNING));
    // meta with charset information
    {
        Element meta = htmldoc.createElement(HTMLUtil.HTML_META_TAG);
        meta.setAttribute(HTMLUtil.HTML_HTTP_EQUIV_ATTRIBUTE, HTMLUtil.HTML_HTTP_EQUIV_CONTENT_TYPE);
        meta.setAttribute(HTMLUtil.HTML_CONTENT_ATTRIBUTE, HTMLUtil.CONTENT_TYPE_HTML_UTF8);
        head.appendChild(meta);
    }
    // stylesheet
    {
        Element css = htmldoc.createElement(HTMLUtil.HTML_LINK_TAG);
        css.setAttribute(HTMLUtil.HTML_REL_ATTRIBUTE, HTMLUtil.HTML_REL_STYLESHEET);
        css.setAttribute(HTMLUtil.HTML_TYPE_ATTRIBUTE, HTMLUtil.CONTENT_TYPE_CSS);
        css.setAttribute(HTMLUtil.HTML_HREF_ATTRIBUTE, CSSFILE);
        head.appendChild(css);
    }
    // title
    {
        Element title = htmldoc.createElement(HTMLUtil.HTML_TITLE_TAG);
        title.setTextContent("Command line parameter overview.");
        head.appendChild(title);
    }
    // Heading
    {
        Element h1 = htmldoc.createElement(HTMLUtil.HTML_H1_TAG);
        h1.setTextContent("ELKI command line parameter overview:");
        body.appendChild(h1);
    }
    // Main definition list
    Element maindl = htmldoc.createElement(HTMLUtil.HTML_DL_TAG);
    body.appendChild(maindl);
    List<Class<?>> classes = new ArrayList<>(byclass.keySet());
    Collections.sort(classes, new ELKIServiceScanner.ClassSorter());
    for (Class<?> cls : classes) {
        // DT = definition term
        Element classdt = htmldoc.createElement(HTMLUtil.HTML_DT_TAG);
        // Anchor for references
        {
            Element classan = htmldoc.createElement(HTMLUtil.HTML_A_TAG);
            classan.setAttribute(HTMLUtil.HTML_NAME_ATTRIBUTE, cls.getName());
            classdt.appendChild(classan);
        }
        // Link back to original class
        {
            Element classa = htmldoc.createElement(HTMLUtil.HTML_A_TAG);
            classa.setAttribute(HTMLUtil.HTML_HREF_ATTRIBUTE, linkForClassName(cls.getName()));
            classa.setTextContent(cls.getName());
            classdt.appendChild(classa);
        }
        maindl.appendChild(classdt);
        // DD = definition description
        Element classdd = htmldoc.createElement(HTMLUtil.HTML_DD_TAG);
        maindl.appendChild(classdd);
        // nested definition list for options
        Element classdl = htmldoc.createElement(HTMLUtil.HTML_DL_TAG);
        classdd.appendChild(classdl);
        for (Parameter<?> opt : byclass.get(cls)) {
            // DT definition term: option name, in TT for typewriter optics
            Element elemdt = htmldoc.createElement(HTMLUtil.HTML_DT_TAG);
            {
                Element elemtt = htmldoc.createElement(HTMLUtil.HTML_TT_TAG);
                elemtt.setTextContent(SerializedParameterization.OPTION_PREFIX + opt.getOptionID().getName() + " " + opt.getSyntax());
                elemdt.appendChild(elemtt);
            }
            classdl.appendChild(elemdt);
            // DD definition description - put the option description here.
            Element elemdd = htmldoc.createElement(HTMLUtil.HTML_DD_TAG);
            Element elemp = htmldoc.createElement(HTMLUtil.HTML_P_TAG);
            if (opt.getShortDescription() != null) {
                HTMLUtil.appendMultilineText(htmldoc, elemp, opt.getShortDescription());
            }
            elemdd.appendChild(elemp);
            // class restriction?
            if (opt instanceof ClassParameter<?>) {
                appendClassRestriction(htmldoc, ((ClassParameter<?>) opt).getRestrictionClass(), elemdd);
            }
            // default value? completions?
            appendDefaultValueIfSet(htmldoc, opt, elemdd);
            // known values?
            if (opt instanceof ClassParameter<?>) {
                Class<?> restriction = ((ClassParameter<?>) opt).getRestrictionClass();
                appendKnownImplementationsIfNonempty(htmldoc, restriction, elemdd);
            } else if (opt instanceof ClassListParameter<?>) {
                Class<?> restriction = ((ClassListParameter<?>) opt).getRestrictionClass();
                appendKnownImplementationsIfNonempty(htmldoc, restriction, elemdd);
            }
            classdl.appendChild(elemdd);
        }
    }
    return htmldoc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ELKIServiceScanner(de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)

Aggregations

ClassParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)8 Element (org.w3c.dom.Element)5 TrackedParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)4 RandomParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter)3 ArrayList (java.util.ArrayList)3 OptionID (de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID)2 ClassListParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter)2 Parameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 DOMImplementation (org.w3c.dom.DOMImplementation)2 Document (org.w3c.dom.Document)2 SettingsResult (de.lmu.ifi.dbs.elki.result.SettingsResult)1 ELKIServiceScanner (de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner)1 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)1 ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)1 UnspecifiedParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)1 WrongParameterValueException (de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException)1 SerializedParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization)1