changeset 493:3931711b5671

* RDF importer: add model titles where possible * RDF transform factory: report whether something appears to be RDF or not (so we can avoid trying to load it as something else if the RDF query fails)
author Chris Cannam
date Tue, 25 Nov 2008 13:43:56 +0000
parents 23945cdd7161
children 81963c51b488
files rdf/RDFImporter.cpp rdf/RDFTransformFactory.cpp rdf/RDFTransformFactory.h rdf/SimpleSPARQLQuery.cpp
diffstat 4 files changed, 95 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/rdf/RDFImporter.cpp	Mon Nov 24 16:26:11 2008 +0000
+++ b/rdf/RDFImporter.cpp	Tue Nov 25 13:43:56 2008 +0000
@@ -59,6 +59,8 @@
     void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
     void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
 
+    void getDenseModelTitle(Model *, QString, QString);
+
     void getDenseFeatureProperties(QString featureUri,
                                    int &sampleRate, int &windowLength,
                                    int &hopSize, int &width, int &height);
@@ -184,14 +186,12 @@
              " PREFIX mo: <http://purl.org/ontology/mo/>"
              " PREFIX af: <http://purl.org/ontology/af/>"
              
-             " SELECT ?feature ?signal_source ?feature_signal_type ?value "
+             " SELECT ?feature ?feature_signal_type ?value "
              " FROM <%1> "
              
              " WHERE { "
              
-             "   ?signal a mo:Signal ; "
-             "           mo:available_as ?signal_source ; "
-             "           af:signal_feature ?feature . "
+             "   ?signal af:signal_feature ?feature . "
              
              "   ?feature a ?feature_signal_type ; "
              "            af:value ?value . "
@@ -215,7 +215,6 @@
     for (int i = 0; i < results.size(); ++i) {
 
         QString feature = results[i]["feature"].value;
-        QString source = results[i]["signal_source"].value;
         QString type = results[i]["feature_signal_type"].value;
         QString value = results[i]["value"].value;
 
@@ -259,6 +258,8 @@
                 SparseTimeValueModel::Point point(j * hopSize, f, "");
                 m->addPoint(point);
             }
+
+            getDenseModelTitle(m, feature, type);
         
             models.push_back(m);
 
@@ -284,12 +285,56 @@
                 m->setColumn(x++, column);
             }
 
+            getDenseModelTitle(m, feature, type);
+        
             models.push_back(m);
         }
     }
 }
 
 void
+RDFImporterImpl::getDenseModelTitle(Model *m,
+                                    QString featureUri,
+                                    QString featureTypeUri)
+{
+    QString titleQuery = QString
+        (
+            " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
+            " SELECT ?title "
+            " FROM <%1> " 
+            " WHERE { "
+            "   <%2> dc:title ?title . "
+            " } "
+            ).arg(m_uristring);
+    
+    SimpleSPARQLQuery::Value v;
+
+    v = SimpleSPARQLQuery::singleResultQuery
+        (SimpleSPARQLQuery::QueryFromSingleSource,
+         titleQuery.arg(featureUri),
+         "title");
+
+    if (v.value != "") {
+        std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal) \"" << v.value.toStdString() << "\"" << std::endl;
+        m->setObjectName(v.value);
+        return;
+    }
+
+    v = SimpleSPARQLQuery::singleResultQuery
+        (SimpleSPARQLQuery::QueryFromSingleSource,
+         titleQuery.arg(featureTypeUri),
+         "title");
+    
+    if (v.value != "") {
+        std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal type) \"" << v.value.toStdString() << "\"" << std::endl;
+        m->setObjectName(v.value);
+        return;
+    }
+
+    std::cerr << "RDFImporterImpl::getDenseModelTitle: No title available for feature <" << featureUri.toStdString() << ">" << std::endl;
+}
+
+void
 RDFImporterImpl::getDenseFeatureProperties(QString featureUri,
                                            int &sampleRate, int &windowLength,
                                            int &hopSize, int &width, int &height)
@@ -643,6 +688,19 @@
                 }
             }
 
+            QString titleQuery = QString
+                (
+                    " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
+                    " SELECT ?title "
+                    " FROM <%1> " 
+                    " WHERE { "
+                    "   <%2> dc:title ?title . "
+                    " } "
+                    ).arg(m_uristring).arg(type);
+            QString title = SimpleSPARQLQuery::singleResultQuery
+                (s, titleQuery, "title").value;
+            if (title != "") model->setObjectName(title);
+
             modelMap[source][type][dimensions][haveDuration] = model;
             models.push_back(model);
         }
@@ -665,7 +723,7 @@
                            std::vector<float> &values,
                            QString label)
 {
-    std::cerr << "RDFImporterImpl::fillModel: adding point at frame " << ftime << std::endl;
+//    std::cerr << "RDFImporterImpl::fillModel: adding point at frame " << ftime << std::endl;
 
     SparseOneDimensionalModel *sodm =
         dynamic_cast<SparseOneDimensionalModel *>(model);
--- a/rdf/RDFTransformFactory.cpp	Mon Nov 24 16:26:11 2008 +0000
+++ b/rdf/RDFTransformFactory.cpp	Tue Nov 25 13:43:56 2008 +0000
@@ -39,6 +39,7 @@
     RDFTransformFactoryImpl(QString url);
     virtual ~RDFTransformFactoryImpl();
     
+    bool isRDF();
     bool isOK();
     QString getErrorString() const;
 
@@ -47,6 +48,7 @@
 protected:
     QString m_urlString;
     QString m_errorString;
+    bool m_isRDF;
     bool setOutput(Transform &, QString);
     bool setParameters(Transform &, QString);
 };
@@ -69,6 +71,12 @@
 }
 
 bool
+RDFTransformFactory::isRDF()
+{
+    return m_d->isRDF();
+}
+
+bool
 RDFTransformFactory::isOK()
 {
     return m_d->isOK();
@@ -87,7 +95,8 @@
 }
 
 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) :
-    m_urlString(url)
+    m_urlString(url),
+    m_isRDF(false)
 {
 }
 
@@ -97,6 +106,12 @@
 }
 
 bool
+RDFTransformFactoryImpl::isRDF()
+{
+    return m_isRDF;
+}
+
+bool
 RDFTransformFactoryImpl::isOK()
 {
     return (m_errorString == "");
@@ -137,6 +152,8 @@
         return transforms;
     }
 
+    m_isRDF = true;
+
     if (transformResults.empty()) {
         cerr << "RDFTransformFactory: NOTE: No RDF/TTL transform descriptions found in document at <" << m_urlString.toStdString() << ">" << endl;
         return transforms;
@@ -289,7 +306,7 @@
     }
 
     if (outputValue.type != SimpleSPARQLQuery::URIValue) {
-        m_errorString = "No vamp:output given, or not a URI";
+        m_errorString = QString("vamp:output given for transform <%1> is not a URI").arg(transformUri);
         return false;
     }
 
@@ -310,7 +327,7 @@
          "output_id");
     
     if (outputIdValue.type != SimpleSPARQLQuery::LiteralValue) {
-        m_errorString = "No output vamp:identifier available, or not a literal";
+        m_errorString = QString("No vamp:identifier found for output <%1>, or vamp:identifier is not a literal").arg(outputValue.value);
         return false;
     }
 
--- a/rdf/RDFTransformFactory.h	Mon Nov 24 16:26:11 2008 +0000
+++ b/rdf/RDFTransformFactory.h	Tue Nov 25 13:43:56 2008 +0000
@@ -36,7 +36,8 @@
     RDFTransformFactory(QString url);
     virtual ~RDFTransformFactory();
 
-    bool isOK();
+    bool isRDF(); // true if the file was parseable and had transforms in it
+    bool isOK();  // true if the transforms could be completely constructed
     QString getErrorString() const;
 
     std::vector<Transform> getTransforms(ProgressReporter *reporter);
--- a/rdf/SimpleSPARQLQuery.cpp	Mon Nov 24 16:26:11 2008 +0000
+++ b/rdf/SimpleSPARQLQuery.cpp	Tue Nov 25 13:43:56 2008 +0000
@@ -165,13 +165,19 @@
 void
 WredlandWorldWrapper::freeModel(QString forUri)
 {
+#ifdef DEBUG_SIMPLE_SPARQL_QUERY
+    std::cerr << "SimpleSPARQLQuery::freeModel: Model uri = \"" << forUri.toStdString() << "\"" << std::endl;
+#endif
+
     QMutexLocker locker(&m_mutex);
     if (forUri == "") {
         std::cerr << "SimpleSPARQLQuery::freeModel: ERROR: Can't free default model" << std::endl;
         return;
     }
     if (m_ownModelUris.find(forUri) == m_ownModelUris.end()) {
-        std::cerr << "SimpleSPARQLQuery::freeModel: ERROR: Never heard of this model (uri = \"" << forUri.toStdString() << "\")" << std::endl;
+#ifdef DEBUG_SIMPLE_SPARQL_QUERY
+        std::cerr << "SimpleSPARQLQuery::freeModel: NOTE: Unknown or already-freed model (uri = \"" << forUri.toStdString() << "\")" << std::endl;
+#endif
         return;
     }
 
@@ -408,11 +414,13 @@
     ResultList list;
     librdf_query *query;
 
+#ifdef DEBUG_SIMPLE_SPARQL_QUERY
     static std::map<QString, int> counter;
     if (counter.find(m_query) == counter.end()) counter[m_query] = 1;
     else ++counter[m_query];
     std::cerr << "Counter for this query: " << counter[m_query] << std::endl;
     std::cerr << "Base URI is: \"" << modelUri.toStdString() << "\"" << std::endl;
+#endif
 
     {
         Profiler p("SimpleSPARQLQuery: Prepare LIBRDF query");