C++: Added conversion/constructor between QVariantMap and SmartAPI Map
authorjani <jani@asema.com>
Thu, 14 Mar 2019 18:47:01 +0000 (20:47 +0200)
committerjani <jani@asema.com>
Thu, 14 Mar 2019 18:47:01 +0000 (20:47 +0200)
Common/C++/SmartAPI/smartapi/model/Map.cpp
Common/C++/SmartAPI/smartapi/model/Map.h
Common/C++/SmartAPI/tests/TestSequences.cpp

index 0709a1aa451df81f094e73042fbf8e535af64242..aee15ef77b272ce4519a4a3deff18c1607254071 100755 (executable)
@@ -20,10 +20,41 @@ Map::Map(Map* m) : Obj(m)
        setType(RESOURCE__MAP);
 }
 
+Map::Map(QVariantMap m)
+{
+       QMapIterator<QString, QVariant> i(m);
+       while (i.hasNext()) {
+               i.next();
+               if (i.value().type() == QMetaType::QVariantMap) {
+                       insert(i.key(), new Variant(new Map(i.value().toMap())));
+               } else {
+                       insert(i.key(), new Variant(i.value()));
+               }
+       }
+       setType(RESOURCE__MAP);
+}
+
 Map::~Map()
 {
 }
 
+QVariantMap Map::toVariantMap()
+{
+       QVariantMap m;
+       QMapIterator<QString, Variant*> i(mMap);
+       while (i.hasNext()) {
+               i.next();
+               QVariant v = i.value()->getValue();
+               if (i.value()->getType() == SMARTAPIVARIANT::MAP) {
+                       Map* mv = (Map*)v.value<void*>();
+                       m.insert(i.key(), mv->toVariantMap());
+               } else {
+                       m.insert(i.key(), v);
+               }
+       }
+       return m;
+}
+
 void Map::clearReferences(QSet<GraphItem*>* trash)
 {
        QList<QString> keys = mMap.keys();
index 23fce36f7598c421bf8216c0ce0ada49d86bd605..419bdf5dda1e3392480d127083ead185602f5a81 100644 (file)
@@ -16,6 +16,7 @@ class Map : public Obj
 public:
        Map(QString uri = QString()) : Obj(uri) { this->setType(RESOURCE__MAP); }
        Map(Map* m);
+       Map(QVariantMap m);
        ~Map();
        
        Resource* serialize(Model* model);
@@ -38,6 +39,7 @@ public:
        Variant* get(QString key) { if (mMap.contains(key)) return mMap.value(key); else return NULL; }
        void put(QString key, Variant* value) { mMap.insert(key, value); }
        QMap<QString, Variant*> getMap() { return mMap; };
+       QVariantMap toVariantMap();
        
        void insert(QString key, Variant* value) { mMap.insert(key, value); }
        void insert(QString key, int value) { mMap.insert(key, new Variant(value)); }
index 355bb2e56e1bf9ba8c98d8d922a7625addc48de9..2cf0b6ae1d79df79dd9207456c81db5f4b9649bf 100644 (file)
@@ -1492,74 +1492,28 @@ bool listOfMapsTest()
        }
 }
 
-//bool chargePlanRegistrationTest()
-//{
-//     QString serverIdentity = "http://seasexamples.asema.com/api/Ctestserver";
-//     QString serverName = "A sample charging plan calculation service";
+bool mapConversionTest()
+{
+       bool result;
+       QVariantMap testMap;
+       QVariantMap innerMap;
+       innerMap.insert("inner1", 10);
+       innerMap.insert("inner2", "twenty");
 
-//     Service* service = new Service(serverIdentity);
-
-//     // Service metadata
-//     Organization* owner = new Organization();
-//     Address* acmeAddress = new Address();
-//     InterfaceAddress* iface = new InterfaceAddress();
-
-//     iface->setHost("www.acme.com");
-//     iface->setScheme("https");
-//     iface->setPath("/seas/v1.0e1.0/access");
-//     acmeAddress->setStreetAddress("Giant Rubber Band Street");
-//     acmeAddress->setZipCode("12345");
-//     acmeAddress->setCity("Fairfield, New Jersey");
-//     owner->setName("Acme Industries");
-//     owner->setAddress(acmeAddress);
-
-//     service->addInterface(iface);
-//     service->addOwner(owner);
-//     service->setName(serverName);
-//     service->setDescription("A charging plan service that supplies the available energy for charging in timeslots at a charging point.");
-//     service->addWebsite("http://www.acme.com/acmeoptimizers/roadrunner/beep/beep");
-
-
-//     // API payload data
-//     Activity* a = new Activity();
-//     Input* i = new Input();
-//     Output* o = new Output();
-//     a->addOutput(o);
-//     a->addInput(i);
-//     service->addCapability(a);
-
-//     // Input
-//     Map* charge_need = new Map();
-//     charge_need->insert("Energy", Factory::createValueObject(NS__UNIT + "KilowattHour", "Total amount of energy requested to complete the charge."));
-//     charge_need->insert("PowerMax", Factory::createValueObject(NS__UNIT + "Watt", "Maximum allowed instant power for charging the vehicle."));
-//     charge_need->insert("PowerMin", Factory::createValueObject(NS__UNIT + "Watt", "Minumum allowed instant power for charging the vehicle."));
-//     charge_need->insert("Priority", new Obj(DATATYPE__INTEGER));
-//     charge_need->insert("Type", new Enumeration("Immediate", "Delayed"));
-//     TemporalContext* tci = new TemporalContext();
-//     tci->setDescription("The desired schedule at which the charging should take place (start (optional) and end)");
-//     charge_need->insert("Schedule", tci);
-//     i->add("ChargingNeed", charge_need);
-
-//     // Output
-//     Map* charge_plan = new Map();
-//     QList<Map*> orders;
-//     Map* order = new Map();
-//     TemporalContext* tco = new TemporalContext();
-//     tco->setDescription("The start and end times of this charging slot.");
-//     order->insert("Schedule", tco);
-//     order->insert("Power", Factory::createValueObject(NS__UNIT + "Watt", "The instant power supplied in this slot."));
-//     orders.append(order);
-//     charge_plan->insert("Orders", orders);
-//     o->add("ChargingPlan", charge_plan);
-
-//     RegistrationAgent* agent = new RegistrationAgent(serverIdentity);
-//     QString payload = agent->generateRegistrationMessage(service);
-//     printf("%s\n", payload.toUtf8().constData());
-//     service->destroyCascade();
-//    delete agent;
+       testMap.insert("outer1", 5);
+       testMap.insert("outer2", "eleven");
+       testMap.insert("outer3", innerMap);
 
-//    return true;
-//}
+       Map* map = new Map(testMap);
+       map->turtlePrint();
+       QVariantMap extractedMap = map->toVariantMap();
+       qDebug() << extractedMap;
+       result = (extractedMap.value("outer1").toInt() == 5 &&
+                       extractedMap.value("outer2").toString() == "eleven" &&
+                       extractedMap.value("outer3").toMap().value("inner2").toString() == "twenty");
+       map->destroyCascade();
+       return result;
+}
 
 EVP_PKEY* getPemPublicKey(QString fileName)
 {
@@ -3278,7 +3232,7 @@ bool serializeParseRouteTest()
        qDebug() << serialized;
 
        Route* parsed = (Route*)Tools::fromStringAsObj(serialized, SERIALIZATION__TURTLE);
-    qDebug() << "Parsed2:";
+       qDebug() << "Parsed2:";
        parsed->turtlePrint();
        parsed->destroyCascade();
        return true;
@@ -3492,6 +3446,13 @@ int main(int argc, char *argv[])
                return -1;
        }
        */
+       //------- MAP TESTS -------
+       qDebug() << "\n\nMAP CONVERSION TEST";
+       if (!mapConversionTest()) {
+               qDebug() << "\nMAP CONVERSION TEST FAILED!";
+               return -1;
+       }
+
        //------- LIST TESTS ------
        qDebug() << "\n\nLIST SERIALIZE / PARSE TEST";
        if (!listSerializeParseTest()) {
@@ -3512,7 +3473,7 @@ int main(int argc, char *argv[])
        }
        */
        //-------- TIMESERIES TESTS ------
-       qDebug() << "\n\nSIMPLE TIMESERIES TEST";
+       /*qDebug() << "\n\nSIMPLE TIMESERIES TEST";
        if (!simpleTimeSeriesTest()) {
                qDebug() << "\nSIMPLE TIMESERIES FAILED!";
                return -1;
@@ -3522,7 +3483,7 @@ int main(int argc, char *argv[])
        if (!valueObjectTimeSeriesTest()) {
                qDebug() << "\nVALUEOBJECT TIMESERIES TEST FAILED!";
                return -1;
-       }
+       }*/
        /*
        qDebug() << "\n\nVALUEOBJECT TIMESERIES LIST SUBSTITUTE TEST";
        if (!substitutiveValueObjectTimeSeriesTest()) {
@@ -3560,11 +3521,7 @@ int main(int argc, char *argv[])
 
 
        //-------- GEO TESTS ------
-       qDebug() << "\n\nWAYPOINTS TEST";
-       if (!waypointsTest()) {
-               qDebug() << "\nWAYPOINTS TEST FAILED!";
-               return -1;
-       }
+
 
        qDebug() << "\n\nROUTE TEST";
        if (!routeTest()) {
@@ -3611,7 +3568,7 @@ int main(int argc, char *argv[])
        }
 
 
-    //-------- PRICING AND TRADING ------
+       //-------- PRICING AND TRADING ------
        qDebug() << "\n\nBASIC PRICE SPEC TEST";
        if (!priceSpecificationSerializeParseTest()) {
                qDebug() << "\nBASIC PRICE SPEC TEST FAILED!";
@@ -3624,15 +3581,15 @@ int main(int argc, char *argv[])
                return -1;
        }
 
-    //currencyConversionTest();
+       //currencyConversionTest();
 
        */
 
-       qDebug() << "\n\nTIME DEPENDENT PRICELIST TEST";
+       /*qDebug() << "\n\nTIME DEPENDENT PRICELIST TEST";
        if (!priceSpecificationTimeDependentSerializeParseTest()) {
                qDebug() << "\nTIME DEPENDENT PRICELIST TEST!";
                return -1;
-       }
+       }*/
 
        /*
        //registrationTest();