Search in sources :

Example 1 with Parameter

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter 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 2 with Parameter

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

the class DocumentParameters method main.

/**
 * @param args Command line arguments
 */
public static void main(String[] args) {
    LoggingConfiguration.setVerbose(Level.VERBOSE);
    if (args.length != 2 && args.length != 4) {
        LOG.warning("I need exactly two or four file names to operate!");
        System.exit(1);
    }
    if (!args[0].endsWith(".html")) {
        LOG.warning("First file name doesn't end with .html!");
        System.exit(1);
    }
    if (!args[1].endsWith(".html")) {
        LOG.warning("Second file name doesn't end with .html!");
        System.exit(1);
    }
    if (args.length > 2 && !args[2].endsWith(".trac")) {
        LOG.warning("Third file name doesn't end with .trac!");
        System.exit(1);
    }
    if (args.length > 3 && !args[3].endsWith(".trac")) {
        LOG.warning("Fourth file name doesn't end with .trac!");
        System.exit(1);
    }
    File byclsname = new File(args[0]);
    File byoptname = new File(args[1]);
    File byclsnamew = args.length >= 3 ? new File(args[2]) : null;
    File byoptnamew = args.length >= 4 ? new File(args[3]) : null;
    try {
        Files.createDirectories(byclsname.toPath().getParent());
        Files.createDirectories(byoptname.toPath().getParent());
        if (byclsnamew != null) {
            Files.createDirectories(byclsnamew.toPath().getParent());
        }
        if (byoptnamew != null) {
            Files.createDirectories(byoptnamew.toPath().getParent());
        }
    } catch (IOException e) {
        LOG.exception(e);
        System.exit(1);
    }
    Map<Class<?>, List<Parameter<?>>> byclass = new HashMap<>();
    Map<OptionID, List<Pair<Parameter<?>, Class<?>>>> byopt = new HashMap<>();
    try {
        buildParameterIndex(byclass, byopt);
    } catch (Exception e) {
        LOG.exception(e);
        System.exit(1);
    }
    try (// 
    FileOutputStream byclassfo = new FileOutputStream(byclsname);
        OutputStream byclassstream = new BufferedOutputStream(byclassfo)) {
        Document byclassdoc = makeByClassOverviewHTML(byclass);
        HTMLUtil.writeXHTML(byclassdoc, byclassstream);
    } catch (IOException e) {
        LOG.exception("IO Exception writing output.", e);
        System.exit(1);
    }
    if (byclsnamew != null) {
        try (// 
        FileOutputStream byclassfo = new FileOutputStream(byclsnamew);
            PrintStream byclassstream = new PrintStream(new BufferedOutputStream(byclassfo), false, "UTF-8")) {
            makeByClassOverviewWiki(byclass, new WikiStream(byclassstream));
        } catch (IOException e) {
            LOG.exception("IO Exception writing output.", e);
            System.exit(1);
        }
    }
    try (// 
    FileOutputStream byoptfo = new FileOutputStream(byoptname);
        OutputStream byoptstream = new BufferedOutputStream(byoptfo)) {
        Document byoptdoc = makeByOptOverviewHTML(byopt);
        HTMLUtil.writeXHTML(byoptdoc, byoptfo);
    } catch (IOException e) {
        LOG.exception("IO Exception writing output.", e);
        System.exit(1);
    }
    if (byoptnamew != null) {
        try (// 
        FileOutputStream byoptfo = new FileOutputStream(byoptnamew);
            PrintStream byoptstream = new PrintStream(new BufferedOutputStream(byoptfo))) {
            makeByOptOverviewWiki(byopt, new WikiStream(byoptstream));
        } catch (IOException e) {
            LOG.exception("IO Exception writing output.", e);
            throw new RuntimeException(e);
        }
    }
    // Forcibly terminate, as some class may have spawned a thread.
    System.exit(0);
}
Also used : PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OptionID(de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID) FileOutputStream(java.io.FileOutputStream) 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) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with Parameter

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

the class DocumentParameters method buildParameterIndex.

