Search in sources :

Example 1 with ParameterException

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

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

the class MiniGUI method reportErrors.

/**
 * Report errors in a single error log record.
 *
 * @param config Parameterization
 */
protected void reportErrors(SerializedParameterization config) {
    StringBuilder buf = new StringBuilder(500).append("Task is not completely configured:").append(NEWLINE).append(NEWLINE);
    for (ParameterException e : config.getErrors()) {
        if (e instanceof UnspecifiedParameterException) {
            // 
            buf.append("The parameter ").append(// 
            ((UnspecifiedParameterException) e).getParameterName()).append(" is required.").append(NEWLINE);
        } else {
            buf.append(e.getMessage()).append(NEWLINE);
        }
    }
    LOG.warning(buf.toString());
    config.clearErrors();
}
Also used : ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException) UnspecifiedParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)

Example 3 with ParameterException

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

the class DynamicParameters method updateFromTrackParameters.

/**
 * Update the Parameter list from the collected options of an ELKI context
 *
 * @param track Tracked Parameters
 */
public synchronized void updateFromTrackParameters(TrackParameters track) {
    parameters.clear();
    for (TrackedParameter p : track.getAllParameters()) {
        Parameter<?> option = p.getParameter();
        String value = null;
        if (option.isDefined()) {
            if (option.tookDefaultValue()) {
                value = DynamicParameters.STRING_USE_DEFAULT + option.getDefaultValueAsString();
            } else {
                value = option.getValueAsString();
            }
        }
        if (value == null) {
            value = (option instanceof Flag) ? Flag.NOT_SET : "";
        }
        int bits = 0;
        if (option.isOptional()) {
            bits |= BIT_OPTIONAL;
        }
        if (option.hasDefaultValue() && option.tookDefaultValue()) {
            bits |= BIT_DEFAULT_VALUE;
        }
        if (value.length() <= 0) {
            if ((bits & BIT_DEFAULT_VALUE) == 0 && (bits & BIT_OPTIONAL) == 0) {
                bits |= BIT_INCOMPLETE;
            }
        } else {
            try {
                if (!option.tookDefaultValue() && !option.isValid(value)) {
                    bits |= BIT_INVALID;
                }
            } catch (ParameterException e) {
                bits |= BIT_INVALID;
            }
        }
        int depth = 0;
        {
            Object pos = track.getParent(option);
            while (pos != null) {
                pos = track.getParent(pos);
                depth += 1;
                if (depth > 10) {
                    break;
                }
            }
        }
        parameters.add(new Node(option, value, bits, depth));
    }
}
Also used : ParameterException(de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException) TrackedParameter(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter) Flag(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag)

Example 4 with ParameterException

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

ParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException)4 UnspecifiedParameterException (de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException)3 SerializedParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.SerializedParameterization)2 TrackParameters (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackParameters)2 Flag (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag)2 DynamicParameters (de.lmu.ifi.dbs.elki.gui.util.DynamicParameters)1 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)1 WrongParameterValueException (de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException)1 TrackedParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.TrackedParameter)1 ClassParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ClassParameter)1 StringParameter (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter)1