use of org.kanonizo.exception.SystemConfigurationException in project kanonizo by kanonizo.
the class Main method getAlgorithm.
private static SearchAlgorithm getAlgorithm(String algorithmChoice) throws InstantiationException, IllegalAccessException {
Reflections r = Util.getReflections();
Set<Class<?>> algorithms = r.getTypesAnnotatedWith(Algorithm.class);
Optional<?> algorithmClass = algorithms.stream().map(cl -> {
try {
return cl.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
logger.error(e);
}
return null;
}).filter(obj -> ((SearchAlgorithm) obj).readableName().equals(algorithmChoice)).findFirst();
if (algorithmClass.isPresent()) {
SearchAlgorithm algorithm = (SearchAlgorithm) algorithmClass.get();
List<Method> requirements = Framework.getPrerequisites(algorithm);
boolean anyFail = false;
for (Method requirement : requirements) {
try {
boolean passed = (boolean) requirement.invoke(null, null);
if (!passed) {
anyFail = true;
String error = requirement.getAnnotation(Prerequisite.class).failureMessage();
logger.error("System is improperly configured: " + error);
}
} catch (InvocationTargetException e) {
logger.error(e);
}
}
if (anyFail) {
throw new SystemConfigurationException("");
}
return algorithm;
}
List<String> algorithmNames = Framework.getAvailableAlgorithms().stream().map(alg -> alg.readableName()).collect(Collectors.toList());
throw new RuntimeException("Algorithm could not be created. The list of available algorithms is given as: " + algorithmNames.stream().reduce((s, s2) -> s + "\n" + s2));
}
Aggregations