Clustering support added 10/910/2
authorTasuku Suzuki <stasuku@gmail.com>
Wed, 28 Jan 2015 03:51:48 +0000 (12:51 +0900)
committerTasuku Suzuki <stasuku@gmail.com>
Tue, 10 Feb 2015 16:01:44 +0000 (01:01 +0900)
Change-Id: I2dbad81091d9b2438315648cfaafd1501463dc03
Reviewed-on: http://cr.qtquick.me/910
Tested-by: Tasuku Suzuki <stasuku@gmail.com>
Reviewed-by: Tasuku Suzuki <stasuku@gmail.com>
src/jubatus/client/qjubatusclient.cpp
src/jubatus/clustering/clustering.pri [new file with mode: 0644]
src/jubatus/clustering/qjubatusclustering.cpp [new file with mode: 0644]
src/jubatus/clustering/qjubatusclustering.h [new file with mode: 0644]
src/jubatus/jubatus.pro

index 86e8a72..aa47e02 100644 (file)
@@ -152,6 +152,9 @@ jubatus::client::common::datum QJubatusClient::convert(const QVariantMap &data)
         case QVariant::Int:
             ret.add_number(key.toStdString(), value.toInt());
             break;
+        case QVariant::UInt:
+            ret.add_number(key.toStdString(), value.toUInt());
+            break;
         case QMetaType::Float:
             ret.add_number(key.toStdString(), value.toFloat());
             break;
