Search in sources :

Example 1 with TrackParameters

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters in project elki by elki-project.

the class OptionUtil method describeParameterizable.

/**
 * Format a description of a Parameterizable (including recursive options).
 *
 * @param buf Buffer to append to.
 * @param pcls Parameterizable class to describe
 * @param width Width
 * @param indent Text indent
 * @return Formatted description
 */
public static StringBuilder describeParameterizable(StringBuilder buf, Class<?> pcls, int width, String indent) {
    try {
        println(buf, width, "Description for class " + pcls.getName(), "");
        String title = DocumentationUtil.getTitle(pcls);
        if (title != null && title.length() > 0) {
            println(buf, width, title, "");
        }
        String desc = DocumentationUtil.getDescription(pcls);
        if (desc != null && desc.length() > 0) {
            println(buf, width, desc, "  ");
        }
        Reference ref = DocumentationUtil.getReference(pcls);
        if (ref != null) {
            if (ref.prefix().length() > 0) {
                println(buf, width, ref.prefix(), "");
            }
            println(buf, width, ref.authors() + ":", "");
            println(buf, width, ref.title(), "  ");
            println(buf, width, "in: " + ref.booktitle(), "");
            if (ref.url().length() > 0) {
                println(buf, width, "see also: " + ref.url(), "");
            }
        }
        SerializedParameterization config = new SerializedParameterization();
        TrackParameters track = new TrackParameters(config);
        @SuppressWarnings("unused") Object p = ClassGenericsUtil.tryInstantiate(Object.class, pcls, track);
        Collection<TrackedParameter> options = track.getAllParameters();
        if (!options.isEmpty()) {
            OptionUtil.formatForConsole(buf, width, indent, options);
        }
        return buf;
    } catch (Exception e) {
        LoggingUtil.exception("Error instantiating class to describe.", e.getCause());
        return buf.append("No description available: ").append(e);
    }
}
Also used : Reference(de.lmu.ifi.dbs.elki.utilities.documentation.Reference) SerializedParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)

Example 2 with TrackParameters

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters in project elki by elki-project.

the class MiniGUI method doSetParameters.

/**
 * Do the actual setParameters invocation.
 *
 * @param params Parameters
 */
protected void doSetParameters(List<String> params) {
    if (!params.isEmpty()) {
        String first = params.get(0);
        if (!first.startsWith("-")) {
            Class<? extends AbstractApplication> c = ELKIServiceRegistry.findImplementation(AbstractApplication.class, first);
            if (c != null) {
                maincls = c;
                params.remove(0);
            }
        }
    }
    outputArea.clear();
    SerializedParameterization config = new SerializedParameterization(params);
    TrackParameters track = new TrackParameters(config);
    track.tryInstantiate(LoggingStep.class);
    track.tryInstantiate(maincls);
    config.logUnusedParameters();
    // config.logAndClearReportedErrors();
    final boolean hasErrors = (config.getErrors().size() > 0);
    if (hasErrors && !params.isEmpty()) {
        reportErrors(config);
    }
    runButton.setEnabled(!hasErrors);
    List<String> remainingParameters = config.getRemainingParameters();
    String mainnam = maincls.getCanonicalName();
    if (mainnam.startsWith(APP_PREFIX)) {
        mainnam = mainnam.substring(APP_PREFIX.length());
    }
    commandLine.setText(format(mainnam, params));
    // update table:
    parameterTable.removeEditor();
    parameterTable.setEnabled(false);
    parameters.updateFromTrackParameters(track);
    // Add remaining parameters
    if (remainingParameters != null && !remainingParameters.isEmpty()) {
        DynamicParameters.RemainingOptions remo = new DynamicParameters.RemainingOptions();
        try {
            remo.setValue(FormatUtil.format(remainingParameters, " "));
        } catch (ParameterException e) {
            LOG.exception(e);
        }
        int bits = DynamicParameters.BIT_INVALID | DynamicParameters.BIT_SYNTAX_ERROR;
        parameters.addParameter(remo, remo.getValue(), bits, 0);
    }
    config.clearErrors();
    parameterTable.revalidate();
    parameterTable.setEnabled(true);
}
Also used : SerializedParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization) DynamicParameters(de.lmu.ifi.dbs.elki.gui.util.DynamicParameters) ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)

Example 3 with TrackParameters

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters 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 TrackParameters

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters in project elki by elki-project.

the class ParameterTabPanel method setParameters.

/**
 * Do the actual setParameters invocation.
 *
 * @param config Parameterization
 */
