use of org.kanonizo.algorithms.SearchAlgorithm in project kanonizo by kanonizo.
the class KanonizoFrame method initialize.
@Override
public void initialize(URL location, ResourceBundle resources) {
assert sourceTree != null : "fx:id sourceTree was not injected";
this.fw = Framework.getInstance();
fw.addPropertyChangeListener(Framework.ROOT_FOLDER_PROPERTY_NAME, (e) -> {
File newRoot = (File) e.getNewValue();
rootFolderTextField.setText(newRoot.getAbsolutePath());
sourceTree.setRoot(GuiUtils.createDynamicFileTree(newRoot));
testTree.setRoot(GuiUtils.createDynamicFileTree(newRoot));
});
fw.addPropertyChangeListener(Framework.ALGORITHM_PROPERTY_NAME, (e) -> {
SearchAlgorithm alg = (SearchAlgorithm) e.getNewValue();
// remove existing children
paramLayout.getChildren().clear();
addParams(alg, paramLayout, true);
addErrors(alg);
});
fw.addPropertyChangeListener(Framework.INSTRUMENTER_PROPERTY_NAME, (e) -> {
org.kanonizo.framework.instrumentation.Instrumenter inst = (org.kanonizo.framework.instrumentation.Instrumenter) e.getNewValue();
instParamLayout.getChildren().clear();
addParams(inst, instParamLayout, false);
addErrors(inst);
});
fw.addPropertyChangeListener(Framework.LIBS_PROPERTY_NAME, (e) -> {
libs.getItems().add((File) e.getNewValue());
});
fw.setDisplay(this);
Screen main = Screen.getPrimary();
Rectangle2D bounds = main.getVisualBounds();
rootFolderTextField.setText(fw.getRootFolder().getAbsolutePath());
addSourceListeners();
addTestListeners();
addLibListeners();
selectRoot.setOnAction(ev -> selectRoot());
try {
algorithmChoices.getItems().addAll(Framework.getAvailableAlgorithms());
algorithmChoices.setConverter(new ReadableConverter());
algorithmChoices.valueProperty().addListener((ov, t, t1) -> {
SearchAlgorithm alg = (SearchAlgorithm) ov.getValue();
fw.setAlgorithm(alg);
});
algorithmChoices.getSelectionModel().select(fw.getAlgorithm());
instrumenterChoices.getItems().addAll(Framework.getAvailableInstrumenters());
instrumenterChoices.setConverter(new ReadableConverter());
instrumenterChoices.valueProperty().addListener((ov, t, t1) -> {
org.kanonizo.framework.instrumentation.Instrumenter inst = (org.kanonizo.framework.instrumentation.Instrumenter) ov.getValue();
fw.setInstrumenter(inst);
});
instrumenterChoices.getSelectionModel().select(fw.getInstrumenter());
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.kanonizo.algorithms.SearchAlgorithm in project kanonizo by kanonizo.
the class KanonizoFrame method addErrors.
private void addErrors(Object alg) {
if (alg instanceof SearchAlgorithm) {
bottom.getChildren().removeAll(activeErrors);
activeErrors.clear();
ProgressIndicator p = new ProgressIndicator();
List<Method> prerequisites = Framework.getPrerequisites((SearchAlgorithm) alg);
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
boolean anyFail = false;
int row = 2;
// run all prerequisites, not terminating on first failure
for (int i = 0; i < prerequisites.size(); i++) {
Method requirement = prerequisites.get(i);
try {
boolean passed = (boolean) requirement.invoke(null, null);
if (!passed) {
anyFail = true;
// find readable error message
String error = requirement.getAnnotation(Prerequisite.class).failureMessage();
Label er = new Label(error);
activeErrors.add(er);
// css styling to make errors red
er.getStyleClass().add("error");
int errorRow = row++;
Platform.runLater(() -> bottom.add(er, 0, errorRow, 2, 1));
}
} catch (InvocationTargetException e) {
logger.error(e);
}
}
// if any pre-requisite failed, we can't run the algorithm
if (anyFail && !goButton.isDisabled()) {
goButton.setDisable(true);
} else if (!anyFail) {
goButton.setDisable(false);
}
return null;
}
};
// show progress indicator over original layout
VBox box = new VBox(p);
box.setAlignment(Pos.CENTER);
task.setOnSucceeded(e -> {
mainLayout.getChildren().remove(box);
borderPane.setDisable(false);
});
mainLayout.getChildren().add(box);
borderPane.setDisable(true);
new Thread(task).start();
}
}
use of org.kanonizo.algorithms.SearchAlgorithm 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.algorithms.SearchAlgorithm 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));
}
use of org.kanonizo.algorithms.SearchAlgorithm in project kanonizo by kanonizo.
the class Framework method getAvailableAlgorithms.
public static List<SearchAlgorithm> getAvailableAlgorithms() throws InstantiationException, IllegalAccessException {
Reflections r = Util.getReflections();
Set<Class<?>> algorithms = r.getTypesAnnotatedWith(Algorithm.class);
List<SearchAlgorithm> algorithmsInst = algorithms.stream().map(cl -> {
try {
return (SearchAlgorithm) cl.newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
throw new RuntimeException("Could not instantiate one of more search algorithms");
}).collect(Collectors.toList());
return algorithmsInst;
}
Aggregations