LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
FsaAccessResource.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 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-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
10#include "FsaAccessResource.h"
16
17#include <QReadLocker>
18#include <QWriteLocker>
19#include <QFileInfo>
20
22using namespace Lima::Common;
23using namespace std;
24
25namespace Lima
26{
27namespace Common {
28 namespace FsaAccess {
29 extern template class LIMA_FSAACCESS_EXPORT FsaAccessReader16<class boost::adjacency_list<struct boost::vecS,struct boost::vecS,struct boost::bidirectionalS,struct boost::property<enum vertex_count_t,class std::vector<int,class std::allocator<int> >,struct boost::property<enum boost::vertex_name_t,uint64_t,struct boost::property<enum vertex_text_t,class QString,struct boost::no_property> > >,struct boost::no_property,struct boost::no_property,struct boost::listS> >;
30 }
31}
32namespace LinguisticProcessing
33{
34namespace AnalysisDict
35{
36
37#define FSAACCESSRESSOURCE_CLASSID "FsaAccess"
38
40
41FsaAccessResource::FsaAccessResource(QObject* parent)
42 : AbstractAccessResource(parent),m_fsaAccess(0)
43{
44 connect(this,SIGNAL(resourceFileChanged(QString)),this,SLOT(accessFileChanged(QString)));
45}
46
47
49{
50 // if (m_fsaAccess!=0)
51 // {
52 // delete m_fsaAccess;
53 // }
54}
55
58 Manager* manager)
59{
66 try
67 {
68 QString keyFile = getResourceFileName(unitConfiguration.getParamsValueAtKey("keyFile").c_str());
69 if (! keyFile.isEmpty())
70 {
71 auto fsaAccess = std::make_shared<FsaAccess::FsaAccessSpare16>();
72 QWriteLocker locker(&m_lock);
73 LINFO << "FsaAccessResource::init read keyFile" << keyFile;
74 fsaAccess->read(keyFile.toStdString());
75 m_fsaAccess=fsaAccess;
76 }
77 if (!m_fsaAccess) {
78 // FIXME: In this case, the m_fsaAccess pointer is still NULL. Try to access to
79 // this ressource will crash the application.
80 LERROR << "resource file" << unitConfiguration.getParamsValueAtKey("keyFile") << "not found in path"
82 }
83 }
84 catch (NoSuchParam& )
85 {
86 QString errorString;
87 QTextStream qts(&errorString);
88 qts << "no param 'keyFile' in FsaAccessResource group for language "
89 << (int) manager->getInitializationParameters().language;
90 LERROR << errorString;
91 throw InvalidConfiguration(errorString.toStdString());
92 }
94 {
95 QString errorString;
96 QTextStream qts(&errorString);
97 qts << "keyfile "
98 << unitConfiguration.getParamsValueAtKey("keyFile").c_str()
99 << " not found for language "
100 << (int) manager->getInitializationParameters().language
101 << " in paths "
103 LERROR << errorString;
104 throw InvalidConfiguration(errorString.toStdString());
105 }
106}
107
108std::shared_ptr<AbstractAccessByString> FsaAccessResource::getAccessByString() const
109 { return m_fsaAccess;}
110
111void FsaAccessResource::accessFileChanged ( const QString & path )
112{
114 // Check if the file exists as, when a file is replaced, accessFileChanged can be triggered
115 // two times, when it is first suppressed and when the new version is available. One should not
116 // try to load the missing file
117 if (QFileInfo::exists(path))
118 {
119 LINFO << "FsaAccessResource::accessFileChanged reload" << path;
120 auto fsaAccess = std::make_shared<FsaAccess::FsaAccessSpare16>();
121 QWriteLocker locker(&m_lock);
122 fsaAccess->read(path.toUtf8().constData());
123 // delete m_fsaAccess;
124 m_fsaAccess=fsaAccess;
126 }
127 else
128 {
129 LINFO << "FsaAccessResource::accessFileChanged deleted, ignoring" << path;
130 }
131}
132
133} // AnalysisDict
134} // LinguisticProcessing
135} // Lima
#define FSAACCESSRESSOURCE_CLASSID
#define LIMA_FSAACCESS_EXPORT
Definition LimaCommon.h:130
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define ANALYSISDICTLOGINIT
Defines a Factory to create Object of type Base.
Use this exception to signal the used of a wrongly initialized LIMA dictionary.
Definition LimaCommon.h:428
const std::string & getResourcesPath() const
return a message when a 'param' was not found
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
void resourceFileChanged(const QString &path)
Signal triggered whenever one of the registered files changes on disk.
QString getResourceFileName(const QString &paramName)
void accessFileReloaded(std::shared_ptr< Common::AbstractAccessByString > fsaAccess)
virtual std::shared_ptr< Common::AbstractAccessByString > getAccessByString() const override
std::shared_ptr< Common::AbstractAccessByString > m_fsaAccess
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
SimpleFactory< AbstractResource, FsaAccessResource > fsaAccessResourceFactory(FSAACCESSRESSOURCE_CLASSID)
NAUTITIA.
STL namespace.
launch exception related to the configuration file parsing