Giter Site home page Giter Site logo

Comments (7)

uklimaschewski avatar uklimaschewski commented on July 18, 2024

There is this method:

  /**
   * Returns all variables or constants that are used i the expression, excluding the standard
   * constants like e.g. <code>PI</code> or <code>TRUE</code> and <code>FALSE</code>.
   *
   * @return All used variables and constants less the standard constants.
   * @throws ParseException If there were problems while parsing the expression.
   */
  public Set<String> getUsedVariables() throws ParseException;

from evalex.

VincentJoshuaET avatar VincentJoshuaET commented on July 18, 2024

There is this method:

  /**
   * Returns all variables or constants that are used i the expression, excluding the standard
   * constants like e.g. <code>PI</code> or <code>TRUE</code> and <code>FALSE</code>.
   *
   * @return All used variables and constants less the standard constants.
   * @throws ParseException If there were problems while parsing the expression.
   */
  public Set<String> getUsedVariables() throws ParseException;

In version 2.x, getUsedVariables and getDeclaredVariables are different. Used variables are variables inside the expression string. Declared variables are variables that we input with with or and. They may or may not be equal.

from evalex.

VincentJoshuaET avatar VincentJoshuaET commented on July 18, 2024

https://github.com/ezylang/EvalEx/blob/2.x/src/main/java/com/udojava/evalex/Expression.java

/**
   * Exposing declared variables in the expression.
   *
   * @return All declared variables.
*/
public Set<String> getDeclaredVariables() {
    return Collections.unmodifiableSet(variables.keySet());
}
/**
   * Returns a list of the variables in the expression.
   *
   * @return A list of the variable names in this expression.
   */
  public List<String> getUsedVariables() {
    List<String> result = new ArrayList<String>();
    Tokenizer tokenizer = new Tokenizer(expressionString);
    while (tokenizer.hasNext()) {
      Token nextToken = tokenizer.next();
      String token = nextToken.toString();
      if (nextToken.type != TokenType.VARIABLE || DEFAULT_CONSTANTS.containsKey(token)) {
        continue;
      }
      result.add(token);
    }
    return result;
}

from evalex.

uklimaschewski avatar uklimaschewski commented on July 18, 2024

There are two possibilities.
One is to write an own data accessor that allows you to also control the declared variables.
Or you could code it like this, checking the data accessor for each used variable:

  void checkVariables() throws ParseException {

    Expression expression = new Expression("a+B*b-A/PI*(1/2)*pi+e-E+a").with("a", 12);

    DataAccessorIfc dataAccessor = expression.getConfiguration().getDataAccessorSupplier().get();

    for (String variable : expression.getUsedVariables()) {
      if (dataAccessor.getData(variable) == null) {
        System.out.println("Variable not declared: " + variable);
      }
    }
  }

from evalex.

VincentJoshuaET avatar VincentJoshuaET commented on July 18, 2024

Will it not be possible for a straightforward method anymore like in 2.x?

from evalex.

uklimaschewski avatar uklimaschewski commented on July 18, 2024

Sure is this possible, but why not a method that directly returns the undeclared variables? e.g. public Set<String> getUndeclaredVariables() ?

from evalex.

VincentJoshuaET avatar VincentJoshuaET commented on July 18, 2024

Yeah that would work too! Or maybe a method that checks if the expression can be evaluated or not (because some variables needed were not declared)

from evalex.

Related Issues (20)

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.