C++: define Duration as a metatype in Qt to be able to parse it in
authorjani <jani@asema.com>
Thu, 17 Oct 2019 21:58:01 +0000 (00:58 +0300)
committerjani <jani@asema.com>
Thu, 17 Oct 2019 21:58:01 +0000 (00:58 +0300)
Variant

Common/C++/SmartAPI/smartapi/common/Duration.cpp
Common/C++/SmartAPI/smartapi/common/Duration.h
Common/C++/SmartAPI/smartapi/common/Tools.cpp
Common/C++/SmartAPI/smartapi/model/Route.h
Common/C++/SmartAPI/smartapi/model/TemporalContext.h
Common/C++/SmartAPI/smartapi/rdf/Variant.cpp
Common/C++/SmartAPI/tests/TestSequences.cpp

index 532d7bf728858afa6a593bb093cb6de4780f9f1d..2611e73ebed8a63ba0b9266dcaa25230425c094a 100644 (file)
@@ -55,8 +55,8 @@ QDateTime Duration::add(QDateTime date)
        return date.addYears(mIsPositive ? mYears : -mYears)
                                .addMonths(mIsPositive ? mMonths : -mMonths)
                                .addDays(mIsPositive ? mDays : -mDays)
-                               .addSecs(mIsPositive ? (((mHours * 24) + mMinutes) * 60 + mSeconds)
-                                                               : -(((mHours * 24) + mMinutes) * 60 + mSeconds));
+                               .addSecs(mIsPositive ? (((mHours * 60) + mMinutes) * 60 + mSeconds)
+                                                               : -(((mHours * 60) + mMinutes) * 60 + mSeconds));
 }
 
 QString Duration::toString()
@@ -64,6 +64,13 @@ QString Duration::toString()
        return constructLexicalRepresentation();
 }
 
