Comparing objects
#1
Posted 10 August 2011 - 07:33 PM
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
#2
Posted 10 August 2011 - 07:43 PM
Have you tried using "To More Generic Class"? You should get an error if it's not a child.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?
Tim
"If this was easy our kids would be doing it." - Coworker
#3
Posted 10 August 2011 - 07:50 PM
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.Have you tried using "To More Generic Class"? You should get an error if it's not a child.
-- James
#4
Posted 10 August 2011 - 08:30 PM
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.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.
Tim
"If this was easy our kids would be doing it." - Coworker
#5
Posted 10 August 2011 - 09:14 PM
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: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.
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
#6
Posted 10 August 2011 - 10:02 PM
-- James
#7
Posted 10 August 2011 - 10:13 PM
I could do what I want if I could programmatically get a list of an objects ancestors; does anyone know how to do that?
Check mike5's posts at the end of this thread. I am not sure it will work for you as it is a specific implementation.
http://lavag.org/top...me-at-run-time/
-Kurt
#8
Posted 11 August 2011 - 08:40 AM
I need to get around to upgrading; I can't seem to do this in 8.6 eitherCheck mike5's posts at the end of this thread. I am not sure it will work for you as it is a specific implementation.
http://lavag.org/top...me-at-run-time/
#9
Posted 28 August 2011 - 04:39 AM
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.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?
#10
Posted 28 August 2011 - 03:32 PM
It is?It's a highly unusual request.
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
#11
Posted 28 August 2011 - 08:50 PM
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.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.
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.
#12
Posted 30 August 2011 - 09:04 AM
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 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'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, 30 August 2011 - 09:10 AM.
#13
Posted 31 August 2011 - 09:41 PM
Actually, I just came across an example of exactly this use, in an "LVOOP Event Handler" by Francois Normandin: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.
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












