Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

concept.h

Go to the documentation of this file.
00001 /*  This file is part of OMCSNetCPP
00002 
00003     OMCSNetCPP is free software; you can redistribute it and/or modify
00004     it under the terms of the GNU Lesser General Public License as published by
00005     the Free Software Foundation; either version 2.1 of the License, or
00006     (at your option) any later version.
00007 
00008     OMCSNetCPP is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011     GNU Lesser General Public License for more details.
00012 
00013     You should have received a copy of the GNU Lesser General Public License
00014     along with OMCSNetCPP; if not, write to the Free Software
00015     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016  */
00017 
00020 #ifndef _CONCEPT
00021 
00022 #define _CONCEPT
00023 
00024 #include <string>
00025 
00026 #include "semantic_id.h"
00027 #include "link.h"
00028 #include "link_map.h"
00029 
00030 class CSemanticLinkList;
00031 class CFindPathConfig;
00032 
00034 class CSemanticConcept
00035 {
00036         public:
00038                 bool Load(FILE       *a_fp,
00039                           CTextIDMap &a_idMap)
00040                 {
00041                         if (!a_fp)
00042                         {
00043                                 return false;
00044                         }
00045 
00046                         u_int l_size;
00047 
00048                         fread(&l_size, sizeof(u_int), 1, a_fp);
00049 
00050                         char *l_textualName = new char[l_size];
00051 
00052                         fread(l_textualName, l_size, 1, a_fp);
00053 
00054                         std::string l_textualStr;
00055 
00056                         l_textualStr.assign(l_textualName, l_size);
00057 
00058                         delete l_textualName;
00059 
00060                         fread(&m_conceptID, sizeof(SemanticID), 1, a_fp);
00061 
00062                         m_textualName =a_idMap.Store(l_textualStr, m_conceptID);
00063 
00064                         if (!l_textualName)
00065                         {
00066                                 printf("invalid name\n");
00067 
00068                                 return false;
00069                         }
00070 
00071                         // forward and backward links filled in after link_map
00072                         // and concept_map loading (within omcsnet main)
00073 
00074                         return true;
00075                 }
00076 
00078                 bool Save(FILE *a_fp)
00079                 {
00080                         if (!a_fp)
00081                         {
00082                                 return false;
00083                         }
00084 
00085                         u_int l_size = m_textualName->size();
00086 
00087                         fwrite(&l_size, sizeof(u_int), 1, a_fp);
00088 
00089                         fwrite(m_textualName->c_str(), l_size, 1, a_fp);
00090 
00091                         fwrite(&m_conceptID, sizeof(SemanticID), 1, a_fp);
00092 
00093                         // no need to save links as link_map saves them
00094                         // globally
00095 
00096                         return true;
00097                 }
00098 
00100                 std::string GetName() const
00101                 {
00102                         if (m_textualName)
00103                         {
00104                                 return *m_textualName;
00105                         }
00106                         
00107                         return "";
00108                 }               
00109 
00111                 bool FindPathTo(const SemanticID       a_dstID,
00112                                       CFindPathConfig &a_config);
00113 
00115                 SemanticID GetID() const
00116                 {
00117                         return m_conceptID;
00118                 }
00119 
00121                 void AddLink(      CSemanticLink *a_link,
00122                              const RelationType   a_relation)
00123                 {
00124                         if (!m_forwardLinks)
00125                         {
00126                                 p_InitForwardLinks();
00127                         }
00128 
00129                         if (!m_forwardLinks[a_relation])
00130                         {
00131                                 m_forwardLinks[a_relation] =
00132                                                 new CLinkMap;
00133                         }
00134                         
00135                         m_forwardLinks[a_relation]->Store(a_link);
00136                 }
00137 
00139                 CLinkMap *GetLinks(const RelationType a_relation)
00140                 {
00141                         if (m_forwardLinks)
00142                         {
00143                                 return m_forwardLinks[a_relation];
00144                         }
00145 
00146                         return NULL;
00147                 }
00148 
00150                 void AddBackwardLink(      CSemanticLink *a_link,
00151                                      const RelationType   a_relation)
00152                 {
00153                         if (!m_backwardLinks)
00154                         {
00155                                 p_InitBackwardLinks();
00156                         }
00157 
00158                         if (!m_backwardLinks[a_relation])
00159                         {
00160                                 m_backwardLinks[a_relation] =
00161                                          new CLinkMap;
00162                         }
00163 
00164                         m_backwardLinks[a_relation]->Store(a_link);
00165                 }
00166 
00168                 CLinkMap *GetBackwardLinks(
00169                                                 const RelationType a_relation)
00170                 {
00171                         if (m_backwardLinks)
00172                         {
00173                                 return m_backwardLinks[a_relation];
00174                         }
00175 
00176                         return NULL;
00177                 }
00178 
00180                 CSemanticConcept(const std::string          &a_name,
00181                                        CSemanticIDGenerator *a_idGen,
00182                                        CTextIDMap           *a_idMap)
00183                 {
00184                         m_textualName = NULL;
00185 
00186                         if (a_idGen)
00187                         {
00188                                 m_conceptID   = a_idGen->Get();
00189                         }
00190 
00191                         if (a_idMap)
00192                         {
00193                                 m_textualName = a_idMap->Store(a_name,
00194                                                                m_conceptID);
00195                         }
00196 
00197                         m_forwardLinks  = NULL;
00198 
00199                         m_backwardLinks = NULL;
00200                 }
00201 
00203                 ~CSemanticConcept()
00204                 {
00205                         if (m_forwardLinks)
00206                         {
00207                                 for (u_int i = 0;i < MAX_RELATIONS;++i)
00208                                 {
00209                                         if (m_forwardLinks[i])
00210                                         {
00211                                                 delete m_forwardLinks[i];
00212                                         }
00213                                 }
00214 
00215                                 delete m_forwardLinks;
00216                         }
00217 
00218                         if (m_backwardLinks)
00219                         {
00220                                 for (u_int i = 0;i < MAX_RELATIONS;++i)
00221                                 {
00222                                         if (m_backwardLinks[i])
00223                                         {
00224                                                 delete m_backwardLinks[i];
00225                                         }
00226                                 }
00227 
00228                                 delete m_backwardLinks;
00229                         }
00230 
00231                         // Note: links themselves are deleted by the main
00232                         // link map, rather than here.
00233                 }
00234 
00235         private:
00237                 void p_InitForwardLinks()
00238                 {
00239                         m_forwardLinks = new (CLinkMap *)[MAX_RELATIONS];
00240 
00241                         for (u_int i = 0;i < MAX_RELATIONS;++i)
00242                         {
00243                                 m_forwardLinks[i] = NULL;
00244                         }
00245                 }
00246 
00248                 void p_InitBackwardLinks()
00249                 {
00250                         m_backwardLinks = new (CLinkMap *)[MAX_RELATIONS];
00251 
00252                         for (u_int i = 0;i < MAX_RELATIONS;++i)
00253                         {
00254                                 m_backwardLinks[i] = NULL;
00255                         }
00256                 }
00257 
00259                 const std::string *m_textualName;
00260 
00262                 SemanticID  m_conceptID;
00263 
00265                 CLinkMap   **m_forwardLinks;
00266 
00268                 CLinkMap   **m_backwardLinks;
00269 };
00270 
00271 
00272 #endif
00273 

Generated on Tue Sep 16 09:43:49 2003 for OMCSNetCPP by doxygen 1.3.3