Search in sources :

Example 1 with APIViolationException

use of de.lmu.ifi.dbs.elki.utilities.exceptions.APIViolationException in project elki by elki-project.

the class AbstractAlgorithm method run.

@SuppressWarnings("unchecked")
@Override
public R run(Database database) {
    final Object[] relations1;
    final Class<?>[] signature1;
    final Object[] relations2;
    final Class<?>[] signature2;
    // Build candidate method signatures
    {
        final TypeInformation[] inputs = getInputTypeRestriction();
        relations1 = new Object[inputs.length + 1];
        signature1 = new Class<?>[inputs.length + 1];
        relations2 = new Object[inputs.length];
        signature2 = new Class<?>[inputs.length];
        // First parameter is the database
        relations1[0] = database;
        signature1[0] = Database.class;
        // Other parameters are the bound relations
        for (int i = 0; i < inputs.length; i++) {
            // TODO: don't bind the same relation twice?
            // But sometimes this is wanted (e.g. using projected distances)
            relations1[i + 1] = database.getRelation(inputs[i]);
            signature1[i + 1] = Relation.class;
            relations2[i] = database.getRelation(inputs[i]);
            signature2[i] = Relation.class;
        }
    }
    // Find appropriate run method.
    try {
        Method runmethod1 = this.getClass().getMethod("run", signature1);
        return (R) runmethod1.invoke(this, relations1);
    } catch (NoSuchMethodException e) {
    // continue below.
    } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
        throw new APIViolationException("Invoking the real 'run' method failed.", e);
    } catch (InvocationTargetException e) {
        final Throwable cause = e.getTargetException();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        if (cause instanceof Error) {
            throw (Error) cause;
        }
        throw new APIViolationException("Invoking the real 'run' method failed: " + cause.toString(), cause);
    }
    try {
        Method runmethod2 = this.getClass().getMethod("run", signature2);
        return (R) runmethod2.invoke(this, relations2);
    } catch (NoSuchMethodException e) {
    // continue below.
    } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
        throw new APIViolationException("Invoking the real 'run' method failed.", e);
    } catch (InvocationTargetException e) {
        final Throwable cause = e.getTargetException();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        if (cause instanceof Error) {
            throw (Error) cause;
        }
        throw new APIViolationException("Invoking the real 'run' method failed: " + cause.toString(), cause);
    }
    throw new APIViolationException("No appropriate 'run' method found.");
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) Database(de.lmu.ifi.dbs.elki.database.Database) APIViolationException(de.lmu.ifi.dbs.elki.utilities.exceptions.APIViolationException)

Aggregations

Database (de.lmu.ifi.dbs.elki.database.Database)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 APIViolationException (de.lmu.ifi.dbs.elki.utilities.exceptions.APIViolationException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1