NCBI C++ ToolKit
relation.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: relation.cpp 38265 2017-04-19 15:41:13Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any partic2ular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 
36 #include <gui/objutils/label.hpp>
37 
38 #include <serial/serial.hpp>
39 #include <serial/objistr.hpp>
40 #include <serial/objostr.hpp>
41 
45 
48 
49 //
50 // CRelation
51 //
52 
53 const char* CRelation::m_szRelationName = "RelationName";
54 const char* CRelation::m_szUserObjectType = "PolyRelation";
55 
57 {
58  CRef<CUser_object> user_obj(new CUser_object());
59  in >> *user_obj;
60  return RelationFromUserObject(*user_obj);
61 }
62 
64 {
66  out << *user_obj;
67 }
68 
70 {
71  CRef<CUser_object> user_obj(new CUser_object());
72  user_obj->SetType().SetStr(m_szUserObjectType);
73  user_obj->AddField(m_szRelationName, string(relation.GetName()));
74  relation.ToUserObject(*user_obj);
75  return user_obj.Release();
76 }
77 
79 {
80  _ASSERT(user_obj.GetType().GetStr() == m_szUserObjectType);
81 
82  const string& name = user_obj.GetField(m_szRelationName).GetData().GetStr();
83 
84  CConstRef<CRelation> relation;
85  if (name == CComplexRelation::m_Name) {
86  CRef<CRelation> complex(new CComplexRelation());
87  complex->FromUserObject(user_obj);
88  relation.Reset(complex);
89  }
90  else if (name == CIdentityRelation::m_Name) {
91  CRef<CRelation> identity(new CIdentityRelation());
92  relation.Reset(identity);
93  }
94  else
96 
97  return relation.Release();
98 }
99 
100 
101 
103 {
104 }
105 
107  : object(&obj)
108 {
109 }
110 
111 CRelation::SObject::SObject( const CObject& obj, const string& comm )
112  : object(&obj)
113  , comment(comm)
114 {
115 }
116 
117 //
118 // Identity Relation
119 //
120 
121 const char* CIdentityRelation::m_Name = "IdentityRelation";
122 
123 void CIdentityRelation::GetRelated(objects::CScope& scope, const CObject& obj,
124  TObjects& related,
125  TFlags /*flags*/,
126  ICanceled*) const
127 {
128  related.push_back(SObject(obj));
129 }
130 
131 
132 void CIdentityRelation::Dump(ostream& ostream) const
133 {
134  ostream << "Identity Relation" << endl;
135 }
136 
137 //
138 // CComplexRelation
139 //
140 
141 const char* CComplexRelation::m_szRelationsFieldName = "RelationList";
142 const char* CComplexRelation::m_szParallelFieldName = "Parallel";
143 const char* CComplexRelation::m_Name = "ComplexRelation";
144 
146 {
147  return (m_relations.size() > 0) ? (*m_relations.front()).GetTypeName() : "";
148 }
149 
151 {
152  return (m_relations.size() > 0) ? (*m_relations.back()).GetRelatedTypeName() : "";
153 }
154 
155 void CComplexRelation::GetRelated(objects::CScope& scope, const CObject& obj,
156  TObjects& related,
157  TFlags flags,
158  ICanceled* cancel) const
159 {
160  if (m_Parallel) {
161  ITERATE (RelVector, iter, m_relations)
162  (**iter).GetRelated(scope, obj, related, flags, cancel);
163  }
164  else if (m_relations.size()) {
165  TObjects new_objs;
166  new_objs.push_back(SObject(obj));
167  ITERATE (RelVector, iter, m_relations) {
168  TObjects tmp;
169  ITERATE (TObjects, obj_iter, new_objs) {
170  (**iter).GetRelated(scope, **obj_iter, tmp, flags, cancel);
171  }
172  new_objs.swap(tmp);
173  }
174 
175  related.insert(related.end(), new_objs.begin(), new_objs.end());
176  }
177 }
178 
180 {
181  m_relations.push_back(CConstRef<CRelation>(relation));
182 }
183 
185 {
186  vector< CRef<CUser_object> > objs;
187  ITERATE (RelVector, iter, m_relations)
188  objs.push_back(CRef<CUser_object>(CRelation::UserObjectFromRelation(**iter)));
189  user_obj.AddField(m_szRelationsFieldName, objs);
190  user_obj.AddField(m_szParallelFieldName, m_Parallel);
191 }
192 
194 {
195  const vector<CRef<CUser_object> >& objs =
196  user_obj.GetField(m_szRelationsFieldName).GetData().GetObjects();
197 
198  m_Parallel = user_obj.GetField(m_szParallelFieldName).GetData().GetBool();
199 
200  m_relations.clear();
202  AddRelation(CRelation::RelationFromUserObject(**iter));
203 }
204 
205 void CComplexRelation::Dump(ostream& ostream) const
206 {
207  ostream << (m_Parallel ? "Parallel" : "Sequential") << endl;
208 
209  ITERATE (RelVector, iter, m_relations) {
210  ostream << " ";
211  (*iter)->Dump(ostream);
212  ostream << endl;
213  }
214 }
215 
216 string CComplexRelation::GetProperty(const string& key) const
217 {
218  string retVal;
219 
220  if (m_Parallel) {
221  retVal = "All Conversions";
222  }
223  else {
224  ITERATE (RelVector, iter, m_relations) {
225  string prop = (*iter)->GetProperty(key);
226  if (!prop.empty())
227  retVal = prop;
228  }
229  }
230 
231  return retVal;
232 }
233 
234 //
235 // CBasicRelation
236 //
237 
238 string CBasicRelation::GetProperty(const string& key) const
239 {
240  string retVal;
241 
242  if (m_Properties == 0)
243  return retVal;
244 
245  for (int i = 0;; ++i) {
246  const SPropPair* iter = m_Properties + i;
247  if (iter->typeName.empty())
248  break;
249  if (key == iter->typeName) {
250  retVal = iter->value;
251  break;
252  }
253  }
254 
255  return retVal;
256 }
257 
258 
virtual string GetProperty(const string &key) const
Definition: relation.cpp:238
virtual string GetProperty(const string &key) const
Definition: relation.cpp:216
static const char * m_szRelationsFieldName
Definition: relation.hpp:217
void AddRelation(const CRelation *relation)
Definition: relation.cpp:179
virtual string GetTypeName() const
Definition: relation.cpp:145
virtual string GetRelatedTypeName() const
Definition: relation.cpp:150
virtual void GetRelated(objects::CScope &scope, const CObject &obj, TObjects &related, TFlags flags=eDefault, ICanceled *cancel=NULL) const
Definition: relation.cpp:155
virtual void FromUserObject(const objects::CUser_object &user_obj)
Definition: relation.cpp:193
static const char * m_Name
Definition: relation.hpp:207
virtual void ToUserObject(objects::CUser_object &user_obj) const
Definition: relation.cpp:184
virtual void Dump(ostream &ostream) const
Definition: relation.cpp:205
vector< CConstRef< CRelation > > RelVector
Definition: relation.hpp:215
static const char * m_szParallelFieldName
Definition: relation.hpp:218
CConstRef –.
Definition: ncbiobj.hpp:1266
virtual void Dump(ostream &ostream) const
Definition: relation.cpp:132
static const char * m_Name
Definition: relation.hpp:183
virtual void GetRelated(objects::CScope &scope, const CObject &obj, TObjects &related, TFlags flags=eDefault, ICanceled *cancel=NULL) const
Definition: relation.cpp:123
static const CRelation * FindRelationByName(const string &name)
Definition: obj_convert.cpp:65
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStream –.
Definition: objostr.hpp:83
CObject –.
Definition: ncbiobj.hpp:180
vector< SObject > TObjects
Definition: relation.hpp:130
virtual void ToUserObject(objects::CUser_object &) const
Definition: relation.hpp:155
virtual string GetTypeName() const =0
virtual string GetRelatedTypeName() const =0
static const char * m_szRelationName
Definition: relation.hpp:160
static objects::CUser_object * UserObjectFromRelation(const CRelation &relation)
Definition: relation.cpp:69
int TFlags
Definition: relation.hpp:65
void WriteObject(CObjectOStream &out) const
Definition: relation.cpp:63
static const CRelation * ReadObject(CObjectIStream &in)
Definition: relation.cpp:56
static const char * m_szUserObjectType
Definition: relation.hpp:159
static const CRelation * RelationFromUserObject(const objects::CUser_object &user_obj)
Definition: relation.cpp:78
virtual string GetName() const =0
CUser_object & AddField(const string &label, const string &value, EParseField parse=eParse_String)
add a data field to the user object that holds a given value
const CUser_field & GetField(const string &str, const string &delim=".", NStr::ECase use_case=NStr::eCase) const
Access a named field in this user object.
Definition: User_object.cpp:71
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
static char tmp[3200]
Definition: utf8.c:42
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:1439
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:1512
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:846
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const TStr & GetStr(void) const
Get the variant data.
const TData & GetData(void) const
Get the Data member data.
TBool GetBool(void) const
Get the variant data.
vector< CRef< CUser_object > > TObjects
const TStr & GetStr(void) const
Get the variant data.
Definition: Object_id_.hpp:297
void SetType(TType &value)
Assign a value to Type data member.
const TType & GetType(void) const
Get the Type member data.
const TObjects & GetObjects(void) const
Get the variant data.
int i
const struct ncbi::grid::netcache::search::fields::KEY key
std::istream & in(std::istream &in_, double &x_)
USING_SCOPE(objects)
struct SObject provides an interface for defining what is returned from object conversion.
Definition: relation.hpp:71
#define _ASSERT
Modified on Mon Apr 22 04:03:05 2024 by modify_doxy.py rev. 669887