Search in sources :

Example 1 with KeyValuePair

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

the class LFValidator method checkTargetProperties.

/**
 * Check for consistency of the target properties, which are
 * defined as KeyValuePairs.
 *
 * @param targetProperties The target properties defined
 *  in the current Lingua Franca program.
 */
@Check(CheckType.EXPENSIVE)
public void checkTargetProperties(KeyValuePairs targetProperties) {
    EList<KeyValuePair> fastTargetProperties = new BasicEList<>(targetProperties.getPairs());
    fastTargetProperties.removeIf(pair -> TargetProperty.forName(pair.getName()) != TargetProperty.FAST);
    KeyValuePair fastTargetProperty = fastTargetProperties.size() > 0 ? fastTargetProperties.get(0) : null;
    if (fastTargetProperty != null) {
        // Check for federated
        for (Reactor reactor : info.model.getReactors()) {
            // Check to see if the program has a federated reactor
            if (reactor.isFederated()) {
                error("The fast target property is incompatible with federated programs.", fastTargetProperty, Literals.KEY_VALUE_PAIR__NAME);
                break;
            }
        }
        // Check for physical actions
        for (Reactor reactor : info.model.getReactors()) {
            // Check to see if the program has a physical action in a reactor
            for (Action action : reactor.getActions()) {
                if (action.getOrigin().equals(ActionOrigin.PHYSICAL)) {
                    error("The fast target property is incompatible with physical actions.", fastTargetProperty, Literals.KEY_VALUE_PAIR__NAME);
                    break;
                }
            }
        }
    }
    EList<KeyValuePair> clockSyncTargetProperties = new BasicEList<>(targetProperties.getPairs());
    // Check to see if clock-sync is defined
    clockSyncTargetProperties.removeIf(pair -> TargetProperty.forName(pair.getName()) != TargetProperty.CLOCK_SYNC);
    KeyValuePair clockSyncTargetProperty = clockSyncTargetProperties.size() > 0 ? clockSyncTargetProperties.get(0) : null;
    if (clockSyncTargetProperty != null) {
        boolean federatedExists = false;
        for (Reactor reactor : info.model.getReactors()) {
            if (reactor.isFederated()) {
                federatedExists = true;
            }
        }
        if (!federatedExists) {
            warning("The clock-sync target property is incompatible with non-federated programs.", clockSyncTargetProperty, Literals.KEY_VALUE_PAIR__NAME);
        }
    }
    EList<KeyValuePair> schedulerTargetProperties = new BasicEList<>(targetProperties.getPairs());
    schedulerTargetProperties.removeIf(pair -> TargetProperty.forName(pair.getName()) != TargetProperty.SCHEDULER);
    KeyValuePair schedulerTargetProperty = schedulerTargetProperties.size() > 0 ? schedulerTargetProperties.get(0) : null;
    if (schedulerTargetProperty != null) {
        String schedulerName = schedulerTargetProperty.getValue().getId();
        if (!TargetProperty.SchedulerOption.valueOf(schedulerName).prioritizesDeadline()) {
            // Check if a deadline is assigned to any reaction
            if (info.model.getReactors().stream().filter(reactor -> {
                // has a deadline handler.
                return ASTUtils.allReactions(reactor).stream().filter(reaction -> {
                    return reaction.getDeadline() != null;
                }).count() > 0;
            }).count() > 0) {
                warning("This program contains deadlines, but the chosen " + schedulerName + " scheduler does not prioritize reaction execution " + "based on deadlines. This might result in a sub-optimal " + "scheduling.", schedulerTargetProperty, Literals.KEY_VALUE_PAIR__VALUE);
            }
        }
    }
}
Also used : Action(org.lflang.lf.Action) KeyValuePair(org.lflang.lf.KeyValuePair) BasicEList(org.eclipse.emf.common.util.BasicEList) ImportedReactor(org.lflang.lf.ImportedReactor) Reactor(org.lflang.lf.Reactor) Check(org.eclipse.xtext.validation.Check)

Example 2 with KeyValuePair

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

the class GeneratorUtils method getLFResource.

/**
 * Return the {@code LFResource} representation of the
 * given resource.
 * @param resource The {@code Resource} to be
 *                 represented as an {@code LFResource}
 * @param srcGenBasePath The root directory for any
 * generated sources associated with the resource.
 * @param context The generator invocation context.
 * @param errorReporter An error message acceptor.
 * @return the {@code LFResource} representation of the
 * given resource.
 */
public static LFResource getLFResource(Resource resource, Path srcGenBasePath, LFGeneratorContext context, ErrorReporter errorReporter) {
    TargetDecl target = GeneratorUtils.findTarget(resource);
    KeyValuePairs config = target.getConfig();
    var targetConfig = new TargetConfig();
    if (config != null) {
        List<KeyValuePair> pairs = config.getPairs();
        TargetProperty.set(targetConfig, pairs != null ? pairs : List.of(), errorReporter);
    }
    try {
        FileConfig fc = new FileConfig(resource, srcGenBasePath, context.useHierarchicalBin());
        return new LFResource(resource, fc, targetConfig);
    } catch (IOException e) {
        throw new RuntimeException("Failed to instantiate an imported resource because an I/O error " + "occurred.");
    }
}
Also used : TargetDecl(org.lflang.lf.TargetDecl) FileConfig(org.lflang.FileConfig) KeyValuePair(org.lflang.lf.KeyValuePair) KeyValuePairs(org.lflang.lf.KeyValuePairs) IOException(java.io.IOException) TargetConfig(org.lflang.TargetConfig)

Aggregations

KeyValuePair (org.lflang.lf.KeyValuePair)2 IOException (java.io.IOException)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 Check (org.eclipse.xtext.validation.Check)1 FileConfig (org.lflang.FileConfig)1 TargetConfig (org.lflang.TargetConfig)1 Action (org.lflang.lf.Action)1 ImportedReactor (org.lflang.lf.ImportedReactor)1 KeyValuePairs (org.lflang.lf.KeyValuePairs)1 Reactor (org.lflang.lf.Reactor)1 TargetDecl (org.lflang.lf.TargetDecl)1