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

org.jamesii.mlrules.parser.nodes.NormNode Maven / Gradle / Ivy

Go to download

The main project for the external domain-specific modeling language ML-Rules, which is used to model hierarchical biochemical reaction networks with nested and attributed species

There is a newer version: 2.2.13-headless
Show newest version
/*
 *   Copyright 2015 University of Rostock
 *
 *   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 org.jamesii.mlrules.parser.nodes;

import org.jamesii.core.math.parsetree.INode;
import org.jamesii.core.math.parsetree.Node;
import org.jamesii.core.math.parsetree.ValueNode;
import org.jamesii.core.math.parsetree.variables.IEnvironment;
import org.jamesii.core.math.random.distributions.NormalDistribution;
import org.jamesii.core.math.random.generators.IRandom;
import org.jamesii.mlrules.model.Model;
import org.jamesii.mlrules.util.MLEnvironment;

/**
 * Use the RNG of the model to sample a value of a Normal distribution with the given mean and variance.
 * 
 * @author Tobias Helms
 *
 */
public class NormNode extends Node {

  private static final long serialVersionUID = 1L;

  public static String MEAN = "§mean";

  public static String VAR = "§var";

  public static String NORM = "§norm";

  @SuppressWarnings("unchecked")
  @Override
  public  N calc(IEnvironment cEnv) {
    if (cEnv instanceof MLEnvironment) {
      MLEnvironment env = (MLEnvironment) cEnv;
      Object m = env.getValue(MEAN);
      Object v = env.getValue(VAR);
      if (m instanceof Number
          && v instanceof Number) {
        NormalDistribution dist = (NormalDistribution) env.getValue(NORM);
        if (dist == null) {
          IRandom rng = (IRandom) env.getValue(Model.RNG);
          dist = new NormalDistribution(rng);
          env.setGlobalValue(NORM, dist);
        }
        dist.setMean(((Number) m).doubleValue());
        dist.setDeviation(((Number) v).doubleValue());
        return (N) new ValueNode(dist.getRandomNumber());
      }
    }
    throw new IllegalArgumentException(
        String.format("Could not compute a normal distributed number."));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy