Skip to content
Snippets Groups Projects
ProfileLibrarySegment.cpp 2.93 KiB
Newer Older
#include "ProfileLibrarySegment.h"
#include <vector>

#include <SimoxUtility/algorithm/string.h>

#include <ArmarXCore/core/time/ice_conversions.h>

#include "RobotAPI/libraries/armem/client/query/Builder.h"
#include "RobotAPI/libraries/armem/core/Commit.h"
#include "RobotAPI/libraries/armem/core/MemoryID.h"
#include <RobotAPI/libraries/armem/server/MemoryToIceAdapter.h>
#include <RobotAPI/libraries/armem_skills/aron_conversions.h>
#include <RobotAPI/libraries/aron/converter/datatype/DatatypeConverterVisitor.h>
#include <RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.h>
#include <RobotAPI/libraries/skills/core/aron/FluxioProfile.aron.generated.h>

namespace armarx::skills::segment
{
    ProfileLibraryCoreSegment::ProfileLibraryCoreSegment(
        armem::server::MemoryToIceAdapter& iceMemory) :
        Base(iceMemory, CoreSegmentName, skills::manager::arondto::FluxioProfile::ToAronType())
    {
    }

    void
    ProfileLibraryCoreSegment::defineProperties(PropertyDefinitionsPtr defs,
                                                      const std::string& prefix)
    {
        this->setDefaultMaxHistorySize(3);
        Base::defineProperties(defs, prefix);
    }

    void
    ProfileLibraryCoreSegment::init()
    {
        Base::init();
    }

    void
    ProfileLibraryCoreSegment::addProfile(const skills::manager::arondto::FluxioProfile& profile)
    {
        // add profile
        armem::MemoryID provId = id().withProviderSegmentName("FluxioProfile");

        armem::Commit commit;
        armem::EntityUpdate& entityUpdate = commit.add();
        entityUpdate.confidence = 1.0;
        entityUpdate.referencedTime = armem::Time::Now();
        entityUpdate.sentTime = armem::Time::Now();
        entityUpdate.arrivedTime = armem::Time::Now();
        entityUpdate.instancesData = {profile.toAron()};
        entityUpdate.entityID = provId.withEntityName(profile.name);

        // Commit data to memory and notify
        iceMemory.commit(commit);

    std::optional<std::vector<skills::manager::arondto::FluxioProfile>> ProfileLibraryCoreSegment::getProfiles() const {
        std::vector<skills::manager::arondto::FluxioProfile> ret;
        armarx::armem::client::query::Builder qb;
        qb.allLatestInCoreSegment(id());
        armem::client::QueryResult qresult = iceMemory.query(qb.buildQueryInput());
        if (!qresult.success)
        {
            ARMARX_WARNING << "Query failed: " << qresult.errorMessage;
            return std::nullopt;
        }

        const armem::wm::Memory& data = qresult.memory;

        data.forEachInstance(
            [&ret](const armem::wm::EntityInstance& instance)
            {
                auto profile = instance.dataAs<skills::manager::arondto::FluxioProfile>();
                if (profile.deleted)
                {
                    return;
                }
                ret.push_back(profile);
            });

        return ret;
    }
} // namespace armarx::skills::segment