Jump to content

Comparing objects


Recommended Posts

I'm probably missing something obvious but I can't seem to do the following:

Imagine I have a class tree with parent, children, grandchildren, etc. How do I take two objects in on parent wires and tell if one object is a child of the other?

So for example, if Parent P has Children A and B, with grandchildren A1 and A2 of A, and B1 and B2 of B, I want a subVI (with two "P" inputs) that if comparing A and A1 returns "true", but if passed a B and A1 returns false.

-- James

Link to comment

Imagine I have a class tree with parent, children, grandchildren, etc. How do I take two objects in on parent wires and tell if one object is a child of the other?

Have you tried using "To More Generic Class"? You should get an error if it's not a child.

Tim

Link to comment

Doesn't work, because those functions use one of the inputs just for the wire type (which has to be the Parent), ignoring the specific child on the wire.

I'm confused; why wouldn't it? If you have two To More Specific Class primitives where class A goes into the reference of the first and into the target of the second, and class B goes into the other inputs, then you should get an error on both only if there is no relation between the classes.

Tim

Link to comment

I'm confused; why wouldn't it? If you have two To More Specific Class primitives where class A goes into the reference of the first and into the target of the second, and class B goes into the other inputs, then you should get an error on both only if there is no relation between the classes.

It's because I don't know either child at runtime; I only have parent-type wires. The "To More Specific Class" function only uses the type of the wire, not the actual object on the wire at runtime. See below:

post-18176-0-44694600-1313010714_thumb.p

I could do what I want if I could programmatically get a list of an objects ancestors; does anyone know how to do that?

-- James

Link to comment
  • 3 weeks later...
Imagine I have a class tree with parent, children, grandchildren, etc. How do I take two objects in on parent wires and tell if one object is a child of the other?
This is actually something that you should pretty much never have to do. Although the Preserve Run-Time Class function will do what you're asking, the PRTC was put in so you could reasonably make *assertions* that two objects are the exact same class, not for runtime testing. What code are you writing where you need this type of functionality? It's a highly unusual request.
Link to comment

It's a highly unusual request.

It is?

What I have is an "Observer Registry" (mentioned in my "Parallel Process" topic) that deals with "Message" objects published by one process, to which other processes can register for. I would like the other processes to be able to specify what specific types of messages they would like to be notified of. For example, Process A may be interested in all "CommandMessages", a child class of Message, and any children of CommandMessage. This could be done by Process A passing a default CommandMessage object to the Observer Register, and then having the Register check every Message published to see if it is a child of CommandMessage. The Register would then be dealing with two children of Message (on Message-type wires) and needs to see if one is the child of the other.

-- James

Link to comment

It is?

What I have is an "Observer Registry" (mentioned in my "Parallel Process" topic) that deals with "Message" objects published by one process, to which other processes can register for. I would like the other processes to be able to specify what specific types of messages they would like to be notified of. For example, Process A may be interested in all "CommandMessages", a child class of Message, and any children of CommandMessage. This could be done by Process A passing a default CommandMessage object to the Observer Register, and then having the Register check every Message published to see if it is a child of CommandMessage. The Register would then be dealing with two children of Message (on Message-type wires) and needs to see if one is the child of the other.

Ok, when you put it that way, it sounds perfectly reasonable, but I have never needed that functionality in any other programming language, despite having written several systems of similar nature. I haven't needed it in LabVIEW either, but I haven't tried in LabVIEW to write the kind of dynamic registration system that you are attempting. I couldn't come up with a quick reason why I haven't needed it. I thought, "Well, maybe *I* haven't needed it, but it'll turn up in code by others," but in a brief search this morning I can't find any examples of typical programs that do this in other languages.

Curiouser and curiouser, thought Alice.

In any case, the Preserve Run-Time Class would (if you had a later LV version) do what you're looking for, and the only disadvantage I can see is that the other process has to register for a subtree of message types (as opposed to some more arbitrary filter mechanism). It will be faster than anything you can do involving Library refnums (most things are faster if you can do them without Library refnums).

I can't really come up with a way to do this in LV 8.6 short of you adding a new method to your class hierarchy for "Is X a child of me?" and a second method for "get class name", which each class overrides correctly.

Link to comment
I can't really come up with a way to do this in LV 8.6 short of you adding a new method to your class hierarchy for "Is X a child of me?" and a second method for "get class name", which each class overrides correctly.

I ended up leaving this feature on the to-do-list awaiting an upgrade, and just hard-wired in the ability to register for all children of "ErrorMessage", that being the only use case I need at the moment.

I'm also considering adding the ability for the publishing process to organize groups of messages into "topics", with subscribing processes registering for topics of interest. This would obviate the need for registering message classes.

Edited by drjdpowell
Link to comment
I thought, "Well, maybe *I* haven't needed it, but it'll turn up in code by others," but in a brief search this morning I can't find any examples of typical programs that do this in other languages.

Actually, I just came across an example of exactly this use, in an "LVOOP Event Handler" by Francois Normandin:

http://lavag.org/top...dpost__p__79074 (you can see "Preserve Run-Time Class" being used in the image)

He uses it slightly differently, to allow the publisher to specify who can subscribe to an event, while I intend to use it to allow the subscriber to specify what events they are interested, but it's basically the same.

-- James

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.