programblock.cpp 10.4 KB
Newer Older
Leander Schulten's avatar
Leander Schulten committed
1
#include "programblock.h"
Leander Schulten's avatar
Leander Schulten committed
2
#include <QJsonArray>
3
4
5
#include <stdexcept>
#include "modulemanager.h"

Leander Schulten's avatar
Leander Schulten committed
6
7
8
9

namespace Modules {


Leander Schulten's avatar
Leander Schulten committed
10
    namespace detail {
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    template<typename PointerType>
    class SharedPtrComperator{
        public:
        bool operator()(const PointerType *l,const PointerType *r)const{
            return l<r;
        }
        bool operator()(const std::shared_ptr<PointerType> &l,const std::shared_ptr<PointerType> &r)const{
            return l.get()<r.get();
        }
        bool operator()(const PointerType*l,const std::shared_ptr<PointerType> &r)const{
            return l<r.get();
        }
        bool operator()(const std::shared_ptr<PointerType> &l,const PointerType*r)const{
            return l.get()<r;
        }
    };
Leander Schulten's avatar
Leander Schulten committed
27
28
    }

Leander Schulten's avatar
Leander Schulten committed
29
30
31
32
33
    ProgramBlock::ProgramBlock()
    {

    }

Leander Schulten's avatar
Leander Schulten committed
34
    ProgramBlock::ProgramBlock(const QJsonObject& o){
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        // Wir erstellen erstmal alle objecte mit passendem Typ und ID
        std::map<QString,std::shared_ptr<Programm>> programs;
        std::map<QString,std::shared_ptr<Filter>> filter;
        std::map<QString,std::shared_ptr<Consumer>> consumer;
        {
            const auto progs = o["programs"].toArray();
            for(const auto & v_ : progs){
                QJsonObject obj = v_.toObject();
                const auto qstr = obj["typename"].toString();
                const auto str = qstr.toStdString();
                auto p = ModuleManager::singletone()->createProgramm(str);
                if(!p){
                    throw std::runtime_error(std::string("Program with name '") + str + "' does not exist.");
                }
                programs.insert(std::make_pair(qstr,p));
            }
        }
        {
            const auto progs = o["filter"].toArray();
            for(const auto & v_ : progs){
                QJsonObject obj = v_.toObject();
                const auto qstr = obj["typename"].toString();
                const auto str = qstr.toStdString();
                auto p = ModuleManager::singletone()->createFilter(str);
                if(!p){
                    throw std::runtime_error(std::string("Filter with name '") + str + "' does not exist.");
                }
                filter.insert(std::make_pair(qstr,p));
            }
        }
        {
            const auto progs = o["consumer"].toArray();
            for(const auto & v_ : progs){
                QJsonObject obj = v_.toObject();
                const auto qstr = obj["typename"].toString();
                const auto str = qstr.toStdString();
                auto p = ModuleManager::singletone()->createConsumer(str);
                if(!p){
                    throw std::runtime_error(std::string("Consumer with name '") + str + "' does not exist.");
                }
                consumer.insert(std::make_pair(qstr,p));
            }
        }
        // Wir haben schon alle benötogten Objecte erstellt und wissen zu welcher ID welches Object gehört.
        {
            const auto filterCon = o["filterConnections"].toArray();
            for(const auto & i : filterCon){
                const auto iter = filter.find(i["source"].toString());
                if(iter == filter.cend()){
                    throw std::runtime_error("Connection has invalid source.");
                }
                detail::Connection c(iter->second);
                const auto targets = i["targets"].toArray();
                for(const auto & t : targets){
                    OutputDataProducer * outputDataProducer;
                    const auto targetName = t["target"].toString();
                    const auto pIter = programs.find(targetName);
                    if(pIter != programs.cend()){
                        outputDataProducer = pIter->second.get();
                    }else{
                        const auto fIter = filter.find(targetName);
                        if(fIter != filter.cend()){
                            outputDataProducer = fIter->second.get();
                        }else{
                            throw std::runtime_error("Connection target not found :" + targetName.toStdString());
                        }
                    }
                    c.addTarget(t["length"].toInt(),outputDataProducer,t["targetStartIndex"].toInt());
                }
                this->filter.insert(std::make_pair(i["level"].toInt(),c));
            }
        }
        {
            const auto consumerCon = o["consumerConnections"].toArray();
            for(const auto & i : consumerCon){
                const auto iter = consumer.find(i["source"].toString());
                if(iter == consumer.cend()){
                    throw std::runtime_error("Connection has invalid source.");
                }
                detail::Connection c(iter->second);
                const auto targets = i["targets"].toArray();
                for(const auto & t : targets){
                    OutputDataProducer * outputDataProducer;
                    const auto targetName = t["target"].toString();
                    const auto pIter = programs.find(targetName);
                    if(pIter != programs.cend()){
                        outputDataProducer = pIter->second.get();
                    }else{
                        const auto fIter = filter.find(targetName);
                        if(fIter != filter.cend()){
                            outputDataProducer = fIter->second.get();
                        }else{
                            throw std::runtime_error("Connection target not found :" + targetName.toStdString());
                        }
                    }
                    c.addTarget(t["length"].toInt(),outputDataProducer,t["targetStartIndex"].toInt());
                }
                this->consumer.push_back(c);
            }
        }
    }

