Search in sources :

Example 6 with Parameter

use of org.lflang.lf.Parameter in project lingua-franca by lf-lang.

the class PortInstance method setInitialWidth.

/**
 * Set the initial multiport width, if this is a multiport, from the widthSpec
 * in the definition. This will be set to -1 if the width cannot be determined.
 * @param errorReporter For reporting errors.
 */
private void setInitialWidth(ErrorReporter errorReporter) {
    // If this is a multiport, determine the width.
    WidthSpec widthSpec = definition.getWidthSpec();
    if (widthSpec != null) {
        if (widthSpec.isOfVariableLength()) {
            errorReporter.reportError(definition, "Variable-width multiports not supported (yet): " + definition.getName());
        } else {
            isMultiport = true;
            // Determine the initial width, if possible.
            // The width may be given by a parameter or even sum of parameters.
            width = 0;
            for (WidthTerm term : widthSpec.getTerms()) {
                Parameter parameter = term.getParameter();
                if (parameter != null) {
                    Integer parameterValue = parent.initialIntParameterValue(parameter);
                    // Only a Literal is supported.
                    if (parameterValue != null) {
                        width += parameterValue;
                    } else {
                        width = -1;
                        return;
                    }
                } else if (term.getWidth() != 0) {
                    width += term.getWidth();
                } else {
                    width = -1;
                    return;
                }
            }
        }
    }
}
Also used : Parameter(org.lflang.lf.Parameter) WidthTerm(org.lflang.lf.WidthTerm) WidthSpec(org.lflang.lf.WidthSpec)

Example 7 with Parameter

use of org.lflang.lf.Parameter in project lingua-franca by lf-lang.

the class PythonParameterGenerator method generatePythonGetters.

/**
 * Generate Python code getters for parameters of reactor 'decl'.
 *
 * @param decl The reactor declaration
 * @return The generated code as a StringBuilder
 */
public static String generatePythonGetters(ReactorDecl decl) {
    List<String> lines = new ArrayList<>();
    for (Parameter param : getAllParameters(decl)) {
        if (!param.getName().equals("bank_index")) {
            lines.addAll(List.of("@property", "def " + param.getName() + "(self):", "    return self._" + param.getName() + " # pylint: disable=no-member", ""));
        }
    }
    // Create a special property for bank_index
    lines.addAll(List.of("@property", "def bank_index(self):", "    return self._bank_index # pylint: disable=no-member", ""));
    return String.join("\n", lines);
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.lflang.lf.Parameter)

Example 8 with Parameter

use of org.lflang.lf.Parameter in project lingua-franca by lf-lang.

the class CGeneratorExtension method getNetworkDelayLiteral.

/**
 * Given a connection 'delay' predicate, return a string that represents the
 * interval_t value of the additional delay that needs to be applied to the
 * outgoing message.
 *
 * The returned additional delay in absence of after on network connection
 * (i.e., if delay is passed as a null) is  NEVER. This has a special
 * meaning in C library functions that send network messages that carry
 * timestamps (@see send_timed_message and send_port_absent_to_federate
 * in lib/core/federate.c). In this case, the sender will send its current
 * tag as the timestamp of the outgoing message without adding a microstep delay.
 * If the user has assigned an after delay to the network connection (that
 * can be zero) either as a time value (e.g., 200 msec) or as a literal
 * (e.g., a parameter), that delay in nsec will be returned.
 *
 * @param delay
 * @param generator
 * @return
 */
public static String getNetworkDelayLiteral(Delay delay) {
    String additionalDelayString = "NEVER";
    if (delay != null) {
        Parameter p = delay.getParameter();
        TimeValue tv;
        if (delay.getParameter() != null) {
            // The parameter has to be parameter of the main reactor.
            // And that value has to be a Time.
            tv = ASTUtils.getDefaultAsTimeValue(p);
        } else {
            tv = ASTUtils.toTimeValue(delay.getTime());
        }
        additionalDelayString = Long.toString(tv.toNanoSeconds());
    }
    return additionalDelayString;
}
Also used : Parameter(org.lflang.lf.Parameter) TimeValue(org.lflang.TimeValue)

Example 9 with Parameter

use of org.lflang.lf.Parameter in project lingua-franca by lf-lang.

the class ValueGenerator method getInitializerList.

/**
 * Create a list of parameter initializers in target code in the context
 * of an reactor instantiation.
 *
 * This respects the parameter assignments given in the reactor
 * instantiation and falls back to the reactors default initializers
 * if no value is assigned to it.
 *
 * @param param The parameter to create initializers for
 * @return A list of initializers in target code
 */
public List<String> getInitializerList(Parameter param, Instantiation i) {
    List<Assignment> assignments = i.getParameters().stream().filter(it -> it.getLhs() == param).collect(Collectors.toList());
    if (// Case 0: The parameter was not overwritten in the instantiation
    assignments.isEmpty())
        return getInitializerList(param);
    // Case 1: The parameter was overwritten in the instantiation
    List<String> list = new ArrayList<>();
    if (assignments.get(0) == null)
        return list;
    for (Value init : assignments.get(0).getRhs()) list.add(getTargetValue(init, ASTUtils.isOfTimeType(param)));
    return list;
}
Also used : Assignment(org.lflang.lf.Assignment) Delay(org.lflang.lf.Delay) Time(org.lflang.lf.Time) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) StateVar(org.lflang.lf.StateVar) Parameter(org.lflang.lf.Parameter) List(java.util.List) Value(org.lflang.lf.Value) Assignment(org.lflang.lf.Assignment) Instantiation(org.lflang.lf.Instantiation) TimeValue(org.lflang.TimeValue) TimeUnit(org.lflang.TimeUnit) ASTUtils(org.lflang.ASTUtils) ArrayList(java.util.ArrayList) Value(org.lflang.lf.Value) TimeValue(org.lflang.TimeValue)

Aggregations

Parameter (org.lflang.lf.Parameter)9 ArrayList (java.util.ArrayList)4 TimeValue (org.lflang.TimeValue)3 Time (org.lflang.lf.Time)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ASTUtils (org.lflang.ASTUtils)2 TimeUnit (org.lflang.TimeUnit)2 Assignment (org.lflang.lf.Assignment)2 Instantiation (org.lflang.lf.Instantiation)2 StateVar (org.lflang.lf.StateVar)2 Test (org.junit.jupiter.api.Test)1 CodeBuilder (org.lflang.generator.CodeBuilder)1 Action (org.lflang.lf.Action)1 Delay (org.lflang.lf.Delay)1 Expression (org.lflang.lf.Expression)1 ImportedReactor (org.lflang.lf.ImportedReactor)1 Input (org.lflang.lf.Input)1 Literal (org.lflang.lf.Literal)1 Model (org.lflang.lf.Model)1