/
cellmlfile.cpp
1233 lines (888 loc) · 43 KB
/
cellmlfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// CellML file
//==============================================================================
#include "cellmlfile.h"
#include "cellmlfilecellml10exporter.h"
#include "cellmlfilecellml11exporter.h"
#include "cellmlfilemanager.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "filemanager.h"
//==============================================================================
#include <QDomDocument>
#include <QFile>
#include <QStringList>
#include <QUrl>
//==============================================================================
#ifdef Q_OS_LINUX
#include <stdint.h>
// Note: the above header file is required on Linux, so we can use uint32_t
// (required to 'properly' make use of the CellML API). Now, we would
// normally use
// #include <cstdint>
// but this is not supported by the current ISO C++ standard. Indeed,
// to include it will generate the following error at compile time:
// error: #error This file requires compiler and library support
// for the upcoming ISO C++ standard, C++0x. This support is
// currently experimental, and must be enabled with the -std=c++0x
// or -std=gnu++0x compiler options.
// and well, we don't want to enable either of these options since
// support is still only experimental...
#endif
//==============================================================================
#include "cellmlapibegin.h"
#include "IfaceCeLEDSExporter.hxx"
#include "IfaceVACSS.hxx"
#include "CeLEDSExporterBootstrap.hpp"
#include "CellMLBootstrap.hpp"
#include "VACSSBootstrap.hpp"
#include "cellmlapiend.h"
//==============================================================================
namespace OpenCOR {
namespace CellMLSupport {
//==============================================================================
CellmlFile::CellmlFile(const QString &pFileName) :
StandardSupport::StandardFile(pFileName),
mRdfTriples(CellmlFileRdfTriples(this)),
mRuntime(new CellmlFileRuntime(this)),
mUpdated(false)
{
// Reset ourselves
reset();
}
//==============================================================================
CellmlFile::~CellmlFile()
{
// Reset ourselves
reset();
// Delete some internal objects
delete mRuntime;
}
//==============================================================================
void CellmlFile::reset()
{
// Don't reset ourselves if we were updated
if (mUpdated) {
mUpdated = false;
return;
}
// Reset all of our properties
mModel = 0;
mRdfApiRepresentation = 0;
mRdfDataSource = 0;
Core::resetList(mRdfTriples);
mIssues.clear();
Core::FileManager::instance()->setDependencies(mFileName, QStringList());
mLoadingNeeded = true;
mFullInstantiationNeeded = true;
mRuntimeUpdateNeeded = true;
mDependenciesNeeded = true;
mImportContents.clear();
mUsedCmetaIds.clear();
}
//==============================================================================
iface::cellml_api::Model * CellmlFile::model()
{
// Return the model associated with our CellML file, after loading ourselves
// if necessary
load();
return mModel;
}
//==============================================================================
iface::rdf_api::DataSource * CellmlFile::rdfDataSource()
{
// Return the data source associated with our CellML file, after loading
// ourselves if necessary
load();
return mRdfDataSource;
}
//==============================================================================
void CellmlFile::retrieveImports(const QString &pXmlBase,
iface::cellml_api::Model *pModel,
QList<iface::cellml_api::CellMLImport *> &pImportList,
QStringList &pImportXmlBaseList)
{
// Retrieve all the imports of the given model
ObjRef<iface::cellml_api::CellMLImportSet> imports = pModel->imports();
ObjRef<iface::cellml_api::CellMLImportIterator> importsIter = imports->iterateImports();
for (ObjRef<iface::cellml_api::CellMLImport> import = importsIter->nextImport();
import; import = importsIter->nextImport()) {
import->add_ref();
pImportList << import;
pImportXmlBaseList << pXmlBase;
}
}
//==============================================================================
bool CellmlFile::fullyInstantiateImports(iface::cellml_api::Model *pModel,
CellmlFileIssues &pIssues,
const bool &pWithBusyWidget)
{
// Fully instantiate all the imports, but only if we are not directly
// dealing with our model or if we are dealing with a non CellML 1.0 model,
// and then keep track of that fact (so we don't fully instantiate everytime
// we come here)
Version cellmlVersion = version(pModel);
if ( ((pModel != mModel) || mFullInstantiationNeeded)
&& (cellmlVersion != Unknown) && (cellmlVersion != Cellml_1_0)) {
QStringList dependencies = QStringList();
try {
// Note: the below is based on CDA_Model::fullyInstantiateImports().
// Indeed, CDA_Model::fullyInstantiateImports() doesn't work
// with CellML imports that rely on https (see issue #417), so
// rather than calling CDA_CellMLImport::instantiate(), we
// call CDA_CellMLImport::instantiateFromText() instead, which
// requires loading the imported CellML file. Otherwise, to
// speed things up as much as possible, we cache the contents
// of the URLs that we load...
// Retrieve the list of imports, together with their XML base values
ObjRef<iface::cellml_api::URI> baseUri = pModel->xmlBase();
QList<iface::cellml_api::CellMLImport *> importList = QList<iface::cellml_api::CellMLImport *>();
QStringList importXmlBaseList = QStringList();
retrieveImports(QString::fromStdWString(baseUri->asText()),
pModel, importList, importXmlBaseList);
// Instantiate all the imports in our list
while (!importList.isEmpty()) {
// Retrieve the first import and instantiate it, if needed
ObjRef<iface::cellml_api::CellMLImport> import = importList.first();
QString importXmlBase = importXmlBaseList.first();
importList.removeFirst();
importXmlBaseList.removeFirst();
if (!import->wasInstantiated()) {
// Note: CDA_CellMLImport::instantiate() would normally be
// called, but it doesn't work with https, so we
// retrieve the contents of the import ourselves and
// instantiate it from text instead...
ObjRef<iface::cellml_api::URI> xlinkHref = import->xlinkHref();
QString url = QUrl(importXmlBase).resolved(QString::fromStdWString(xlinkHref->asText())).toString();
bool isLocalFile;
QString fileNameOrUrl;
Core::checkFileNameOrUrl(url, isLocalFile, fileNameOrUrl);
if (!fileNameOrUrl.compare(mFileName)) {
// We want to import ourselves, something we can't do
throw(std::exception());
} else if (mImportContents.contains(fileNameOrUrl)) {
// We have already loaded the import contents, so
// directly instantiate the import with it
import->instantiateFromText(mImportContents.value(fileNameOrUrl).toStdWString());
} else {
// We haven't already loaded the import contents, so do
// so now
QString fileContents;
if ( ( isLocalFile && Core::readFileContentsFromFile(fileNameOrUrl, fileContents))
|| (!isLocalFile && pWithBusyWidget && Core::readFileContentsFromUrlWithBusyWidget(fileNameOrUrl, fileContents))
|| (!isLocalFile && !pWithBusyWidget && Core::readFileContentsFromUrl(fileNameOrUrl, fileContents))) {
// We were able to retrieve the import contents, so
// instantiate the import with it
import->instantiateFromText(fileContents.toStdWString());
// Keep track of the import contents
mImportContents.insert(fileNameOrUrl, fileContents);
// Keep track of the import as being one of our
// dependencies, should it be local and should we be
// directly dealing with our model
if (isLocalFile && (pModel == mModel))
dependencies << fileNameOrUrl;
} else {
throw(std::exception());
}
}
// Now that the import is instantiated, add its own imports
// to our list
ObjRef<iface::cellml_api::Model> importModel = import->importedModel();
if (!importModel)
throw(std::exception());
retrieveImports(isLocalFile?
QUrl::fromLocalFile(fileNameOrUrl).toString():
fileNameOrUrl,
importModel, importList, importXmlBaseList);
}
}
} catch (...) {
// Something went wrong with the full instantiation of the imports
pIssues << CellmlFileIssue(CellmlFileIssue::Error,
tr("the imports could not be fully instantiated"));
return false;
}
// Finalise a few things, should we be directly dealing with our model
if (pModel == mModel) {
mFullInstantiationNeeded = false;
mDependenciesNeeded = false;
// Set the dependencies for our CellML file
Core::FileManager::instance()->setDependencies(mFileName, dependencies);
}
}
return true;
}
//==============================================================================
bool CellmlFile::doLoad(const QString &pFileContents,
ObjRef<iface::cellml_api::Model> *pModel,
CellmlFileIssues &pIssues)
{
// Make sure that pIssues is empty
pIssues.clear();
// Get a bootstrap object and its model loader
ObjRef<iface::cellml_api::CellMLBootstrap> cellmlBootstrap = CreateCellMLBootstrap();
ObjRef<iface::cellml_api::DOMModelLoader> modelLoader = cellmlBootstrap->modelLoader();
// Try to create the model
try {
if (pFileContents.isEmpty())
*pModel = modelLoader->loadFromURL(QUrl::fromPercentEncoding(QUrl::fromLocalFile(mFileName).toEncoded()).toStdWString());
else
*pModel = modelLoader->createFromText(pFileContents.toStdWString());
} catch (iface::cellml_api::CellMLException &exception) {
// Something went wrong with the loading of the model
if (pFileContents.isEmpty()) {
pIssues << CellmlFileIssue(CellmlFileIssue::Error,
tr("the model could not be loaded (%1)").arg(Core::formatMessage(QString::fromStdWString(exception.explanation))));
} else {
pIssues << CellmlFileIssue(CellmlFileIssue::Error,
tr("the model could not be created (%1)").arg(Core::formatMessage(QString::fromStdWString(exception.explanation))));
}
return false;
}
// Update the base URI, should the CellML file be a remote one or its
// contents be directly passed onto us
Core::FileManager *fileManagerInstance = Core::FileManager::instance();
ObjRef<iface::cellml_api::URI> baseUri = (*pModel)->xmlBase();
if (fileManagerInstance->isRemote(mFileName)) {
// We are dealing with a remote file, so its XML base value should point
// to its remote location
baseUri->asText(fileManagerInstance->url(mFileName).toStdWString());
} else if (!pFileContents.isEmpty()) {
// We are dealing with a file which contents was directly passed onto
// us, so its XML base value should point to its actual location
baseUri->asText(mFileName.toStdWString());
}
return true;
}
//==============================================================================
void CellmlFile::retrieveCmetaIdsFromCellmlElement(iface::cellml_api::CellMLElement *pElement)
{
// Keep track of the given CellML element's cmeta:id
QString cmetaId = QString::fromStdWString(pElement->cmetaId());
if (!cmetaId.isEmpty())
mUsedCmetaIds << cmetaId;
// Do the same for all the child elements of the given CellML element
ObjRef<iface::cellml_api::CellMLElementSet> childElements = pElement->childElements();
ObjRef<iface::cellml_api::CellMLElementIterator> childElementsIter = childElements->iterate();
try {
for (ObjRef<iface::cellml_api::CellMLElement> childElement = childElementsIter->next();
childElement; childElement = childElementsIter->next()) {
retrieveCmetaIdsFromCellmlElement(childElement);
}
} catch (...) {
// Note: we should never reach this point, but it may still happen if a
// CellML file contains a child element that is not known to the
// CellML API. We are taking the view that this is a limitation of
// the CellML API and shouldn't therefore generate an error for
// something that should have been working fine in the first
// place...
}
}
//==============================================================================
void CellmlFile::clearCmetaIdsFromCellmlElement(const QDomElement &pElement,
const QStringList &pUsedCmetaIds)
{
// Remove the given CellML element's cmeta:id, if it is not actually being
// used
static const QString CmetaId = "cmeta:id";
static const QString XmlnsCmeta = "xmlns:cmeta";
if (!pUsedCmetaIds.contains(pElement.attribute(CmetaId))) {
pElement.attributes().removeNamedItem(CmetaId);
pElement.attributes().removeNamedItem(XmlnsCmeta);
}
// Do the same for all the child elements of the given CellML element
for (QDomElement childElement = pElement.firstChildElement();
!childElement.isNull(); childElement = childElement.nextSiblingElement()) {
clearCmetaIdsFromCellmlElement(childElement, pUsedCmetaIds);
}
}
//==============================================================================
bool CellmlFile::load()
{
// Check whether we are already loaded and without any issues
if (!mLoadingNeeded)
return mIssues.isEmpty();
mLoadingNeeded = false;
// Try to load the model
if (!doLoad(QString(), &mModel, mIssues))
return false;
// Retrieve all the RDF triples associated with the model and initialise our
// list of original RDF triples
ObjRef<iface::cellml_api::RDFRepresentation> rdfRepresentation = mModel->getRDFRepresentation(L"http://www.cellml.org/RDF/API");
if (rdfRepresentation) {
mRdfApiRepresentation = QueryInterface(rdfRepresentation);
if (mRdfApiRepresentation) {
mRdfDataSource = mRdfApiRepresentation->source();
ObjRef<iface::rdf_api::TripleSet> rdfTriples = mRdfDataSource->getAllTriples();
ObjRef<iface::rdf_api::TripleEnumerator> rdfTriplesEnumerator = rdfTriples->enumerateTriples();
for (ObjRef<iface::rdf_api::Triple> rdfTriple = rdfTriplesEnumerator->getNextTriple();
rdfTriple; rdfTriple = rdfTriplesEnumerator->getNextTriple()) {
mRdfTriples << new CellmlFileRdfTriple(this, rdfTriple);
}
mRdfTriples.updateOriginalRdfTriples();
}
}
// Determine which cmeta:ids are currently in use, be they in the various
// CellML elements or RDF triples
retrieveCmetaIdsFromCellmlElement(mModel);
foreach (CellmlFileRdfTriple *rdfTriple, mRdfTriples)
mUsedCmetaIds << rdfTriple->metadataId();
mUsedCmetaIds.removeDuplicates();
return true;
}
//==============================================================================
bool CellmlFile::save(const QString &pFileName)
{
// Make sure that we are properly loaded and have no issues
if (mLoadingNeeded || !mIssues.isEmpty())
return false;
// Make sure that the RDF API representation is up to date by updating its
// data source
mRdfApiRepresentation->source(mRdfDataSource);
// Update our list of original RDF triples
mRdfTriples.updateOriginalRdfTriples();
// Get a DOM representation of our CellML file and remove its XML base
// value, its RDF child node (should there be no annotations) and all the
// cmeta:id's (in CellML elements) that are not used in the CellML file's
// annotations
// Note #1: as part of good practices, a CellML file should never contain an
// XML base value. Yet, upon loading a CellML file, we set one (see
// doLoad()), so that we can properly import CellML files, if
// needed. So, now, we need to undo what we did...
// Note #2: normally, we would be asking QDomDocument::setContent() to
// process namespaces, but this would then result in a very messy
// serialisation with namespaces being referenced all over the
// place. So, in the end, we do everything without processing
// namespaces...
QDomDocument domDocument;
domDocument.setContent(QString::fromStdWString(mModel->serialisedText()));
QDomElement domElement = domDocument.documentElement();
domElement.attributes().removeNamedItem("xml:base");
for (QDomElement childElement = domElement.firstChildElement();
!childElement.isNull(); childElement = childElement.nextSiblingElement()) {
if (!childElement.nodeName().compare("rdf:RDF")) {
if (!childElement.childNodes().count())
domElement.removeChild(childElement);
break;
}
}
QStringList usedCmetaIds = QStringList();
foreach (CellmlFileRdfTriple *rdfTriple, mRdfTriples)
usedCmetaIds << rdfTriple->metadataId();
usedCmetaIds.removeDuplicates();
clearCmetaIdsFromCellmlElement(domElement, usedCmetaIds);
// Write out the contents of our DOM document to our CellML file
return Core::writeFileContentsToFile(pFileName.isEmpty()?mFileName:pFileName,
Core::serialiseDomDocument(domDocument))?
StandardFile::save(pFileName):
false;
}
//==============================================================================
bool CellmlFile::update(const QString &pFileName)
{
// Our model parameters were modified (e.g. through the Simulation
// Experiment view) and we want to update ourselves accordingly, so save
// ourselves and keep track of the fact that we were 'simply' updated
mUpdated = save(pFileName);
return mUpdated;
}
//==============================================================================
bool CellmlFile::doIsValid(iface::cellml_api::Model *pModel,
CellmlFileIssues &pIssues)
{
// Check whether the given model is CellML valid
// Note: validateModel() is somewhat slow, but there is (unfortunately)
// nothing we can do about it. Then, there is getPositionInXML() which
// is painfully slow, but unlike for validateModel() its use is not
// essential (even though it would be nice from an end-user's
// perspective). So, rather than retrieve the line/column of every
// single warning/error, we only keep track of the various
// warnings/errors and only retrieve their corresponding line/column
// when requested (definitely not neat from an end-user's perspective,
// but we just can't afford the time it takes to fully validate a
// model that has many warnings/errors)...
// Make sure that pIssues is empty
pIssues.clear();
// Determine the number of errors and warnings
// Note: CellMLValidityErrorSet::nValidityErrors() returns any type of
// validation issue, be it an error or a warning, so we need to
// determine the number of true errors
ObjRef<iface::cellml_services::VACSService> vacssService = CreateVACSService();
ObjRef<iface::cellml_services::CellMLValidityErrorSet> cellmlValidityErrorSet = vacssService->validateModel(pModel);
int cellmlErrorsCount = 0;
for (int i = 0, iMax = cellmlValidityErrorSet->nValidityErrors(); i < iMax; ++i) {
ObjRef<iface::cellml_services::CellMLValidityError> cellmlValidityIssue = cellmlValidityErrorSet->getValidityError(i);
ObjRef<iface::cellml_services::CellMLRepresentationValidityError> cellmlRepresentationValidityError = QueryInterface(cellmlValidityIssue);
// Determine the issue's location
uint32_t line = 0;
uint32_t column = 0;
QString importedFile = QString();
if (cellmlRepresentationValidityError) {
// We are dealing with a CellML representation issue, so determine
// its line and column
ObjRef<iface::dom::Node> errorNode = cellmlRepresentationValidityError->errorNode();
line = vacssService->getPositionInXML(errorNode,
cellmlRepresentationValidityError->errorNodalOffset(),
&column);
} else {
// We are not dealing with a CellML representation issue, so check
// whether we are dealing with a semantic one
ObjRef<iface::cellml_services::CellMLSemanticValidityError> cellmlSemanticValidityError = QueryInterface(cellmlValidityIssue);
if (cellmlSemanticValidityError) {
// We are dealing with a CellML semantic issue, so determine its
// line and column
ObjRef<iface::cellml_api::CellMLElement> cellmlElement = cellmlSemanticValidityError->errorElement();
ObjRef<iface::cellml_api::CellMLDOMElement> cellmlDomElement = QueryInterface(cellmlElement);
ObjRef<iface::dom::Element> domElement = cellmlDomElement->domElement();
line = vacssService->getPositionInXML(domElement, 0, &column);
// Also determine its imported file, if any
ObjRef<iface::cellml_api::CellMLElement> cellmlElementParent = cellmlElement->parentElement();
if (cellmlElementParent) {
// Check whether the parent is an imported file
ObjRef<iface::cellml_api::Model> importedCellmlFile = QueryInterface(cellmlElementParent);
if (importedCellmlFile) {
// Retrieve the imported CellML element
ObjRef<iface::cellml_api::CellMLElement> importedCellmlElement = importedCellmlFile->parentElement();
if (importedCellmlElement) {
// Check whether the imported CellML element is an
// import CellML element
ObjRef<iface::cellml_api::CellMLImport> importCellmlElement = QueryInterface(importedCellmlElement);
if (importCellmlElement) {
ObjRef<iface::cellml_api::URI> xlinkHref = importCellmlElement->xlinkHref();
importedFile = QString::fromStdWString(xlinkHref->asText());
}
}
}
}
}
}
// Determine the issue's type
CellmlFileIssue::Type issueType;
if (cellmlValidityIssue->isWarningOnly()) {
// We are dealing with a warning
issueType = CellmlFileIssue::Warning;
} else {
// We are dealing with an error
++cellmlErrorsCount;
issueType = CellmlFileIssue::Error;
}
// Append the issue to our list
pIssues << CellmlFileIssue(issueType, line, column,
QString::fromStdWString(cellmlValidityIssue->description()),
importedFile);
}
// Sort our issues (since the CellML API may generate them in a non-ordered
// manner)
std::sort(pIssues.begin(), pIssues.end(), CellmlFileIssue::compare);
return !cellmlErrorsCount;
}
//==============================================================================
bool CellmlFile::doIsValid(const QString &pFileContents,
ObjRef<iface::cellml_api::Model> *pModel,
CellmlFileIssues &pIssues,
const bool &pWithBusyWidget)
{
// Try to load our model
if (doLoad(pFileContents, pModel, pIssues)) {
// The file contents was properly loaded, so make sure that its imports,
// if any, are fully instantiated
if (fullyInstantiateImports(*pModel, pIssues, pWithBusyWidget)) {
// Now, we can check whether the file contents is CellML valid
return doIsValid(*pModel, pIssues);
} else {
return false;
}
} else {
return false;
}
}
//==============================================================================
bool CellmlFile::isValid(const QString &pFileContents,
CellmlFileIssues &pIssues, const bool &pWithBusyWidget)
{
// Check whether the given file contents is CellML valid, so for this create
// a temporary model
ObjRef<iface::cellml_api::Model> model;
return doIsValid(pFileContents, &model, pIssues, pWithBusyWidget);
}
//==============================================================================
bool CellmlFile::isValid(const bool &pWithBusyWidget)
{
// Return whether we are valid
return doIsValid(QString(), &mModel, mIssues, pWithBusyWidget);
}
//==============================================================================
CellmlFileIssues CellmlFile::issues() const
{
// Return our issues
return mIssues;
}
//==============================================================================
CellmlFileRuntime * CellmlFile::runtime(const bool &pWithBusyWidget)
{
// Check whether the runtime needs to be updated
if (!mRuntimeUpdateNeeded)
return mRuntime;
// Load (but not reload!) ourselves, if needed
if (load()) {
// Make sure that our imports, if any, are fully instantiated
if (fullyInstantiateImports(mModel, mIssues, pWithBusyWidget)) {
// Now, we can return an updated version of our runtime
mRuntime->update();
mRuntimeUpdateNeeded = false;
return mRuntime;
} else {
return 0;
}
} else {
return 0;
}
}
//==============================================================================
QStringList CellmlFile::dependencies(const bool &pWithBusyWidget)
{
// Check whether the dependencies need to be retrieved
if (!mDependenciesNeeded)
return Core::FileManager::instance()->dependencies(mFileName);
// Load (but not reload!) ourselves, if needed
if (load()) {
// Make sure that our imports, if any, are fully instantiated
if (fullyInstantiateImports(mModel, mIssues, pWithBusyWidget)) {
// Now, we can return our dependencies
return Core::FileManager::instance()->dependencies(mFileName);
} else {
return QStringList();
}
} else {
return QStringList();
}
}
//==============================================================================
CellmlFileRdfTriples & CellmlFile::rdfTriples()
{
// Return all the RDF triples associated with ourselves
return mRdfTriples;
}
//==============================================================================
CellmlFileRdfTriples CellmlFile::rdfTriples(iface::cellml_api::CellMLElement *pElement) const
{
// Return all the RDF triples associated with the given element
return mRdfTriples.associatedWith(pElement);
}
//==============================================================================
CellmlFileRdfTriple * CellmlFile::rdfTriple(iface::cellml_api::CellMLElement *pElement,
const QString &pQualifier,
const QString &pResource,
const QString &pId) const
{
// Return the RDF triple, associated with the given CellML element, which
// qualifier, resource and id match those given
if (QString::fromStdWString(pElement->cmetaId()).isEmpty()) {
// The given CellML element doesn't have a 'proper' cmeta:id, so it
// cannot have RDF triples associated with it
return 0;
}
// Go through the RDF triples associated with the given CellML element and
// check whether it is the one we are after
foreach (CellmlFileRdfTriple *rdfTriple, rdfTriples(pElement)) {
if ( !pQualifier.compare(rdfTriple->qualifierAsString())
&& !pResource.compare(rdfTriple->resource())
&& !pId.compare(rdfTriple->id())) {
// This is the RDF triple we are after
return rdfTriple;
}
}
// We couldn't find the RDF triple
return 0;
}
//==============================================================================
CellmlFileRdfTriple * CellmlFile::rdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::ModelQualifier &pModelQualifier,
const QString &pResource,
const QString &pId) const
{
// Call our generic rdfTriple() function
return rdfTriple(pElement,
CellmlFileRdfTriple::modelQualifierAsString(pModelQualifier),
pResource, pId);
}
//==============================================================================
CellmlFileRdfTriple * CellmlFile::rdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::BioQualifier &pBioQualifier,
const QString &pResource,
const QString &pId) const
{
// Call our generic rdfTriple() function
return rdfTriple(pElement,
CellmlFileRdfTriple::bioQualifierAsString(pBioQualifier),
pResource, pId);
}
//==============================================================================
QString CellmlFile::rdfTripleSubject(iface::cellml_api::CellMLElement *pElement)
{
// Make sure that we have a 'proper' cmeta:id or generate one, if needed
QString cmetaId = QString::fromStdWString(pElement->cmetaId());
if (cmetaId.isEmpty()) {
// We don't have a 'proper' cmeta:id for the CellML element, so we need
// to generate one and in order to do so, we need to try different
// cmeta:id values until we find one that is not used
int counter = 0;
while (true) {
cmetaId = QString("id_%1").arg(++counter, 9, 10, QChar('0'));
if (!mUsedCmetaIds.contains(cmetaId)) {
// We have found a unique cmeta:id, so update our CellML
// element, consider ourselves modified, update our list of
// cmeta:ids and leave
pElement->cmetaId(cmetaId.toStdWString());
setModified(true);
mUsedCmetaIds << cmetaId;
break;
}
}
}
// Return the subject which should be used for an RDF triple
return xmlBase()+"#"+cmetaId;
}
//==============================================================================
CellmlFileRdfTriple * CellmlFile::addRdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::ModelQualifier &pModelQualifier,
const QString &pResource,
const QString &pId)
{
// Add an RDF triple to ourselves
return mRdfTriples.add(new CellmlFileRdfTriple(this, rdfTripleSubject(pElement),
pModelQualifier, pResource, pId));
}
//==============================================================================
CellmlFileRdfTriple * CellmlFile::addRdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::BioQualifier &pBioQualifier,
const QString &pResource,
const QString &pId)
{
// Add an RDF Triple to ourselves
return mRdfTriples.add(new CellmlFileRdfTriple(this, rdfTripleSubject(pElement),
pBioQualifier, pResource, pId));
}
//==============================================================================
bool CellmlFile::removeRdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::ModelQualifier &pModelQualifier,
const QString &pResource, const QString &pId)
{
// Remove an RDF triple from ourselves
return mRdfTriples.remove(rdfTriple(pElement, pModelQualifier, pResource, pId));
}
//==============================================================================
bool CellmlFile::removeRdfTriple(iface::cellml_api::CellMLElement *pElement,
const CellmlFileRdfTriple::BioQualifier &pBioQualifier,
const QString &pResource, const QString &pId)
{
// Remove an RDF triple from ourselves
return mRdfTriples.remove(rdfTriple(pElement, pBioQualifier, pResource, pId));
}
//==============================================================================
QStringList CellmlFile::importedFileNames() const
{
// Return our imported file names
return mImportContents.keys();
}
//==============================================================================
QString CellmlFile::importedFileContents(const QString &pImportedFileName) const
{
// Return the contents of our given imported file name
return mImportContents.value(pImportedFileName);
}
//==============================================================================
QString CellmlFile::cmetaId()
{
// Return the CellML model's cmeta:id
if (load())
return QString::fromStdWString(mModel->cmetaId());
else
return QString();
}
//==============================================================================
QString CellmlFile::xmlBase()
{