diff --git a/src/jubatus/clustering/clustering.pri b/src/jubatus/clustering/clustering.pri
new file mode 100644 (file)
index 0000000..2b54716
--- /dev/null
@@ -0,0 +1,6 @@
+INCLUDEPATH += $$PWD
+DEPENDPATH += $$PWD
+
+PUBLIC_HEADERS += $$PWD/qjubatusclustering.h
+HEADERS += $$PUBLIC_HEADERS
+SOURCES += $$PWD/qjubatusclustering.cpp
diff --git a/src/jubatus/clustering/qjubatusclustering.cpp b/src/jubatus/clustering/qjubatusclustering.cpp
new file mode 100644 (file)
index 0000000..25b7ebb
--- /dev/null
@@ -0,0 +1,115 @@
+/* Copyright (c) 2012 Silk Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the Silk nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL SILK BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "qjubatusclustering.h"
+
+#include <QtCore/QDebug>
+
+#include <jubatus/client/clustering_client.hpp>
+
+QJubatusClustering::QJubatusClustering(QObject *parent)
+    : QJubatusClient(parent)
+{
+}
+
+bool QJubatusClustering::push(const QList<QVariantMap> &data)
+{
+    bool ret = false;
+    EXEC_JUBATUS_COMMAND( ret = client()->push(convert(data)); )
+    return ret;
+}
+
+uint QJubatusClustering::getRevision()
+{
+    uint ret = 0;
+    EXEC_JUBATUS_COMMAND( ret = client()->get_revision(); )
+    return ret;
+}
+
+QList<QList<QJubatusClustering::WeightedDatum> > QJubatusClustering::getCoreMembers()
+{
+    QList<QList<QJubatusClustering::WeightedDatum> > ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_core_members()); )
+    return ret;
+}
+
+QList<QVariantMap> QJubatusClustering::getKCenter()
+{
+    QList<QVariantMap> ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_k_center()); )
+    return ret;
+}
+
+QVariantMap QJubatusClustering::getNearestCenter(const QVariantMap &point)
+{
+    QVariantMap ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_nearest_center(convert(point))); )
+    return ret;
+}
+
+QList<QJubatusClustering::WeightedDatum> QJubatusClustering::getNearestMembers(const QVariantMap &point)
+{
+    QList<QJubatusClustering::WeightedDatum> ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_nearest_members(convert(point))); )
+    return ret;
+}
+
+QJubatusClustering::WeightedDatum QJubatusClustering::convert(const jubatus::clustering::weighted_datum &data) const
+{
+    QJubatusClustering::WeightedDatum ret;
+    ret.weight = data.weight;
+    ret.point = convert(data.point);
+    return ret;
+}
+
+QList<QJubatusClustering::WeightedDatum> QJubatusClustering::convert(const std::vector<jubatus::clustering::weighted_datum> &data) const
+{
+    QList<QJubatusClustering::WeightedDatum> ret;
+    for (auto it = data.begin(); it != data.end(); ++it) {
+        ret.append(convert(*it));
+    }
+    return ret;
+}
+QList<QList<QJubatusClustering::WeightedDatum> > QJubatusClustering::convert(const std::vector<std::vector<jubatus::clustering::weighted_datum> > &data) const
+{
+    QList<QList<QJubatusClustering::WeightedDatum> > ret;
+    for (auto it = data.begin(); it != data.end(); ++it) {
+        ret.append(convert(*it));
+    }
+    return ret;
+}
+
+jubatus::clustering::client::clustering *QJubatusClustering::client() {
+    return QJubatusClient::client<jubatus::clustering::client::clustering>();
+}
+
+#ifndef QT_NO_DEBUG_STREAM
+QDebug &operator<<(QDebug &dbg, const QJubatusClustering::WeightedDatum &weightedDatum)
+{
+    dbg.nospace() << "QJubatusClustering::WeightedDatum(weight:" << weightedDatum.weight << ", point: " << weightedDatum.point << ')';
+    return dbg.space();
+}
+#endif
diff --git a/src/jubatus/clustering/qjubatusclustering.h b/src/jubatus/clustering/qjubatusclustering.h
new file mode 100644 (file)
index 0000000..a382077
--- /dev/null
@@ -0,0 +1,76 @@
+/* Copyright (c) 2012 Silk Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the Silk nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL SILK BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef QJUBATUSCLUSTERING_H
+#define QJUBATUSCLUSTERING_H
+
+#include "jubatus_global.h"
+#include "qjubatusclient.h"
+
+#include <QtCore/QVariant>
+
+namespace jubatus {
+    namespace clustering {
+        struct weighted_datum;
+        namespace client {
+            class clustering;
+        }
+    }
+}
+
+class JUBATUS_EXPORT QJubatusClustering : public QJubatusClient
+{
+    Q_OBJECT
+public:
+    explicit QJubatusClustering(QObject *parent = 0);
+
+    struct WeightedDatum {
+        double weight;
+        QVariantMap point;
+    };
+
+    Q_INVOKABLE bool push(const QList<QVariantMap> &data);
+    Q_INVOKABLE uint getRevision();
+    Q_INVOKABLE QList<QList<QJubatusClustering::WeightedDatum> > getCoreMembers();
+    Q_INVOKABLE QList<QVariantMap> getKCenter();
+    Q_INVOKABLE QVariantMap getNearestCenter(const QVariantMap &point);
+    Q_INVOKABLE QList<QJubatusClustering::WeightedDatum> getNearestMembers(const QVariantMap &point);
+
+protected:
+    using QJubatusClient::convert;
+    QJubatusClustering::WeightedDatum convert(const jubatus::clustering::weighted_datum &data) const;
+    QList<QJubatusClustering::WeightedDatum> convert(const std::vector<jubatus::clustering::weighted_datum> &data) const;
+    QList<QList<QJubatusClustering::WeightedDatum> > convert(const std::vector<std::vector<jubatus::clustering::weighted_datum> > &data) const;
+
+private:
+    jubatus::clustering::client::clustering *client();
+};
+
+#ifndef QT_NO_DEBUG_STREAM
+JUBATUS_EXPORT QDebug &operator<<(QDebug &, const QJubatusClustering::WeightedDatum &);
+#endif
+
+#endif // QJUBATUSCLUSTERING_H
index 188ae60..bc2ab2b 100644 (file)
@@ -8,6 +8,7 @@ CONFIG += link_pkgconfig
 PKGCONFIG += jubatus-client
 
 CONFIG += exception
+CONFIG -= create_cmake
 
 DEFINES += JUBATUS_LIBRARY
 HEADERS += \
@@ -18,3 +19,4 @@ include(./anomaly/anomaly.pri)
 include(./classifier/classifier.pri)
 include(./recommender/recommender.pri)
 include(./regression/regression.pri)
+include(./clustering/clustering.pri)