algorithms – Formula Reader and parser java

I have a task im working on which should read formulas from db and interchange the fields in the formula with real values and evaluate them. i will give a descriptive explanation of what ive done and what im not sure how to do, also it has been pointed out that some part of my approach is poor. So i will like anyone to point me in the right direction, with a piece of code where neccessary
In the database i have such formulas

name                             formula
VEHICLE_ABOVE_15YEARS   (current-year - vehicleProdYear>15)

now there is going to be so many formulars for example OC+AC+NW + vehicleUk
all fields in the formula are boolean, but it could also be of different types, the point is that its a formula. also vehicleProdYear > 13 && currentYear < 2022

Now the fields in the formula are usually from class i have called InsurancePolicy- which is a complex object for brevity i will display a few of those fields in the class

public class InsurancePolicy extends BaseEntity {

    private Person person;
    private Vehicle vehicle;
    private InsurancePolicyStatus status;

    private LocalDate policyStart =;

    private LocalDate policyEnd = policyStart.plusYears(1).minusDays(1);

    private boolean policy_AC = true;

    private boolean policy_OC = true;

    private boolean policy_ASS;
    private InsuranceVehicle insuranceVehicle;

    private InsuranceCustomer customer;
    //  shortened

public class InsuranceVehicle {

    private Integer vehicleProdYear;

    private String vehicleMake;

    private String vehicleDamagesDesc;

    private Boolean vehicleUk;
// shortened

the goal of the task is to create some sort of exit condition in the application.
take for example if a user has a vehicle from uk vehicleUk is true amd vehicleMake is TOYOTA then we wont
continue with the insurance process hence and exit condition.


my idea was
1, first get all posible fields of insurancepolicy, insurancevehicle etc and value
2, use this fields as possible key to replace in a formula
3, use expression language to compile and execute the formula and produce a result


public Map<String, Object> getValueMapFromInsuranceVehicle(Long insuranceId) throws InvocationTargetException, IllegalAccessException {
        InsurancePolicy insurance = repository.findById(insuranceId).get();

        Method() methods = insurance.getInsuranceVehicle().getClass().getMethods();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Method m : methods) {
            if (m.getName().startsWith("get")) {
                Object value =  m.invoke(insurance.getInsuranceVehicle());
                map.put(m.getName().substring(3), value);
        // add other fields specific to our needs like currentYear
        return map;

problem with this method is it gets fields for only insurancevehicle,
the only idea i have is may be to use instanceOf iterate for each object. if i did Object value = m.invoke(insurance); with insurance object it will fail.

  public String prepareFormula(String formula, Long insuranceId) throws InvocationTargetException, IllegalAccessException {
        String formula_replaced = null;
        for (String element :  getValueMapFromInsuranceVehicle(insuranceId).keySet()) {
            if (formula.contains(element)) {
                formula_replaced  = formula.replace(element,  getValueMapFromInsuranceVehicle(insuranceId).get(element).toString());
        return formula_replaced;

for example using prepareFormula() a return value for formula “vehicleUk==TRUE” is false==true for a specific insurancepolicy gotten by id insuranceId

next step is

how to use expression language to execute the different formulars that have had their keys replaced with actual values