Re: Locate token in expression?
Mark Taylor <[email protected]> Wed, 28 Nov 2018 14:28:25 +0000
| Newsgroups | gmane.comp.java.jel.general |
|---|---|
| Message-ID | <[email protected]> |
Kontantin, for my particular purposes I don't think I'll need to do anything more than trivial graph analysis, but the trick of intercepting calls to DVmap.getTypeName was what I was after and hadn't thought of. Thanks! Mark On Tue, 27 Nov 2018, Konstantin L. Metlov wrote: > Dear Mark, > > if you consider your variable names as nodes, then every expression adds a > certain number of directed links (from the nodes, corresponding to all > used variables, to the target node, which is assigned the value of the > expression). The directed graph, formed by these nodes and links, must be > acyclic (have no loops in it). > > The question you are facing is whether adding another edge (newly compiled > expression will add a number of edges, equal to the number of variables it > references) to the DAG (directed acyclic graph) will produce a cycle in > it. > > This is quite an interesting problem, which is efficiently addressed by a > number of well known algorithms. See e.g. > > https://cs.stackexchange.com/questions/65633/check-if-adding-an-edge-to-a-dag-results-in-a-cycle > > JEL can help you to obtain a list of the referenced variables. You can > catch amd collect them in the gnu.jel.DVMap.getTypeName(String name) > method of your dynamic variable resolver, which is called for every > potential variable name JEL encounters in the expression. > > With the best regards, > Konstantin. > > > Dear Konstantin, > > > > in my application, the user can define a named value at run time > > by supplying a JEL expression, and this name can be used in > > subsequent definitions. As a convenience, they can also > > redefine an existing name by editing the expression. > > > > So for instance one can define a name "A" as "B + C", > > assuming that B and C are already known to JEL. > > Later, one can redefine "A" as, e.g., "2*B + C". So far so good. > > However, if C has already been defined in terms of A, there's trouble: > > the definition is recursive, and evaluation never terminates. > > > > So, I want some way to catch this condition. If I can analyse > > the expression "2*B + C" to see whether it contains any > > (explicit or implicit) reference to the name "A", > > then I can issue an error message and prevent the user from > > assigning the new expression as the value of "A". > > > > I've had a look at the JEL API (though not the source code) and > > I have some ideas of what might work ... but I'm not sure. > > Is this possible with the existing API? If so, could you give me > > an outline of how to go about it? > > > > Many thanks, > > > > Mark > > > > -- > > Mark Taylor Astronomical Programmer Physics, Bristol University, UK > > [email protected] +44-117-9288776 http://www.star.bris.ac.uk/~mbt/ > > > > _______________________________________________ > > Help-jel mailing list > > [email protected] > > https://lists.gnu.org/mailman/listinfo/help-jel > > > > > -- Mark Taylor Astronomical Programmer Physics, Bristol University, UK [email protected] +44-117-9288776 http://www.star.bris.ac.uk/~mbt/