Search in sources :

Example 1 with Algorithm

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;
}
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 2 with Algorithm

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));
}
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)

Aggregations

Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)2 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 SearchAlgorithm (org.kanonizo.algorithms.SearchAlgorithm)2 Algorithm (org.kanonizo.annotations.Algorithm)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapter (com.google.gson.TypeAdapter)1 Expose (com.google.gson.annotations.Expose)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 InstrumentingClassLoader (com.scythe.instrumenter.instrumentation.InstrumentingClassLoader)1 MutationProperties (com.scythe.instrumenter.mutation.MutationProperties)1