new JAVA method to check condition (difficult!), Please help !
Michael Zohner
Greenhorn
Joined: Nov 24, 2004
Posts: 9
posted
0
Hi Programmers !
I have a problem:
I have to flag objects which come into an application from outside if they are "acceptable" or "NOT acceptable".
There are a few conditions. The objects come with the following condition (I think this is a String). For the database it is easy to substring the relevant things and understand the logic behind this. But fopr JAVA, I have no Idea !!!
PLEASE HELP !
Example:
Object 1 with condition (exactly like this, with these brackets and brackets within brackets):
condition=
(Location = 'Berlin') and ((Papernumber = '41111') or (Papernumber = '42255') or (Papernumber = 'XNF1225'))
&& / || (and/or)
(Papernumber not in('XXY52IFR','TTZHE225','77568ZZ','12558JJ','DCSTEST1'))
&& / || (and/or)
(PaperReference not like '%125546NY4-XCDI%')
How to put it in a JAVA structure ? 10.000 of these objects with these conditions come every morning via a batch into the applicatuion and on the basis of these conditions abouve these objects should flagged with a character ('A' for acceptable and 'B' for NOT acceptale)
Could you please help me ? The problem is too, that this has to be done as fast as possible... :-)
Very important. I would be very glad, if someone could show me a java method which would do that. The input (the cionditions above) comes as ONE String.
Thankls a lot !!!
best regards
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35221
7
posted
0
You can tackle on this on several levels: - Write a grammar for a parser (e.g. Antlr or SableCC), although I'd say that's overkill here - Write a grammar for a lexer (e.g. www.jflex.de); that's probably what I'd do - Find the source code for a simple arithmetic expression evaluator and adapt it to this syntax. But with the IN and LIKE clauses it's probably just different enough to make this not worthwhile. - Build your own by using String functions and regular expressions
Hm... Sounds good, but a bit too difficult for me Could you or anyone show me a method ro example or give me some code to check out ?
- Find the source code for a simple arithmetic expression evaluator and adapt it to this syntax. But with the IN and LIKE clauses it's probably just different enough to make this not worthwhile. Where to find ? Any ideas ? :-))
- Build your own by using String functions and regular expressions. Hm, any ideas how to code ?
I would be glad to have an example to check the conditions...
Thanks a lot !!!
Michael
Michael Zohner
Greenhorn
Joined: Nov 24, 2004
Posts: 9
posted
0
It is a little bit like a tree (set/map) I think:
Lets say we have a "check matrix". This matrix contains MANY objects which contains the conditions which I meant in first thread above. The conditions are differnt in each of these objects.
It should make this:
1. The SOURCE object (which should be checked for matching conditions) comes in
2. First check: Is Attribute 1 (from SOURCE object which is coming in) available in Check matrix (in any of the objects with the conditions) ? -> IF TRUE > continue !
3. Second check: Is Attribute 2 available in REMAINING objects of the Check matrix (remaining objects, because I have to check second attribute against remaining objects after first check)? -> IF TRUE > continue !
4. Third check: Is Attribute 3 within a list of attributes (for e.g.) of the remaining objects from second check ? -> IF TRUE -> FLAG THIS INCOMING OBJECT AS "A". Otherwise (when FALSE at first/second/third check) -> FLAG AS "B".
Understand the logic ? It is a bit difficlut.
Can I do something with "TreeSet" or "TreeMap" ?
Does anyone hav an idea of the needed method ? Some source for that ?
cheers Michael
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35221
7
posted
0
I used to have an expression evaluator on my web page, but removed it - I'll put it up again and post the link here tonight. It implements a recursive descent parser, which should be sufficient for your needs, and not overly complicated.
Michael Zohner
Greenhorn
Joined: Nov 24, 2004
Posts: 9
posted
0
Great !!! Thanks !!!
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
This would be great fun to build by hand, though maybe not the best use of your or your employer's time.
Build something that can evaluate boolean "term operator term" like "true and true". I'd parse those three tokens and call a method based on operator. The "and" method would try to cast the two terms to boolean.
Next allow more operators and terms like "a and b and c" with no punctuation. Should just be a loop on the first routine to go until there are no more tokens.
Now recognize that an expression in parens is just a term. In
a and ( b or c )
( b or c ) is a term, that also happens to be another whole expression. That screams for recursion, no? Once it handles one level of parens, I bet it will handle any number of levels.
Next recognize expressions that are comparisons like ( a = b ). If you follow the pattern of obtaining "term operator term" and then calling a subroutine based on operator, you should be able to introduce new operators all the time.
The in(set) syntax is a bit strange because you'll have to know not to evaluate (set) as a nested expression. I wonder if you'd want to recognize "in" as a function and handle in(set) as a single term.
Anyhow, does this sound like fine geek entertainment or what?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Michael Zohner: Hi Programmers !
Example:
Object 1 with condition (exactly like this, with these brackets and brackets within brackets):
condition=
(Location = 'Berlin') and ((Papernumber = '41111') or (Papernumber = '42255') or (Papernumber = 'XNF1225'))
&& / || (and/or)
(Papernumber not in('XXY52IFR','TTZHE225','77568ZZ','12558JJ','DCSTEST1'))
&& / || (and/or)
(PaperReference not like '%125546NY4-XCDI%')
I'm not sure whether I understood the question correctly. is '&& / || (and/or)' what you get, or do you get either an 'and' or an 'or'-operator? If you get the second, you could replace these && with AND and || with OR and get a part of a sql-string.
If you build a database, you could send the query /condition=/WHERE / to get the result, couldn't you?
I've posted the recursive-descent parser on this page. It's not the prettiest code (I ported it from C and enhanced it), but it's fairly straightforward to extend. Let me know if you have any questions.