changeset 30:8bed05455706

* Make classical code build
author Chris Cannam
date Tue, 16 Mar 2010 17:36:28 +0000
parents 9729919e589c
children 07efb25d24d6
files common/Matcher.cpp common/Matcher.h common/Objects.h common/common.pro testapp/testapp.pro utilities/composer/composer.cpp utilities/composer/composer.pro utilities/widgettest/widgettest.cpp utilities/widgettest/widgettest.pro widgets/TypingSelectWidget.cpp widgets/TypingSelectWidget.h widgets/widgets.pro
diffstat 12 files changed, 245 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/common/Matcher.cpp	Mon Mar 01 16:55:27 2010 +0000
+++ b/common/Matcher.cpp	Tue Mar 16 17:36:28 2010 +0000
@@ -9,46 +9,58 @@
 
 namespace ClassicalData {
 
-UriList
+ComposerTypingQuickMatcher::ComposerTypingQuickMatcher(QList<Composer *> cl)
+{
+    foreach (Composer *c, cl) {
+	m_composers[c->property("uri").value<Uri>()] = c;
+    }
+}
+
+GuessList
 ComposerTypingQuickMatcher::match(QString text, int maxResults) const
 {
-    UriList results;
+    GuessList results;
 
-    QMultiMap<float, Composer *> matches;
+    QMap<Guess, int> matches;
     foreach (Composer *c, m_composers) {
         float value = c->matchTypingQuick(text);
-        matches.insert(value, c);
+        if (value <= 0) continue;
+        matches.insert(Guess(value, c), 1);
     }
     
     int n = 0;
-    for (QMultiMap<float, Composer *>::const_iterator i = matches.end();
-         i != matches.begin(); ) {
-        --i;
-        if (i.key() <= 0) continue;
-	results.push_back(i.value()->property("uri").value<Uri>());
+    for (QMap<Guess, int>::const_iterator i = matches.begin();
+         i != matches.end(); ++i) {
+        results.push_back(i.key());
         if (++n > maxResults) break;
     }
 
     return results;
 }
 
-UriList
+ComposerTypingThoroughMatcher::ComposerTypingThoroughMatcher(QList<Composer *> cl)
+{
+    foreach (Composer *c, cl) {
+	m_composers[c->property("uri").value<Uri>()] = c;
+    }
+}
+
+GuessList
 ComposerTypingThoroughMatcher::match(QString text, int maxResults) const
 {
-    UriList results;
+    GuessList results;
 
-    QMultiMap<float, Composer *> matches;
+    QMap<Guess, int> matches;
     foreach (Composer *c, m_composers) {
         float value = c->matchTyping(text);
-        matches.insert(value, c);
+        if (value <= 0) continue;
+        matches.insert(Guess(value, c), 1);
     }
     
     int n = 0;
-    for (QMultiMap<float, Composer *>::const_iterator i = matches.end();
-         i != matches.begin(); ) {
-        --i;
-        if (i.key() <= 0) continue;
-	results.push_back(i.value()->property("uri").value<Uri>());
+    for (QMap<Guess, int>::const_iterator i = matches.begin();
+         i != matches.end(); ++i) {
+        results.push_back(i.key());
         if (++n > maxResults) break;
     }
 
--- a/common/Matcher.h	Mon Mar 01 16:55:27 2010 +0000
+++ b/common/Matcher.h	Tue Mar 16 17:36:28 2010 +0000
@@ -5,43 +5,66 @@
 
 #include <dataquay/Uri.h>
 
+#include <QHash>
+
 namespace ClassicalData {
 
 class Composer;
+class NamedEntity;
+
+class Guess
+{
+public:
+    Guess(float c, NamedEntity *e) : m_confidence(c), m_entity(e) { }
+
+    float confidence() const { return m_confidence; }
+    void setConfidence(float c) { m_confidence = c; }
+    
+    NamedEntity *entity() { return m_entity; }
+    void setEntity(NamedEntity *e) { m_entity = e; }
+
+    bool operator<(const Guess &g) const {
+        return (confidence() > g.confidence());
+    }
+    
+private:
+    float m_confidence;
+    NamedEntity *m_entity;
+};
+
+typedef QList<Guess> GuessList;
 
 class Matcher
 {
 public:
-    virtual Dataquay::UriList match(QString text, int maxResults) const = 0;
+    // Results are guaranteed to be returned in order from most to
+    // least confident
+    virtual GuessList match(QString text, int maxResults) const = 0;
 };
 
 class ComposerTypingQuickMatcher : public Matcher
 {
 public:
-    ComposerTypingQuickMatcher(QList<Composer *> cl) :
-	m_composers(cl) { }
-    
-    virtual Dataquay::UriList match(QString text, int maxResults) const;
+    ComposerTypingQuickMatcher(QList<Composer *> cl);
+    virtual GuessList match(QString text, int maxResults) const;
 
 private:
-    QList<Composer *> m_composers;
+    QHash<Dataquay::Uri, Composer *> m_composers;
 };
 
 class ComposerTypingThoroughMatcher : public Matcher
 {
 public:
-    ComposerTypingThoroughMatcher(QList<Composer *> cl) :
-	m_composers(cl) { }
-    
-    virtual Dataquay::UriList match(QString text, int maxResults) const;
+    ComposerTypingThoroughMatcher(QList<Composer *> cl);
+    virtual GuessList match(QString text, int maxResults) const;
 
 private:
-    QList<Composer *> m_composers;
+    QHash<Dataquay::Uri, Composer *> m_composers;
 };
 
-
-
 }
 
+Q_DECLARE_METATYPE(ClassicalData::Guess*);
+
 #endif
 
--- a/common/Objects.h	Mon Mar 01 16:55:27 2010 +0000
+++ b/common/Objects.h	Tue Mar 16 17:36:28 2010 +0000
@@ -520,5 +520,6 @@
 Q_DECLARE_METATYPE(ClassicalData::Composer*);
 Q_DECLARE_METATYPE(ClassicalData::Form*);
 Q_DECLARE_METATYPE(QSet<ClassicalData::Form*>);
+Q_DECLARE_METATYPE(ClassicalData::NamedEntity*);
 	
 #endif
--- a/common/common.pro	Mon Mar 01 16:55:27 2010 +0000
+++ b/common/common.pro	Tue Mar 16 17:36:28 2010 +0000
@@ -1,7 +1,7 @@
 TEMPLATE = lib
 TARGET = 
 
-load(../platform.prf)
+load(../../all.prf)
 
 CONFIG += staticlib
 
--- a/testapp/testapp.pro	Mon Mar 01 16:55:27 2010 +0000
+++ b/testapp/testapp.pro	Tue Mar 16 17:36:28 2010 +0000
@@ -1,13 +1,15 @@
 TEMPLATE = app
 TARGET = testapp
 
-load(../platform.prf)
+load(../../all.prf)
 
 SOURCES += Loader.cpp
 
 RESOURCES += testapp.qrc
 
+INCLUDEPATH += ../common
+
 PRE_TARGETDEPS += ../common/libcommon.a
 
-LIBS += ../common/libcommon.a ../../turbot/dataquay/libdataquay.a ../../turbot/ext/libext.a
+LIBS += ../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a
 
--- a/utilities/composer/composer.cpp	Mon Mar 01 16:55:27 2010 +0000
+++ b/utilities/composer/composer.cpp	Tue Mar 16 17:36:28 2010 +0000
@@ -404,11 +404,14 @@
         storer->setTypeMapping(tm);
 
         storer->setPropertyStorePolicy(ObjectStorer::StoreIfChanged);
-        storer->setObjectStorePolicy(ObjectStorer::StoreAllObjects);
+//        storer->setObjectStorePolicy(ObjectStorer::StoreAllObjects);
         storer->setBlankNodePolicy(ObjectStorer::NoBlankNodes);
 
         cerr << "Mapping results back to store...";
-        storer->storeAllObjects(root->children());
+//        storer->storeAllObjects(root->children());
+//        storer->setFollowPolicy(ObjectStorer::FollowAll);
+        storer->setFollowPolicy(ObjectStorer::FollowObjectProperties);
+        storer->store(root->children());
         cerr << " done" << endl;
 
         cerr << "Saving to file out.ttl...";
--- a/utilities/composer/composer.pro	Mon Mar 01 16:55:27 2010 +0000
+++ b/utilities/composer/composer.pro	Tue Mar 16 17:36:28 2010 +0000
@@ -2,11 +2,13 @@
 TARGET = composer
 QT -= gui network xml
 
-load(../../platform.prf)
+load(../../../all.prf)
 
 SOURCES += composer.cpp
 
 PRE_TARGETDEPS += ../../common/libcommon.a
 
-LIBS += ../../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a
+INCLUDEPATH += ../../common
 
+LIBS += ../../common/libcommon.a ../../../../turbot/dataquay/libdataquay.a ../../../../turbot/ext/libext.a
+
--- a/utilities/widgettest/widgettest.cpp	Mon Mar 01 16:55:27 2010 +0000
+++ b/utilities/widgettest/widgettest.cpp	Tue Mar 16 17:36:28 2010 +0000
@@ -1,17 +1,104 @@
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 
 #include "TypingSelectWidget.h"
+#include "TypeRegistrar.h"
+#include "Objects.h"
+#include "Matcher.h"
+
+#include <dataquay/BasicStore.h>
+#include <dataquay/Debug.h>
+#include <dataquay/Uri.h>
+#include <dataquay/RDFException.h>
+#include <dataquay/objectmapper/ObjectLoader.h>
+#include <dataquay/objectmapper/TypeMapping.h>
 
 #include <QApplication>
 
+#include <iostream>
+
 using namespace ClassicalData;
+using namespace Dataquay;
+using namespace std;
+
+ostream &operator<<(ostream &target, const QString &str)
+{
+    return target << str.toLocal8Bit().data();
+}
+
+ostream &operator<<(ostream &target, const QUrl &u)
+{
+    return target << "<" << u.toString() << ">";
+}
+
+void
+usage(const char *name)
+{
+    int s = 0;
+    for (int i = 0; name[i]; ++i) if (name[i] == '/') s = i + 1;
+    name = name + s;
+    cerr << "Usage:" << endl;
+    cerr << "  " << name << " <input-rdf-file>" << endl;
+    exit(2);
+}
+
+bool
+load(BasicStore *store, QString fileName)
+{
+    QUrl url = QUrl::fromLocalFile(fileName);
+
+    cerr << "Importing from URL " << url << " ...";
+    try {
+	store->import(url, BasicStore::ImportPermitDuplicates);
+    } catch (RDFException e) {
+        cerr << " retrying with explicit ntriples type...";
+        try {
+            store->import(url, BasicStore::ImportPermitDuplicates, "ntriples");
+        } catch (RDFException e) {
+            cerr << "failed" << endl;
+            cerr << "Import failed: " << e.what() << endl;
+            return false;
+        }
+    }
+
+    cerr << " done" << endl;
+    return true;
+}
 
 int
 main(int argc, char **argv)
 {
     QApplication app(argc, argv);
 
+    if (argc != 2) usage(argv[0]);
+    QString inFileName = argv[1];
+
+    BasicStore *store = new BasicStore();
+    store->setBaseUri(Uri("http://dbtune.org/classical/resource/"));
+    ObjectLoader *loader = new ObjectLoader(store);
+
+    TypeMapping tm;
+
+    TypeRegistrar::registerTypes();
+    TypeRegistrar::addMappings(store, &tm);
+
+    loader->setTypeMapping(tm);
+
+    if (!load(store, inFileName)) {
+	cerr << "Failed to load data source" << endl;
+	return 1;
+    }
+
+    cerr << "Imported RDF data, mapping to objects...";
+    QObject *root = loader->loadAllObjects(0);
+    cerr << " done" << endl;
+
+    delete loader;
+
+    QList<Composer *> composers = root->findChildren<Composer *>();
+    ComposerTypingThoroughMatcher matcher(composers);
+
     TypingSelectWidget *w = new TypingSelectWidget();
+    w->addMatcher(&matcher);
     
     w->show();
     
--- a/utilities/widgettest/widgettest.pro	Mon Mar 01 16:55:27 2010 +0000
+++ b/utilities/widgettest/widgettest.pro	Tue Mar 16 17:36:28 2010 +0000
@@ -1,11 +1,13 @@
 TEMPLATE = app
 TARGET = widgettest
 
-load(../../platform.prf)
+load(../../../all.prf)
 
 PRE_TARGETDEPS += ../../common/libcommon.a ../../widgets/libwidgets.a
 
-LIBS += ../../widgets/libwidgets.a ../../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a
+INCLUDEPATH += ../../widgets ../../common
+
+LIBS += ../../widgets/libwidgets.a ../../common/libcommon.a ../../../../turbot/dataquay/libdataquay.a ../../../../turbot/ext/libext.a
 
 HEADERS += widgettest.h
 SOURCES += widgettest.cpp
--- a/widgets/TypingSelectWidget.cpp	Mon Mar 01 16:55:27 2010 +0000
+++ b/widgets/TypingSelectWidget.cpp	Tue Mar 16 17:36:28 2010 +0000
@@ -2,8 +2,17 @@
 
 #include "TypingSelectWidget.h"
 
+#include <dataquay/Uri.h>
+
 #include <QGridLayout>
 #include <QLineEdit>
+#include <QListWidget>
+
+#include "Matcher.h"
+
+#include "Objects.h"
+
+using namespace Dataquay;
 
 namespace ClassicalData {
 
@@ -13,11 +22,47 @@
     QGridLayout *layout = new QGridLayout;
     setLayout(layout);
 
-    QLineEdit *editor = new QLineEdit;
-    layout->addWidget(editor);
+    m_editor = new QLineEdit;
+    layout->addWidget(m_editor);
+
+    connect(m_editor, SIGNAL(textEdited(const QString &)),
+	    this, SLOT(textEdited(const QString &)));
+
+    m_list = new QListWidget();
+    m_list->setWindowFlags(Qt::Window | Qt::Tool | Qt::FramelessWindowHint);
+    m_list->hide();
+}
+
+void
+TypingSelectWidget::textEdited(const QString &s)
+{
+    if (m_matchers.empty()) return;
+    
+    GuessList results = m_matchers[0]->match(s, 10);
+    m_list->clear();
+
+    if (results.empty()) {
+	m_list->hide();
+	return;
+    }
+
+    foreach (Guess g, results) {
+	Composer *c = qobject_cast<Composer *>(g.entity());
+	if (!c) continue;
+	m_list->addItem(c->getSortName(true));
+    }
+
+    m_list->show();
+    m_list->move(m_editor->mapToGlobal(QPoint(0, m_editor->height())));
+    m_list->resize(m_list->sizeHint());
+//    m_list->setMinimumWidth((width()*3)/4);
+//    m_list->setMinimumHeight(height() * 5);
+
+
+    std::cerr << std::endl;
+}
+   
+
 
 }
 
-
-}
-
--- a/widgets/TypingSelectWidget.h	Mon Mar 01 16:55:27 2010 +0000
+++ b/widgets/TypingSelectWidget.h	Tue Mar 16 17:36:28 2010 +0000
@@ -6,6 +6,9 @@
 #include <QWidget>
 #include <QList>
 
+class QLineEdit;
+class QListWidget;
+
 namespace ClassicalData
 {
 
@@ -13,13 +16,20 @@
 
 class TypingSelectWidget : public QWidget
 {
+    Q_OBJECT
+
 public:
     TypingSelectWidget(QWidget *parent = 0);
     
-    void setMatchers(QList<Matcher *> m) { m_matchers = m; }
+    void addMatcher(Matcher *m) { m_matchers.push_back(m); }
+
+private slots:
+    void textEdited(const QString &);
 
 private:
     QList<Matcher *> m_matchers;
+    QLineEdit *m_editor;
+    QListWidget *m_list;
 };
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/widgets.pro	Tue Mar 16 17:36:28 2010 +0000
@@ -0,0 +1,12 @@
+TEMPLATE = lib
+TARGET = 
+
+load(../../all.prf)
+
+CONFIG += staticlib
+
+INCLUDEPATH += ../common
+
+HEADERS += TypingSelectWidget.h
+SOURCES += TypingSelectWidget.cpp
+