Loop through List(Integer)

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Tue Nov 29, 2016 2:23 pm

Ok, good to know. I think I wont do that! :D This was just an example to test the functionality.
Hmm, I am not really sure. The calcStartTime of tuple n is the calcArrivalTime of tuple n-1 as long as its the same path. This means I have to pass the calculated value between adjacent tuples. As I understand, I can get the arrivalTime of the very first tuple (calculated by calcStep1) in the second tuple by using __last_1.calcArrivalTime. After that I calculate some values and finally get the calcArrivalTime for the second tuple (calcStep2). But how should I get the calcStartTime of the third tuple (that is equal to the calcArrivalTime of the second tuple). That information is not available in the input stream of the operator calcStep2. Therefore I would need a new operator that uses the results of calcStep2. Do you know what I try to explain? Am I correct?

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Tue Nov 29, 2016 2:55 pm

Just inbetween:

__last_1 is the last seen element,
__last_2 is the element seen before,

etc.

not the first, the seconds, etc.

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Tue Nov 29, 2016 4:05 pm

Sorry, that was not described accurately enough by me.
Sure, __last_1 ist the last, __last_2 that one before the last etc.

But if I have e. g. 5 tuples, I want to achieve in calcStep2 something like this. Of course simplified, I neglect looking for the next departure times, calculating the waiting time etc.

Input:

Code: Select all

ID; timeToAdd; calcStartTime; calcArrivalTime
1; 5; 705; 0 
2; 7; 0; 0
3; 9; 0; 0
4; 11; 0; 0
5; 13; 0; 0
Output:

Code: Select all

ID; timeToAdd; calcStartTime; calcArrivalTime
1; 5; 705; 710
2; 7; 710; 717
3; 9; 717; 726
4; 11; 726; 737
5; 13; 737; 750
This means I have to use the value of tuple n-1 (the last calculated one) in the current tuple. Lets assume I am at tuple 3. __last_1.calcArrivalTime = 0 because the operator takes the value of tuple 2 of the input stream instead the calculated values. Is this correct?

In my case I want to access the values of tuple 2, calculated in calcStep2, on tuple 3, also calculated in calcStep3. If this is possible, I did something wrong in my tests.

greetings & thank you for patience!
Stefan

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Tue Nov 29, 2016 4:52 pm

Hmm. Could you explain your algorithm in another language (e.g. Java). I think I did not really got the problem ...

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Tue Nov 29, 2016 5:18 pm

No problem. Lets assume that the tuples are sorted/partitioned by uuid, pathCnt and nodeCnt then I would do in Java something like this:

Code: Select all

//tuples represents the actual input stream

int lastArrivalTime = 0;	//helper var to store the arrival time of the last tuple
for(tuple t: tuples) {
	if (t.nodeCnt == 0) {
		t.calcStartTime = t.desiredDepartureTime;
	}
	else {
		t.calcStartTime = lastArrivalTime;
	}
	
	t.calcArrivalTime = t.calcStartTime + t.timeToAdd;
	lastArrivalTime = t.calcArrivalTime;
}

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Tue Nov 29, 2016 5:45 pm

Ok ... now I see the problem. Statemap cannot use the _output_ of a former calculation but only the _input_ and you want to add the values of the last "calcuation" to the current one.

No, I think with standard map functions this is not possible, but you can use kv with only a single key, can't you? kvwrite("lastArrivalTime", ...). In this case there is only one value that is overwritten every time.

I will think about this ...

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Tue Nov 29, 2016 7:11 pm

Correct. :)

Ok, then I have to ensure that the tuples are always ordered. But that I have to do anyway.
Yes, I thought about this. But is it possible to read and write the same value for one tuple. If I do something like this:

Code: Select all

#RUNQUERY
calcStep2 := STATEMAP({
      expressions = [
         ['eIf(nodeCnt!=0, kvRead("passingVal"),desiredDepartureTime)','calcStartTime'],
         ['eIf(nodeCnt!=0, kvWrite("passingVal",toInteger(nodeCnt)),0)','tmp_calcArrivalTime'],
      ],
      KEEPINPUT = true,
      REMOVEATTRIBUTES = ['calcStartTime', 'tmp_calcArrivalTime']
   },
   calcStep1
)
Is the kvRead operation always executed before the kvWirte operation? If yes, this would be the solution...

One additional detail/question: Is it possible to execute the kvWrite in the Statemap without creating this temporary attribute tmp_calcArrivalTime?

Thanks,
Stefan

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Tue Nov 29, 2016 8:22 pm

Currently, the map expressions are evaluated in the order they are written in the expressions parameter. So if you use kvread before kvwrite, the answer is true. Important: The keys must globally unique, i.e., no other operator should read or write a key-value with the given key ;-)

Map expressions always create an output. There is no way to avoid this.

I will create an additional kvremove(key) with the same semantics as kvread but removes the value from the store.

stefan
Posts: 85
Joined: Tue Jul 12, 2016 1:03 pm

Re: Loop through List(Integer)

Post by stefan » Tue Nov 29, 2016 8:42 pm

Ok, great. That should solve my problem. I will test this tomorrow.
I will need just a few values because I can replace them. So a kvRemove function is no showstopper for this. :)

Thank you very much!

User avatar
Marco Grawunder
Posts: 272
Joined: Tue Jul 29, 2014 10:29 am
Location: Oldenburg, Germany
Contact:

Re: Loop through List(Integer)

Post by Marco Grawunder » Wed Nov 30, 2016 10:06 am

The kvremove function was very easy to implement ;-)

Additionally, there are now (in the next nightly) some functions with an additional key, where a named store can be choosen. Its a bit slower because of one more indirection but could allow a better separation of multiple queries.

And now documented, also:

https://wiki.odysseus.offis.uni-oldenbu ... e+Function

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest