Warning, file /office/calligra/libs/rdf/KoRdfBasicSemanticItem.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoRdfBasicSemanticItem.h"
0021 
0022 //Calligra
0023 #include <KoDocumentRdf.h>
0024 
0025 // KF5
0026 #include <KDebug>
0027 
0028 //QT
0029 #include <QUuid>
0030 #include <QDateTime>
0031 
0032 using namespace Soprano;
0033 
0034 KoRdfBasicSemanticItem::KoRdfBasicSemanticItem(QObject *parent)
0035     : QObject(parent)
0036     , m_rdf(0)
0037 {
0038 }
0039 
0040 KoRdfBasicSemanticItem::KoRdfBasicSemanticItem(QObject *parent, const KoDocumentRdf *rdf)
0041     : QObject(parent)
0042     , m_rdf(rdf)
0043 {
0044 }
0045 
0046 KoRdfBasicSemanticItem::KoRdfBasicSemanticItem(QObject *parent, const KoDocumentRdf *rdf, Soprano::QueryResultIterator &it)
0047     : QObject(parent)
0048     , m_rdf(rdf)
0049 {
0050     m_context = it.binding("graph");
0051     kDebug(30015) << "KoRdfBasicSemanticItem() context:" << m_context.toString();
0052 }
0053 
0054 KoRdfBasicSemanticItem::~KoRdfBasicSemanticItem()
0055 {
0056 }
0057 
0058 const KoDocumentRdf *KoRdfBasicSemanticItem::documentRdf() const
0059 {
0060     return m_rdf;
0061 }
0062 
0063 QStringList KoRdfBasicSemanticItem::xmlIdList() const
0064 {
0065     QStringList ret;
0066 
0067     StatementIterator it = documentRdf()->model()->listStatements(
0068         linkingSubject(),
0069         Node::createResourceNode(QUrl("http://docs.oasis-open.org/ns/office/1.2/meta/pkg#idref")),
0070         Node(),
0071         documentRdf()->manifestRdfNode()
0072     );
0073     QList<Statement> allStatements = it.allElements();
0074     foreach (const Soprano::Statement &s, allStatements) {
0075         QString xmlid = s.object().toString();
0076         ret << xmlid;
0077     }
0078     return ret;
0079 }
0080 
0081 void KoRdfBasicSemanticItem::updateTriple_remove(const Soprano::LiteralValue &toModify,
0082         const QString &predString,
0083         const Soprano::Node &explicitLinkingSubject)
0084 {
0085     QSharedPointer<Soprano::Model> m = documentRdf()->model();
0086     Node pred = Node::createResourceNode(QUrl(predString));
0087     m->removeStatement(explicitLinkingSubject,pred, Node::createLiteralNode(toModify));
0088     kDebug(30015) << "Rdf.del subj:" << explicitLinkingSubject;
0089     kDebug(30015) << "Rdf.del pred:" << pred;
0090     kDebug(30015) << "Rdf.del  obj:" << Node::createLiteralNode(toModify);
0091     kDebug(30015) << "Rdf.del  ctx:" << context();
0092     //
0093     // Typeless remove, I found that if a object literal did not
0094     // stipulate its type in the input Rdf, just using
0095     // removeStatement() above might not pick it up. So the below code
0096     // looks through all statements with subj+pred and checks typeless
0097     // string identity of the object() and removes it if strings match.
0098     //
0099     StatementIterator it = m->listStatements(explicitLinkingSubject, pred, Node());
0100     QList<Statement> removeList;
0101     QList<Statement> allStatements = it.allElements();
0102     foreach (const Soprano::Statement &s, allStatements) {
0103         kDebug(30015) << "typeless remove,  s:" << s.object().toString();
0104         kDebug(30015) << "typeless remove, tm:" << Node::createLiteralNode(toModify).toString();
0105 
0106         if (s.object().toString() == Node::createLiteralNode(toModify).toString()) {
0107             removeList << s;
0108         }
0109         //
0110         // Sometimes the object value is serialized as 51.47026 or
0111         // 5.1470260000e+01. There are also slight rounding errors
0112         // which are introduced that complicate comparisons.
0113         //
0114         if (toModify.isDouble()) {
0115             removeList << s;
0116         }
0117     }
0118     m->removeStatements(removeList);
0119 }
0120 
0121 void KoRdfBasicSemanticItem::updateTriple_add(const Soprano::LiteralValue &toModify,
0122                                        const QString &predString,
0123                                        const Soprano::Node &explicitLinkingSubject)
0124 {
0125     QSharedPointer<Soprano::Model> m = documentRdf()->model();
0126     Node pred = Node::createResourceNode(QUrl(predString));
0127 
0128     kDebug(30015) << "Rdf.add subj:" << explicitLinkingSubject;
0129     kDebug(30015) << "Rdf.add pred:" << pred;
0130     kDebug(30015) << "Rdf.add  obj:" << Node::createLiteralNode(toModify);
0131     kDebug(30015) << "Rdf.add  ctx:" << context();
0132     m->addStatement(explicitLinkingSubject, pred, Node::createLiteralNode(toModify), context());
0133 }
0134 
0135 
0136 void KoRdfBasicSemanticItem::updateTriple(QString &toModify, const QString &newValue, const QString &predString)
0137 {
0138     kDebug(30015) << "tomod:" << toModify << " newV:" << newValue << " pred:" << predString;
0139     updateTriple_remove(toModify, predString, linkingSubject());
0140     toModify = newValue;
0141     updateTriple_add(toModify, predString, linkingSubject());
0142 }
0143 
0144 void KoRdfBasicSemanticItem::updateTriple(KDateTime &toModify, const KDateTime &newValue, const QString &predString)
0145 {
0146     updateTriple_remove(Soprano::LiteralValue(toModify.dateTime()),
0147                         predString, linkingSubject());
0148     toModify = newValue;
0149     updateTriple_add(Soprano::LiteralValue(toModify.dateTime()),
0150                      predString, linkingSubject());
0151 }
0152 
0153 void KoRdfBasicSemanticItem::updateTriple(double &toModify,
0154                                    double newValue,
0155                                    const QString &predString,
0156                                    const Soprano::Node &explicitLinkingSubject)
0157 {
0158     updateTriple_remove(Soprano::LiteralValue(toModify), predString, explicitLinkingSubject);
0159     toModify = newValue;
0160     updateTriple_add(Soprano::LiteralValue(toModify), predString, explicitLinkingSubject);
0161 }
0162 
0163 void KoRdfBasicSemanticItem::setRdfType(const QString &t)
0164 {
0165     QSharedPointer<Soprano::Model> m = documentRdf()->model();
0166     Q_ASSERT(m);
0167     Node pred = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
0168     m->addStatement(linkingSubject(), pred, Node::createResourceNode(t), context());
0169 }
0170 
0171 Soprano::Node KoRdfBasicSemanticItem::linkingSubject() const
0172 {
0173     return Node::createEmptyNode();
0174 }
0175 
0176 Soprano::Node KoRdfBasicSemanticItem::context() const
0177 {
0178     if (m_context.isValid()) {
0179         return m_context;
0180     }
0181     return documentRdf()->manifestRdfNode();
0182 }
0183 
0184 Soprano::Node KoRdfBasicSemanticItem::createNewUUIDNode() const
0185 {
0186     QString uuid = QUuid::createUuid().toString();
0187     uuid.remove('{');
0188     uuid.remove('}');
0189     QString nodestr = "http://calligra.org/uuidnode/" + uuid;
0190     return Node::createResourceNode(nodestr);
0191 }