06-08-2012 01:24 PM
HI,
I might be making this more difficult thatn it should be, or I'm just lost in OOP land.
Given a gpib with several devices, I want to be able to wire a single (polymorphic??) vi that figures out which device its to talk to and does the operation. For example, Init.vi decides whether it's initializing devA, devB, or devC. So Init.vi is the polymorphic vi that contains InitdevA, InitdevB, and InitdevC.
The goal being to avoid a big case statement and to allow quick change when the devices on the gpib change or get added.
Thing is, if I create a typedef for each dev, I need to be able to programatically select the typedef to wire to the terminal when calling Init.vi.
I tried controls and classes, but can't seem to figure out how to index an array of "things" to pass to Init.vi.
With classes I could create an array of classes that inherited from a top (for example devA, devB, devC inherit the main type from Equip). But I couldnt get a class out of the array that matched the class on devA, devB, etc. So the polymorphic selection didn't work and it was always calling the first vi listed in the polymorph.
Any pointers/comments/ideas/examples appreciated.
Regards,
Mac
06-08-2012 01:39 PM
It sounds like you are trying to invent the "Factory pattern" where it accepts a selector value and spits out the class you want.
What may help you is to take a look at this thread where Cory was asking about LVOOP and I offered up an example but asked him to interpret it. The example uses two classes with a common ancestor. The example shows how a method "Add" can be implmented for a String and a numeric class such tht for the string version "1" Plus "2" equals "12" and for numerics it equals "3".
last i looked polymorphics were not allowed with LVOOP but maybe it has been added.
I hope that helps,
Ben
06-08-2012 02:18 PM
That looks very similar to my last attempt with objects.
I see the index selects the string or numeric class from the array, but where does data class learn that string and numeric classes are its peers (don't know if that's the right term...)?
Mac
06-08-2012 02:25 PM
@MacDroid wrote:
That looks very similar to my last attempt with objects.
I see the index selects the string or numeric class from the array, but where does data class learn that string and numeric classes are its peers (don't know if that's the right term...)?
Mac
If I have the rule right...
siblings have no knowlege of each nor do the parents know about their children. Only the child knows who it's parent is. It learns this when you configure the ancestor of the class.
I believe there is an example of the "Factory Pattern" on this web-site. It uses a case structure to selec the proper class. The data flowing in the wire identifies what class and is used to choose the proper method when an override is called for.
Ben
06-12-2012 08:44 AM
After more playing around I think I see what's going on.
One thing though, how do I get a specific object at run time?
I can use Get LV Class Default Value on a path, but it appears to return a LV object, not the specific object in the path.
I don't think To More Specific Class is useful since, the reason I'm loading from a particular path is to get that type of object cuz the Target Class is unknown.
06-12-2012 08:56 AM
"Come in to my parlor said the spyder to the fly."
This image*
...can be found in my LVOOP albumn found here. If you look below the images you will see a link to the thread where those images where first used.
In the image above I am searching for Classes that are children of the class "Plugins". All of my Plug-ins classes have a "Type" property that allows me identify each of the class types and is what ends-up populating a ring control that allows my users to select the flavor they want.
Have fun!
Ben
* Note the missing error wire is a bug by the way.
06-12-2012 09:20 AM
Are the classes in the folder children of "Plug-ins in"?
For instance in my case I have an object Equipment. It has private data "VISA Name" and "Name" and accessor vi's for each.
Then the children "meter1", "powersupply1", etc. inherit from that. I want to create an array of children where the VISA Name, and Name is different, as well as the functionality. i.e. they both have an init routine (with different inputs), but, one has input vi and one has output.vi
06-12-2012 09:25 AM
@MacDroid wrote:
Are the classes in the folder children of "Plug-ins in"?
...
No not all of them are children of Plug-Ins. That is why I have to check all that I find to determine which ones are the children.
Ben
06-12-2012 09:31 AM
I should asl oemhesize that what I showed above is the more complicated way to do it. The Factory Pattern (hint search this site) uses a case structure where there is a unique class in each case. Since the class constant of the objects the Factory Pattern will return are part of the code they are by default opened when the Factory Pattern is used in a diagram. There is no need to go and find them.
In the case that used the code image I posted, the final executable has the ability to support more widgets in the future by just adding the new child classes to the application folder. No changes to the top level VI required.
Ben
06-12-2012 09:32 AM
Implementing the Factory Pattern in LVOOP