Exemplo n.º 1
0
bool NW::Executive::canProcess( const Nepomuk::Resource & res) const
{
    // If no filter set up, then no filter is necessary
    if ( d->typeFilters.isEmpty() )
        return true;

    // Create a query
    /*
    for( int i = 0; i < d->typeFilters; i++)
    {
    }*/
    static QString unionString = QString( "UNION" );
    QStringList queryExactList;
    QStringList querySubclassList;
    bool currentType = d->typeFilters[0].allowed;

            
    /* General idea - unite consiquent records with same .allowed field into
     * one group, and  when prepare to switch to another group - create a query
     * from accumulated strings, ask it and determine the result. 
     * Reaching the end of filter list is treated as switching to another group
     */
    foreach( const FilterRecord & record, d->typeFilters)
    {
        if ( record.allowed != currentType ) {

            // After this function call, queryExactList will change!
            QString query = buildFilterQuery(res.resourceUri(), queryExactList, querySubclassList);


            Soprano::QueryResultIterator it = res.manager()->mainModel()->executeQuery(query, Soprano::Query::QueryLanguageSparql );
            if ( it.next() ) {
                // Has answer
                if ( it.boolValue() ) {
                    return currentType;
                }
            }
            else {
                // Wow. Ask query doesn't return an answer. May be it is incorrect ?
                qCritical() << "Filter query is incorrect. Report a BUG please";
                return false;
            }

            // Clear
            queryExactList.clear();
            querySubclassList.clear();

            // Switch current type
            currentType = record.allowed;
        }

        // Accumulate to the group
        if ( record.matchType == ExactMatch or record.matchType == SubclassOrExactMatch ) {
            queryExactList << FilterRecord::queryString(ExactMatch, record.typeUrl) << unionString;
        }

        if ( record.matchType == SubclassOrExactMatch or record.matchType == SubclassMatch ) {
            querySubclassList << FilterRecord::queryString(SubclassMatch, record.typeUrl) << unionString;
        }
    }
    // Build for the last group
    QString query = buildFilterQuery(res.resourceUri(), queryExactList, querySubclassList);


    Soprano::QueryResultIterator it = res.manager()->mainModel()->executeQuery(query, Soprano::Query::QueryLanguageSparql );
    if ( it.isBool() ) {
        // Has answer
        if ( it.boolValue() ) {
            return currentType;
        }
        else {
            // return default rule
            return d->defaultTypeRule;
        }
    }
    else {
        // Wow. Ask query doesn't return an answer. May be it is incorrect ?
        qCritical() << "Filter query is incorrect. Report a BUG please";
        return false;
    }


}