All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protelis.vm.impl.SimpleProgramImpl Maven / Gradle / Ivy

There is a newer version: 17.6.0
Show newest version
/*
 * Copyright (C) 2022, Danilo Pianini and contributors listed in the project's build.gradle.kts file.
 *
 * This file is part of Protelis, and is distributed under the terms of the GNU General Public License,
 * with a linking exception, as described in the file LICENSE.txt in this project's top directory.
 */
package org.protelis.vm.impl;

import java.util.Objects;
import java.util.Optional;

import org.protelis.lang.interpreter.ProtelisAST;
import org.protelis.parser.protelis.ProtelisModule;
import org.protelis.vm.ExecutionContext;
import org.protelis.vm.ProtelisProgram;

/**
 * Base implementation of {@link ProtelisProgram}.
 */
public final class SimpleProgramImpl implements ProtelisProgram {

    private static final long serialVersionUID = -986976491484860840L;
    private static final String DEFAULT_PROGRAM_NAME = "default_module:default_program";
    private final ProtelisAST prog;
    private final String name;

    /**
     * @param source
     *            Original {@link ProtelisProgram} parsed by Xtext. Used to get the
     *            module name.
     * @param program
     *            evaluation tree
     */
    public SimpleProgramImpl(
            final ProtelisModule source,
            final ProtelisAST program) {
        this(Optional.ofNullable(source).map(ProtelisModule::getName).orElse(DEFAULT_PROGRAM_NAME), program);
    }

    /**
     * @param pName
     *            Program name
     * @param program
     *            evaluation tree
     */
    public SimpleProgramImpl(
            final String pName,
            final ProtelisAST program) {
        name = Objects.requireNonNull(pName);
        prog = Objects.requireNonNull(program);
    }

    @Override
    public Object compute(final ExecutionContext context) {
        return prog.eval(context);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name + "\n" + prog;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy