Giter Club home page Giter Club logo

Comments (12)

andyHa avatar andyHa commented on July 18, 2024

I just pushed a commit which added a getNames() method to Scope. So you could do something like:

    Scope s = Scope.create();
    Parser.parse("a*b+c", s);
    s.getName(); // Will contain all referenced variables

Note that I didn't update the maven release yet, but that wouldn't be a problem if required...

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

Cool just what I wanted, can you update the Maven repo, cause it just happens I'm using Maven, and would not want to add it to my nexus repo.

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

Also for convienance I think it would be conveniant for the Expr to have a function returning the scope, for places where the expression parsing and evaluation is done in different classes. Now you would need to store both expr and scope, and recall both at usage time.

from parsii.

andyHa avatar andyHa commented on July 18, 2024

Technically the scope is only used when parsing expressions. That's the place where variable names are resolved into Variable instances. Accessing a variable later on is as cheap as a field access - no more lookups are performed. This is one reason for parsii being quite fast. If you still need / want those two tied together, you could create class "Computation" or the like with two fields "expression" and "scope". That should probably do the trick?

I just released parsii-1.2 (should take about one day to be mirrored into the main repo).

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

I see. So for the following use case: let there be a user provided arbitrary expression, such as "a+b" (but may as well be x+y).

To make the calculation my initial impluse is:

 s = Scope.create()
 expr = Expr.parse(stringExpresion, s);

much later on, in different class, expr and scope passed as reference:

 names=s.getNames()
 for i=1:100000
   for name in names: 
         s.getVariable(name).setValue(get(i, name))
   res[i] = expr.evaluate()

Let us assume that get always returns some value. If I understand correctly what you say is that evaluate always returns the expression with the scope variables at parse time? (meaning that I would need to reparse the expression again and again) Am I right?

Furthermore, I could of course write myself an enclosing class, but I have its place would be right inside the parsii.

from parsii.

andyHa avatar andyHa commented on July 18, 2024

Almost - you don't need to reparse the expression and expr doesn't have a setVariable method. The code would roughly look like this:
names=s.getNames()
for i=1:10
for name:names:
Variable v = s.find(name);
v.setValue(get(i, name));
res[i] = expr.evaluate();

We could make this way more performant by providing a Scope.getVariables() method which directly returns all known variables:
for i = 1..10:
for v : s.getVariables():
v.setValue(get(i, v.getName()));
res[i] = expr.evaluate();

This way, no lookups are performed in the inner loop. Would that work for you?

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

That would be excelent. :)

from parsii.

andyHa avatar andyHa commented on July 18, 2024

I just added such a method - A snapshot is available as: parsii-1.3-20140514.161553-1.jar or 1.3-SNAPSHOT should also resolve to this revision. This might also take some hour to be mirrored...

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

So I'like now how it works, but is it neccessary to return pi and euler as these are constants?
I mean for now I would not to write an if to escape them... and all my code would certainly break if later on we introduce another constant. But the extra ifs alone would make akward to use it. Maybe we should add a getNonConstantVariableNames() function where to escape these? Any ideas for these?

from parsii.

andyHa avatar andyHa commented on July 18, 2024

I added a getter: getLocalVariables - this will exclude all global constants (as those would reside in the global root context). A maven snapshot is available...

Another sexy approach would have been using java 8: scope.getVariables().stream().filter(v -> !v.isConstant()).forEach(.....) ;-)

from parsii.

gaborbernat avatar gaborbernat commented on July 18, 2024

I cannot see the snapshot mirrored (http://mvnrepository.com/artifact/com.scireum/parsii), any ideas why?

from parsii.

andyHa avatar andyHa commented on July 18, 2024

As far as I'm aware of, you need to add the snapshot repository of sonatype
to your list of repos:
https://oss.sonatype.org/content/repositories/snapshots (Sorry I'm by no
means a maven expert).

freundliche Grüße
scireum GmbH

Andreas Haufler
Geschäftsführer


scireum GmbH, Alfred-Klingele-Str. 6, 73630 Remshalden

Unser Service - gerne für Sie da:
Vertrieb: (07151) 20637 10
Support: (07151) 20637 20 - E-Mail: [email protected]

Tel.: (07151) 20637 21 - E-Mail: [email protected]
Fax: (07151) 20637 19 - Internet: http://www.scireum.de

Geschäftsführer: Michael Haufler, Andreas Haufler
Amtsgericht Stuttgart, HRB 732171

2014-05-22 10:19 GMT+02:00 Gábor Bernát [email protected]:

I cannot see the snapshot mirrored (
http://mvnrepository.com/artifact/com.scireum/parsii), any ideas why?


Reply to this email directly or view it on GitHubhttps://github.com//issues/2#issuecomment-43860292
.

from parsii.

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.