# Node Functions as UDF in SAP PI

In this post, I have tried to write the node functions which will help in understanding the node functions, as well as how User Defined Function works. I have written the standard node functions code as UDF .

USE ONE AS MANY:

A field that only occurs once is replicated as often as another field in second queue occurs and the context is adjusted as it is in the third queue in result queue. Here C1 is repeated twice and C2 also twice and context changes adjusted as it is in third queue.  The code is given below:

int countarr1=0;int countarr2=0;int countarr3 = 0;

int i,j,k,start;

i=j=k=0;

for(i = 0;i<arr1.length;i++)

{

if( arr1[i].equals(ResultList.CC))

countarr1++;

}// countarr1 counts value in first queue

for(j = 0;j<arr2.length;j++)

{

if( arr2[j].equals(ResultList.CC))

countarr2++;

}}// countarr2 counts value in second queue

for(k = 0;k<arr3.length;k++)

{

if( arr3[k].equals(ResultList.CC))

countarr3++;

}// countarr3 counts value in second queue

start = 0;

ArrayList<String> arrresult = new ArrayList<String>();

/*checking number of context in first and second queue are same or not

And wheteher the number of values in second and third queue are same or not*/

if(countarr1==countarr2 && (arr2.length-countarr2)==(arr3.length-countarr3) )

{

for(i = 0;i<arr1.length;i += 2)

{

// taking the value from first queue,

String temp = arr1[i];

for(j=start;j<arr2.length;j++)

{

//repeating the value based on second queue

if(arr2[j].equals(ResultList.CC))

break;

else

}

start = j+1;

}

j = 0;

// here we are adjusting the result queue based on third queue,

for(k = 0;k<arr3.length && j<arrresult.size();k++)

{

if(arr3[k].equals(ResultList.CC))

else

{

j++;

}

}

}

REMOVE CONTEXT:

We use removeContext() to remove all the context for an element. This deletes all top hierarchy levels, so that all elements of the target queue are assigned to a root element of the source queue.  The code is given below:

int i=0;

for(i = 0;i<arr1.length;i++)

{

if( !arr1[i].equals(ResultList.CC))// checking if context change is there or not

}

SPLIT BY EACH VALUE:

SplitByValue() inserts a context change in the source value queue. You can insert a context change in the queue after each value, after each value change, or after each tag without a value. The code is given below:

int i=0;

for(i = 0;i<arr1.length;i++)

{

if(arr1[i].equals(ResultList.CC))

break;

}

FORMAT BY EXAMPLE:

We use this function if you need to adjust two queues of equal length with context change from 1st queue. The mapping runtime takes the values from the first queue, and the context change from the second queue. If the two inbound queues do not have the same number of values, the mapping runtime will generate an exception. Here the first and second queue has same number of values.  The code is given below:

int countarr1=0;int countarr2=0;

int i,j,k,start;

i=j=k=0;

for(i = 0;i<arr1.length;i++)

{

if( arr1[i].equals(ResultList.CC))

countarr1++; // countarr1 counts value in first queue

}

for(j = 0;j<arr2.length;j++)

{

if( arr2[j].equals(ResultList.CC))

countarr2++; // countarr2 counts value in second queue

}

}

j=0;

if((arr1.length-countarr1)==(arr2.length-countarr2) ) //Checking whether number of values are same or not

{

for(i = 0;i<arr1.length;i++)

{

if(!arr1[i].equals(ResultList.CC))

{

while(arr2[j].equals(ResultList.CC))

j++;

j++;

}

else

}

}

COLLAPSE CONTEXT:

collapseContexts()copies the first value from all context to one context.  The code is given below:

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

{

if(i==0)

{

}

if(arr1[i].equals(ResultList.CC))

{

}

}

SORT:

It sorts all value in a queue  The code is given below:

ArrayList a1 = new ArrayList(Arrays.asList(var1));

1. Collections.sort(a1);

for(int i=0;i<a1.size();i++)

SORTBYKEY:

Sorts value in second queue based on first queue

Here Key is Float Here Key is string Here Key is Integer  The code is given below,

boolean flagint,flagstring,flagfloat;

flagint=flagfloat=flagstring= true;

String temp = key;

try{

// checking it is a character or not

if(temp.matches(“^.*[a-zA-Z].*\$”))

{

flagint = false;

flagfloat = false;

}

else{

throw new Exception() ;

}

}catch(Exception e){

flagstring = false;

}

if(flagstring==false){

try{

Integer.parseInt(temp);

}catch(NumberFormatException e)                  {

flagint = false;

}

try{

if(!temp.matches(“^([+-]?(\\d+\\.)?\\d+)\$”))// checking whether it is a floating point number or not

throw new NumberFormatException();

}catch(NumberFormatException e)

{

flagfloat = false;

}

}

if(flagint==true){

HashMap<Integer, String> hash = new HashMap<Integer, String>();// here key will be integer and value will be string

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

{

int t = Integer.parseInt(key[i]);

hash.put(t,value[i]);

}

Set<Integer> set = hash.keySet();

ArrayList<Integer> list = new ArrayList<Integer>();

2. Collections.sort(list);

for (Integer key1 : list)

}

else

if(flagfloat==true){

HashMap<Float, String> hash1 = new HashMap<Float, String>(); here key will be float and value will be string

for(int j=0;j<key.length;j++)

{

float t = Float.parseFloat(key[j]);

hash1.put(t,value[j]);

}

Set<Float> set = hash1.keySet();

ArrayList<Float> list = new ArrayList<Float>();

2. Collections.sort(list);

for (Float key1 : list)

}

else

if(flagstring==true){

HashMap<String, String> hash2 = new HashMap<String, String>(); here key will be string and value will also be string

for(int k=0;k<key.length;k++)

{

hash2.put(key[k],value[k]);

}

Set<String> set = hash2.keySet();

ArrayList<String> list = new ArrayList<String>();

2. Collections.sort(list);// sorting the value based on key

for (String key1 : list)

}

;

/                 