Adapted scheduler converters to reuse StandardSchedulers from main repository
diff --git a/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/impl/SchedulerConverter.java b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/impl/SchedulerConverter.java index 9edab98..4215f39 100644 --- a/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/impl/SchedulerConverter.java +++ b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/impl/SchedulerConverter.java
@@ -31,7 +31,7 @@ import org.eclipse.app4mc.amalthea.converters.common.utils.HelperUtil; import org.eclipse.app4mc.amalthea.converters.common.utils.ModelVersion; import org.eclipse.app4mc.amalthea.converters200.utils.SchedulerCache; -import org.eclipse.app4mc.amalthea.converters200.utils.StandardSchedulers; +import org.eclipse.app4mc.amalthea.converters200.utils.SchedulerConverterUtil; import org.eclipse.app4mc.util.sessionlog.SessionLogger; import org.jdom2.Document; import org.jdom2.Element; @@ -119,10 +119,10 @@ for (String name : sortedList(myCache.getStandardAlgorithmCache())) { // add standard scheduler definition - StandardSchedulers.addSchedulerDefinition(osModel, name); + SchedulerConverterUtil.addSchedulerDefinition(osModel, name); // add parameter names of scheduler for later processing - standardParameterNames.addAll(StandardSchedulers.getStandardParameters(name)); + standardParameterNames.addAll(SchedulerConverterUtil.getStandardParameterNames(name)); } for (Entry<String, List<String>> entry : sortedEntryList(myCache.getUserSpecificAlgorithmCache().entrySet())) { @@ -131,7 +131,7 @@ .map(s -> EXTENDED + s) .collect(Collectors.toList()); // add custom scheduler definition with algorithm parameters - StandardSchedulers.addSchedulerDefinition(osModel, name, params, null); + SchedulerConverterUtil.addSchedulerDefinition(osModel, name, params, null); } } @@ -140,11 +140,11 @@ if (myCache.isFirstFile()) { for (String name : sortedList(standardParameterNames)) { // add standard parameter definition - StandardSchedulers.addParameterDefinition(osModel, name); + SchedulerConverterUtil.addParameterDefinition(osModel, name); } for (String name : sortedList(myCache.getExtendedParameterCache())) { // add extended parameter definition - StandardSchedulers.addParameterDefinition(osModel, EXTENDED + name); + SchedulerConverterUtil.addParameterDefinition(osModel, EXTENDED + name); } } }
diff --git a/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/SchedulerConverterUtil.java b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/SchedulerConverterUtil.java new file mode 100644 index 0000000..b5a91f3 --- /dev/null +++ b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/SchedulerConverterUtil.java
@@ -0,0 +1,133 @@ +/** + * Copyright (c) 2021 Robert Bosch GmbH and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Robert Bosch - initial API and implementation + */ + +package org.eclipse.app4mc.amalthea.converters200.utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import org.eclipse.app4mc.amalthea.converters.common.utils.AmaltheaNamespaceRegistry; +import org.eclipse.app4mc.amalthea.converters.common.utils.HelperUtil; +import org.eclipse.app4mc.amalthea.converters200.utils.StandardSchedulers.Algorithm; +import org.eclipse.app4mc.amalthea.converters200.utils.StandardSchedulers.Parameter; +import org.eclipse.app4mc.amalthea.converters200.utils.StandardSchedulers.Type; +import org.jdom2.Element; + +public class SchedulerConverterUtil { + + private SchedulerConverterUtil() { + throw new IllegalStateException("Utility class"); + } + + public static List<String> getStandardParameterNames(String algorithmName) { + List<String> result = new ArrayList<>(); + + for (Parameter param : StandardSchedulers.getAllParametersOfAlgorithm(algorithmName)) { + result.add(param.getParameterName()); + } + + return result; + } + + public static void addParameterDefinition(Element osModel, String parameterName) { + // defaults (if no standard definition is available) + Type parameterType = Type.STRING; + boolean parameterMany = false; + boolean parameterMandatory = false; + String parameterDefaultValue = null; + + Parameter param = StandardSchedulers.getParameter(parameterName); + if (param != null) { + // parameter has standard definition + parameterType = param.getType(); + parameterMany = param.isMany(); + parameterMandatory = param.isMandatory(); + parameterDefaultValue = param.getDefaultValue(); + } + + Element paramDefElement = new Element("schedulingParameterDefinitions"); + osModel.addContent(paramDefElement); + + String idValue = HelperUtil.encodeName(parameterName) + "?type=SchedulingParameterDefinition"; + paramDefElement.setAttribute("id", idValue, AmaltheaNamespaceRegistry.getGenericNamespace("xmi")); + paramDefElement.setAttribute("name", parameterName); + paramDefElement.setAttribute("type", parameterType.getTypeName()); + paramDefElement.setAttribute("many", String.valueOf(parameterMany)); + paramDefElement.setAttribute("mandatory", String.valueOf(parameterMandatory)); + + if (parameterDefaultValue != null) { + Element paramDefDefaultElement = new Element("defaultValue"); + paramDefElement.addContent(paramDefDefaultElement); + + paramDefDefaultElement.setAttribute("type", buildTypeString(parameterType), AmaltheaNamespaceRegistry.getGenericNamespace("xsi")); + if (parameterType == Type.TIME) { + Pattern p = Pattern.compile("(-?\\d+)\\s?(s|ms|us|ns|ps)"); + Matcher m = p.matcher(parameterDefaultValue); + if(m.matches()) { + String timeValue = m.group(1); + String timeUnit = m.group(2); + paramDefDefaultElement.setAttribute("value", timeValue); + paramDefDefaultElement.setAttribute("unit", timeUnit); + } + } else { + paramDefDefaultElement.setAttribute("value", parameterDefaultValue); + } + } + } + + private static String buildTypeString(Type type) { + switch (type) { + case TIME: return "am:Time"; + case BOOL: return "am:BooleanObject"; + case INTEGER: return "am:IntegerObject"; + case FLOAT: return "am:FloatObject"; + case STRING: return "am:StringObject"; + default: + return ""; + } + } + + public static void addSchedulerDefinition(Element osModel, String algorithmName) { + Algorithm algorithm = StandardSchedulers.getAlgorithm(algorithmName); + + List<String> paramNames1 = (algorithm == null) ? Collections.emptyList() : algorithm.getAlgorithmParameterNames(); + List<String> paramNames2 = (algorithm == null) ? Collections.emptyList() : algorithm.getProcessParameterNames(); + addSchedulerDefinition(osModel, algorithmName, paramNames1, paramNames2); + } + + public static void addSchedulerDefinition(Element osModel, String algorithmName, List<String> algorithmParameters, List<String> processParameters) { + Element schedulerDefElement = new Element("schedulerDefinitions"); + osModel.addContent(schedulerDefElement); + + String idValue = HelperUtil.encodeName(algorithmName) + "?type=SchedulerDefinition"; + schedulerDefElement.setAttribute("id", idValue, AmaltheaNamespaceRegistry.getGenericNamespace("xmi")); + schedulerDefElement.setAttribute("name", algorithmName); + if ((algorithmParameters != null) && (! algorithmParameters.isEmpty())) { + schedulerDefElement.setAttribute("algorithmParameters", buildParametersString(algorithmParameters)); + } + if ((processParameters != null) && (! processParameters.isEmpty())) { + schedulerDefElement.setAttribute("processParameters", buildParametersString(processParameters)); + } + } + + private static String buildParametersString(List<String> parameters) { + return parameters.stream() + .map(p -> HelperUtil.encodeName(p) + "?type=SchedulingParameterDefinition") + .collect(Collectors.joining(" ")); + } + +}
diff --git a/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/StandardSchedulers.java b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/StandardSchedulers.java index f72a6dc..b8fb1ed 100644 --- a/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/StandardSchedulers.java +++ b/plugins/org.eclipse.app4mc.amalthea.converters.200/src/org/eclipse/app4mc/amalthea/converters200/utils/StandardSchedulers.java
@@ -1,12 +1,12 @@ /** * Copyright (c) 2021 Robert Bosch GmbH and others. - * + * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ - * + * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Robert Bosch - initial API and implementation */ @@ -15,217 +15,173 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; -import org.eclipse.app4mc.amalthea.converters.common.utils.AmaltheaNamespaceRegistry; -import org.eclipse.app4mc.amalthea.converters.common.utils.HelperUtil; -import org.jdom2.Element; - public class StandardSchedulers { + // Suppress default constructor private StandardSchedulers() { throw new IllegalStateException("Utility class"); } - // TYPES (naming convention: T_) + public enum Algorithm { + // *** ADD PREDEFINED SCHEDULER DEFINITIONS HERE *** + GROUPING( + "Grouping", + new Parameter[] {}, + new Parameter[] {}), + PRIORITY_BASED( + "PriorityBased", + new Parameter[] {}, + new Parameter[] { Parameter.PRIORITY, Parameter.PREEMPTIBLE }), - private static final String T_INTEGER = "Integer"; - private static final String T_BOOL = "Bool"; - private static final String T_TIME = "Time"; - private static final String T_STRING = "String"; + OSEK( + "OSEK", + new Parameter[] {}, + new Parameter[] { Parameter.PRIORITY, Parameter.PREEMPTIBLE, Parameter.TASK_GROUP }), + FIXED_PRIORITY_PREEMPTIVE( + "FixedPriorityPreemptive", + new Parameter[] {}, + new Parameter[] { Parameter.PRIORITY, Parameter.PREEMPTIBLE }), + FIXED_PRIORITY_PREEMPTIVE_WITH_BUDGET_ENFORCEMENT( + "FixedPriorityPreemptiveWithBudgetEnforcement", + new Parameter[] {}, + new Parameter[] { Parameter.PRIORITY, Parameter.PREEMPTIBLE, Parameter.MIN_BUDGET, Parameter.MAX_BUDGET, Parameter.REPLENISHMENT }), + DEADLINE_MONOTONIC( + "DeadlineMonotonic", + new Parameter[] {}, + new Parameter[] { Parameter.PREEMPTIBLE, Parameter.DEADLINE }), + RATE_MONOTONIC( + "RateMonotonic", + new Parameter[] {}, + new Parameter[] { Parameter.PREEMPTIBLE, Parameter.PERIOD }), - // PARAMETERS (naming convention: P_) + EARLIEST_DEADLINE_FIRST( + "EarliestDeadlineFirst", + new Parameter[] {}, + new Parameter[] { Parameter.DEADLINE }), + LEAST_LOCAL_REMAINING_EXECUTION_TIME_FIRST( + "LeastLocalRemainingExecutionTimeFirst", + new Parameter[] {}, + new Parameter[] { Parameter.EXECUTION_TIME }), + PRIORITY_BASED_ROUND_ROBIN( + "PriorityBasedRoundRobin", + new Parameter[] { Parameter.TIME_SLICE_LENGTH }, + new Parameter[] { Parameter.PRIORITY }), - private static final String P_PRIORITY = "priority"; - private static final String P_PREEMPTIBLE = "preemptible"; - private static final String P_TASK_GROUP = "taskGroup"; - private static final String P_MIN_BUDGET = "minBudget"; - private static final String P_MAX_BUDGET = "maxBudget"; - private static final String P_REPLENISHMENT = "replenishment"; - private static final String P_DEADLINE = "deadline"; - private static final String P_PERIOD = "period"; - private static final String P_EXECUTION_TIME = "executionTime"; - private static final String P_QUANT_SIZE = "quantSize"; - private static final String P_TIME_SLICE_LENGTH = "timeSliceLength"; - private static final String P_CAPACITY = "capacity"; - private static final String P_REPLENISHMENT_DELAY = "replenishmentDelay"; + P_FAIR_PD2( + "PFairPD2", + new Parameter[] { Parameter.QUANT_SIZE }, + new Parameter[] {}), + PARTLY_P_FAIR_PD2( + "PartlyPFairPD2", + new Parameter[] { Parameter.QUANT_SIZE }, + new Parameter[] {}), + EARLY_RELEASE_FAIR_PD2( + "EarlyReleaseFairPD2", + new Parameter[] { Parameter.QUANT_SIZE }, + new Parameter[] {}), + PARTLY_EARLY_RELEASE_FAIR_PD2( + "PartlyEarlyReleaseFairPD2", + new Parameter[] { Parameter.QUANT_SIZE }, + new Parameter[] {}), - // ALGORITHMS (naming convention: A_) + DEFERRABLE_SERVER( + "DeferrableServer", + new Parameter[] { Parameter.CAPACITY, Parameter.PERIOD }, + new Parameter[] {}), + POLLING_PERIODIC_SERVER( + "PollingPeriodicServer", + new Parameter[] { Parameter.CAPACITY, Parameter.PERIOD }, + new Parameter[] {}), + SPORADIC_SERVER( + "SporadicServer", + new Parameter[] { Parameter.CAPACITY, Parameter.REPLENISHMENT }, + new Parameter[] {}), + CONSTANT_BANDWIDTH_SERVER( + "ConstantBandwidthServer", + new Parameter[] { Parameter.CAPACITY, Parameter.PERIOD }, + new Parameter[] {}), + CONSTANT_BANDWIDTH_SERVER_WITH_CAPACITY_SHARING( + "ConstantBandwidthServerWithCapacitySharing", + new Parameter[] { Parameter.CAPACITY, Parameter.PERIOD }, + new Parameter[] {}); - private static final String A_GROUPING = "Grouping"; - private static final String A_PRIORITY_BASED = "PriorityBased"; + private final String algorithmName; + private final List<Parameter> algorithmParameters; + private final List<Parameter> processParameters; - private static final String A_OSEK = "OSEK"; - private static final String A_FIXED_PRIORITY_PREEMPTIVE = "FixedPriorityPreemptive"; - private static final String A_FIXED_PRIORITY_PREEMPTIVE_WITH_BUDGET_ENFORCEMENT = "FixedPriorityPreemptiveWithBudgetEnforcement"; - private static final String A_DEADLINE_MONOTONIC = "DeadlineMonotonic"; - private static final String A_RATE_MONOTONIC = "RateMonotonic"; + // private enum constructor + private Algorithm(String algorithmName, Parameter[] algorithmParameters, Parameter[] processParameters) { + this.algorithmName = algorithmName; + this.algorithmParameters = Arrays.asList(algorithmParameters); + this.processParameters = Arrays.asList(processParameters); + } - private static final String A_EARLIEST_DEADLINE_FIRST = "EarliestDeadlineFirst"; - private static final String A_LEAST_LOCAL_REMAINING_EXECUTION_TIME_FIRST = "LeastLocalRemainingExecutionTimeFirst"; - private static final String A_PRIORITY_BASED_ROUND_ROBIN = "PriorityBasedRoundRobin"; + public String getAlgorithmName() { + return algorithmName; + } - private static final String A_P_FAIR_PD2 = "PFairPD2"; - private static final String A_PARTLY_P_FAIR_PD2 = "PartlyPFairPD2"; - private static final String A_EARLY_RELEASE_FAIR_PD2 = "EarlyReleaseFairPD2"; - private static final String A_PARTLY_EARLY_RELEASE_FAIR_PD2 = "PartlyEarlyReleaseFairPD2"; + public List<Parameter> getAlgorithmParameters() { + return algorithmParameters; + } - private static final String A_DEFERRABLE_SERVER = "DeferrableServer"; - private static final String A_POLLING_PERIODIC_SERVER = "PollingPeriodicServer"; - private static final String A_SPORADIC_SERVER = "SporadicServer"; - private static final String A_CONSTANT_BANDWIDTH_SERVER = "ConstantBandwidthServer"; - private static final String A_CONSTANT_BANDWIDTH_SERVER_WITH_CAPACITY_SHARING = "ConstantBandwidthServerWithCapacitySharing"; + public List<String> getAlgorithmParameterNames() { + return algorithmParameters.stream().map(Parameter::getParameterName).collect(Collectors.toList()); + } + + public List<Parameter> getProcessParameters() { + return processParameters; + } - - public static final Map<String, ParameterDefinition> PARAMETERS; - public static final Map<String, Parameters> ALGORITHMS; - - static { - // PARAMETERS - - Map<String, ParameterDefinition> map1 = new HashMap<>(); - - map1.put(P_PRIORITY, new ParameterDefinition(T_INTEGER, false, true)); - map1.put(P_PREEMPTIBLE, new ParameterDefinition(T_BOOL, false, false, "value:false")); - map1.put(P_TASK_GROUP, new ParameterDefinition(T_INTEGER, false, true)); - map1.put(P_MIN_BUDGET, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_MAX_BUDGET, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_REPLENISHMENT, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_DEADLINE, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_PERIOD, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_EXECUTION_TIME, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_QUANT_SIZE, new ParameterDefinition(T_TIME, false, false, "value:1", "unit:ns")); - map1.put(P_TIME_SLICE_LENGTH, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_CAPACITY, new ParameterDefinition(T_TIME, false, true)); - map1.put(P_REPLENISHMENT_DELAY, new ParameterDefinition(T_TIME, false, true)); - - PARAMETERS = Collections.unmodifiableMap(map1); - - // ALGORITHMS - - Map<String, Parameters> map2 = new HashMap<>(); - - map2.put(A_GROUPING, - new Parameters( - null, - null - )); - map2.put(A_PRIORITY_BASED, - new Parameters( - null, - Arrays.asList(P_PRIORITY, P_PREEMPTIBLE) - )); - - map2.put(A_OSEK, - new Parameters( - null, - Arrays.asList(P_PRIORITY, P_PREEMPTIBLE, P_TASK_GROUP) - )); - map2.put(A_FIXED_PRIORITY_PREEMPTIVE, - new Parameters( - null, - Arrays.asList(P_PRIORITY, P_PREEMPTIBLE) - )); - map2.put(A_FIXED_PRIORITY_PREEMPTIVE_WITH_BUDGET_ENFORCEMENT, - new Parameters( - null, - Arrays.asList(P_PRIORITY, P_PREEMPTIBLE, P_MIN_BUDGET, P_MAX_BUDGET, P_REPLENISHMENT) - )); - map2.put(A_DEADLINE_MONOTONIC, - new Parameters( - null, - Arrays.asList(P_PREEMPTIBLE, P_DEADLINE) - )); - map2.put(A_RATE_MONOTONIC, - new Parameters( - null, - Arrays.asList(P_PREEMPTIBLE, P_PERIOD) - )); - - map2.put(A_EARLIEST_DEADLINE_FIRST, - new Parameters( - null, - Arrays.asList(P_DEADLINE) - )); - map2.put(A_LEAST_LOCAL_REMAINING_EXECUTION_TIME_FIRST, - new Parameters( - null, - Arrays.asList(P_EXECUTION_TIME) - )); - map2.put(A_PRIORITY_BASED_ROUND_ROBIN, - new Parameters( - Arrays.asList(P_TIME_SLICE_LENGTH), - Arrays.asList(P_PRIORITY) - )); - - map2.put(A_P_FAIR_PD2, - new Parameters( - Arrays.asList(P_QUANT_SIZE), - null - )); - map2.put(A_PARTLY_P_FAIR_PD2, - new Parameters( - Arrays.asList(P_QUANT_SIZE), - null - )); - map2.put(A_EARLY_RELEASE_FAIR_PD2, - new Parameters( - Arrays.asList(P_QUANT_SIZE), - null - )); - map2.put(A_PARTLY_EARLY_RELEASE_FAIR_PD2, - new Parameters( - Arrays.asList(P_QUANT_SIZE), - null - )); - - map2.put(A_DEFERRABLE_SERVER, - new Parameters( - Arrays.asList(P_CAPACITY, P_PERIOD), - null - )); - map2.put(A_POLLING_PERIODIC_SERVER, - new Parameters( - Arrays.asList(P_CAPACITY, P_PERIOD), - null - )); - map2.put(A_SPORADIC_SERVER, - new Parameters( - Arrays.asList(P_CAPACITY, P_REPLENISHMENT), - null - )); - map2.put(A_CONSTANT_BANDWIDTH_SERVER, - new Parameters( - Arrays.asList(P_CAPACITY, P_PERIOD), - null - )); - map2.put(A_CONSTANT_BANDWIDTH_SERVER_WITH_CAPACITY_SHARING, - new Parameters( - Arrays.asList(P_CAPACITY, P_PERIOD), - null - )); - - ALGORITHMS = Collections.unmodifiableMap(map2); + public List<String> getProcessParameterNames() { + return processParameters.stream().map(Parameter::getParameterName).collect(Collectors.toList()); + } } - static class ParameterDefinition { - private String type; - private boolean many; - private boolean mandatory; - private String[] defaultValues; + public enum Parameter { + // *** ADD PREDEFINED PARAMETER DEFINITIONS HERE *** + PRIORITY("priority", Type.INTEGER, false, true), + PREEMPTIBLE("preemptible", Type.BOOL, false, false, "false"), + TASK_GROUP("taskGroup", Type.INTEGER, false, true), + MIN_BUDGET("minBudget", Type.TIME, false, true), + MAX_BUDGET("maxBudget", Type.TIME, false, true), + REPLENISHMENT("replenishment", Type.TIME, false, true), + DEADLINE("deadline", Type.TIME, false, true), + PERIOD("period", Type.TIME, false, true), + EXECUTION_TIME("executionTime", Type.TIME, false, true), + QUANT_SIZE("quantSize", Type.TIME, false, false, "1 ns"), + TIME_SLICE_LENGTH("timeSliceLength", Type.TIME, false, true), + CAPACITY("capacity", Type.TIME, false, true), + REPLENISHMENT_DELAY("replenishmentDelay", Type.TIME, false, true); - public ParameterDefinition(String type, boolean many, boolean mandatory, String... defaultValues) { + private final String parameterName; + private final Type type; + private final boolean many; + private final boolean mandatory; + private final String defaultValue; + + // private enum constructors + private Parameter(String parameterName, Type type, boolean many, boolean mandatory) { + this(parameterName, type, many, mandatory, null); + } + + private Parameter(String parameterName, Type type, boolean many, boolean mandatory, String defaultValue) { + this.parameterName = parameterName; this.type = type; this.many = many; this.mandatory = mandatory; - this.defaultValues = defaultValues; + this.defaultValue = defaultValue; } - public String getType() { + public String getParameterName() { + return parameterName; + } + + public Type getType() { return type; } @@ -237,113 +193,78 @@ return mandatory; } - public List<String> getDefaultValues() { - return Arrays.asList(defaultValues); + public String getDefaultValue() { + return defaultValue; } } - static class Parameters { - private List<String> algorithmParameters; - private List<String> processParameters; + public enum Type { + INTEGER("Integer"), + FLOAT("Float"), + BOOL("Bool"), + TIME("Time"), + STRING("String"); - public Parameters(List<String> algorithmParameters, List<String> processParameters) { - this.algorithmParameters = (algorithmParameters == null) - ? Collections.emptyList() - : Collections.unmodifiableList(algorithmParameters); - this.processParameters = (processParameters == null) - ? Collections.emptyList() - : Collections.unmodifiableList(processParameters); + private final String typeName; + + // private enum constructor + private Type(String typeName) { + this.typeName = typeName; } - public List<String> getAlgorithmParameters() { - return algorithmParameters; - } - - public List<String> getProcessParameters() { - return processParameters; + public String getTypeName() { + return typeName; } } - public static List<String> getStandardParameters(String schedulerDefinition) { - List<String> result = new ArrayList<>(); + private static final Map<String, Parameter> PARAMETERS; + private static final Map<String, Algorithm> ALGORITHMS; - Parameters parameters = ALGORITHMS.get(schedulerDefinition); - if (parameters != null) { - result.addAll(parameters.getAlgorithmParameters()); - result.addAll(parameters.getProcessParameters()); + static { + PARAMETERS = Arrays.stream(Parameter.values()).collect(Collectors.toMap(Parameter::getParameterName, Function.identity())); + ALGORITHMS = Arrays.stream(Algorithm.values()).collect(Collectors.toMap(Algorithm::getAlgorithmName, Function.identity())); + } + + public static Algorithm getAlgorithm(String algorithmName) { + return ALGORITHMS.get(algorithmName); + } + + public static Parameter getParameter(String parameterName) { + return PARAMETERS.get(parameterName); + } + + public static List<Parameter> getAllParametersOfAlgorithm(String algorithmName) { + List<Parameter> result = new ArrayList<>(); + + Algorithm algo = ALGORITHMS.get(algorithmName); + if (algo != null) { + result.addAll(algo.getAlgorithmParameters()); + result.addAll(algo.getProcessParameters()); } - + return result; } - public static void addParameterDefinition(Element osModel, String parameterName) { - ParameterDefinition paramDef = StandardSchedulers.PARAMETERS - .getOrDefault(parameterName, new ParameterDefinition(T_STRING, false, false)); + public static List<Parameter> getAlgorithmParametersOfAlgorithm(String algorithmName) { + List<Parameter> result = new ArrayList<>(); - Element paramDefElement = new Element("schedulingParameterDefinitions"); - osModel.addContent(paramDefElement); - - String idValue = HelperUtil.encodeName(parameterName) + "?type=SchedulingParameterDefinition"; - paramDefElement.setAttribute("id", idValue, AmaltheaNamespaceRegistry.getGenericNamespace("xmi")); - paramDefElement.setAttribute("name", parameterName); - paramDefElement.setAttribute("type", paramDef.getType()); - paramDefElement.setAttribute("many", String.valueOf(paramDef.isMany())); - paramDefElement.setAttribute("mandatory", String.valueOf(paramDef.isMandatory())); - - if (! paramDef.getDefaultValues().isEmpty()) { - Element paramDefDefaultElement = new Element("defaultValue"); - paramDefElement.addContent(paramDefDefaultElement); - - paramDefDefaultElement.setAttribute("type", buildTypeString(paramDef.getType()), AmaltheaNamespaceRegistry.getGenericNamespace("xsi")); - for (String itemString : paramDef.getDefaultValues()) { - String[] itemArray = itemString.split(":"); - paramDefDefaultElement.setAttribute(itemArray[0], itemArray[1]); - } + Algorithm algo = ALGORITHMS.get(algorithmName); + if (algo != null) { + result.addAll(algo.getAlgorithmParameters()); } + + return result; } - private static String buildTypeString(String type) { - switch (type) { - case T_TIME: return "am:Time"; - case T_BOOL: return "am:BooleanObject"; - case T_INTEGER: return "am:IntegerObject"; - case T_STRING: return "am:StringObject"; - default: - return ""; + public static List<Parameter> getProcessParametersOfAlgorithm(String algorithmName) { + List<Parameter> result = new ArrayList<>(); + + Algorithm algo = ALGORITHMS.get(algorithmName); + if (algo != null) { + result.addAll(algo.getProcessParameters()); } - } - public static void addSchedulerDefinition(Element osModel, String algorithmName) { - Parameters parameters = StandardSchedulers.ALGORITHMS - .getOrDefault(algorithmName, new Parameters(null, null)); - - addSchedulerDefinition(osModel, algorithmName, parameters); - } - - public static void addSchedulerDefinition(Element osModel, String algorithmName, List<String> algorithmParameters, List<String> processParameters) { - Parameters parameters = new Parameters(algorithmParameters, processParameters); - addSchedulerDefinition(osModel, algorithmName, parameters); - } - - private static void addSchedulerDefinition(Element osModel, String algorithmName, Parameters parameters) { - Element schedulerDefElement = new Element("schedulerDefinitions"); - osModel.addContent(schedulerDefElement); - - String idValue = HelperUtil.encodeName(algorithmName) + "?type=SchedulerDefinition"; - schedulerDefElement.setAttribute("id", idValue, AmaltheaNamespaceRegistry.getGenericNamespace("xmi")); - schedulerDefElement.setAttribute("name", algorithmName); - if (! parameters.getAlgorithmParameters().isEmpty()) { - schedulerDefElement.setAttribute("algorithmParameters", buildParametersString(parameters.getAlgorithmParameters())); - } - if (! parameters.getProcessParameters().isEmpty()) { - schedulerDefElement.setAttribute("processParameters", buildParametersString(parameters.getProcessParameters())); - } - } - - private static String buildParametersString(List<String> parameters) { - return parameters.stream() - .map(p -> HelperUtil.encodeName(p) + "?type=SchedulingParameterDefinition") - .collect(Collectors.joining(" ")); + return result; } }