[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: playground/base/nepomuk-kde/annotation/plugins/pimo
From: Sebastian Trueg <sebastian () trueg ! de>
Date: 2011-02-24 12:51:30
Message-ID: 20110224125130.03CE7AC8C0 () svn ! kde ! org
[Download RAW message or body]
SVN commit 1222511 by trueg:
Simplified pimo annotation plugin to only use pimo:isRelated with matching pimo \
things. This makes for simpler code and faster results.
The custom property handling will be done in a new plugin.
M +18 -226 pimoannotationplugin.cpp
M +2 -8 pimoannotationplugin.h
--- trunk/playground/base/nepomuk-kde/annotation/plugins/pimo/pimoannotationplugin.cpp \
#1222510:1222511 @@ -1,5 +1,5 @@
/*
- Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
+ Copyright (c) 2008-2011 Sebastian Trueg <trueg@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
@@ -45,47 +45,12 @@
#include <KPluginFactory>
#include <KPluginLoader>
#include <KDebug>
+#include <KIcon>
-#include <math.h>
NEPOMUK_EXPORT_ANNOTATION_PLUGIN( Nepomuk::PimoAnnotationPlugin, \
"nepomuk_pimoannotationplugin" )
-namespace {
- /**
- * QVariant::canConvert does not actually try to convert strings to numbers. It \
for example
- * returns true for strings like "wurst".
- */
- bool canConvert( const QVariant& value, QVariant::Type type )
- {
- if ( value.type() == type ) {
- return true;
- }
- else if ( value.canConvert( type ) ) {
- bool ok = false;
- switch ( type ) {
- case QVariant::Int:
- case QVariant::UInt:
- case QVariant::LongLong:
- case QVariant::ULongLong:
- value.toString().toLongLong( &ok );
- return ok;
-
- case QVariant::Double:
- value.toString().toDouble( &ok );
- return ok;
-
- default:
- return true;
- }
- }
-
- return false;
- }
-}
-
-
-
Nepomuk::PimoAnnotationPlugin::PimoAnnotationPlugin( QObject* parent, const \
QVariantList& ) : AnnotationPlugin( parent )
{
@@ -105,220 +70,47 @@
void Nepomuk::PimoAnnotationPlugin::doGetPossibleAnnotations( const \
AnnotationRequest& request ) {
- m_resource = request.resource();
- QString filter = request.filter();
-
m_queryClient->close();
- if ( !filter.isEmpty() &&
- m_resource.isValid() ) {
- if ( filter.contains( ':' ) ) {
- QStringList s = filter.split( ':', QString::SkipEmptyParts);
- if ( s.count() == 2 ) {
- m_propertyLabel = s[0];
- m_valueLabel = s[1];
- matchProperty( m_propertyLabel );
- return;
- }
- }
- else {
- m_valueLabel = filter;
- createRelationAnnotations( QList<Types::Property>() << \
Nepomuk::Vocabulary::PIMO::isRelated() );
- return;
- }
- }
+ if(!request.filter().isEmpty()) {
+ m_resource = request.resource();
+ m_filter = request.filter();
- // done
- emitFinished();
-}
-
-void Nepomuk::PimoAnnotationPlugin::matchProperty( const QString& label )
-{
- kDebug() << label;
- Query::QueryParser parser;
- m_matchedProperties = parser.matchProperty(label);
- createRelationAnnotations(m_matchedProperties);
-}
-
-void Nepomuk::PimoAnnotationPlugin::createRelationAnnotations( const \
QList<Nepomuk::Types::Property>& properties )
-{
- kDebug() << properties;
-
- m_matchedProperties = properties;
-
- // IDEA: use subproperties if the domain and range match.
- // FIXME: if the direct match properties did not produce any results below, redo \
the indirect query
- // (or always to indirect matches and give a higher score to direct prop \
matches)
-
- QVariant value( m_valueLabel );
-
- if ( !properties.isEmpty() ) {
- //
- // First do the literal relations
- //
- for ( int i = 0; i < properties.count(); ++i ) {
- Types::Property property = properties[i];
- Types::Literal literalRange = property.literalRangeType();
- if ( literalRange.isValid() ) {
- kDebug() << property.uri() << "has literal range of type" << \
literalRange.dataType();
- if ( canConvert( value, literalRange.dataType() ) ) {
- SimplePimoAnnotation* anno = new SimplePimoAnnotation( this );
- anno->setLabel( "FIXME" );
- anno->setComment( i18nc( "@action A property (%1) of a selected \
resource is set to value %2",
- "Set '%1' to %2", property.label(), \
value.toString() ) );
- anno->setRelevance( 1.0 - calculateDistance( property.label(), \
m_propertyLabel ) );
- anno->setProperty( property );
- anno->setValue( Nepomuk::Variant( value ) );
- addNewAnnotation( anno );
- }
- else if ( literalRange.dataType() == QVariant::DateTime ) {
- QString today = i18nc( "@label A fancy way of writing the \
current date.", "today" );
- QString yesterday = i18nc( "@label A fancy way of writing a \
date.", "yesterday" );
- QString now = i18nc( "@label A fancy way of writing the current \
time", "now" );
- if ( !m_valueLabel.compare( today, Qt::CaseInsensitive ) ) {
- // FIXME: do something
- }
- else if ( !m_valueLabel.compare( yesterday, Qt::CaseInsensitive \
) ) {
- // FIXME: do something
-
- }
- else if ( !m_valueLabel.compare( now, Qt::CaseInsensitive ) ) {
- // FIXME: do something
-
- }
- }
-
- // do not try to handle the property below for thing matching
- m_matchedProperties.removeAll( property );
- }
- }
- }
-
- handleNextProperty();
-}
-
-
-void Nepomuk::PimoAnnotationPlugin::handleNextProperty()
-{
- Nepomuk::Types::Class range = Vocabulary::PIMO::Thing();
-
- if ( !m_matchedProperties.isEmpty() ) {
- //
- // Find some things to relate to
- //
- Nepomuk::Types::Class range = m_matchedProperties.first().range();
- }
-
- // we only handle properties which can relate to things
- if ( range.uri() == Vocabulary::PIMO::Thing() ||
- range.isSubClassOf( Vocabulary::PIMO::Thing() ) ||
- Types::Class( Vocabulary::PIMO::Thing() ).isSubClassOf( range ) ) {
Nepomuk::Query::Query query(
- Nepomuk::Query::ResourceTypeTerm(range)
- && Nepomuk::Query::LiteralTerm(m_valueLabel));
- query.setLimit(10);
+ Nepomuk::Query::ResourceTypeTerm(Vocabulary::PIMO::Thing())
+ && Nepomuk::Query::LiteralTerm(request.filter()));
+ query.setLimit(5);
+ query.setFullTextScoringEnabled(true);
+ kDebug() << query.toSparqlQuery();
m_queryClient->query(query);
}
else {
- kDebug() << "Ignoring property" << m_matchedProperties.first();
- m_matchedProperties.takeFirst();
- QMetaObject::invokeMethod( this, "handleNextProperty", Qt::QueuedConnection \
); + emitFinished();
}
}
void Nepomuk::PimoAnnotationPlugin::slotNewEntries( const \
QList<Nepomuk::Query::Result>& entries ) {
- QVariant value( m_valueLabel );
-
- if ( m_matchedProperties.isEmpty() ) {
-
- qreal maxResourceAnnotationRelevance = 0.0;
-
- // 1. Propose to create a new property with a thing that matches \
m_valueLabel
- // score higher than the literal ones
-
Q_FOREACH(const Query::Result& result, entries) {
Resource res = result.resource();
SimplePimoAnnotation* anno = new SimplePimoAnnotation( this );
- anno->setLabel( "FIXME" );
- anno->setComment( i18nc( "@action A property (%1) of a selected resource \
is set to value %2",
- "Create new relation: Set '%1' to %2", \
m_propertyLabel, res.genericLabel() ) );
- anno->setRelevance( 0.99 * ( 1.0 - calculateDistance( \
res.genericLabel(), m_valueLabel ) ) );
- anno->setNewPropertyLabel( m_propertyLabel );
+ anno->setIcon(KIcon(res.genericIcon()));
+ anno->setLabel( res.genericLabel() );
+ anno->setComment( i18nc( "@action A selected resource is set to be related \
to value %1", + "Related to '%1'", res.genericLabel() \
) ); + anno->setRelevance( 1.0 - calculateDistance( res.genericLabel(), \
m_filter) ); + anno->setProperty( Nepomuk::Vocabulary::PIMO::isRelated() );
anno->setValue( Nepomuk::Variant( res ) );
addNewAnnotation( anno );
- maxResourceAnnotationRelevance = qMax( maxResourceAnnotationRelevance, \
anno->relevance() ); }
-
- // 2. propose the creation of a new property with a literal range (handle \
int, qreal, and string)
-
- SimplePimoAnnotation* anno = new SimplePimoAnnotation( this );
- anno->setLabel( "FIXME" );
- // relevance of creating new stuff is never as good as something matching \
perfectly
- // and creating a new property linking to resources is always better than \
linking to literals
- anno->setRelevance( maxResourceAnnotationRelevance > 0.0 ? \
0.99*maxResourceAnnotationRelevance : 0.99 );
- anno->setNewPropertyLabel( m_propertyLabel );
-
- if ( canConvert( value, QVariant::Int ) ) {
- anno->setComment( i18nc("@action A property (%1) of a selected resource \
is set to value %2",
- "Create new relation: Set '%1' to %2", \
m_propertyLabel, value.toInt() ) );
- anno->setValue( Nepomuk::Variant( value.toInt() ) );
}
- else if ( canConvert( value, QVariant::Double ) ) {
- anno->setComment( i18nc("@action A property (%1) of a selected resource \
is set to value %2",
- "Create new relation: Set '%1' to %2", \
m_propertyLabel, value.toDouble() ) );
- anno->setValue( Nepomuk::Variant( value.toDouble() ) );
- }
- else {
- anno->setComment( i18nc("@action A property (%1) of a selected resource \
is set to value %2",
- "Create new relation: Set '%1' to %2", \
m_propertyLabel, value.toString() ) );
- anno->setValue( Nepomuk::Variant( value.toString() ) );
- }
- // FIXME: if we have no thing to relate to, propose to create a new thing \
instead of a string value
- addNewAnnotation( anno );
-
- emitFinished();
- }
-
- else { // !m_matchedProperties.isEmpty()
- Q_FOREACH(const Query::Result& result, entries) {
- Resource res = result.resource();
- for ( int i = 0; i < m_matchedProperties.count(); ++i ) {
- Types::Property& property = m_matchedProperties[i];
- if ( res.hasType( property.range().uri() ) ) {
- SimplePimoAnnotation* anno = new SimplePimoAnnotation( this );
- anno->setLabel( "FIXME" );
- anno->setComment( i18nc( "@action A property (%1) of a selected \
resource is set to value %2",
- "Set '%1' to %2", property.label(), \
res.genericLabel() ) );
- if ( m_propertyLabel.isEmpty() )
- anno->setRelevance( 1.0 - calculateDistance( \
res.genericLabel(), m_valueLabel ) );
- else
- anno->setRelevance( ( 1.0 - calculateDistance( \
property.label(), m_propertyLabel ) )
- * ( 1.0 - calculateDistance( \
res.genericLabel(), m_valueLabel ) ) );
- anno->setProperty( property );
- anno->setValue( Nepomuk::Variant( res ) );
- addNewAnnotation( anno );
- }
- else {
- kDebug() << "resource" << res.genericLabel() << "does not have \
type" << property.range().uri() << "for property" << property.uri();
- }
- }
- }
-
- m_matchedProperties.takeFirst();
- if ( !m_matchedProperties.isEmpty() )
- QMetaObject::invokeMethod( this, "handleNextProperty", \
Qt::QueuedConnection );
- else
- emitFinished();
- }
-}
-
void Nepomuk::PimoAnnotationPlugin::slotThingSearchFinished()
{
m_queryClient->close();
+ emitFinished();
}
#include "pimoannotationplugin.moc"
--- trunk/playground/base/nepomuk-kde/annotation/plugins/pimo/pimoannotationplugin.h \
#1222510:1222511 @@ -1,5 +1,5 @@
/*
- Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
+ Copyright (c) 2008-2011 Sebastian Trueg <trueg@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
@@ -50,21 +50,15 @@
void doGetPossibleAnnotations( const AnnotationRequest& request );
private Q_SLOTS:
- void matchProperty( const QString& label );
- void createRelationAnnotations( const QList<Nepomuk::Types::Property>& \
properties );
void slotNewEntries( const QList<Nepomuk::Query::Result>& entries );
void slotThingSearchFinished();
- void handleNextProperty();
private:
bool useType( Types::Class );
Resource m_resource;
- QString m_propertyLabel;
- QString m_valueLabel;
+ QString m_filter;
- QList<Nepomuk::Types::Property> m_matchedProperties;
-
Query::QueryServiceClient* m_queryClient;
};
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic