LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
PropertyCodeManager.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 * Copyright (C) 2004 by CEA LIST *
8 * *
9 ***************************************************************************/
10
11#include "PropertyCodeManager.h"
12#include "XmlPropertyReader.h"
14
15#include <QFile>
16
17#include <cmath>
18#include <iostream>
19#include <fstream>
20#include <sstream>
21#include <iterator>
22#include <typeinfo>
23
24using namespace std;
25
26namespace Lima
27{
28namespace Common
29{
30namespace PropertyCode
31{
32
33//utility functions for debug
34string hexString(const uint64_t x)
35{
36 ostringstream oss;
37 oss << hex << x;
38 return oss.str();
39}
40
41#ifdef uint128_t
42string hexString(const uint128_t x)
43{
44 ostringstream oss;
45 oss << hex << x;
46 return oss.str();
47}
48#endif
49
51{
52 friend class PropertyCodeManager;
53public:
58
59 std::map<std::string, PropertyManager> m_propertyManagers;
60
66 size_t computeNbBitsNeeded(uint64_t nbvalues) const;
67
77 LinguisticCode computeMask(size_t startBit, size_t nbBits) const;
78
79};
80
85
89
91 m_d(std::make_unique<PropertyCodeManagerPrivate>())
92{
93 m_d->m_propertyManagers = pcm.m_d->m_propertyManagers;
94}
95
97{
98 m_d->m_propertyManagers = pcm.m_d->m_propertyManagers;
99 return *this;
100}
101
102void PropertyCodeManager::readFromXmlFile(const std::string& filename)
103{
105#ifdef DEBUG_LP
106 LDEBUG << typeid(*this).name() << "PropertyCodeManager::readFromXmlFile" << filename;
107#endif
108
109#ifdef DEBUG_LP
110 LDEBUG << typeid(*this).name() << "PropertyCodeManager::readFromXmlFile before creating parser";
111#endif
112 //
113 // Create the reader object then parse the file and catch any exceptions
114 // that propogate out
115 //
116 XmlPropertyReader reader;
117#ifdef DEBUG_LP
118 LDEBUG << "PropertyCodeManager::readFromXmlFile before parsing";
119#endif
120 QFile file(filename.c_str());
121 if (!file.open(QIODevice::ReadOnly))
122 {
123 LIMA_EXCEPTION("PropertyCodeManager::readFromXmlFile Unable to open "
124 << filename.c_str());
125 }
126 if (!reader.parse(&file))
127 {
128 LIMA_EXCEPTION_SELECT("Error while parsing " << filename.c_str()
129 << " : " << reader.errorString(),
131 }
132#ifdef DEBUG_LP
133 LDEBUG << "PropertyCodeManager::readFromXmlFile parsed. before deleting parser";
134#endif
135 // Compute coding properties
136 // size_t, not uint8_t: LinguisticCode may have more than 255 bits
137 size_t usedBits=0;
138
139 // compute data for properties
140 const auto& properties = reader.getProperties();
141#ifdef DEBUG_LP
142 LDEBUG << properties.size() << " properties read from xmlfile ";
143#endif
144 for (auto desc = properties.cbegin();
145 desc != properties.cend();
146 desc++)
147 {
148 // compute mask
149#ifdef DEBUG_LP
150 LDEBUG << "compute data for property " << desc->name;
151#endif
152 std::vector<std::string>::size_type nbvalues=desc->values.size() + 1;
153 auto nbBits = m_d->computeNbBitsNeeded(nbvalues);
154#ifdef DEBUG_LP
155 LDEBUG << nbvalues << " values so use " << nbBits << " bits";
156#endif
157 LinguisticCode mask = m_d->computeMask(usedBits,nbBits);
158#ifdef DEBUG_LP
159 LDEBUG << "mask is " << mask.toHexString();
160#endif
161
162 // compute values
163 std::map<std::string,LinguisticCode> symbol2code;
164 symbol2code["NONE"] = L_NONE;
165 uint64_t i = 1;
166 for (auto valItr = desc->values.cbegin();
167 valItr != desc->values.cend();
168 valItr++)
169 {
170 LinguisticCode new_code = LinguisticCode::fromUInt(i) << usedBits;
171 symbol2code.insert(std::make_pair(*valItr, new_code));
172#ifdef DEBUG_LP
173 LDEBUG << *valItr << " => " << new_code.toHexString();
174#endif
175 i++;
176 }
177 usedBits += nbBits;
178 if (usedBits >= sizeof(LinguisticCode)*8)
179 {
180 std::runtime_error("Error: using more than the number of bits available.");
181 }
182 m_d->m_propertyManagers.insert(
183 std::make_pair(desc->name,
184 PropertyManager(desc->name, mask, mask, symbol2code)));
185 }
186
187 const auto& subproperties = reader.getSubProperties();
188#ifdef DEBUG_LP
189 LDEBUG << subproperties.size() << " subproperties read from file";
190#endif
191 for (const auto& desc: subproperties)
192 {
193#ifdef DEBUG_LP
194 LDEBUG << "compute data for subproperty " << desc.name << "of" << desc.parentName;
195#endif
196 const auto& parentProp = getPropertyManager(desc.parentName);
197 LinguisticCode parentmask = parentProp.getMask();
198
199 const auto& subvalues = desc.values;
200
201 // compute mask
202 // use enough bit to code the biggest subvalues set
203 uint64_t maxsubvalues=0;
204 for (auto it=subvalues.cbegin();
205 it!=subvalues.cend();
206 it++)
207 {
208 if (it->second.size()+1>maxsubvalues)
209 {
210 maxsubvalues=it->second.size()+1;
211 }
212 }
213 auto nbBits = m_d->computeNbBitsNeeded(maxsubvalues);
214#ifdef DEBUG_LP
215 LDEBUG << "maximum subvalues is " << maxsubvalues << " so use "
216 << nbBits << " bits";
217#endif
218 LinguisticCode emptynessmask = m_d->computeMask(usedBits,nbBits);
219 LinguisticCode mask = emptynessmask | parentmask;
220#ifdef DEBUG_LP
221 LDEBUG << "mask = " << mask.toHexString();
222 LDEBUG << "emptyness mask = " << emptynessmask.toHexString();
223#endif
224
225 // compute values
226 std::map<std::string,LinguisticCode> symbol2code;
227 symbol2code["NONE"] = L_NONE;
228 for (const auto& subvalue: subvalues)
229 {
230 LinguisticCode parentValue=parentProp.getPropertyValue(subvalue.first);
231#ifdef DEBUG_LP
232 LDEBUG << "value of" << subvalue.first << "is" << parentValue;
233#endif
234 if (parentValue == L_NONE)
235 {
236 LERROR << "parent value " << subvalue.first << " of subproperty "
237 << desc.name << " is unknown !";
238 }
239#ifdef DEBUG_LP
240 LDEBUG << "compute subvalues of " << subvalue.first << " ("
241 << parentValue.toHexString() << ")";
242#endif
243 symbol2code.insert(std::make_pair(string(subvalue.first)+"-NONE",
244 LinguisticCode(parentValue)));
245 uint64_t i = 1;
246 for (const auto& value: subvalue.second)
247 {
248 symbol2code.insert(std::make_pair(value, (LinguisticCode::fromUInt(i) << usedBits) | parentValue));
249#ifdef DEBUG_LP
250 LDEBUG << value << " => " << symbol2code[value].toHexString();
251#endif
252 i++;
253 }
254 }
255 m_d->m_propertyManagers.insert(std::make_pair(desc.name,
256 PropertyManager(desc.name,
257 mask,
258 emptynessmask,
259 symbol2code)));
260 usedBits += nbBits;
261
262 }
263
264#ifdef DEBUG_LP
265 LDEBUG << "have used " << usedBits << " bits to code all properties";
266#endif
267 if (usedBits > sizeof(LinguisticCode)*8)
268 {
269 LERROR << "needs" << usedBits
270 << "bits to code all properties !! Encoding may be Invalid !!";
271 throw std::runtime_error(
272 "Needs too much bits to code all properties !! Encoding would be Invalid !!");
273 }
274
275}
276
278 const std::string& propertyName) const
279{
280 auto it =m_d->m_propertyManagers.find(propertyName);
281 if (it == m_d->m_propertyManagers.end())
282 {
284 LERROR << "Ask for unknown property accessor:" << propertyName;
285 throw InvalidConfiguration();
286 }
287 return it->second;
288}
289
290const std::shared_ptr<PropertyAccessor> PropertyCodeManager::getPropertySetAccessor(
291 const std::set<std::string>& propertyNames) const
292{
293 ostringstream os;
294 std::copy(propertyNames.begin(), propertyNames.end(),
295 std::ostream_iterator<string>(os,":"));
296#ifdef DEBUG_LP
298 LDEBUG << "create propertysetAccessor for " << os.str();
299#endif
300 LinguisticCode propertySetMask;
301 LinguisticCode propertySetEmptyNessMask;
302 for (auto propItr = propertyNames.cbegin();
303 propItr != propertyNames.cend();
304 propItr++)
305 {
306 const auto& man = getPropertyManager(*propItr);
307 propertySetMask |= man.getMask();
308 propertySetEmptyNessMask |= man.getEmptyNessMask();
309 }
310#ifdef DEBUG_LP
311 LDEBUG << "propertysetMask is " << propertySetMask.toHexString();
312 LDEBUG << "propertysetEmptyNessMask is "
313 << propertySetEmptyNessMask.toHexString();
314#endif
315 return std::shared_ptr<PropertyAccessor>(
316 new PropertyAccessor(os.str(),
317 propertySetMask,
318 propertySetEmptyNessMask));
319}
320
322{
323 size_t nbBits=1;
324 for (uint64_t tmp=2; tmp < nbvalues; tmp*=2)
325 {
326 nbBits++;
327 }
328 if (nbBits > sizeof(LinguisticCode)*8)
329 {
331 LERROR << "Error: nb bits needed ("<<nbBits<<") is larger than sizeof LinguisticCode="
332 << sizeof(LinguisticCode)*8;
333 throw std::runtime_error(
334 "Error: nb bits needed is larger than sizeof LinguisticCode");
335 }
336 return nbBits;
337}
338
340 size_t nbBits) const
341{
342 if (startBit + nbBits > LinguisticCode::size())
343 {
345 LERROR << "Error: cannot compute mask. Would use more bits ("
346 << (startBit + nbBits) <<") than available ("
347 << sizeof(LinguisticCode)*8 << ")";
348 throw std::runtime_error("Error: cannot compute mask. Would use more bits than available");
349 }
350 LinguisticCode mask;
351 for (size_t currentBit = startBit;
352 currentBit < startBit + nbBits;
353 currentBit++)
354 {
355 mask |= (LinguisticCode::fromUInt(1) << currentBit);
356 }
357 return mask;
358}
359
361 std::string>& propValues) const
362{
363#ifdef DEBUG_LP
365#endif
366 LinguisticCode coded;
367#ifdef DEBUG_LP
368 LDEBUG << "encode";
369#endif
370 for (auto it = propValues.cbegin(); it != propValues.cend(); it++)
371 {
372 const auto& man = getPropertyManager(it->first);
373 man.getPropertyAccessor().writeValue(man.getPropertyValue(it->second),coded);
374 #ifdef DEBUG_LP
375 LDEBUG << it->first << " : " << it->second << " coded = " << coded.toHexString();
376 #endif
377 }
378 return coded;
379}
380
382 const std::string& symbolicCodeFile,
383 std::map<std::string,LinguisticCode>& conversionTable) const
384{
385#ifdef DEBUG_LP
387 LDEBUG << "convert Symbolic Code file " << symbolicCodeFile;
388#endif
389
390 //
391 // Create the reader object then parse the file and catch any exceptions
392 // that propogate out
393 //
394 SymbolicCodeXmlReader reader(*this, conversionTable);
395 QFile file(symbolicCodeFile.c_str());
396 if (!file.open(QIODevice::ReadOnly))
397 {
399 LERROR << "An error occurred Error: Cannot open " << symbolicCodeFile ;
400 throw std::runtime_error(
401 std::string("PropertyCodeManager::convertSymbolicCodes Unable to open ")
402 + symbolicCodeFile);
403 }
404 if (!reader.parse(&file))
405 {
407 LERROR << "PropertyCodeManager::convertSymbolicCodes An error occurred parsing"
408 << symbolicCodeFile
409 << ". Error: "
410 << reader.errorString() ;
411 throw XMLException(
412 std::string("Error while parsing " + symbolicCodeFile + " : "
413 + reader.errorString().toUtf8().constData()));
414 }
415}
416
417const std::map<std::string,PropertyManager>& PropertyCodeManager::getPropertyManagers() const
418{
419 return m_d->m_propertyManagers;
420}
421
423 const std::string& propertyName) const
424{
425 return getPropertyManager(propertyName).getPropertyAccessor();
426}
427
428} // PropertyCode
429} // Common
430} // Lima
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
Definition LimaCommon.h:293
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define PROPERTYCODELOGINIT
Definition LimaCommon.h:213
#define LIMA_EXCEPTION_SELECT(X, Y)
This macro writes the message X to a previously configured error stream before throwing a Y exception...
Definition LimaCommon.h:306
#define L_NONE
Definition StdBitset.h:342
Provide function to read write and check a property.
LinguisticCode computeMask(size_t startBit, size_t nbBits) const
The mask of a property is an integer that show which bits are used.
std::map< std::string, PropertyManager > m_propertyManagers
size_t computeNbBitsNeeded(uint64_t nbvalues) const
Compute the number of bit needed to encode nbvalues.
PropertyCodeManagerPrivate(const PropertyCodeManagerPrivate &)=delete
PropertyCodeManagerPrivate & operator=(const PropertyCodeManagerPrivate &)=delete
const std::shared_ptr< PropertyAccessor > getPropertySetAccessor(const std::set< std::string > &propertyNames) const
Get a PropertyAccessor associated to several properties.
void convertSymbolicCodes(const std::string &symbolicCodeFile, std::map< std::string, LinguisticCode > &conversionTable) const
Compute conversion of symbolic codes into linguisticCode and append them into the given map.
const std::map< std::string, PropertyManager > & getPropertyManagers() const
Get the map of all PropertyManagers.
const PropertyManager & getPropertyManager(const std::string &propertyName) const
Get the PropertyManager associated to a property.
void readFromXmlFile(const std::string &filename)
This functions should be called once, just after creation.
PropertyCodeManager & operator=(const PropertyCodeManager &)
const PropertyAccessor & getPropertyAccessor(const std::string &propertyName) const
Get the PropertyAccessor associated to a property.
LinguisticCode encode(const std::map< std::string, std::string > &propValues) const
Compute the code corresponding to given the properties/values.
Provide tools to manage a specific property.
const PropertyAccessor & getPropertyAccessor() const
give the corresponding PropertyAccessor
const std::vector< PropertyDescription > & getProperties()
const std::vector< SubPropertyDescription > & getSubProperties()
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
std::string toHexString() const
Definition StdBitset.h:139
static std::size_t size() noexcept
Definition StdBitset.h:98
static LinguisticCode fromUInt(uint64_t v)
Definition StdBitset.h:69
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
bool parse(QIODevice *device)
string hexString(const uint64_t x)
NAUTITIA.
STL namespace.