View · Search · Index
No registered users in community xowiki
in last 10 minutes

[Xotcl] Re: XOTcl 0.83 questions (continued...)

From: Zoran Vasiljevic <zoran_at_munich.com>
Date: Fri, 8 Dec 2000 04:06:00 -0500 (EST)

------Original Message------
From: <uwe.zdun_at_uni-essen.de>
To: Zoran Vasiljevic <zoran_at_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