Case-insensitive variables

17 views
Skip to first unread message

Daniel Gersten

unread,
Oct 25, 2022, 3:20:01 AM10/25/22
to Jep Java Users
Is there any chance to implement a customable parser with case-insensitive variables?

E.g.:   
jep.addVariable("x",1);
jep.parse("x+X");
Object result = jep.evaluate(); // = 2

Richard Morris

unread,
Oct 25, 2022, 3:25:38 PM10/25/22
to Jep Java Users
There already is a com.singularsys.jep.misc.CaseInsensitiveVariableTable that probably fits the bill. 
Just set up Jep with 

    Jep jep = new Jep(new CaseInsensitiveVariableTable());

There is no need to modify the configurable parser. 

Here's a simple test 
 
package com.singularsys.jeptests.unit;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.singularsys.jep.Jep;
import com.singularsys.jep.JepException;
import com.singularsys.jep.ParseException;
import com.singularsys.jep.misc.CaseInsensitiveVariableTable;

class CaseInsensitiveVariableTableTest {

    Jep jep;
    private CaseInsensitiveVariableTable civt;
    @BeforeEach
    void setUp() throws Exception {
        civt = new CaseInsensitiveVariableTable();
        jep = new Jep(civt);
    }

    @Test
    void testAdd_Get_Variable() throws JepException {
        civt.addVariable("FOO",5.0);
        double val = (double) civt.getVariable("Foo").getValue();
        assertEquals(5.0,val,0.0);
    }

    @Test
    void test_JepJava_Users_25_10_2022() throws JepException {

        jep.addVariable("x",1);
        jep.parse("x+X");
        Object result = jep.evaluate(); // = 2
        assertEquals(2.0,result);
    }
    @Test
    void test_parsing_with_CIVT() throws ParseException, JepException {
        jep.parse("X = 5");
        double r1 = jep.evaluateD();
        assertEquals(5.0,r1,0.0);
        jep.parse("Foo = x * X");
        double r2 = jep.evaluateD();
        assertEquals(25.0,r2,0.0);
        double r3 = (double) jep.getVariableValue("fOO");
        assertEquals(25.0,r3,0.0);        
    }
}

Reply all
Reply to author
Forward
0 new messages