cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping - Context

neilpadilla
Participant
0 Kudos

Hi Guys,

I have a scenario to output the result of the sample formula below.

   

     If TYPE = A, then Total1 = (RATE/UNIT) * NUM

     Else If TYPE = X, then Total2 = ((RATE/UNIT) * (DEN/NUM)) * NUM

There are instances that in the PRICE segment, the segment with type X comes first.

When I perform the test, incorrect value is produce.

Below are my Scenarios. Scenario 1 outputs the correct result and Scenario 2 outputs the incorrect result.

Scenario 1:

<CONVERT>

     <COUNT>

            <NUM>2</NUM>

            <DEN>1</DEN>

     </COUNT>

     <COUNT>

            <NUM>1000</NUM>

            <DEN>1250</DEN>

     </COUNT>

<CONVERT>

<SALES>

        <PRICE SEGMENT="1">

            <RATE>37764.00</RATE>

            <UNIT>1000</UNIT>

           <TYPE>A</TYPE>

       </PRICE>

       <PRICE SEGMENT="1">

            <RATE>0.22</RATE>

            <UNIT>1</UNIT>

            <TYPE>X</TYPE>

       </PRICE>

</SALES>

Correct Output:

     Total1: 75.528 = (37764/1000) * 2

     Total2: .55 = ((0.22/1) * (1250/1000)) * 2

Scenario 2:

<CONVERT>

     <COUNT>

            <NUM>2</NUM>

            <DEN>1</DEN>

     </COUNT>

     <COUNT>

            <NUM>1000</NUM>

            <DEN>1250</DEN>

     </COUNT>

</CONVERT>

<SALES>

       <PRICE SEGMENT="1">

            <RATE>0.22</RATE>

            <UNIT>1</UNIT>

            <TYPE>X</TYPE>

       </PRICE>

       <PRICE SEGMENT="1">

            <RATE>37764.00</RATE>

            <UNIT>1.000</UNIT>

            <TYPE>A</TYPE>

       </PRICE>

</SALES>

Incorrect Output:

     Total1: 75.528 = (37764/1000) * 2

     Total2: ((0.22*1) * (1/2)) * 1

Here's the issue on the second scenario, in the DEN/NUM, the first occurrence of segment COUNT is used for the TYPE = X.

It should always used the second occurrence of COUNT for the computation for TYPE = X.

Could you please help me on this.

Accepted Solutions (1)

Accepted Solutions (1)

engswee
Active Contributor
0 Kudos

Hi Neil

This is not a context issue as the fields in <COUNT> and <PRICE> are under different parents and therefore considered "unrelated". The issue is more related to the sorting of the PRICE segment, as it looks like your preferred sorting is for TYPE = A to come first.

One alternative is to have an intermediate mapping that sorts the price segment before your current mapping. This intermediate mapping will have a 1-1 mapping for all fields, apart from those in PRICE which will be sorted.

Refer below for an example:-

Sort Rate based on Type (ascending) using SortByKey function. Change context of Type and Rate to one level higher. Repeat the similar logic for Unit's mapping.

For mapping of Type, just perform sort. Change content of Type to one level higher.

With this, the results will have the Price segment sorted where Type A will be first.

Once you have this, the next mapping step can process your current logic accordingly.

Regards

Eng Swee

neilpadilla
Participant
0 Kudos

Hi Eng,

Great! This one works. Tried different scenarios and the output are produced as expected.

Although, I got some problem on the other fields that sometimes are empty (really empty fields, never used). For these fields, I just mapped them directly since they are not used in the computation.

I used this logic on the fields that I am sure contains values.

Thanks,

Neil

Answers (2)

Answers (2)

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Neil,

You can use copyValue[1] for type X computations.

Regards,

Mark

neilpadilla
Participant
0 Kudos

Hi Mark,

Thanks. I tried this one and it works. However in other scenarios, the output produced were incorrect.

There are instances that the second <COUNT> node does not exists.

Some scenarios have 1 <COUNT> and 2 <PRICE_SEGMENT> (vice-versa), depends on the country of the data.

Regards,

Neil

konrad_thalheim3
Active Participant
0 Kudos

Hi Neil,

at the first glace I thought that you can optimize your calculation for total2, because r/u * d/n* n = r/u *d. But then I realized that you want use the NUM value of the first context always for the secound NUM in your calculation.

Maybe you can use a combination of udfs to achieve this:

One snipped can be for always returing the last value of an context (execution type: value of contexts)

        if (contextValues != null && contextValues.length > 0) {

            String value = ResultList.SUPPRESS;

            for (int i = 0; i < contextValues.length; i++) {

                String str = contextValues[i];

                if (str != null && !ResultList.SUPPRESS.equalsIgnoreCase(str)) {

                    value = str;}}

            result.addValue(value);

        }

So it would look like this:

If TYPE = X, then Total2 = ((RATE/UNIT) * ( getLastContextValue{setContext higher(DEN/NUM)} )) * NUM

Kind regards

Konrad

former_member186851
Active Contributor
0 Kudos

Hello Neil.

Guess the issue is because of values occurring in different contexts.

Change the context to root node then try split by value at output side and lets see how it works.

neilpadilla
Participant
0 Kudos

Hi Konrad,

I tried this one but I cannot produce the correct output, mind if you share some visualization.

Regards,

Neil

konrad_thalheim3
Active Participant
0 Kudos

Hi Neil,

mapping logic looks like this:

Result for your Inputs Scenario 1 and Scenario 2 is:

Input 1:

Input 2: