コード例 #1
0
ファイル: sopranoliteraltest.cpp プロジェクト: KDE/soprano
void SopranoLiteralTest::testDataTypes()
{
    const QString xmlSchemaNs( "http://www.w3.org/2001/XMLSchema#" );

    QFETCH(QString, dataType);
    QFETCH(LiteralValue, value);

    Node subject( QUrl("http://iamarandomuri/yessir") );
    Node predicate( QUrl("http://iamarandomuri/yesmadam/" + QString(QTest::currentDataTag())) );

    Node object( value );

    Statement s( subject, predicate, object );

    // add the value
    QVERIFY( m_model->addStatement( s ) == 0 );

    // and retrieve it
    StatementIterator it = m_model->listStatements( Statement( subject, predicate, Node() ) );
    QVERIFY( it.isValid() );
    QVERIFY( it.next() );
    Statement s2 = *it;
    QCOMPARE( s2.object().literal().toString(), value.toString() );
    QCOMPARE( s2.object().dataType(), object.dataType() );
    QCOMPARE( s2.object().literal().dataTypeUri().toString(), xmlSchemaNs + dataType );
}
コード例 #2
0
ファイル: sopranoliteraltest.cpp プロジェクト: KDE/soprano
void SopranoLiteralTest::testPlainLiterals()
{
    QFETCH(QString, str);
    QFETCH(QString, lang);
    QFETCH(LiteralValue, value);

    Node subject( QUrl("http://iamarandomuri/yessir") );
    Node predicate( QUrl("http://iamarandomuri/yesmadam/" + QString(QTest::currentDataTag())) );

    Node object( value );

    Statement s( subject, predicate, object );

    // add the value
    QVERIFY( m_model->addStatement( s ) == 0 );

    // and retrieve it
    StatementIterator it = m_model->listStatements( Statement( subject, predicate, Node() ) );
    QVERIFY( it.isValid() );
    QVERIFY( it.next() );
    Statement s2 = *it;
    QCOMPARE( s2.object().literal().toString(), str );
    QCOMPARE( s2.object().literal().language(), object.literal().language() );
    QCOMPARE( s2.object().literal().language(), LanguageTag(lang) );
}
コード例 #3
0
ファイル: TestRdf.cpp プロジェクト: KDE/calligra
void TestRdf::basicload()
{
    RDEBUG;

    QString odt = QString(FILES_DATA_DIR) + "/weekend-hike.odt";
    KoDocumentRdf *rdf = loadDocument(odt);
    QVERIFY(rdf);
    QSharedPointer<Soprano::Model> m = rdf->model();
    QVERIFY(m);
    QCOMPARE (234, m->statementCount());

    StatementIterator it;
    QList<Statement> allStatements;

    // check a triple
    it = m->listStatements(
                Node::createResourceNode(QUrl("uri:james")),
                Node::createResourceNode(QUrl("http://xmlns.com/foaf/0.1/name")),
                Node());
    allStatements = it.allElements();
    QCOMPARE (allStatements.size(), 1);
    foreach (Soprano::Statement s, allStatements) {
        // RDEBUG << "HAVE:" << s;
        QVERIFY (s.object().toString() == "James Smith");
    }
コード例 #4
0
QString KoRdfSemanticItemViewSite::getProperty(const QString &prop, const QString &defval) const
{
    Soprano::Node ls = linkingSubject();
    QString fqprop = "http://koffice.org/rdf/site#" + prop;
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    Soprano::Model *m = rdf->model();
    StatementIterator it = m->listStatements(ls, Node::createResourceNode(QUrl(fqprop)),
                               Node(), rdf->manifestRdfNode());
    QList<Statement> allStatements = it.allElements();
    foreach (Soprano::Statement s, allStatements) {
        return s.object().toString();
    }
    return defval;
}
コード例 #5
0
QStringList KoRdfBasicSemanticItem::xmlIdList() const
{
    QStringList ret;

    StatementIterator it = documentRdf()->model()->listStatements(
        linkingSubject(),
        Node::createResourceNode(QUrl("http://docs.oasis-open.org/ns/office/1.2/meta/pkg#idref")),
        Node(),
        documentRdf()->manifestRdfNode()
    );
    QList<Statement> allStatements = it.allElements();
    foreach (const Soprano::Statement &s, allStatements) {
        QString xmlid = s.object().toString();
        ret << xmlid;
    }
コード例 #6
0
Soprano::Node KoRdfSemanticItemViewSite::linkingSubject() const
{
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    Soprano::Model *m = rdf->model();
    Node pred(QUrl("http://koffice.org/rdf/site/package/common#idref"));
    Node obj = Node::createLiteralNode(d->m_xmlid);
    Node context = rdf->manifestRdfNode();
    // try to find it if it already exists
    StatementIterator it = m->listStatements(Node(), pred, obj, context);
    QList<Statement> allStatements = it.allElements();
    foreach (Soprano::Statement s, allStatements) {
        return s.subject();
    }
    Node ret = m->createBlankNode();
    m->addStatement(ret, pred, obj, context);
    return ret;
}
コード例 #7
0
ファイル: nrlmodel.cpp プロジェクト: KDE/soprano
Soprano::Error::ErrorCode Soprano::NRLModel::addNrlStatement( const Statement& statement )
{
    // 1. check if any cardinality restrictions are defined for s.predicate()
    // 2. if so -> enforce
    // 3. if not -> check if some for superproperties are defined (optional advanced feature)

    QString query = QString( "select ?min ?max ?c where { { <%1> <%2> ?min } UNION { <%1> <%3> ?max } UNION { <%1> <%4> ?c } }" )
                    .arg( statement.predicate().toString() )
                    .arg( Vocabulary::NRL::minCardinality().toString() )
                    .arg( Vocabulary::NRL::maxCardinality().toString() )
                    .arg( Vocabulary::NRL::cardinality().toString() );

    QueryResultIterator it = FilterModel::executeQuery( query, Query::QueryLanguageSparql );
    if ( !it.isValid() ) {
        setError( QString( "Query failed: '%1'" ).arg( query ) );
        return Error::ErrorUnknown;
    }

    int min = -1;
    int max = -1;
    int c = -1;

    while ( it.next() ) {
        if ( it.binding( "min" ).isLiteral() ) {
            min = it.binding( "min" ).literal().toInt();
        }
        else if ( it.binding( "max" ).isLiteral() ) {
            max = it.binding( "max" ).literal().toInt();
        }
        if ( it.binding( "c" ).isLiteral() ) {
            c = it.binding( "c" ).literal().toInt();
        }
    }

    if ( min >= 0 || max >= 0 || c >= 0 ) {
        qDebug() << "Predicate " << statement.predicate() << " has cardinalities: " << min << "; " << max << "; " << c;

        // the simple case (and also the most frequently used I suppose)
        if ( c == 1 || max == 1 ) {
            if ( ignoreContext() ) {
                FilterModel::removeAllStatements( Statement( statement.subject(), statement.predicate(), Node() ) );
            }
            else {
                FilterModel::removeAllStatements( Statement( statement.subject(), statement.predicate(), Node(), statement.context() ) );
            }
            return FilterModel::addStatement( statement );
        }

        // the general case
        else if ( max > 1 || c > 1 ) {
            if ( c > 1 )
                max = c;

            StatementIterator sit;
            if ( ignoreContext() ) {
                sit = FilterModel::listStatements( Statement( statement.subject(), statement.predicate(), Node() ) );
            }
            else {
                sit = FilterModel::listStatements( Statement( statement.subject(), statement.predicate(), statement.context() ) );
            }

            QList<Statement> matchingStatements;
            while ( sit.next() ) {
                Statement s = *sit;
                if ( ignoreContext() || s.context() == statement.context() ) {
                    matchingStatements.append( s );
                }
            }

            if ( matchingStatements.count() >= max ) {
                qDebug() << "Found " << matchingStatements.count() << " statements that define " << statement.predicate() << " for " << statement.subject() << endl
                         << "  -> need to remove " << ( matchingStatements.count()-max+1 ) << " before adding the new statement.";
                if ( matchingStatements.count() == 1 ) {
                    qDebug() << "Removing one statement is easy...";
                    FilterModel::removeStatement( matchingStatements[0] );
                }
                else {
                    qDebug() << "FIXME: which statements to remove? Random? Best would be to remove the oldest...";
                    setError( "Max cardinality for predicate reached" );
                    return Error::ErrorUnknown;
                }
            }

            return FilterModel::addStatement( statement );
        }
        else {
            qDebug()  << "Predicate " << statement.predicate() << " has unusable cardinality restrictions";
            return FilterModel::addStatement( statement );
        }
    }
    else {
        qDebug() << "Predicate " << statement.predicate() << " has no cardinality restrictions";
        return FilterModel::addStatement( statement );
    }

    // make gcc shut up
    return Error::ErrorNone;
}