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);
}
}
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);
}
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));
}
}
}
}
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();
}
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);
}
}
Aggregations