LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EntityType.cpp
Go to the documentation of this file.
1// Copyright 2002-2018 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file EntityType.cpp
9 * @author Romaric Besancon (romaric.besancon@cea.fr)
10 * @date Mon Jan 22 2007
11 ***********************************************************************/
12
13#include "EntityType.h"
14#include "common/LimaCommon.h"
15
16namespace Lima {
17namespace Common {
18namespace MediaticData {
19
21{
22 friend class EntityType;
23 friend QTextStream& operator << (QTextStream&, const EntityType&);
24 friend std::ostream& operator << (std::ostream&, const EntityType&);
25 friend QDebug& operator << (QDebug&, const EntityType&);
26
29 EntityTypePrivate& operator=(const EntityTypePrivate& etp);
30 EntityTypePrivate(EntityTypeId id, EntityGroupId groupId);
32
33 EntityTypeId m_id;
34 EntityGroupId m_groupId;
35};
36
37EntityTypePrivate::EntityTypePrivate():
38m_id(0),
39m_groupId(0)
40{
41}
42
43EntityTypePrivate::EntityTypePrivate(const EntityTypePrivate& etp):
44m_id(etp.m_id),
45m_groupId(etp.m_groupId)
46{
47}
48
49EntityTypePrivate& EntityTypePrivate::operator=(const EntityTypePrivate& etp)
50{
51 m_id = etp.m_id;
52 m_groupId = etp.m_groupId;
53 return *this;
54}
55
56EntityTypePrivate::EntityTypePrivate(EntityTypeId id, EntityGroupId groupId):
57m_id(id),
58m_groupId(groupId)
59{
60}
61
62EntityTypePrivate::~EntityTypePrivate()
63{
64}
65
66//***********************************************************************
67// constructors and destructors
72
74m_d(new EntityTypePrivate(*et.m_d))
75{
76}
77
79{
80 *m_d = *et.m_d;
81 return *this;
82}
83
84EntityType::EntityType(EntityTypeId id, EntityGroupId groupId):
85m_d(new EntityTypePrivate(id, groupId))
86{
87}
88
90{
91 delete m_d;
92}
93
94bool EntityType::operator==(const EntityType& other) const
95{
96 return (m_d->m_groupId==other.m_d->m_groupId && m_d->m_id==other.m_d->m_id);
97}
98
99bool EntityType::operator!=(const EntityType& other) const
100{
101 return !(operator==(other));
102}
103
104bool EntityType::operator<(const EntityType& other) const
105{
106 if (m_d->m_groupId < other.m_d->m_groupId) return true;
107 if (m_d->m_groupId == other.m_d->m_groupId)
108 {
109 if (m_d->m_id < other.m_d->m_id)
110 return true;
111 }
112 return false;
113}
114
116{
117 return (m_d->m_id == static_cast<EntityTypeId>(0)
118 && m_d->m_groupId == static_cast<EntityGroupId>(0));
119}
120
121EntityTypeId EntityType::getTypeId() const
122{
123 return m_d->m_id;
124}
125
126EntityGroupId EntityType::getGroupId() const
127{
128 return m_d->m_groupId;
129}
130
131void EntityType::setTypeId(EntityTypeId id)
132{
133 m_d->m_id=id;
134}
135void EntityType::setGroupId(EntityGroupId groupId)
136{
137 m_d->m_groupId=groupId;
138}
139
140QTextStream& operator << (QTextStream& os, const EntityType& type)
141{
142 return os << type.m_d->m_groupId << "." << type.m_d->m_id;
143}
144
145std::ostream& operator << (std::ostream& os, const EntityType& type)
146{
147 return os << type.m_d->m_groupId << "." << type.m_d->m_id;
148}
149
150QDebug& operator << (QDebug& os, const EntityType& type)
151{
152 os.nospace() << type.m_d->m_groupId << "." << type.m_d->m_id;
153 return os.space();
154}
155
156//***********************************************************************
157// Hierarchy
158// simple child->parent map (does not handle multiple inheritance)
159// structure allows entities from different groups to be in the same hierarchy, but this will
160// not occur with the current parsing of entity declaration in config file
161
162// inherit from map in case we need specific member functions for the interface
163class EntityTypeHierarchyPrivate : public std::map<EntityType, EntityType>
164{
165public:
168};
169
175
180
182 const EntityType& parent)
183{
184 (*m_d)[child]=parent;
185}
186
188 const EntityType& parent) const
189{
190 const auto& it=m_d->find(child);
191 if (it==m_d->end())
192 {
193 return false;
194 }
195 return ((*it).second==parent);
196}
197
199 const EntityType& parent) const
200{
201 const auto& it=m_d->find(child);
202 if (it==m_d->end())
203 {
204 return false;
205 }
206 if ((*it).second==parent)
207 {
208 return true;
209 }
210 return isAncestor((*it).second,parent);
211}
212
214{
215 // get oldest ancestor
216 EntityType current=child;
217 auto it=m_d->find(current);
218 while (it != m_d->end()) {
219 current=(*it).second;
220 it=m_d->find(current);
221 }
222 return current; // if no parent, return type itself
223}
224
226{
227 // get first ancestor
228 EntityType current=child;
229 auto it=m_d->find(current);
230 if (it != m_d->end()) {
231 current=(*it).second;
232 }
233 return current; // if no parent, return type itself
234}
235
236
237bool EntityTypeHierarchy::getChildren(const EntityType& ancestor, std::map<EntityType,EntityType>& childList) const
238{
239 for(auto it = m_d->begin(); it != m_d->end(); it++){
240 if( isParent( (*it).first, ancestor) ){
241 childList.insert( std::make_pair( (*it).first, (*it).second) );
242 getChildren((*it).first, childList);
243 }
244 }
245 return true;
246}
247
248} // end namespace
249} // end namespace
250} // end namespace
bool isParent(const EntityType &child, const EntityType &parent) const
void addParentLink(const EntityType &child, const EntityType &parent)
bool getChildren(const EntityType &ancestor, std::map< EntityType, EntityType > &childList) const
EntityType getAncestor(const EntityType &child) const
bool isAncestor(const EntityType &child, const EntityType &parent) const
EntityType getParent(const EntityType &child) const
friend QTextStream & operator<<(QTextStream &, const EntityType &)
bool operator==(const EntityType &other) const
bool operator!=(const EntityType &other) const
EntityType & operator=(const EntityType &et)
bool operator<(const EntityType &other) const
void setGroupId(EntityGroupId groupId)
holds data about codes and names for grammatical categories, etc.
QTextStream & operator<<(QTextStream &os, const EntityType &type)
NAUTITIA.
STL namespace.