    void ProgramBlock::addFilter(detail::Connection c, int layer){
        for(const auto & con : c.targeds){
            if(!haveOutputDataProducer(con.second.targed)){
                throw std::runtime_error("one outputdataproducer is unknown");
            }
        }
        filter.emplace(layer,std::move(c));
    }

    void ProgramBlock::addConsumer(detail::Connection c){
        for(const auto & con : c.targeds){
            if(!haveOutputDataProducer(con.second.targed)){
                throw std::runtime_error("one outputdataproducer is unknown");
            }
        }
        consumer.push_back(std::move(c));
Leander Schulten's avatar
Leander Schulten committed
153
154
155
156
157
158
159
160
    }


    bool ProgramBlock::doStep(time_diff_t diff){
        bool finished = false;
        for(auto & p : programs){
            const auto state = p->doStep(diff);
            finished |= state.finished;
161
            dataChanged[p.get()] = state.outputDataChanged;
Leander Schulten's avatar
Leander Schulten committed
162
163
164
        }
        for(auto i = filter.cbegin(); i!= filter.cend();++i){
            if(doConnection(i->second)){
165
                static_cast<Filter*>(i->second.source.get())->filter();
Leander Schulten's avatar
Leander Schulten committed
166
            }
167
            dataChanged[i->second.source.get()] |= static_cast<Filter*>(i->second.source.get())->doStep(diff);
Leander Schulten's avatar
Leander Schulten committed
168
169
170
        }
        for(auto i = consumer.cbegin(); i != consumer.cend();++i){
            if(doConnection(*i)){
171
                static_cast<Consumer*>(i->source.get())->show();
Leander Schulten's avatar
Leander Schulten committed
172
173
174
175
176
177
            }
        }
        return finished;
    }


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    void ProgramBlock::writeJsonObject(QJsonObject &o){
        // Wir speichern zu jedem Programm/Filter/Consumer ein Object ab, das den Typnamen und eine ID, wir nehmen die Adresse im Arbeitsspeicher.
        // Wir speichern die Connections, indem wir für die Source und die Targets die Adressen(IDs) der jeweiligen Pointer speichern.
        // --- Programs ---
        {QJsonArray progs;
        for(auto i = programs.cbegin();i!=programs.cend();++i){
            QJsonObject p;
            p["typename"] = (*i)->getName();
            p["id"] = QString::number(reinterpret_cast<size_t>(i->get()));
            static_assert (sizeof (size_t) == sizeof (Programm*), "Pointer must have same size as size_t");
            progs.push_back(p);
        }
        o["programs"] = progs;
        }
        // --- Filter ---
        {
        QJsonArray filter;
        QJsonArray filterCon;
        for(const auto & i : this->filter){
            QJsonObject e;
            QJsonObject con;
            con["source"] = QString::number(reinterpret_cast<size_t>(i.second.source.get()));
            e["typename"] = reinterpret_cast<Named*>(i.second.source.get())->getName();
            e["id"] = QString::number(reinterpret_cast<size_t>(i.second.source.get()));
            con["level"] = i.first;
            static_assert (sizeof (size_t) == sizeof (Filter*), "Pointer must have same size as size_t");
            filter.push_back(e);
            QJsonArray targets;
            for(const auto & c : i.second.targeds){
                QJsonObject target;
                target["target"] = QString::number(reinterpret_cast<size_t>(c.second.targed));
                target["length"] = static_cast<int>(c.first);
                target["targetStartIndex"] = static_cast<int>(c.second.startIndex);
                targets.push_back(target);
            }
            con["targets"] = targets;
        }
        o["filter"] = filter;
        o["filterConnections"] = filterCon;
        }
        // --- Consumer ---
        {
            QJsonArray consumer;
            QJsonArray consumerCon;
            for(const auto & i : this->consumer){
                QJsonObject e;
                QJsonObject con;
                con["source"] = QString::number(reinterpret_cast<size_t>(i.source.get()));
                e["typename"] = reinterpret_cast<Named*>(i.source.get())->getName();
                e["id"] = QString::number(reinterpret_cast<size_t>(i.source.get()));
                {
                    QJsonObject prop;
                    // TODO save properties of object
                    //e["properties"] =
                }
                static_assert (sizeof (size_t) == sizeof (Filter*), "Pointer must have same size as size_t");
                consumer.push_back(e);
                QJsonArray targets;
                for(const auto & c : i.targeds){
                    QJsonObject target;
                    target["target"] = QString::number(reinterpret_cast<size_t>(c.second.targed));
                    target["length"] = static_cast<int>(c.first);
                    target["targetStartIndex"] = static_cast<int>(c.second.startIndex);
                    targets.push_back(target);
                }
                con["targets"] = targets;
                consumerCon.push_back(con);
            }
            o["consumer"] = consumer;
            o["consumerConnections"] = consumerCon;
        }
    }

Leander Schulten's avatar
Leander Schulten committed
251
252
253



Leander Schulten's avatar
Leander Schulten committed
254
}