+int Duration::toSeconds()
+{
+       // Note: gives only an approximation as months have differing number of days
+       int secs = ((((((365 * mYears) + (30 * mMonths) + mDays) * 24 + mHours) * 60) + mMinutes) * 60 + mSeconds);
+       return secs;
+}
+
 bool Duration::parseLexicalRepresentation(QString str)
 {
        if (str.startsWith("-")) {
index 207595c207073baadd226fa2f0a4a46c111a06db..a596f4411851e2ec55bd7c4e9a4f92e8bad8eb37 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __DURATION_H__
 #define __DURATION_H__
 
+#include <QVariant>
 #include <QDateTime>
 #include "smartapi/rdf/GraphItem.h"
 
@@ -12,9 +13,11 @@ public:
        Duration(bool isPositive, int years, int months, int days, int hours, int minutes, int seconds);
        Duration(int years, int months, int days, int hours, int minutes, int seconds);
        Duration(Duration* d);
+       ~Duration() {}
 
        QDateTime add(QDateTime date);
        QString toString();
+       int toSeconds();
 
        bool isPositive()               { return mIsPositive; }
        int getYears()                  { return mYears; }
@@ -48,4 +51,6 @@ private:
        int mSeconds;
 };
 
+Q_DECLARE_METATYPE(Duration*)
+
 #endif // __DURATION_H__
index eba40b1128d70ad7236c89cea574c9a7f9150051..c6e8be6c18f24702d3ceefd32a2fa31c6fd949bb 100644 (file)
@@ -885,6 +885,11 @@ QVariant Tools::raptorLiteralToQVariant(raptor_term_literal_value value)
                return val.toFloat();
        else if (typeUri == "http://www.w3.org/2001/XMLSchema#decimal")
                { return val.toFloat(); }
+       else if (typeUri == "http://www.w3.org/2001/XMLSchema#duration")
+               {
+                       Duration* v = new Duration(val);
+                       return qVariantFromValue(v);
+               }
        else if (typeUri == "http://www.w3.org/2001/XMLSchema#dateTime")
                return QDateTime::fromString(val, Qt::ISODate);
        else if (typeUri == "http://www.w3.org/2001/XMLSchema#date")
index 6f88364da527634d5557d8cb1e90f2419438625f..4e8268cd74d35d45b624491aa85bdc20cd5d508e 100644 (file)
@@ -30,12 +30,12 @@ public:
        void setRoutePoints(PropertyList<GraphItem*> l);
        void addRoutePoint(Coordinates* l)                                      { mRoutePoints.append(l); }
 
-    PropertyList<Zone*> getRouteZones()                 { return mRouteZones; }
-    bool hasRouteZones()                                                               { return (mRouteZones.length() > 0); }
-    void setRouteZones(PropertyList<Zone*> l)           { mRouteZones = l; add(PROPERTY__ZONEROUTE, &mRouteZones);  }
-    void setRouteZones(QList<Zone*> l);
-    void setRouteZones(PropertyList<GraphItem*> l);
-    void addRouteZone(Zone* l)                          { mRouteZones.append(l); }
+       PropertyList<Zone*> getRouteZones()                                     { return mRouteZones; }
+       bool hasRouteZones()                                                            { return (mRouteZones.length() > 0); }
+       void setRouteZones(PropertyList<Zone*> l)                       { mRouteZones = l; add(PROPERTY__ZONEROUTE, &mRouteZones);  }
+       void setRouteZones(QList<Zone*> l);
+       void setRouteZones(PropertyList<GraphItem*> l);
+       void addRouteZone(Zone* l)                                                      { mRouteZones.append(l); }
 
        bool hasLength()                                                                        { return (mLength != NULL); }
        ValueObject* getLength()                                                        { return mLength; }
@@ -56,8 +56,8 @@ public:
        
 private:
        PropertyList<Coordinates*> mRoutePoints;
-    PropertyList<Zone*> mRouteZones;
-    ValueObject* mLength;
+       PropertyList<Zone*> mRouteZones;
+       ValueObject* mLength;
        ValueObject* mEnergyConsumption;
        Velocity* mAverageVelocity;
        TemporalContext* mDuration;
index 56455fa71aa203610560e2bb2942cb2baaf0cec2..d1953455575c98d578c31e18fb9d112e392beb43 100644 (file)
@@ -5,6 +5,7 @@
 #include <QDateTime>
 
 #include "smartapi/common/PROPERTY.h"
+#include "smartapi/common/Duration.h"
 #include "smartapi/model/Obj.h"
 #include "smartapi/rdf/Variant.h"
 
@@ -30,31 +31,33 @@ public:
        void parse(Statement* statement);
        void clearReferences(QSet<GraphItem*>* trash);
        
-       bool hasStart()                                         { return (mStart != NULL); }
+       bool hasStart()                                         { return (mStart != nullptr); }
        QDateTime getStart()                            { PROPERTYVAL(mStart, asDateTime, QDateTime()); }
        QTime getStartTime()                            { PROPERTYVAL(mStart, asTime, QTime()); }
        void setStart(Variant* s)                       { add(PROPERTY__START, s); mStart = s; }
        void setStart(QDateTime _start)         { setStart(new Variant(_start)); }
        void setStart(QTime _start)                     { setStart(new Variant(_start)); }
 
-       bool hasEnd()                                           { return (mEnd != NULL); }
+       bool hasEnd()                                           { return (mEnd != nullptr); }
        QDateTime getEnd()                                      { PROPERTYVAL(mEnd, asDateTime, QDateTime()); }
        QTime getEndTime()                                      { PROPERTYVAL(mEnd, asTime, QTime()); }
        void setEnd(Variant* e)                         { add(PROPERTY__END, e); mEnd = e; }
        void setEnd(QDateTime _end)                     { setEnd(new Variant(_end)); }
        void setEnd(QTime _end)                         { setEnd(new Variant(_end)); }
 
-       bool hasCount()                                         { return mCount != NULL; }
+       bool hasCount()                                         { return mCount != nullptr; }
        int getCount()                                          { PROPERTYVAL(mCount, asInt, -1); }
        void setCount(Variant* c)                       { add(PROPERTY__COUNT, c); mCount = c; }
        void setCount(int _count)                       { setCount(new Variant(_count)); }
 
-       bool hasDuration()                                      { return mDuration != NULL; }
-       QString getDuration()                           { PROPERTYVAL(mDuration, asString, QString()); }
+       bool hasDuration()                                      { return mDuration != nullptr; }
+       QString getDuration()                           { return (mDuration != nullptr ? mDuration->asDuration()->toString() : QString()); }
+       Duration* getDurationObject()           { return (mDuration != nullptr ? mDuration->asDuration() : nullptr); }
        void setDuration(Variant* d)            { add(PROPERTY__DURATION, d); mDuration = d; }
-       void setDuration(QString _duration)     { setDuration(new Variant(_duration)); }
+       void setDuration(Duration* d)           { setDuration(new Variant(d)); }
+       void setDuration(QString _duration)     { setDuration(new Duration(_duration)); }
 
-       bool hasDuring()                                        { return mDuring != NULL; }
+       bool hasDuring()                                        { return mDuring != nullptr; }
        QString getDuring()                                     { PROPERTYVAL(mDuring, asString, QString()); }
        void setDuring(Variant* d)                      { add(PROPERTY__DURING, d); mDuring = d; }
        void setDuring(QString _during)         { setDuring(new Variant(_during)); }
@@ -63,8 +66,8 @@ private:
        Variant* mStart;
        Variant* mEnd;
        Variant* mCount;
-       Variant* mDuration;
        Variant* mDuring;
+       Variant* mDuration;
 };
 
 #endif // _TEMPORALCONTEXT_H_
