Giter VIP home page Giter VIP logo

Comments (1)

fathzer avatar fathzer commented on August 24, 2024

Hi,
I'm not sure to exactly understand your question, but maybe what you need is just to override the AbstractEvaluator.evaluate(Constant ,Object) method of your evaluator.

Here is an example based on DoubleEvaluator:

import java.util.*;
import com.fathzer.soft.javaluator.*;

public class Test {
	private static class MyEvaluator extends DoubleEvaluator {
		private static final Parameters PARAMS;
		private static final Map<String, Double> MY_CONSTANTS;
		
		static {
			MY_CONSTANTS = new HashMap<>();
			MY_CONSTANTS.put("workHours", 2.0);
			MY_CONSTANTS.put("workHours1", 1.0);
			MY_CONSTANTS.put("workers", 10.0);
			PARAMS = DoubleEvaluator.getDefaultParameters();
			for (String constant : MY_CONSTANTS.keySet()) {
				PARAMS.add(new Constant(constant));
			}
		}
		
		protected MyEvaluator() {
			super(PARAMS);
		}
		
		@Override
		protected Double evaluate(Constant constant, Object evaluationContext) {
			Double result = MY_CONSTANTS.get(constant.getName());
			if (result==null) {
				result = super.evaluate(constant, evaluationContext);
			}
			if (evaluationContext instanceof Set) {
				((Set<String>) evaluationContext).add(constant.getName());
			}
			return result;
		}
		
	}
	
	public static void main(String[] args) {
		final MyEvaluator myEval = new MyEvaluator();
		test(myEval, "(workHours^2 * workers)/100");
		test(myEval, "(workHours1^2 * workers)/100");
	}
	
	private static void test(MyEvaluator myEval, String expression) {
		final Set<String> used = new HashSet<>();
		final Double result = myEval.evaluate(expression, used);
		System.out.println(expression + "="+result+" with use of "+used);
	}
}

It gives the following output:

(workHours^2 * workers)/100=0.4 with use of [workHours, workers]
(workHours1^2 * workers)/100=0.1 with use of [workHours1, workers]

Hope this could help.
Best regards,

from javaluator.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.