예제 #1
0
파일: util.cpp 프로젝트: KDE/nepomuk-core
//
// We don't really care if the indexing level is in the incorrect graph
//
void Nepomuk2::updateIndexingLevel(const QUrl& uri, int level)
{
    QString uriN3 = Soprano::Node::resourceToN3( uri );

    QString query = QString::fromLatin1("select ?g ?l where { graph ?g { %1 kext:indexingLevel ?l . } }")
                    .arg ( uriN3 );
    Soprano::Model* model = ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );

    QUrl graph;
    Soprano::Node prevLevel;
    if( it.next() ) {
        graph = it[0].uri();
        prevLevel = it[1];
        it.close();
    }

    if( !graph.isEmpty() ) {
        QString graphN3 = Soprano::Node::resourceToN3( graph );
        QString removeCommand = QString::fromLatin1("sparql delete { graph %1 { %2 kext:indexingLevel %3 . } }")
                                .arg( graphN3, uriN3, prevLevel.toN3() );
        model->executeQuery( removeCommand, Soprano::Query::QueryLanguageUser, QLatin1String("sql") );

        QString insertCommand = QString::fromLatin1("sparql insert { graph %1 { %2 kext:indexingLevel %3 . } }")
                                .arg( graphN3, uriN3, Soprano::Node::literalToN3(level) );
        model->executeQuery( insertCommand, Soprano::Query::QueryLanguageUser, QLatin1String("sql") );
    }
    // Practically, this should never happen, but still
    else {
        QScopedPointer<KJob> job( Nepomuk2::setProperty( QList<QUrl>() << uri, KExt::indexingLevel(),
                                                                QVariantList() << QVariant(level) ) );
        job->setAutoDelete(false);
        job->exec();
    }
}
예제 #2
0
        NepomukStatementIterator(const QUrl& property = QUrl(), bool inference=true) {
            QString propertyFilter;
            if( !property.isEmpty() ) {
                propertyFilter = QString("FILTER(?p=%1)").arg( Soprano::Node::resourceToN3(property) );
            }

            QString query = QString::fromLatin1("select ?r ?p ?o where { ?r ?p ?o. "
                                                "FILTER(REGEX(STR(?r), '^nepomuk:/res')) . %1 }")
                            .arg( propertyFilter );
            Soprano::Model* model = ResourceManager::instance()->mainModel();
            if( inference )
                m_it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
            else
                m_it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
        }
