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

org.jamesii.ml3.parser.buildIns.mathFunctions.MaximumFunction Maven / Gradle / Ivy

Go to download

The Modeling Language for Linked Lives, a domain specific modeling language for agent-based computational demography.

There is a newer version: 0.2.8
Show newest version
/*
 * Copyright 2018 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.ml3.parser.buildIns.mathFunctions;

import org.jamesii.ml3.model.types.BasicType;
import org.jamesii.ml3.model.types.IType;
import org.jamesii.ml3.model.validation.typeChecking.Scope;
import org.jamesii.ml3.model.validation.typeChecking.Utils;
import org.jamesii.ml3.model.values.INumericalValue;
import org.jamesii.ml3.model.values.IValue;
import org.jamesii.ml3.parser.IParseTreeNode;
import org.jamesii.ml3.parser.buildIns.BuildIns;
import org.jamesii.ml3.simulator.context.IContext;

import java.util.Collection;
import java.util.Iterator;

/**
 * Represents the "max : Numeric x Numeric -> Numeric" function, maximum.
 * Return type is the more general of the argument types.
 */
public class MaximumFunction implements IMathFunction {
    @Override
    public IValue evaluate(Collection parameters, IContext context) {
        Iterator iter = parameters.iterator();
        IValue v1 = iter.next();
        IValue v2 = iter.next();
        if (((INumericalValue) v1).compareTo((INumericalValue) v2) >= 0) {
            return v1;
        } else
            return v2;
    }

    @Override
    @SuppressWarnings("Duplicates")
    public IType getType(Collection  parameters, Scope scope, IParseTreeNode currentNode) {
        if(!Utils.checkParameterSize(scope, parameters, 2, currentNode)){
            return BasicType.UNKNOWN;
        }
        Utils.checkNumeric(scope, currentNode, parameters);
        return Utils.getMostGeneralType(parameters);
    }

	@Override
	public String getName() {
		return BuildIns.FUNC_MAX;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy