[Xotcl] Re: XOTcl is great!!
Gustaf Neumann
neumann at wu-wien.ac.at
Mon Sep 5 11:14:30 CEST 2005
Taral schrieb:
> Hello,
>
> I recently came across XOTcl and found it very very useful. I am
> working on optimizing some legacy compiler code which is written in
> TCL. I would like to make it object-oritented and so I started using
> XOTcl. So far I have found aouut 90% reduction in execution time.
> Overall I'm getting great performance by using XOTcl.
wow, this is an impressive number; good to hear that.
> I would like to Thank You guys for writing such a great library.
>
> Of course I have couple of questions for you. I will really appreciate
> it if you can help me here.
>
> (1) Is there any way to all the objects or instances based on
> parameter value? For example, I would like to request object names of
> all objects where given parameter's value is equal to 5.
There is no built-in support for this. a straigthforward approach is the
following which might
be sufficient in many situations:
===========
# define method expr for all Objects, the operands are taken from
# the instance variables
Object instforward expr -objscope
# define method select with some abitrary tcl expression. All objects,
# for which the expression returns 1 are returned
Class instproc select {expr} {
set result_list [list]
foreach o [my allinstances] {
if {![catch {$o expr $expr} result]} {
puts "$o expr {$expr} -> $result"
if {$result} {
lappend result_list $o
}
}
}
return $result_list
}
===========
so, now we define a view classes and objects with some instance variables
Class C -parameter {{x 10}}
Class D -superclass C
C c1
C c2 -x 11
D d1 -x 100
D d2
D d3
d3 unset x
finally, we try it out with some demo queries
foreach q {
{$x > 10}
{$x >= 10}
{$x < 1}
{[string match *00* $x]}
} {
puts "C select $q --> {[lsort [C select $q]]}\n"
}
Note that in this example, all instances of C are checked for the
expression, if
you use "... [Object select {$x > 10}]", all objects will be checked.
This approach might be good enough for small examples and medium
speed requirements. If you have larger number of objects, and you cant
restrict the query to smaller classes, you should try to build an index
based on associative arrays, which could replace the "allinstances"
in the code above.
> (2) Do you have more example code pieces? I would like to see more
> sample implementation to help me with the syntax. I'm also learning
> TCL language at the same time using XOTcl :)
i am sure, you have seen the tutorial and the examples in xotcl*/apps
and xotcl*/library. There are a few more on:
http://mini.net/tcl/XOTcl
http://wiki.tcl.tk/references/10971!
> (3) Is there any way to represent relationship across two classes? For
> example, Class A is related to Class B with one-to-many relationship.
a few class/class relations and object/class relations are maintained
by xotcl (e.g. superclass,
class, mixins). A start point might be ::xotcl::Relations, which is
used to implement a
uniform interface to mixins (and instmixins ...), which allows to
specify "... mixin add ...", #
"... mixin delete ...", "... mixin set ...", etc. In principle, the
same interface could be used
for application level relations as well...
In current applications, the classes use and maintain a list of related
object/classes.
Often, it is conveniant to use aggregations to express 1:1 or 1:n
relationships.
> (4) If there is a way to define relationship, is it possible to query
> objects of classes in the relationship? For example, I would like to
> get all the objects of Class B related to given object of Class A
> (like a1) across given one-to-many relationship. In this case result
> would be list of object handles of Class B that matches based on given
> primary key (identifier) value between class A and B.
a good key identifier is the object name. see the following snipplet,
how such behavior
could be obtained. We define class A, which maintains the relationship
to some other
objects. if an instances exists that relates e.g. to object c1, we could
check there some
properties with expr like above...
Class A -parameter {relates_to}
A a1
A a2
a1 set relates_to {c1 c2}
foreach o [A allinstances] {
catch {
if {[lsearch -exact [$o relates_to] c2] > -1} {
puts "could check $o"
}
}
}
hope theses examples help a little.
-gustaf
PS: when i was writing this mail i saw that the classtack information
within methods called via the method "expr" is not correct. It will
be fixed in the next release....
>
> Please provide some guidance to implement above functionality using XOTcl.
>
> Thanks a lot in advance.
>
> May be in future I would like to contribute to XOTcl. I wish to be in
> touch with you guys.
>
> Regards,
> Taral
More information about the Xotcl
mailing list