------Original Message------ From: uwe.zdun@uni-essen.de To: Zoran Vasiljevic zoran@munich.com Sent: November 21, 2000 1:36:57 PM GMT Subject: Re: XOTcl 0.83 questions (continued...)
here, is some example code that shows two ways how to retrieve >all
instances, classes, and metaclasses in the system:
Hi !
Thanks for this good example. But I still somehow have problems. The main problem is the ORDER! What I need to do is run a script against an initialized interpreter and construct another script which will replicate all XOTcl objects, classes etc,etc in an fresh interpreter. So the *ORDER* of commands is crucial.
If somebody says:
Class Foo Class Bar -superclass Foo
Then the introspection script !!!MUST NOT!! emit:
Class Bar -superclass Foo Class Foo
The example below seems not to care about the order. The "allObjs" list below has all objects, right, but the order is not preserved, or not correctly computed.
So I would *realy* appreciate if somebody could point me to the right direction. I apologize If I'm being to ignorant or stupid. Maybe this is just another piece of cake for XOTcl experts but for me (as XOTcl beginner) it's not!
Thanks, Zoran Vasiljevic
--------------
###################################################################
# set up some classes for testing
# # some meta classes # Class Meta -superclass Class Class Meta2 -superclass Meta Class Meta3 -superclass Meta2 # # some classes # Class X Class Y Meta Z Meta2 A Meta2 B # # some objs # X x Object o Y y1 Y y1 A a1 A a2
# # if you only use the global namespace it is quite simple to retrieve # the instances, like: # (you can also traverse the namespaces ... but then the # variant down below is more elegant) set objects "" foreach c [info commands] { if {[Object isobject $c]} {lappend objects $c} } puts "Global Objects are: $objects"
# # we can also go along the class hierarchy # # we define a separate class that computes the instances (could # also be instprocs on Object ... but then we won't have the ability # to use the code on a subclass) #
Class InstanceRetrieval
InstanceRetrieval instproc getAllSubClasses cl { set result "" set sc [$cl info subclass] foreach c $sc { lappend result $c set result [concat $result [[self] getAllSubClasses $c]] } return $result }
InstanceRetrieval instproc getAllInstances {} { set result "" # get all subclasses of this class set subclasses [[self] getAllSubClasses [self]] # now get the instances of every subclass foreach sc $subclasses { set result [concat $result [$sc info instances]] } return $result }
# now we register that class on Object as a mixin # -> we can all instances of the object class (including the # nested objects
Object mixinappend InstanceRetrieval set allObjs [Object getAllInstances] puts "All objects are: $allObjs"
# # with isclass and ismetaclass we can additionally sort out the # classes and metaclasses #
foreach o $allObjs { if {[Object isclass $o]} { lappend classes $o if {[Object ismetaclass $o]} {lappend metaclasses $o} } }
puts "All classes are: $classes" puts "All metaclasses are: $metaclasses"
###################################################################
______________________________________________ FREE Personalized Email at Mail.com Sign up at http://www.mail.com/?sr=signup
Hi Zoran,
ZV> The main problem is the ORDER! What I need to ZV> do is run a script against an initialized interpreter ZV> and construct another script which will replicate all ZV> XOTcl objects, classes etc,etc in an fresh interpreter. ZV> So the *ORDER* of commands is crucial.
I've played around with ordering, when I've created the -- still very incomplete -- script creation for agents. So what I've done now is to extend the ScirptCreator component (in packages/script-creation) to support several classes & objects with a method makeScriptForAll. It then writes scripts with one possible order regarding the object & class hierarchy.
It is still very incomplete ... but should already show how things could function. It only handles procs and instprocs, but not:
-- adavanded features, like filters, mixins, etc. (for them ordering matters, too .. you cannot define a mixin without the class being defined) -- tcl namespaces -- indentation
I'll hope to extend & test it ASAP to support more functionalities, but today I will not make it. Therefore, I send what I have so far.
It already handles another crucial issue besides ordering: it can exclude certain objects & classes from re-creation. You surely do not want the script creator itself or the predefined stuff (like Object and Class)to be re-created.
--Uwe
Here's the example script:
################################################################### # load the ScriptCreator package require ScriptCreator ScriptCreator s
# exclude everything defined so far s excludedObjs [s getAllInstances Object] puts "EXCLUDED [s excludedObjs]"
# set up some classes for testing
# # some meta classes # Class Meta -superclass Class Class Meta2 -superclass Meta Class Meta3 -superclass Meta2 # # some classes # Class X Class Y Meta Z Meta2 A Meta2 B # # some objs # X x Object o Y y1 Y y2 A a1 A a2
puts [s makeScriptForAll]
###################################################################