private static void buildParameterIndex(Map<Class<?>, List<Parameter<?>>> byclass, Map<OptionID, List<Pair<Parameter<?>, Class<?>>>> byopt) {
    final ArrayList<TrackedParameter> options = new ArrayList<>();
    ExecutorService es = Executors.newSingleThreadExecutor();
    List<Class<?>> objs = ELKIServiceRegistry.findAllImplementations(Object.class, false, true);
    Collections.sort(objs, new ELKIServiceScanner.ClassSorter());
    Class<?> appc = appBaseClass();
    for (final Class<?> cls : objs) {
        // Doesn't have a proper name?
        if (cls.getCanonicalName() == null) {
            continue;
        }
        // constructors / parameterizers and may start AWT threads - skip them.
        if (appc != null && appc.isAssignableFrom(cls)) {
            continue;
        }
        UnParameterization config = new UnParameterization();
        TrackParameters track = new TrackParameters(config, cls);
        try {
            // Wait up to one second.
            es.submit(new // 
            FutureTask<Object>(new Instancer(cls, track, options), // 
            null)).get(1L, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            LOG.warning("Timeout on instantiating " + cls.getName());
            es.shutdownNow();
            throw new RuntimeException(e);
        } catch (Exception e) {
            LOG.warning("Error instantiating " + cls.getName(), e.getCause());
            continue;
        }
    }
    LOG.debug("Documenting " + options.size() + " parameter instances.");
    for (TrackedParameter pp : options) {
        if (pp.getOwner() == null || pp.getParameter() == null) {
            LOG.debugFiner("Null: " + pp.getOwner() + " " + pp.getParameter());
            continue;
        }
        Class<?> c = (Class<?>) ((pp.getOwner() instanceof Class) ? pp.getOwner() : pp.getOwner().getClass());
        Parameter<?> o = pp.getParameter();
        // just collect unique occurrences
        {
            List<Parameter<?>> byc = byclass.get(c);
            boolean inlist = false;
            if (byc != null) {
                for (Parameter<?> par : byc) {
                    if (par.getOptionID() == o.getOptionID()) {
                        inlist = true;
                        break;
                    }
                }
            }
            if (!inlist) {
                List<Parameter<?>> ex = byclass.get(c);
                if (ex == null) {
                    byclass.put(c, ex = new ArrayList<>());
                }
                ex.add(o);
            }
        }
        {
            List<Pair<Parameter<?>, Class<?>>> byo = byopt.get(o.getOptionID());
            boolean inlist = false;
            if (byo != null) {
                for (Pair<Parameter<?>, Class<?>> pair : byo) {
                    if (pair.second.equals(c)) {
                        inlist = true;
                        break;
                    }
                }
            }
            if (!inlist) {
                List<Pair<Parameter<?>, Class<?>>> ex = byopt.get(o.getOptionID());
                if (ex == null) {
                    byopt.put(o.getOptionID(), ex = new ArrayList<>());
                }
                ex.add(new Pair<Parameter<?>, Class<?>>(o, c));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.UnParameterization) FutureTask(java.util.concurrent.FutureTask) ELKIServiceScanner(de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner) ExecutorService(java.util.concurrent.ExecutorService) 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) ArrayList(java.util.ArrayList) List(java.util.List) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) TimeoutException(java.util.concurrent.TimeoutException) Pair(de.lmu.ifi.dbs.elki.utilities.pairs.Pair)

Example 4 with Parameter

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

the class DocumentParameters method makeByOptOverviewWiki.

private static void makeByOptOverviewWiki(Map<OptionID, List<Pair<Parameter<?>, Class<?>>>> byopt, WikiStream out) {
    List<OptionID> opts = new ArrayList<>(byopt.keySet());
    Collections.sort(opts, new SortByOption());
    for (OptionID oid : opts) {
        final Parameter<?> firstopt = byopt.get(oid).get(0).getFirst();
        out.indent = 1;
        // 
        out.printitem("").print("{{{").print(SerializedParameterization.OPTION_PREFIX).print(firstopt.getOptionID().getName()).print(' ').print(firstopt.getSyntax()).println("}}}:: ");
        // No BR needed, we increase the indent.
        out.newline = 1;
        out.indent = 2;
        appendMultilineTextWiki(out, firstopt.getShortDescription());
        // class restriction?
        Class<?> superclass = getRestrictionClass(oid, firstopt, byopt);
        if (superclass != null) {
            appendClassRestrictionWiki(out, superclass);
        }
        // default value?
        if (firstopt.hasDefaultValue()) {
            appendDefaultValueWiki(out, firstopt);
        }
        if (FULL_WIKI_OUTPUT) {
            // known values?
            if (superclass != null) {
                appendKnownImplementationsWiki(out, superclass);
            }
            // List of classes that use this parameter
            out.println("Used by:");
            for (Pair<Parameter<?>, Class<?>> clinst : byopt.get(oid)) {
                out.indent = 3;
                out.printitem("* ").javadocLink(clinst.getSecond(), null).println();
                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)) {
                            appendClassRestrictionWiki(out, cls.getRestrictionClass());
                        }
                    } else {
                        appendNoClassRestrictionWiki(out);
                    }
                }
                Parameter<?> param = clinst.getFirst();
                if (param.getDefaultValue() != null) {
                    if (!param.getDefaultValue().equals(firstopt.getDefaultValue())) {
                        appendDefaultValueWiki(out, param);
                    }
                } else if (firstopt.getDefaultValue() != null) {
                    appendNoDefaultValueWiki(out);
                }
                out.println();
            }
        }
    }
}
Also used : OptionID(de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID) ArrayList(java.util.ArrayList) 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)

Aggregations

TrackedParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)4 ClassListParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter)4 ClassParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)4 Parameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter)4 RandomParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter)4 ArrayList (java.util.ArrayList)4 OptionID (de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID)3 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Pair (de.lmu.ifi.dbs.elki.utilities.pairs.Pair)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 Document (org.w3c.dom.Document)2 ELKIServiceScanner (de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner)1 TrackParameters (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)1 UnParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.UnParameterization)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1