Question
Cognizant Technology Solutions India PVT Ltd
IN
Last activity: 6 Jul 2018 5:37 EDT
Creating a Fibonacci series
How do we create a Fibonacci series using activity in PEGA?
***Updated by moderator: Lochan to update platform capability***
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Pegasystems Inc.
US
I'll need to know more about your expected use case/requirement as well as inputs/outputs.
Pegasystems Inc.
GB
First attempt for you - it's an Activity....
I have cheated twice here; once because of my brain refusing to solve the initial "what-happens-when-there-are-fewer-than-two-items-in-the-list" problem; and the second time , to work round an issue I was having get the expression language to recognize my data type....
First: here's the example output ; first 10 fibs:
First attempt for you - it's an Activity....
I have cheated twice here; once because of my brain refusing to solve the initial "what-happens-when-there-are-fewer-than-two-items-in-the-list" problem; and the second time , to work round an issue I was having get the expression language to recognize my data type....
First: here's the example output ; first 10 fibs:
<pagedata>
<pxObjClass />
<pyLabel>Count is:10</pyLabel>
<pzStatus>valid</pzStatus>
<Accumulator REPEATINGTYPE="PageList">
<rowdata REPEATINGINDEX="1">
<pyValue>0</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="2">
<pyValue>1</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="3">
<pyValue>1</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="4">
<pyValue>2</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="5">
<pyValue>3</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="6">
<pyValue>5</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="7">
<pyValue>8</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="8">
<pyValue>13</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="9">
<pyValue>21</pyValue>
</rowdata>
<rowdata REPEATINGINDEX="10">
<pyValue>34</pyValue>
</rowdata>
</Accumulator>
</pagedata>
So firstly - I defined a Property (".Accumulator") PageList of Type 'SingleValue-Integer' - this will hold the sequence.
Then my first cheat: I preloaded it with 0 and 1:
Property-Set .Accumulator(1).pyValue 0
Property-Set .Accumulator(2).pyValue 1
I set up a Param.max (the maximum size of the list) and a Local.count (the current number of items in the list; and (because PRPC has 1-based lists) it's also the current item to be added.
I set up a 'Loop' of type 'for' : I had some issues using the <current> keyword in my expression language otherwise.....so this is a weird implementation detail I have introduced.
So I run the loop , but I'm still keeping count in a 'local.count' variable.
I use a JUMP step like this to exit the loop:
local.count>=Param.max
The second cheat is using a java step - to add two numbers :-\ .
Because I couldn't this Property-Set Expression to work (it was concat'ing my numbers as strings).
local.new_number=.Accumulator(local.count-1).pyValue +.Accumulator(local.count-2).pyValue
.Accumulator(local.count).pyValue=local.new_number
So I transfered to two locals and did this:
oLog.infoForced("P1:"+p1);
oLog.infoForced("P2:"+p2);
new_number=p1 + p2;
Here's a screenshot of the activity - I'll try and fix this up when I get chance.
If anybody can shed some light on why I can't add two numbers; lets me know !
-
DHARMARAJU RACHURI Mayank Arya Vikash Kumar Sahu
Pegasystems Inc.
IN
Hi ,
PFA for solution.
Regards,
Vinay Reddy :)
Pegasystems Inc.
GB
So: the behaviour wherein my expression :
.
pxResults(1).pyValue + .pxResults(2).pyValue
Was performing a String Concatenation, rather than a arithmetic addition is down to this:
If I defined a Page on 'Page and Classes' - but did NOT specify a Class; the Expression was (apparently) assuming that the embedded Property was of type text.
This is weird - since it *did* correctly assign a 'pxObjClass' to the page at (designer studio) runtime; and correctly resolved the property .Accumulator with the correct type.
In fact: (aside from the subsequent validation failures; "page-messages"); the pages (in XML format) look identical....(attaching for reference).
Anyway: either of these three things eliminates this issue:
1. Explicitly identify the Class of the Page "Results"
2. Remove the Definition of "Results" and the leave all Step Page Blank.
3. Remove the Definition of "Results" and enter "Primary" in the Step Pages.
Additionally: I have remove the JUMP from my loop - it is not needed; but I still found I needed a 'shadow' variable for the automatic position index "<current>" - since I wasn't able to get the following expression to save:
.Accumulator(<current>-1).pyValue + .Accumulator(<current>-2).pyValue
So: the behaviour wherein my expression :
.
pxResults(1).pyValue + .pxResults(2).pyValue
Was performing a String Concatenation, rather than a arithmetic addition is down to this:
If I defined a Page on 'Page and Classes' - but did NOT specify a Class; the Expression was (apparently) assuming that the embedded Property was of type text.
This is weird - since it *did* correctly assign a 'pxObjClass' to the page at (designer studio) runtime; and correctly resolved the property .Accumulator with the correct type.
In fact: (aside from the subsequent validation failures; "page-messages"); the pages (in XML format) look identical....(attaching for reference).
Anyway: either of these three things eliminates this issue:
1. Explicitly identify the Class of the Page "Results"
2. Remove the Definition of "Results" and the leave all Step Page Blank.
3. Remove the Definition of "Results" and enter "Primary" in the Step Pages.
Additionally: I have remove the JUMP from my loop - it is not needed; but I still found I needed a 'shadow' variable for the automatic position index "<current>" - since I wasn't able to get the following expression to save:
.Accumulator(<current>-1).pyValue + .Accumulator(<current>-2).pyValue