[alicebot-developer] Dynamic tag instantiation in C++ (was: ChatterBean 00.007 is now available)

Helio Perroni Filho xperroni at yahoo.com
Mon Dec 12 03:36:02 PST 2005


--- mehri <foreverlinux at yahoo.com> escreveu:

> > Hence my idea of providing factory functions for
each
> > tag, mapping the tag names to pointers for the
factoy
> > functions, and then retrieving the pointers from a
map
> > as the tags are read from the XML document. As I
> > exposed, it is not as simple as reflection, but it
> > can be done, and is more efficient than
conditional
> > chains.
> 
> Agreed when considering the overall worse case
> senario using Big-O.  O(log n) with a balanced tree
> (std::map) versus the worst case run time of a
conditional
> statement O(n)
> 
> Now what about the best case senario?  With a
> std::map, you still get Omega(log n) but with a
> conditional statement you end up with Omega(1).

Not if the (key, value) pairs are sorted
appropriately, something you can do with std::map,
admitedly with some extra work.

> Certain tags are going to occur higher in frequency
> on average than others so I group those at the top. 
> The overall runtime is actually a bit quicker than
if I
> were to search a map since I know ahead of time
> which tags occur at a higher frequency.

Some tags are indeed bound to be on the top of the
frequency list, such as (category), (pattern) and
(template); others can appear very often in some AIML
sets and not so much in others. With a map, you could
eventually provide features to adapt the ordering
according to run-time hit frequency, or provide a
config file for the botmaster to define the
priorities.

> > // Ugly hack to allow load-time execution of code.
> > class CategoryRegister
> > {
> > public:
> >   CategoryRegister()
> >   {
> >     factories["Category"] = newCategory;
> >   }
> > }
> 
> So you still have to write code to say, new Category
> here?  With each new tag you add you still have to
> hand code the "new TagName"?

Yes, but this is code you keep on the same unit of the
tag itself. The other way you have to go back to the
createTag() function each time you add a new tag,
which means at least one extra unit to recompile.

> That and you even say Ugly hack in your comments.

In that specific case it is an ugly hack, yes. But I
know dynamic libraries provide better ways to run code
at load time, and since you said you intended to use
DLL's on your project...

Anyway, this is how I would do things, but what do I
know? If I was such a great C++ programmer I wouldn't
do everything in Java. ^_^'

-- 
Ja mata ne.
Helio Perroni Filho



	



	
		
_______________________________________________________ 
Yahoo! doce lar. Faça do Yahoo! sua homepage. 
http://br.yahoo.com/homepageset.html 



More information about the alicebot-developer mailing list