public void setParameters(Parameterization config) {
    TrackParameters track = new TrackParameters(config);
    configureStep(track);
    // update parameter table
    {
        parameterTable.setEnabled(false);
        parameterTable.clear();
        for (TrackedParameter pair : track.getAllParameters()) {
            parameterTable.addParameter(pair.getOwner(), pair.getParameter(), track);
        }
        // parameters.updateFromTrackParameters(track);
        parameterTable.revalidate();
        parameterTable.setEnabled(true);
    }
    // Update status and notify observers
    updateStatus();
    firePanelUpdated();
}
Also used : TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)

Example 5 with TrackParameters

use of de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters in project elki by elki-project.

the class AbstractApplication method runCLIApplication.

/**
 * Generic command line invocation.
 *
 * Refactored to have a central place for outermost exception handling.
 *
 * @param cls Application class to run.
 * @param args the arguments to run this application with
 */
public static void runCLIApplication(Class<?> cls, String[] args) {
    SerializedParameterization params = new SerializedParameterization(args);
    Flag helpF = new Flag(Parameterizer.HELP_ID);
    params.grab(helpF);
    Flag helpLongF = new Flag(Parameterizer.HELP_LONG_ID);
    params.grab(helpLongF);
    try {
        ClassParameter<Object> descriptionP = new ClassParameter<>(Parameterizer.DESCRIPTION_ID, Object.class, true);
        params.grab(descriptionP);
        if (descriptionP.isDefined()) {
            params.clearErrors();
            printDescription(descriptionP.getValue());
            System.exit(1);
        }
        // Parse debug parameter
        StringParameter debugP = new StringParameter(Parameterizer.DEBUG_ID).setOptional(true);
        params.grab(debugP);
        if (debugP.isDefined()) {
            Parameterizer.parseDebugParameter(debugP);
        }
        // Fail silently on errors.
        if (params.getErrors().size() > 0) {
            params.logAndClearReportedErrors();
            System.exit(1);
        }
    } catch (Exception e) {
        printErrorMessage(e);
        System.exit(1);
    }
    try {
        TrackParameters config = new TrackParameters(params);
        Flag verboseF = new Flag(Parameterizer.VERBOSE_ID);
        if (config.grab(verboseF) && verboseF.isTrue()) {
            // Extra verbosity by repeating the flag:
            Flag verbose2F = new Flag(Parameterizer.VERBOSE_ID);
            LoggingConfiguration.setVerbose((config.grab(verbose2F) && verbose2F.isTrue()) ? Level.VERYVERBOSE : Level.VERBOSE);
        }
        AbstractApplication task = ClassGenericsUtil.tryInstantiate(AbstractApplication.class, cls, config);
        if ((helpF.isDefined() && helpF.getValue()) || (helpLongF.isDefined() && helpLongF.getValue())) {
            LoggingConfiguration.setVerbose(Level.VERBOSE);
            LOG.verbose(usage(config.getAllParameters()));
            System.exit(1);
        }
        if (params.getErrors().size() > 0) {
            LoggingConfiguration.setVerbose(Level.VERBOSE);
            LOG.verbose("ERROR: The following configuration errors prevented execution:");
            for (ParameterException e : params.getErrors()) {
                LOG.verbose(e.getMessage());
                LOG.verbose("\n");
            }
            params.logUnusedParameters();
            LOG.verbose("Stopping execution because of configuration errors above.");
            System.exit(1);
        }
        params.logUnusedParameters();
        task.run();
    } catch (Exception e) {
        printErrorMessage(e);
    }
}
Also used : StringParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter) SerializedParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) ClassParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter) Flag(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) WrongParameterValueException(de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException) ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) TrackParameters(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)

Aggregations

TrackParameters (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)5 SerializedParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization)3 TrackedParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)3 ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)2 UnspecifiedParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)2 ClassParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)2 DynamicParameters (de.lmu.ifi.dbs.elki.gui.util.DynamicParameters)1 ELKIServiceScanner (de.lmu.ifi.dbs.elki.utilities.ELKIServiceScanner)1 Reference (de.lmu.ifi.dbs.elki.utilities.documentation.Reference)1 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)1 WrongParameterValueException (de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException)1 UnParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.UnParameterization)1 ClassListParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassListParameter)1 Flag (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag)1 Parameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter)1 RandomParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.RandomParameter)1 StringParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter)1 Pair (de.lmu.ifi.dbs.elki.utilities.pairs.Pair)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1