Search in sources :

Example 1 with SearchAlgorithm

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();
    }
}
Also used : ReadableConverter(org.kanonizo.display.fx.converters.ReadableConverter) Screen(javafx.stage.Screen) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) Rectangle2D(javafx.geometry.Rectangle2D) File(java.io.File) ScriptException(javax.script.ScriptException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with SearchAlgorithm

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();
    }
}
Also used : Task(javafx.concurrent.Task) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) Label(javafx.scene.control.Label) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProgressIndicator(javafx.scene.control.ProgressIndicator) VBox(javafx.scene.layout.VBox) Prerequisite(org.kanonizo.annotations.Prerequisite)

Example 3 with SearchAlgorithm

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;
}
Also used : Arrays(java.util.Arrays) Reflections(org.reflections.Reflections) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) CoverageWriter(org.kanonizo.reporting.CoverageWriter) Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) TestCaseSelectionListener(org.kanonizo.listeners.TestCaseSelectionListener) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) TestingUtils(org.kanonizo.junit.TestingUtils) Expose(com.google.gson.annotations.Expose) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) PropertyChangeListener(java.beans.PropertyChangeListener) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Display(org.kanonizo.display.Display) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Parameters(org.junit.runners.Parameterized.Parameters) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ClassParser(org.apache.bcel.classfile.ClassParser) JsonReader(com.google.gson.stream.JsonReader) APFDFunction(org.kanonizo.algorithms.metaheuristics.fitness.APFDFunction) ArrayList(java.util.ArrayList) CsvWriter(org.kanonizo.reporting.CsvWriter) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Prerequisite(org.kanonizo.annotations.Prerequisite) JsonWriter(com.google.gson.stream.JsonWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) MiscStatsWriter(org.kanonizo.reporting.MiscStatsWriter) Iterator(java.util.Iterator) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) TestCaseOrderingWriter(org.kanonizo.reporting.TestCaseOrderingWriter) File(java.io.File) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) PropertyChangeSupport(java.beans.PropertyChangeSupport) FileReader(java.io.FileReader) NullDisplay(org.kanonizo.display.NullDisplay) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Method(java.lang.reflect.Method)

Example 4 with SearchAlgorithm

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));
}
Also used : TestSuite(org.kanonizo.framework.objects.TestSuite) ConsoleDisplay(org.kanonizo.display.ConsoleDisplay) Options(org.apache.commons.cli.Options) Reflections(org.reflections.Reflections) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) HelpFormatter(org.apache.commons.cli.HelpFormatter) MutationProperties(com.scythe.instrumenter.mutation.MutationProperties) DefaultParser(org.apache.commons.cli.DefaultParser) TestCase(org.kanonizo.framework.objects.TestCase) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) CommandLine(org.apache.commons.cli.CommandLine) Prerequisite(org.kanonizo.annotations.Prerequisite) SystemConfigurationException(org.kanonizo.exception.SystemConfigurationException) Method(java.lang.reflect.Method) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) InstrumentingClassLoader(com.scythe.instrumenter.instrumentation.InstrumentingClassLoader) Set(java.util.Set) Field(java.lang.reflect.Field) MissingOptionException(org.apache.commons.cli.MissingOptionException) Algorithm(org.kanonizo.annotations.Algorithm) Collectors(java.util.stream.Collectors) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) KanonizoFrame(org.kanonizo.display.fx.KanonizoFrame) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) Optional(java.util.Optional) Display(org.kanonizo.display.Display) LogManager(org.apache.logging.log4j.LogManager) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) SystemConfigurationException(org.kanonizo.exception.SystemConfigurationException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Reflections(org.reflections.Reflections) Prerequisite(org.kanonizo.annotations.Prerequisite)

Example 5 with SearchAlgorithm

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;
}
Also used : Arrays(java.util.Arrays) Reflections(org.reflections.Reflections) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) CoverageWriter(org.kanonizo.reporting.CoverageWriter) Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) TestCaseSelectionListener(org.kanonizo.listeners.TestCaseSelectionListener) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) TestingUtils(org.kanonizo.junit.TestingUtils) Expose(com.google.gson.annotations.Expose) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) PropertyChangeListener(java.beans.PropertyChangeListener) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Display(org.kanonizo.display.Display) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Parameters(org.junit.runners.Parameterized.Parameters) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ClassParser(org.apache.bcel.classfile.ClassParser) JsonReader(com.google.gson.stream.JsonReader) APFDFunction(org.kanonizo.algorithms.metaheuristics.fitness.APFDFunction) ArrayList(java.util.ArrayList) CsvWriter(org.kanonizo.reporting.CsvWriter) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Prerequisite(org.kanonizo.annotations.Prerequisite) JsonWriter(com.google.gson.stream.JsonWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) MiscStatsWriter(org.kanonizo.reporting.MiscStatsWriter) Iterator(java.util.Iterator) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) TestCaseOrderingWriter(org.kanonizo.reporting.TestCaseOrderingWriter) File(java.io.File) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) PropertyChangeSupport(java.beans.PropertyChangeSupport) FileReader(java.io.FileReader) NullDisplay(org.kanonizo.display.NullDisplay) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) Reflections(org.reflections.Reflections)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SearchAlgorithm (org.kanonizo.algorithms.SearchAlgorithm)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 Prerequisite (org.kanonizo.annotations.Prerequisite)4 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)3 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 TypeAdapter (com.google.gson.TypeAdapter)2 Expose (com.google.gson.annotations.Expose)2 JsonReader (com.google.gson.stream.JsonReader)2 JsonWriter (com.google.gson.stream.JsonWriter)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2