Jump to content

Read Class hierarchy at runtime


Recommended Posts

Is there any way to read the class hierarchy at runtime in LVOOP?

Given a class, I want to be able to know who its parent is and what decendents are valid for that class. Specifically I will be needing the paths of the .lvclass eventually so that I can dynamically instantiate the decendent/parent classes.

~Jon

Link to comment

Hi Jon,

You'll have to separate this in two approaches: ancestors and descendants.

For ancestors, there is a private method for getting the ancestor class. Running it recursively will get you all the ancestors. You can cut the chase by installing this API. There are methods found in the installed palette for doing what you want.

For descendants, the parent doesn't have any record if a class is its descendant. So you've got to typecast the potential child to its alleged parent class and see if it throws you an error.

post-10515-127125654764_thumb.png

Link to comment

Hi Jon,

You'll have to separate this in two approaches: ancestors and descendants.

For ancestors, there is a private method for getting the ancestor class. Running it recursively will get you all the ancestors. You can cut the chase by installing this API. There are methods found in the installed palette for doing what you want.

For descendants, the parent doesn't have any record if a class is its descendant. So you've got to typecast the potential child to its alleged parent class and see if it throws you an error.

post-10515-127125654764_thumb.png

I've run the toolkit, but it does not appear to retrieve descendent or child classes. The List_Ancestor_class.vi only lists the ancestors of my target class, and the original class I gave as a target.

Here is the code:

post-10515-127125654764_thumb.png

post-17025-12712595197_thumb.png

Link to comment

The List_Ancestor_class.vi only lists the ancestors of my target class, and the original class I gave as a target.

Yes, this is expected behavior. Ancestors cannot be children of your intial class.

If C is child of B, and B is child of A, then:

1- Wiring A.lvclass as input will give {A.lvclass} (self -> no ancestor class except LabVIEW Object)

2- Wiring B.lvclass as input will give {B.lvclass ; A.lvclass} (self + parent "A")

3- Wiring C.lvclass as input will give {C.lvclass; B.lvclass ; A.lvclass} (self + parent "B" + parent of B )

etc.

To find Child classes, you can't ask the parent class "Who are your children?". You need to list the directory where your classes reside at runtime and typecast to see if they match the class type of the parent.

post-10515-127126229075_thumb.png

Link to comment

There is no way to accomplish what you are requesting. A parent class is saved without any knowledge of its children. There may be infinite children on your disk, out on the network, anywhere. Some subset of those children may currently be in memory. Those are the only children that a parent knows about, and since those are already loaded, they're of no help to you.

A class does know who its parent is. When the class loads into memory, it automatically loads its parent class, so you don't have to dynamically load that.

Once in memory, a class can be asked for a list of all its children *in the editor environment*. You cannot do this in the runtime engine.

Link to comment

There is no way to accomplish what you are requesting. A parent class is saved without any knowledge of its children. There may be infinite children on your disk, out on the network, anywhere. Some subset of those children may currently be in memory. Those are the only children that a parent knows about, and since those are already loaded, they're of no help to you.

A class does know who its parent is. When the class loads into memory, it automatically loads its parent class, so you don't have to dynamically load that.

Once in memory, a class can be asked for a list of all its children *in the editor environment*. You cannot do this in the runtime engine.

Ok, perhaps a work around of registering all the classes and their hierarchy in an initialization step (from .ini file?) will do the trick then. At any rate, it sounds like I MUST load the parent of a particular target class before loading that target or labview will spit out errors.

~Jon

Link to comment
A parent class is saved without any knowledge of its children.

As it should - parents should never be child aware, children should always be parent aware.

Hey Jon: can you give us an idea of your application? Maybe we can help with a work around if we know a little more about what you're trying to do.

Link to comment

As it should - parents should never be child aware, children should always be parent aware.

Hey Jon: can you give us an idea of your application? Maybe we can help with a work around if we know a little more about what you're trying to do.

Basically what I'm trying to do is add classes later but allow them to be user selectable without actually adding any new code. Something like a tree control where the decendents can be viewed dynamically depending on hardware/software configurations on a given system.

This is in the context of a factory pattern and plugin architexture.

I'll try to work this out by registering new classes in a .ini file so that the main can scan who is actually out there and what is available for runtime purposes.

we'll see how this goes, I'm getting a little experimental now...

~Jon

Link to comment
At any rate, it sounds like I MUST load the parent of a particular target class before loading that target or labview will spit out errors.
No. A child will load its parent automatically as part of its own load into memory.
This is in the context of a factory pattern and plugin architexture.
OH. In that case... you want these:

http://decibel.ni.com/content/docs/DOC-2875 <<< search for "Factory Pattern" for an implementation of exactly what you're trying to do. This is a very CS oriented write up of the pattern and its implementation in LabVIEW.

http://zone.ni.com/wv/app/doc/p/id/wv-1295 <<< Even more detailed online presentation about the Factory pattern, how it works and why it works and variations that may be of interest to you.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.