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

one.empty3.growth.FunctionalParameter Maven / Gradle / Ivy

There is a newer version: 2024.5.10
Show newest version
/*
 * Copyright (c) 2023. Manuel Daniel Dahmen
 *
 *
 *    Copyright 2012-2023 Manuel Daniel Dahmen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package one.empty3.growth;

import one.empty3.library.core.raytracer.tree.AlgebraicFormulaSyntaxException;
import one.empty3.library.core.raytracer.tree.AlgebricTree;
import one.empty3.library.core.raytracer.tree.TreeNodeEvalException;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FunctionalParameter extends Parameter {
    private String formula;

    public FunctionalParameter(String name, Double parameter, String formula) {
        super(name, parameter);
        this.formula = formula;
    }

    public String getFormula() {
        return formula;
    }


    public void setFormula(String formula) {
        this.formula = formula;
    }

    public Double eval(int t, int recurse) {
        recurse--;
        if (t == 0 || recurse < 0)
            return getValue();
        else {
            Map parametersValues = new HashMap<>();
            parametersValues.put("t", (double) t);
            final Parameter that = this;

            final HashMap paramsT0 = new HashMap<>();

            paramsT0.putAll(lSystem.getParams(t - 1));

            if (paramsT0.size() == 0) {
                lSystem.getInitialParameters().forEach(new BiConsumer() {
                    @Override
                    public void accept(Symbol symbol, Parameter parameter) {
                        paramsT0.put("" + symbol.getValue(), parameter);
                    }
                });
            }
            paramsT0.forEach((s, parameter) -> {
                if (/*parameter != that && */ parameter.getName() != that.getName()) {

                    parametersValues.put(s, parameter.eval(t, 0));

                }
            });
            AlgebricTree tree = null;
            try {
                tree = new AlgebricTree(getFormula(), parametersValues);
                tree.construct();
            } catch (AlgebraicFormulaSyntaxException e) {
                e.printStackTrace();
}
            try {
                Double d = (Double) (tree.eval());
                setValue(d);
                Logger.getAnonymousLogger().log(Level.INFO,""+ d);
                return d;
            } catch (TreeNodeEvalException | AlgebraicFormulaSyntaxException e) {
                e.printStackTrace();
            }

        }
        return 0.;
    }

    @Override
    public String toString() {
        String s = "";
        return "Parameter: " + getName() + " Value: " + getValue() + " Formula: " + getFormula();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy