Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/metaobjects/misc/search.hh

Go to the documentation of this file.
00001 /* $Id: search.hh,v 1.22 2003/08/05 21:13:52 tetron Exp $ */
00002 
00003 
00004 /* This file was generated by otd2cpp.pl, a script for VOS by Reed Hedges <reed@zerohour.net>
00005    and Peter Amstutz <tetron@interreality.org>
00006 
00007    Search for TODO for stuff that needs editing. You will want to especially fill in constructors,
00008    destructors, message handlers, non-property subobjects...
00009 
00010    I can make no guarantee that this code will work, or even that it even is safe to run.
00011    Use it at your own risk.
00012 */
00013 
00014 /** @file search.hh Defines MetaObject class for Search type (revision 0).
00015 
00016 */
00017 
00018 #ifndef _SEARCH_HH_
00019 #define _SEARCH_HH_
00020 
00021 #include <vos/corelibs/vos/vos.hh>
00022 #include <vos/metaobjects/property/property.hh>
00023 #include <vos/metaobjects/misc/cod.hh>
00024 
00025 #if defined(HAVE_BOOST_REGEX_H) && defined(HAVE_LIBBOOST_REGEX)
00026 # define BOOST_REGEX_NO_LIB 1
00027 # include <boost/regex.h>
00028 #else
00029 extern "C" {
00030 # include <regex.h>
00031 }
00032 #endif
00033 
00034 #if defined(_WIN32) && defined(_MSC_VER)
00035 # ifdef MISC_EXPORTS
00036 #  define MISC_API __declspec(dllexport)
00037 # else
00038 #  define MISC_API __declspec(dllimport)
00039 # endif
00040 #else
00041 # define MISC_API
00042 #endif
00043 
00044 class MISC_API CatchUpdatesFilter : public MessageFilter
00045 {
00046 public:
00047     string nonce;
00048     MessageBlock* mb;
00049     bool savechildren;
00050 
00051     CatchUpdatesFilter(const string& n) : nonce(n), mb(new MessageBlock()), savechildren(false) { }
00052     ~CatchUpdatesFilter() { mb->release(); }
00053     virtual bool checkMessage(Message* m) {
00054         if(m->getNonce().substr(0, nonce.size()) == nonce) {
00055             if(m->getMethod() != "core:set-child-update"
00056                && m->getMethod() != "core:insert-child-update")
00057             {
00058                 mb->insertMessage(-1, m);
00059             } else savechildren = true;
00060             return false;
00061         } else return true;
00062     }
00063 };
00064 
00065 /** MetaObject implementing Search Object Type.
00066 
00067       Apply a rule-based, recursive filter to a set of Vobjects on a site.
00068 
00069 */
00070 class MISC_API Search : public virtual MetaObject
00071 {
00072 
00073 
00074 protected:
00075     PropertyAccessControl* accessControl;
00076 
00077 
00078 public:
00079 
00080     /** Constructor */
00081     Search(MetaObject* superobject);
00082 
00083     /** Destructor */
00084     virtual ~Search();
00085 
00086     /** Return type string ("search:search") */
00087     virtual const string getType();
00088 
00089     /** Register Extenders */
00090     static void registerExtenders();
00091 
00092     /** Set default access control policy */
00093     virtual void setPropertyAccessControl(PropertyAccessControl* ac);
00094 
00095     /** Get default access control policy */
00096     virtual PropertyAccessControl* getPropertyAccessControl();
00097 
00098     /** @name Access Subobjects */
00099     //@{
00100 
00101     //@}
00102 
00103 
00104     /** @name Actuators. User-functions that correspond to message actions. */
00105 //@{
00106 
00107 
00108     //@}
00109 
00110 };
00111 
00112 
00113 
00114 /** Local version of Search. */
00115 class MISC_API LocalSearch : public virtual Search
00116 {
00117 public:
00118 
00119     struct PatternRule
00120     {
00121         regex_t typepattern;
00122     bool typePatternEmpty;
00123         bool negateChild;
00124         vector<regex_t> childpattern;
00125         vector<string> actions;
00126         string plaintext;
00127     };
00128 
00129     /** Constructor */
00130     LocalSearch(MetaObject* superobject);
00131 
00132     /** Destructor */
00133     virtual ~LocalSearch();
00134 
00135     /** Initialize required subproperties with given access control. */
00136     virtual void initialize(PropertyAccessControl* ac);
00137 
00138     /** Initialize required subproperties. If access control has been
00139     previously set with initialize(PropertyAccessControl*) or
00140     setPropertyAccessControl(), then that policy will be used. Otherwise,
00141     NoPropertyAccessControl will be used.
00142     */
00143     virtual void initialize();
00144 
00145     /** Static generator, for factory. */
00146     static MetaObject* new_LocalSearch(MetaObject* superobject, const string& type);
00147 
00148     deque<PatternRule*> makeRules(Message* rules);
00149 
00150     bool doActions(Vobject* currentobject,
00151                    const deque<PatternRule*>& rules,
00152                    const PatternRule& thisrule,
00153                    RemoteCOD* cod,
00154                    list<Vobject*>& touchedObjects);
00155 
00156     bool actOnChildren(Vobject* currentobject,
00157                        const deque<PatternRule*>& rules,
00158                        const PatternRule& thisrule,
00159                        int patternpart,
00160                        RemoteCOD* cod,
00161                        list<Vobject*>& touchedObjects);
00162 
00163     void search(Vobject* currentobject,
00164                 const deque<PatternRule*>& rules,
00165                 RemoteCOD* cod,
00166                 list<Vobject*>& touchedObjects);
00167 
00168 protected:
00169 
00170     /** @name Message Handlers */
00171     //@{
00172 
00173     /** Handle message search (  Initiate a search.  Starting from each vobject listed in  "start", apply a type and child name based filter to determine  which vobjects to select, then apply a list of actions to each  selected vobject.) */
00174     virtual void handleSearch(Message* m);
00175 
00176     //@}
00177 
00178     int calledregex;
00179 
00180 };
00181 
00182 
00183 class MISC_API SearchListener {
00184 public:
00185     virtual ~SearchListener() {}
00186     virtual void notifySearchDone() = 0;
00187 };
00188 
00189 /** Remote version of Search. */
00190 class MISC_API RemoteSearch : public virtual Search
00191 {
00192 private:
00193     set<string> searches;
00194     map<string, SearchListener*> listeners;
00195 
00196 public:
00197 
00198     /** Constructor */
00199     RemoteSearch(MetaObject* superobject);
00200 
00201     /** Destructor */
00202     virtual ~RemoteSearch();
00203 
00204     /** Static generator, for factory. */
00205     static MetaObject* new_RemoteSearch(MetaObject* superobject, const string& type);
00206 
00207     /** Called when this object gets an update message from its Local master across the network. */
00208     void sendUpdateMessage(Message *m);
00209 
00210     /** @name Actuators.
00211         These are user-functions corresponding to messages. The remote actuators marshal arguments and send a message to the Local object. */
00212     //@{
00213 
00214     /** Send a search messsage to the local object.
00215 
00216         @param objects  Objects under this one to search.
00217         @param rules    Every three items in this list form a
00218             TypeExpression-ChildExpression-Actions triplet. Expressions are
00219             regular expressions that are matched against type strings and
00220             children respectively. Actions are comma seperated, and be any
00221             message or messageblock method, or the special actions "acquire"
00222             (a child inserted notification is sent back) or "recurse" (search
00223             children as well).
00224 
00225             If you want, you can also use the SearchRules class for your
00226             convenience.
00227         @param async    If true, this method will return immediately. If false,
00228             will return when search is complete.
00229 
00230         Example usage:
00231         @code
00232             deque<string> objects;
00233             deque<rules> rules;
00234 
00235             // send the search to these two children of the searchable
00236             // object.
00237             objects.push_back("someChildObject");
00238             objects.push_back("someOtherObject");
00239 
00240             // if either object has type "foo", then request an insert child
00241             // notification, and do this recursively to children.
00242             rules.push_back("foo");
00243             rules.push_back("");
00244             rules.push_back("acquire,recurse");
00245 
00246             // if object has a type that begins with "bar" ar "baz", then
00247             // send a "shazzam" message to all children named "fred".
00248             rules.push_back("((bar)|(baz)).*");
00249             rules.push_back("/fred");
00250             rules.push_back("shazzam");
00251 
00252             // send search and wait for it to finish
00253             search.doSearch(objects, rules, false);
00254         @endcode
00255 
00256      */
00257     string doSearch(const deque<string>& objects, const deque<string>& rules, bool async = false, RemoteCOD** cod = 0);
00258 
00259     /** Like the other doSearch, except registers a SearchListener which is
00260      * notified when search finishes.
00261      * @param listener  Pointer to listener to notify when search finishes
00262      * (ie, search-done message is recieved in reply to this search).
00263      */
00264     string doSearch(const deque<string>& objects, const deque<string>& rules, SearchListener* listener);
00265 
00266     //@}
00267 
00268 
00269 protected:
00270 
00271     /** @name Update Handlers. Handlers for remote update messages */
00272     //@{
00273 
00274     /** Handle a search-done messsage from the local object. */
00275     virtual void handleSearchDone(Message* m);
00276 
00277     //@}
00278 
00279 };
00280 
00281 /** Convenience wrapper around deque<string> for use with
00282  * RemoteSearch::doSearch.
00283  * @see RemoteSearch::doSearch().
00284  */
00285 class SearchRules : public deque<string> {
00286 public:
00287     inline void addRule(string type, string name, string actions) {
00288         this->push_back(type);
00289         this->push_back(name);
00290         this->push_back(actions);
00291     }
00292 };
00293 
00294 #endif // #ifdef _SEARCH_HH_
00295 

Generated on Tue Aug 12 03:55:45 2003 for Interreality Project - VOS by doxygen 1.3.2