Re: [FBDK] New array entry overwrite existing entries

38 views
Skip to first unread message

James Christensen

unread,
Nov 23, 2012, 2:34:36 PM11/23/12
to fb...@googlegroups.com
Try PART_array.value[ct-1].value = PART.value;


On Fri, Nov 23, 2012 at 1:48 AM, Karel Kruger <kareljo...@gmail.com> wrote:
> Hi All
>
> I created a basic FB to store several PART (WSTRING) input variables in an
> output PART_Array (WSTRING[)] variable, during the occurrence of several
> events (i.e. the first time the input event triggers, PART must be stored in
> PART_Array[0], the second event means storage in PART_Array[1], etc.). The
> problem is that when a new entry is stored, it overwrites all the existing
> array elements, as follows:
>
> After first event:
> PART_Array = ["1"," "," "," "," "," "]
> and after the second event:
> PART_Array = ["2","2"," "," "," "," "] (instead of PART_Array = ["1","2","
> "," "," "," "]).
>
> I give the piece of code below where I think the problem might be...what I
> gather is that because a use the same object during the storage action, it
> refers to all the values in the array? The solution might be to somehow
> reinitialize the PART or index variable? If this is the case, can anyone
> please tell me how and where to initialize which variables?
>
> package fb.rt;
> import java.util.ArrayList;
>
> import fb.datatype.*;
> import fb.rt.*;
> import fb.rt.events.*;
>
> public class STORE_TO_ARRAY extends fb.rt.FBInstance {
>
> /** Normal Execution Request */
> public final EventInput REQ = new EventInput(this);
>
> /** Execution Confirmation */
> public final EventOutput CNF = new EventOutput();
> /** EVENT NEXT */
> public final EventOutput NEXT = new EventOutput();
> /** VAR NumOfTasks:INT */
> public INT NumOfTasks = new INT();
> /** VAR current_task:INT */
> public INT current_task = new INT();
> /** VAR PART:WSTRING */
> public WSTRING PART = new WSTRING();
>
> /** VAR next_task:INT */
> public final INT next_task = new INT();
> /** VAR PART_array:WSTRING */
> public final ARRAY PART_array = new ARRAY(new WSTRING(), 6);
>
> /** The default constructor. */
> public STORE_TO_ARRAY(){
> super();
> }
> /** The index (0) of state START. */
> public static final int index_START = 0;
> /** The actions to take upon entering state START. */
> private void state_START(){
> eccState = index_START;
> serviceEvent(null);}
>
> /** The index (1) of state REQ. */
> public static final int index_REQ = 1;
> /** The actions to take upon entering state REQ. */
> private void state_REQ(){
> eccState = index_REQ;
> alg_REQ();
> serviceEvent(null);}
>
> public void serviceEvent(EventServer e){
> switch(eccState){
> case index_START:
> if ((e == REQ)) state_REQ();
>
> if (true) state_START();
> return;
> case index_REQ:
> if (true) state_START();
> return;
> }
> }
> /** ALGORITHM REQ IN Java*/
> public void alg_REQ(){
> int ct = 0;
> ct = current_task.value; //internal index variable equal to input data
> variable
>
> if(ct <= NumOfTasks.value){
>
> //write recieved input data to output variable array element
> PART_array.value[ct-1] = PART;
> if(ct == NumOfTasks.value){ //then the CNF output variable should be
> triggered
>
> for(int i = 0;i<= ct-1;i++){
> System.out.println("PART[] = " + PART_array.value[i].toString());
> //display array
>
> }
> CNF.serviceEvent(this); //output event NEXT must be triggered
> }
> else{
> NEXT.serviceEvent(this); //
> System.out.println("NEXT triggered");
> }
> }
> }
> }
>
>
> Any help will be greatly appreciated!
>
> Regards
> Karel
>
> --
> You received this message because you are subscribed to the Google Groups
> "FBDK" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/fbdk/-/1CUTZf1h8QMJ.
> To post to this group, send email to fb...@googlegroups.com.
> To unsubscribe from this group, send email to
> fbdk+uns...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/fbdk?hl=en.



--
Best regards,
Jim Christensen

Karel Kruger

unread,
Nov 24, 2012, 2:42:43 PM11/24/12
to fb...@googlegroups.com
Hi Jim

Thanks for the reply - I found a workaround, but your suggestion seems more elegant.

I have another question relating to the use of arrays in FBDK. I created a basic function block, which requires a WSTRING[6] data input variable. The FB compiles, but when I run it is gives a "truncated class file" exception - I think this is due to the array input variable (as it works when I remove it). I have also tested the FB with ARRAY[] and ANY[] datatype arrays - same issue. Can you tell what is my problem?

Strange thing is, you can have a WSTRING[6] output data variable without any trouble...

I really appreciate your help!
K
Reply all
Reply to author
Forward
0 new messages