how to do a substring on elements in an array?

26 views
Skip to first unread message

Mike Goshorn

unread,
Dec 27, 2011, 11:45:39 AM12/27/11
to DTRules
I have an array of strings called mvx_core_reasons

I want to set up a local variable at the top of the decision table
under contexts where I count the number of elements in the array that
start with an 'X'

so I tried this:

local int NumberOfX = the number of mvx_core_reasons where SUBSTRING
of mvx_core_reason FROM 1 to 1 == 'X'

but this gives compile errors because it doesn't recognize
mvx_core_reason.

is there anyway to get what I want?

I know that I could iterate over the array on a different decision
table and count the number there, but I was wondering if there was
anyway to fix it here as a local variable. How do you reference the
elements of the array when doing something like this?

Mike Goshorn

unread,
Dec 27, 2011, 12:22:30 PM12/27/11
to DTRules
Ok, its worse that I thought. I can't event iterate over them in a
separate Decision Table
I tried the following:

context:
for all mvx_core_reasons
conditions:
substring mvx_core_reason from 1 to 1 == 'X'
actions:
blah blah blah

it complains that it doesn't know what mvx_core_reason is on the
condition.

so again how do I do what I want to do?

mauro

unread,
Dec 27, 2011, 12:59:55 PM12/27/11
to DTRules
I am quite new to DTRules but I think that mvx_core_reason should be
loaded in the context in some way.
In order to make it you have two possibilities:

- instead of using for all in the context try to use for each (always
in the context): for each mvx_core_reason in mvx_core_reasons
- if you want to use for all I think that in the init you should add:
Add <entity> to the context of this table
> > elements of the array when doing something like this?- Hide quoted text -
>
> - Show quoted text -

paul snow

unread,
Dec 27, 2011, 2:06:34 PM12/27/11
to dtr...@googlegroups.com
Most of the interation in MaxEL and EL is based on iterating over arrays of entities.  What you have here are arrays of strings.

But we do have syntax for this, even if it isn't quite as English like that we might like.

You can create indexes like this:

context statement 1: local integer mvx_index;
context statement 2: for mvx_index = 0; mvx_index < the number of mvx_core_reasons; add 1 to mvx_index ;

Then you can test 

(string) mx_core_reasons [mvx_index]

I know this is ugly.  We could add some syntax that makes this a little prettier.  We have tests that look for a match to a string, and we could expand that to include looking for a match for a regular expression.

Hope this helps.

paul snow

unread,
Dec 27, 2011, 2:09:28 PM12/27/11
to dtr...@googlegroups.com
This is also one way of solving the problem.  Mauro's suggestion would require an Entity to hold each string, and thus convert the string into an array of entities.  (Maybe add an entity MVX_Reason_Entity with one attribute MVX_Reason).

While this would work, there are other reasons Mike won't like this solution.  

The best solution would be to extend the language a bit to handle this situation more gracefully.  See my previous post.

Paul

Mike Goshorn

unread,
Dec 27, 2011, 4:40:35 PM12/27/11
to DTRules
I tried your solution with the for loop:

when I tried the local integer mvx_index;
on line 1 and then the for stuff on the second line of the context it
complained that mvx_index did not exist.

so then I tried adding mvx_index to the job level in the edd.
as
job mvx_index integer 0 rw

then I tried this:
context: for mvx_index = 0; mvx_index < the number of
mvx_core_reasons; add 1 to mvx_index ;

condition:
substring (string) mx_core_reasons [mvx_index] from 1 to 1
== 'X'


but when I compile I get the following error:

any ideas?

Error:
Decision Table: Client_Setup_1
Filename: NY_EnrollmentRules.xls

Source: for mvx_index = 0; mvx_index < the number of
mvx_core_reasons; add 1 to mvx_index ;
Error: java.lang.Exception: Error found at Line:Char =0:12
java.lang.Exception: Can't recover from previous error(s)
Info: Old Postfix: None in XML
25 CONTEXT context
56 FOR for
124 RLONG mvx_index


Error:
Decision Table: Client_Setup_1
Filename: NY_EnrollmentRules.xls

Source: substring (string) mx_core_reasons [mvx_index] from 1
to 1 == 'X'
Error: java.lang.Exception: Error found at Line:Char =0:20
java.lang.Exception: Can't recover from previous error(s)
Info: Old Postfix: None in XML
27 CONDITION condition
138 SUBSTRING substring
83 LPAREN (
> > > - Show quoted text -- Hide quoted text -

paul snow

unread,
Dec 27, 2011, 9:05:11 PM12/27/11
to dtr...@googlegroups.com

I'll look at this tonight.

paul snow

unread,
Dec 27, 2011, 10:19:43 PM12/27/11
to dtr...@googlegroups.com
This is of course the danger of going on memory.  The syntax for the local variable should have been:

   local int max_index

   for max_index = 0; max_index < the number of mvx_core_reasons; add 1 to mvx_index;


I looked at using a regular variable instead of a local variable.  Using EL this just works.  So I expect you have a bug in MaxEL.  I did have my son generate tests for all the syntax in EL and I did find and fix a number of bugs.  You might want to compare MaxEL to EL and port over any fixes in EL's parser.cup associated with the For statement.  (Line 486 in parser.cup for EL.  Use the 5.0-SNAPSHOT branch, as that is where all the fixes are).

Hope this helps. 

Paul
Reply all
Reply to author
Forward
0 new messages