index ca840b1fb73c573d91069f2cb2aa5c30acd6ee6b..42fb2024ce417c48d32a6b732980096be64f9a41 100644 (file)
@@ -33,6 +33,16 @@ Variant::Variant(QVariant v) : GraphItem()
        else if (v.type() == QMetaType::QDateTime)              type = SMARTAPIVARIANT::DATETIME;
        else if (v.type() == QMetaType::QTime)                  type = SMARTAPIVARIANT::TIME;
        else if (v.type() == QMetaType::QVariantList)   type = SMARTAPIVARIANT::LIST;
+       else if (v.userType() == qMetaTypeId<Duration*>()) {
+               type = SMARTAPIVARIANT::DURATION;
+               Duration* d = v.value<Duration*>();
+               if (d != nullptr) {
+                       value = d->toString();
+                       delete d;
+               } else {
+                       qWarning() << "The duration value passed to Variant is NULL";
+               }
+       }
        else if (v.type() == QMetaType::VoidStar)       {
                // If the source value is a Variant itself, remove the nesting. Variants should not carry
                // variants but other primitive objects
index a2f60e4817869bfcbf2904155abedef5cd991b3b..9f0572c8aacf8ff7306d6a9e9825ed871d6d21c7 100644 (file)
@@ -2690,6 +2690,28 @@ bool durationTest()
                        d10.getYears() == 10 && d10.getMonths() == 20 && d10.getDays() == 30 &&  d10.getHours() == 40 && d10.getMinutes() == 50 && d10.getSeconds() == 16);
 }
 
+bool durationInTemporalContextTest()
+{
+       TemporalContext* tc = new TemporalContext();
+       tc->setStart(QDateTime::currentDateTime());
+       tc->setEnd(QDateTime::currentDateTime());
+       tc->setDuration("P10Y20M30DT40H50M16S");
+       tc->setDuring("month");
+       tc->turtlePrint();
+
+       TemporalContext* tc2 = new TemporalContext(tc);
+       tc2->turtlePrint();
+
+       QString serialized = Tools::toString(tc2, SERIALIZATION__TURTLE);
+       TemporalContext* tc3 = (TemporalContext*)Tools::fromStringAsObj(serialized, SERIALIZATION__TURTLE);
+       tc3->turtlePrint();
+
+       tc->destroyCascade();
+       tc2->destroyCascade();
+       tc3->destroyCascade();
+       return true;
+}
+
 //bool sharingTest()
 //{
 //    // generate key pairs
@@ -3275,6 +3297,7 @@ int main(int argc, char *argv[])
        QCoreApplication app(argc, argv);
 
        durationTest();
+       durationInTemporalContextTest();
 
        //modelReadTest();