예제 #3
0
void moviemanager::queryTag(QString userTag)
{
    //This module is not working
    //get help from pnh for debugging
    //pnh code
    Nepomuk::Tag myTag;
    myTag.setLabel(userTag);
    QString query
       = QString("select distinct ?r where { ?r %1 %2 . ?r a %3 }")
         .arg( Soprano::Node::resourceToN3(Soprano::Vocabulary::NAO::hasTag()) )
         .arg( Soprano::Node::resourceToN3(myTag.resourceUri()) )
         .arg( Soprano::Node::resourceToN3(Nepomuk::Vocabulary::NFO::Video()) );

    QList<Nepomuk::Resource> myResourceList;
    Soprano::Model *model = Nepomuk::ResourceManager::instance()->mainModel();

    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
    while( it.next() ) {
        qDebug() << "looping";
       myResourceList << Nepomuk::Resource( it.binding( "r" ).uri() );
    }

    Q_FOREACH (const Nepomuk::Resource& r, myResourceList)
    {
        mainMovieList->addItem(r.property(Nepomuk::Vocabulary::NFO::fileName()).toString());
        //if(r.tags().contains(new Nepomuk:Tag("video"))) newList.append(r)
    }
예제 #4
0
void RemoveDuplicates::loadDuplicates()
{
    Soprano::Model* model = Nepomuk::ResourceManager::instance()->mainModel();
    QString query
       = QString( "select distinct ?u1 where { "
                 "?r1 a %1 . ?r2 a %1. ?r1 %2 ?h. ?r2 %2 ?h. "
                 "?r1 %3 ?u1. ?r2 %3 ?u2. filter(?r1!=?r2) . }order by ?h limit 50")
         .arg( Soprano::Node::resourceToN3(Nepomuk::Vocabulary::NFO::FileDataObject()))
         .arg( Soprano::Node::resourceToN3(Nepomuk::Vocabulary::NFO::hasHash()))
         .arg( Soprano::Node::resourceToN3(Nepomuk::Vocabulary::NIE::url()));

    Soprano::QueryResultIterator it
       = model->executeQuery( query,
                              Soprano::Query::QueryLanguageSparql );
    Nepomuk::File tempRsc;
    while( it.next() ) {
        tempRsc = it.binding("u1").uri() ;
        QString usagecount = QString::number(tempRsc.usageCount());
        QListWidgetItem* item = new QListWidgetItem(tempRsc.genericLabel() + ":: Usage Count:" + usagecount,m_resourceList);
        item->setCheckState(Qt::Unchecked);
        item->setToolTip(tempRsc.url().path());
        qDebug()<<tempRsc.url().path();
    }

}
// static
void Nepomuk2::SubtitleLoader::eraseAllSubtitles()
{
    QString query = QString::fromLatin1("select distinct ?r where { ?r a %1 . }")
                    .arg( Soprano::Node::resourceToN3( NSBO::Subtitle() ) );
    kDebug() << query;

    Soprano::Model * model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
    while( it.next() ) {
        Nepomuk2::Resource res( it["r"].uri() );
        kDebug() << "Removing " << res.uri();
        res.remove();
    }
}
예제 #6
0
QList< Result > MainWindow::generateMultipleSubtitleResults()
{
    QString fromWords = m_fromLineEdit->text();
    QString toWords = m_toLineEdit->text();

    QString query = QString::fromLatin1("select distinct ?url ?st1 ?st2 ?en1 ?en2 where  {"
                                        "?r nie:url ?url . ?sub1 nsbo:subtitleFor ?r . ?sub2 nsbo:subtitleFor ?r ."
                                        "?sub1 nie:plainTextContent ?text1 . ?sub2 nie:plainTextContent ?text2 . "
                                        "?sub1 nsbo:startTime ?st1 . ?sub1 nsbo:endTime ?en1 . "
                                        "?sub2 nsbo:startTime ?st2 . ?sub2 nsbo:endTime ?en2. "
                                        "%1 %2 } ")
                    .arg( createFilter( fromWords, "text1" ),
                          createFilter( toWords, "text2" ) );

    kDebug() << query;

    // Need to implement the second time restriction
    Soprano::Model * model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
    QList<Result> results;
    while( it.next() ) {
        Result r;
        r.m_filePath = it["url"].uri().toLocalFile();

        QTime st1 = it["st1"].literal().toTime();
        QTime st2 = it["st2"].literal().toTime();
        QTime en1 = it["en1"].literal().toTime();
        QTime en2 = it["en2"].literal().toTime();

        int duration = en1.msecsTo(st2) / 1000;
        int minTime = m_minSpinBox->value();
        int maxTime = m_maxSpinBox->value();
        //kDebug() << "Duration : " << duration;
        if( minTime <= duration && duration <= maxTime ) {
            r.startTime = st1;
            r.endTime = en2;
            r.text = it["text1"].literal().toString() + it["text2"].literal().toString();

            kDebug() << "Pushing with duration " << duration;
            results << r;
        }
    }

    return results;
}
void Nepomuk2::SubtitleLoader::erase()
{
    //TODO: Avoid using Nepomuk2::Resource. Just query the nie:url
    Nepomuk2::Resource videoRes( m_videoUrl );
    kDebug() << videoRes.isFile();
    kDebug() << videoRes.uri();

    QString query = QString::fromLatin1("select distinct ?r where { ?r %1 %2 . }")
                    .arg( Soprano::Node::resourceToN3( NSBO::subtitleFor() ),
                          Soprano::Node::resourceToN3( videoRes.uri() ) );
    kDebug() << query;
    Soprano::Model * model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
    while( it.next() ) {
        //TODO: Use datamangement APIs
        Nepomuk2::Resource res( it["r"].uri() );
        kDebug() << "Removing " << res.uri();
        res.remove();
    }
}
예제 #8
0
bool Nepomuk2::Indexer::indexFile(const KUrl& url)
{
    QFileInfo info( url.toLocalFile() );
    if( !info.exists() ) {
        m_lastError = QString::fromLatin1("'%1' does not exist.").arg(info.filePath());
        return false;
    }

    QString query = QString::fromLatin1("select ?r ?mtype ?l where { ?r nie:url %1; nie:mimeType ?mtype ;"
                                        " kext:indexingLevel ?l . }")
                    .arg( Soprano::Node::resourceToN3( url ) );
    Soprano::Model* model = ResourceManager::instance()->mainModel();

    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );

    QUrl uri;
    QString mimeType;
    if( it.next() ) {
        uri = it[0].uri();
        mimeType = it[1].literal().toString();
        int level = it[2].literal().toInt();

        if( level > 1 ) {
            clearIndexingData( url );
            if( !simpleIndex( url, &uri, &mimeType ) )
                return false;
        }
    }
    else {
        if( !simpleIndex( url, &uri, &mimeType ) )
            return false;
    }

    kDebug() << uri << mimeType;
    return fileIndex( uri, url, mimeType );
}
예제 #9
0
QList< Result > MainWindow::generateSingleSubtitleResults(const QString& expression)
{
    if( expression.isEmpty() )
        return QList<Result>();

    QString filterExpression = createFilter( expression, "text" );

    QString query = QString::fromLatin1("select distinct ?url ?st ?end ?text where {"
                                        "?r %1 ?url . ?sub %2 ?r ."
                                        "?sub %3 ?st . ?sub %4 ?end . "
                                        "?sub %5 ?text . "
                                        "%6 } order by ?url ?st ")
                    .arg( Soprano::Node::resourceToN3( NIE::url() ),
                          Soprano::Node::resourceToN3( NSBO::subtitleFor() ),
                          Soprano::Node::resourceToN3( NSBO::startTime() ),
                          Soprano::Node::resourceToN3( NSBO::endTime() ),
                          Soprano::Node::resourceToN3( NIE::plainTextContent() ),
                          filterExpression );

    kDebug() << query;

    Soprano::Model * model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
    QList<Result> results;
    while( it.next() ) {
        Result r;
        r.m_filePath = it["url"].uri();
        r.startTime = it["st"].literal().toTime();
        r.endTime = it["end"].literal().toTime();
        r.text = it["text"].literal().toString();
        kDebug() << r.text;
        results << r;
    }

    return results;
}
예제 #10
0
파일: worker.cpp 프로젝트: KDE/scribo
void Worker::run()
{
    m_canceled = false;
    m_errorText.truncate( 0 );

    Soprano::Model* model = Soprano::createModel( Soprano::BackendSettings() << Soprano::BackendSetting( Soprano::BackendOptionStorageMemory ) );
    if ( !model ) {
        m_errorText = i18n( "Failed to create Soprano memory model. Most likely the installation misses Soprano backend plugins" );
        return;
    }

    model->addStatements( m_data.toList() );

    // select all instances that do not have a subject -> these should be the extracted entities
    Soprano::QueryResultIterator it = model->executeQuery( QString::fromLatin1( "select * where { "
                                                                                "?r a ?type . "
                                                                                "OPTIONAL { ?r <http://s.opencalais.com/1/pred/subject> ?sub . } . "
                                                                                "FILTER(!bound(?sub)) . "
                                                                                "}" ),
                                                           Soprano::Query::QueryLanguageSparql );
    while ( !m_canceled && it.next() ) {
        Soprano::Node r = it["r"];
        Soprano::Graph graph;
        Soprano::QueryResultIterator it2 = model->executeQuery( QString::fromLatin1( "construct { ?entity ?ep ?eo . ?rel ?relp ?relo . } "
                                                                                     "where { "
                                                                                     "?entity <http://s.opencalais.com/1/pred/subject> %1 . "
                                                                                     "?entity ?ep ?eo . "
                                                                                     "?rel a <http://s.opencalais.com/1/type/sys/RelevanceInfo> . "
                                                                                     "?rel <http://s.opencalais.com/1/pred/subject> %1 . "
                                                                                     "?rel ?relp ?relo . "
                                                                                     "}" )
                                                                .arg( r.toN3() ),
                                                                Soprano::Query::QueryLanguageSparql );
        while ( !m_canceled && it2.next() ) {
            graph << it2.currentStatement();
        }

        if ( m_canceled )
            break;

        it2 = model->executeQuery( QString::fromLatin1( "select ?name ?rel ?type ?offset ?length where { "
                                                        "%1 <http://s.opencalais.com/1/pred/name> ?name . "
                                                        "%1 a ?type . "
                                                        "?relInfo a <http://s.opencalais.com/1/type/sys/RelevanceInfo> . "
                                                        "?relInfo <http://s.opencalais.com/1/pred/subject> %1 . "
                                                        "?relInfo <http://s.opencalais.com/1/pred/relevance> ?rel . "
                                                        "?info a <http://s.opencalais.com/1/type/sys/InstanceInfo> . "
                                                        "?info <http://s.opencalais.com/1/pred/subject> %1 . "
                                                        "?info <http://s.opencalais.com/1/pred/offset> ?offset . "
                                                        "?info <http://s.opencalais.com/1/pred/length> ?length . "
                                                        "}" )
                                   .arg( r.toN3() ),
                                   Soprano::Query::QueryLanguageSparql );
        if ( !m_canceled && it2.next() ) {
            Nepomuk::Types::Class type( m_plugin->matchPimoType( it2["type"].uri() ) );
            QString name = it2["name"].toString();

            // FIXME: actually the opencalais resource should be used as the pimo:hasOtherRepresentation of the pimo thing
            //        but that would mean that Entity needs more information or the graph needs to be really used
            Scribo::Entity entity( name, type, graph );
            do {
                double rel = it2["rel"].literal().toDouble();
                int offset = it2["offset"].literal().toInt();
                int length = it2["length"].literal().toInt();

                Scribo::TextOccurrence oc;
                oc.setStartPos( offset );
                oc.setLength( length );
                oc.setRelevance( rel );
                entity.addOccurrence( oc );

                kDebug() << type << type.label() << name << rel << offset << length;
            } while ( !m_canceled && it2.next() );

            if ( m_canceled )
                break;

            addNewMatch( entity );

            // find relations for the entity
            Soprano::QueryResultIterator it3 = model->executeQuery( QString::fromLatin1( "select ?verb ?offset ?length ?exact where { "
                                                                                         "?s a <http://s.opencalais.com/1/type/em/r/GenericRelations> . "
                                                                                         "?s <http://s.opencalais.com/1/pred/relationsubject> %1 . "
                                                                                         "?s <http://s.opencalais.com/1/pred/verb> ?verb . "
                                                                                         "?info a <http://s.opencalais.com/1/type/sys/InstanceInfo> . "
                                                                                         "?info <http://s.opencalais.com/1/pred/subject> ?s . "
                                                                                         "?info <http://s.opencalais.com/1/pred/offset> ?offset . "
                                                                                         "?info <http://s.opencalais.com/1/pred/length> ?length . "
                                                                                         "?info <http://s.opencalais.com/1/pred/exact> ?exact . "
                                                                                         "}" )
                                                                    .arg( r.toN3() ),
                                                                    Soprano::Query::QueryLanguageSparql );
            if ( !m_canceled && it3.next() ) {
                QString verb = it3["verb"].toString();
                QString exact = it3["exact"].toString();
                int offset = it3["offset"].literal().toInt();
                int length = it3["length"].literal().toInt();

                // FIXME: get the graph
                Scribo::Statement s( verb, entity, exact, Soprano::Graph() );

                Scribo::TextOccurrence oc;
                oc.setStartPos( offset );
                oc.setLength( length );
                s.addOccurrence( oc );

                addNewMatch( s );
            }
        }
    }

    delete model;
}
예제 #11
0
QList<CachedRowEntry> EventQuery::processQueryResults(const QString &query, const QUrl &uri)
{
    Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );

    // combine all search results again, so we get a just a single resource with a list of all authors and the list of types
    // instead of many resources with all types again
    QMap<QString, QStringList> resultList;
    while( it.next() ) {
        Soprano::BindingSet p = it.current();

        // get either a new entry or what we have inserted beforehand
        QStringList curEntry = resultList.value( p.value("r").toString(), QStringList());

        // now set / add each queried value into the StringList
        if (curEntry.isEmpty() ) {

            curEntry << p.value("star").toString() << p.value("title").toString()
                     << p.value("date").toString() << p.value("publication").toString() << p.value("r").toString();
        }
        else {
            QString star = p.value("star").toString();
            if(!star.isEmpty())
                curEntry[Column_StarRate] = star;

            QString title = p.value("title").toString();
            if(!title.isEmpty())
                curEntry[Column_Title] = title;

            QString date = p.value("date").toString();
            if( !date.isEmpty() )
                curEntry[Column_Date] = date;

            QString publication = p.value("publication").toString();
            if(!publication.isEmpty() && !curEntry[Column_Publication] .contains(publication)) {

                //create content for the HTMLDelegate looks a lot better when several entries are being displayed
                curEntry[Column_Publication] = curEntry[Column_Publication].append("&#8226; ");
                curEntry[Column_Publication] = curEntry[Column_Publication].append(publication);
                curEntry[Column_Publication] = curEntry[Column_Publication].append("<br/>");
            }
        }

        // and save the result back into the map
        if(p.value("r").isEmpty()) {
            Q_ASSERT(!uri.isEmpty());
            resultList.insert(uri.toString(), curEntry);
        }
        else {
            resultList.insert(p.value("r").toString(), curEntry);
        }
    }

    // now create the cache entries from all returned search results
    QList<CachedRowEntry> newCache;
    QMapIterator<QString, QStringList> i(resultList);
    while (i.hasNext()) {
        i.next();

        CachedRowEntry cre;
        cre.uri = QUrl( i.key() );
        cre.displayColums = createDisplayData(i.value());
        cre.decorationColums = createDecorationData(i.value());
        cre.resource = Nepomuk2::Resource::fromResourceUri( cre.uri  );
        cre.timestamp = QDateTime::currentDateTime();
        cre.resourceType = 0; //Unused
        newCache.append(cre);
    }

    return newCache;
}