Search in sources :

Example 1 with Algorithm

use of org.eclipse.elk.alg.test.framework.annotations.Algorithm in project elk by eclipse.

the class TestAlgorithm method fromTestClass.

/**
 * Loads the test layout algorithms defined in the test class.
 *
 * @param testClass
 *            the test class.
 * @param errors
 *            list of error conditions encountered while evaluating the layout algorithms.
 * @return a list of layout algorithms to use during the test.
 */
public static List<TestAlgorithm> fromTestClass(final TestClass testClass, final List<Throwable> errors) {
    List<LayoutAlgorithmData> algorithmData = new ArrayList<>();
    // Obtain annotations
    Algorithm[] algorithmAnnotations = testClass.getJavaClass().getAnnotationsByType(Algorithm.class);
    AllAlgorithms allAlgorithmsAnnotation = testClass.getAnnotation(AllAlgorithms.class);
    LayoutMetaDataService service = LayoutMetaDataService.getInstance();
    if (algorithmAnnotations.length != 0 && allAlgorithmsAnnotation != null) {
        // Only one of the two may be specified
        errors.add(new Exception("If @AllAlgorithms is specified, @Algorithm cannot be used anymore."));
    } else if (allAlgorithmsAnnotation != null) {
        // Simply add all known algorithm IDs
        algorithmData.addAll(service.getAlgorithmData());
    } else {
        // Add all specifically supplied IDs as long as they refer to known layout algorithms
        for (Algorithm algAnnotation : algorithmAnnotations) {
            LayoutAlgorithmData algData = service.getAlgorithmData(algAnnotation.value());
            if (algData != null) {
                algorithmData.add(algData);
            } else {
                errors.add(new Exception("Unknown layout algorithm: " + algAnnotation.value()));
            }
        }
    }
    // Produce the list of algorithms
    List<TestAlgorithm> algorithms = new ArrayList<>();
    algorithmData.stream().sorted((data1, data2) -> data1.getId().compareTo(data2.getId())).map(algData -> new TestAlgorithm(algData)).forEach(strategy -> algorithms.add(strategy));
    return algorithms;
}
Also used : AllAlgorithms(org.eclipse.elk.alg.test.framework.annotations.AllAlgorithms) CoreOptions(org.eclipse.elk.core.options.CoreOptions) ElkUtil(org.eclipse.elk.core.util.ElkUtil) List(java.util.List) LayoutConfigurator(org.eclipse.elk.core.LayoutConfigurator) TestClass(org.junit.runners.model.TestClass) Algorithm(org.eclipse.elk.alg.test.framework.annotations.Algorithm) LayoutAlgorithmData(org.eclipse.elk.core.data.LayoutAlgorithmData) ElkNode(org.eclipse.elk.graph.ElkNode) LayoutMetaDataService(org.eclipse.elk.core.data.LayoutMetaDataService) ArrayList(java.util.ArrayList) LayoutAlgorithmData(org.eclipse.elk.core.data.LayoutAlgorithmData) ArrayList(java.util.ArrayList) AllAlgorithms(org.eclipse.elk.alg.test.framework.annotations.AllAlgorithms) Algorithm(org.eclipse.elk.alg.test.framework.annotations.Algorithm) LayoutMetaDataService(org.eclipse.elk.core.data.LayoutMetaDataService)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Algorithm (org.eclipse.elk.alg.test.framework.annotations.Algorithm)1 AllAlgorithms (org.eclipse.elk.alg.test.framework.annotations.AllAlgorithms)1 LayoutConfigurator (org.eclipse.elk.core.LayoutConfigurator)1 LayoutAlgorithmData (org.eclipse.elk.core.data.LayoutAlgorithmData)1 LayoutMetaDataService (org.eclipse.elk.core.data.LayoutMetaDataService)1 CoreOptions (org.eclipse.elk.core.options.CoreOptions)1 ElkUtil (org.eclipse.elk.core.util.ElkUtil)1 ElkNode (org.eclipse.elk.graph.ElkNode)1 TestClass (org.junit.runners.model.TestClass)1