Actor Framework Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing the State Pattern in Actor Framework

The state pattern is a behavioral design pattern that “allow an object to alter its behavior when its internal state changes.  The object will appear to change its class.”[1]  Use this pattern if an object must change its behavior based on its internal state (i.e. if the class has two or more modes of operation such that the mode change alters the behavior of most of the class methods).  This pattern lets you avoid adding case structures that test for mode to all of your VIs, generally making for more maintainable code. This package demonstrates a straightforward way to create a family of actors that together implement the state pattern. We refer to this collection of actors as a State Pattern Actor.

This package adds a new actor, called State Actor, to your user.lib folder.  It also adds the State Pattern Actor project to your list of available sample projects.

This is an EXPERIMENTAL_FORK compatible with AF 4.1.

[1] Gamma, Helm, Johnson, and Vlissides, Design Patterns:  Elements of Reusable Object-Oriented Software, 1994, p. 305

Message was edited by: niACS, to add the latest version (1.0.1.7)

Message was edited by: niACS to add version 1.1 (LabVIEW 2013 or later).  There are two changes to State Actor.lvclass.  The Entry and Exit methods are no longer required overrides, and Actor Core now invokes the Exit method just before it returns.

Message 1 of 61
(36,265 Views)

Hi niACS,

I spent some time to analyze the behavior of this package. This is very interesting, bu I'm still wondering how the life time of the "Running State" is Managed.

I don't understand or identify  what trig the "Exit" Method of the "Running" State. Could you precise this?

Thanks for your answer.

Cordialement,
Raphael T
0 Kudos
Message 2 of 61
(13,195 Views)

Hi Jabba,

The Running state automatically transitions to Idle when it has completed its work.  Take a look at Running.lvclass:Update from Process.vi.  This is the method that gets called when Process Actor passes a new value to the state machine.

Update from Process.png

Notice that after I increment the count, I pass the value to a case structure.  If the count is 3 or more, I invoke Process.lvclass:Go to Idle.vi.  This triggers the transition from Running to Idle.

0 Kudos
Message 3 of 61
(13,195 Views)

Hi niCAS,

This is clear now. Previously, I missed the last "case structure" on the "Update from Process".

Thanks for your answer.

Cordialement,
Raphael T
0 Kudos
Message 4 of 61
(13,195 Views)

Hi niCAS,

I m implementing an application with the state pattern.

I started from the example and modified it as described in the documentation.

When I start adding more code i' running into the following error message.

Error 1448 occurred at To More Specific Class in DNV.lvlib:TDS.lvlib:Controller.lvlib:Go to Error Msg.lvclass:Do.vi:1->Actor Framework.lvlib:Actor.lvclass:Actor Core.vi:1->State Actor.lvclass:Actor Core.vi:1->DNV.lvlib:TDS.lvlib:Controller.lvlib:Base Class.lvclass:Actor Core.vi:1->Actor Framework.lvlib:Actor.lvclass:Actor.vi:16->Actor Framework.lvlib:Actor.lvclass:Actor.vi.ACBRProxyCaller.A5C0000B

Possible reason(s):

LabVIEW:  Bad type cast.  LabVIEW cannot treat the run-time value of this LabVIEW class as an instance of the given LabVIEW class.

Inheritance is ok

Any idea how to debug this?

Kind regards

0 Kudos
Message 5 of 61
(13,195 Views)

This happens when you send a message to an actor that doesn't know how to handle the mesage.

In general you will want the message to go to the parent type of the State Actor.  If a method is dynamic dispatch one of the child type actors can implement its own method override.

So, to me it looks like the message may have been for a child class and you are not in that state.

Casey

Casey Lamers


Phoenix, LLC


casey.lamers@phoenixwi.com


CLA, LabVIEW Champion


Check Out the Software Engineering Processes, Architecture, and Design track at NIWeek. 2018 I guarantee you will learn things you can use daily! I will be presenting!

0 Kudos
Message 6 of 61
(13,195 Views)

I can't find the example for the state pattern. Could someone tell me where it is?

thanks

0 Kudos
Message 7 of 61
(13,195 Views)

In LabVIEW 2012, sample projects...

CLA, CTA
0 Kudos
Message 8 of 61
(13,195 Views)

Hi,

I got it, thanks.

When I generate the project and open it, there is a missing vi called Substitute Actor.vi

So, there is no way I can run it and see how it works.

Helcio


0 Kudos
Message 9 of 61
(13,195 Views)

You need AF 4.1 installed.  I thought I had made that a prerequisite in the .vip file.  I willd double-check that.

0 Kudos
Message 10 of 61
(13,195 Views)