use of org.kanonizo.annotations.Algorithm in project kanonizo by kanonizo.
the class Framework method getPrerequisites.
public static List<Method> getPrerequisites(SearchAlgorithm algorithm) {
List<Method> requirements = Arrays.asList(algorithm.getClass().getMethods()).stream().filter(m -> m.isAnnotationPresent(Prerequisite.class)).collect(Collectors.toList());
Iterator<Method> it = requirements.iterator();
while (it.hasNext()) {
Method requirement = it.next();
if (Modifier.isStatic(requirement.getModifiers())) {
if (requirement.getReturnType().equals(Boolean.class) || requirement.getReturnType().equals(boolean.class)) {
if (!requirement.isAccessible()) {
requirement.setAccessible(true);
}
} else {
logger.info("Ignoring requirement " + requirement.getName() + " because the return type is not boolean");
it.remove();
}
} else {
logger.info("Ignoring requirement " + requirement.getName() + " because it is not static");
it.remove();
}
}
return requirements;
}
use of org.kanonizo.annotations.Algorithm 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