From: <vo...@us...> - 2008-01-11 18:18:18
|
Revision: 571 http://opde.svn.sourceforge.net/opde/?rev=571&view=rev Author: volca Date: 2008-01-11 10:18:22 -0800 (Fri, 11 Jan 2008) Log Message: ----------- Archetype and MetaPropery handling methods Modified Paths: -------------- trunk/src/services/inherit/InheritService.cpp trunk/src/services/inherit/InheritService.h Modified: trunk/src/services/inherit/InheritService.cpp =================================================================== --- trunk/src/services/inherit/InheritService.cpp 2008-01-11 18:17:33 UTC (rev 570) +++ trunk/src/services/inherit/InheritService.cpp 2008-01-11 18:18:22 UTC (rev 571) @@ -237,8 +237,110 @@ return res; } } + + //------------------------------------------------------ + void InheritService::setArchetype(int objID, int archetypeID) { + if (getArchetype(objID) != 0) { + OPDE_EXCEPT("Given object already has an archetype set","InheritService::setArchetype"); + } + + _createMPLink(objID, archetypeID, 0); + } + + //------------------------------------------------------ + int InheritService::getArchetype(int objID) const { + InheritMap::const_iterator it = mInheritSources.find(objID); + if (it != mInheritSources.end()) { + // The 'it' is a pointer to a map (dstID, LinkDataPtr) + InheritLinkMap::const_iterator it2 = it->second.begin(); + + while (it2 != it->second.end()) { + if (it2->second->priority == 0) { + return it2->second->srcID; + } + } + } + + return 0; + } + + //------------------------------------------------------ + void InheritService::addMetaProperty(int objID, int mpID) { + // First, look if we inherit from the object or not. + // if so, we simply return + if (inheritsFrom(objID, mpID)) + return; + + // does not inherit from the mpID, we can proceed. + + int mpPrio = 1024; + + // see if we can have some greater one + InheritMap::iterator it = mInheritSources.find(objID); + + if (it != mInheritSources.end()) { + // object seems to have some MP links + // search for the max mp priority + + InheritLinkMap::iterator it2 = it->second.begin(); + + while (it2 != it->second.end()) { + int actPrio = it2->second->priority; + if (actPrio > mpPrio) { + mpPrio = actPrio + 8; // next free. MP priorities are stepped by 8 + } + } + } + + // whe have our priority. Let's create a new MP link! + _createMPLink(objID, mpID, mpPrio); + } + + //------------------------------------------------------ + void InheritService::removeMetaProperty(int objID, int mpID) { + if (getArchetype(objID) == mpID) { + LOG_ERROR("Trying to remove MP(%d) from object (%d) which archetype-inherits from it!", objID, mpID); + return; + } + + if (!inheritsFrom(objID, mpID)) + return; + + + // we inherit some mp from the obj. Remove + // We simply query mp relation for links that come from objID to mpID, then remove them + LinkPtr res = mMetaPropRelation->getOneLink(objID, mpID); + + mMetaPropRelation->remove(res->id()); + // done! + } + + //------------------------------------------------------ + bool InheritService::hasMetaProperty(int objID, int mpID) const { + if (getArchetype(objID) == mpID) + return false; + + // The object does not archetype-inherit, so if it inherits at all, it is by MP + return inheritsFrom(objID, mpID); + } + //------------------------------------------------------ + bool InheritService::inheritsFrom(int objID, int srcID) const { + InheritMap::const_iterator it = mInheritSources.find(objID); + + if (it != mInheritSources.end()) { + // object seems to have some links + InheritLinkMap::const_iterator it2 = it->second.find(srcID); + + if (it2 != it->second.end()) + return true; + } + + return false; + } + + //------------------------------------------------------ void InheritService::_addLink(LinkPtr link, unsigned int priority) { // It works like this. link.src() is the target for inheritance, link.dst() is the source for inheritance InheritLinkPtr ilp = new InheritLink; @@ -282,7 +384,6 @@ //------------------------------------------------------ void InheritService::_removeLink(LinkPtr link) { -// Modify priority of the link InheritMap::iterator it = mInheritSources.find(link->dst()); if (it != mInheritSources.end()) { @@ -310,6 +411,18 @@ OPDE_EXCEPT("Could not find the link to change the priority for", "InheritService::_changeLink"); } + //------------------------------------------------------ + void InheritService::_createMPLink(int objID, int srcID, int priority) { + // Modify priority of the link + DTypeDefPtr type = mMetaPropRelation->getDataType(); + DTypePtr ndata = new DType(type); + + ndata->set("priority", priority); + + // Create a new link, that will do all the work... + mMetaPropRelation->create(objID, srcID, ndata); + } + //-------------------------- Factory implementation std::string InheritServiceFactory::mName = "InheritService"; Modified: trunk/src/services/inherit/InheritService.h =================================================================== --- trunk/src/services/inherit/InheritService.h 2008-01-11 18:17:33 UTC (rev 570) +++ trunk/src/services/inherit/InheritService.h 2008-01-11 18:18:22 UTC (rev 571) @@ -84,8 +84,42 @@ * */ InheritQueryResultPtr getTargets(int objID) const; - /// Todo: AddMetaProp, RemoveMetaProp, ... + /** Sets an archetype for given object (MP link with priority 0) + * @param objID the target object to set archetype for + * @param archetypeID the id of the archetype object to link to + * @note This should be only done on object creation. The method checks if the object has archetype already set and excepts if so + */ + void setArchetype(int objID, int archetypeID); + + /** Returns the archetype ID for the given object, or 0 if the object has no archetype */ + int getArchetype(int objID) const; + /** Adds a new mp link that causes the object to inherit from a metaproperty object. + * Will use the first free priority possible. + * @param objID the object id to add MP to + * @param mpID the ID of the metaproperty to add */ + void addMetaProperty(int objID, int mpID); + + /** Removes a metaproperty from an object. + * @param objID the object id to remove the MP from + * @param mpID the ID of the metaproperty to remove */ + void removeMetaProperty(int objID, int mpID); + + /** Tester for object's inclusion of certain MP. + * @param objID the object id to remove the MP from + * @param mpID the ID of the metaproperty to remove + * @return true if the object inherits from the given mpID with priority > 0, false otherwise + */ + bool hasMetaProperty(int objID, int mpID) const; + + + /** A tester for object inheritance. Returns true if the given object inherits, in any way, from the given source. + * @param objID the object ID to look for + * @param srcID the source object ID to test for inheritance on the objID + * @return true if the objID inherits from srcID, false otherwise + */ + bool inheritsFrom(int objID, int srcID) const; + /// Clears out the inheritance map (leaves the other things intact) void clear(); @@ -117,6 +151,9 @@ /// Listener for the metaprop void onMetaPropMsg(const LinkChangeMsg& msg); + + /// Creates a new metaproperty link with the specified priority + void _createMPLink(int objID, int srcID, int priority); /// Inheritance sources InheritMap mInheritSources; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |