static Poco::AutoPtr<Poco::XML::Element> portInfoToElem(Poco::AutoPtr<Poco::XML::Document> xmlDoc, const Poco::JSON::Array::Ptr &portsInfo, const std::string &prefix) { auto nodeTd = xmlDoc->createElement("td"); nodeTd->setAttribute("border", "0"); auto table = xmlDoc->createElement("table"); nodeTd->appendChild(table); table->setAttribute("border", "0"); table->setAttribute("cellspacing", "0"); for (size_t i = 0; i < portsInfo->size(); i++) { const auto portInfo = portsInfo->getObject(i); auto name = portInfo->getValue<std::string>("name"); auto isSigSlot = portInfo->getValue<bool>("isSigSlot"); auto tr = xmlDoc->createElement("tr"); table->appendChild(tr); auto td = xmlDoc->createElement("td"); tr->appendChild(td); td->setAttribute("border", "1"); if (prefix == "in" and isSigSlot) td->setAttribute("bgcolor", "#AEC6CF"); if (prefix == "in" and not isSigSlot) td->setAttribute("bgcolor", "#779ECB"); if (prefix == "out" and isSigSlot) td->setAttribute("bgcolor", "#77DD77"); if (prefix == "out" and not isSigSlot) td->setAttribute("bgcolor", "#03C03C"); td->setAttribute("port", "__"+prefix+"__"+name); unsigned value = 0; if (Poco::NumberParser::tryParseUnsigned(name, value)) name = prefix+name; td->appendChild(xmlDoc->createTextNode(name)); } return nodeTd; }
/*! * paste only one object type so handlePaste can control the order of creation */ static GraphObjectList handlePasteType(GraphDraw *draw, const Poco::JSON::Array::Ptr &graphObjects, const std::string &type) { GraphObjectList newObjects; for (size_t objIndex = 0; objIndex < graphObjects->size(); objIndex++) { const auto jGraphObj = graphObjects->getObject(objIndex); const auto what = jGraphObj->getValue<std::string>("what"); GraphObject *obj = nullptr; if (what != type) continue; if (what == "Block") obj = new GraphBlock(draw); if (what == "Breaker") obj = new GraphBreaker(draw); if (what == "Connection") obj = new GraphConnection(draw); if (what == "Widget") obj = new GraphWidget(draw); if (obj == nullptr) continue; try {obj->deserialize(jGraphObj);} catch (const Pothos::NotFoundException &) { delete obj; continue; } obj->setSelected(true); newObjects.push_back(obj); } return newObjects; }
/*********************************************************************** * Helper method for unit tests **********************************************************************/ static bool connectionsHave( const Poco::JSON::Array::Ptr &connsArray, const std::string &srcId, const std::string &srcName, const std::string &dstId, const std::string &dstName ) { for (size_t c_i = 0; c_i < connsArray->size(); c_i++) { const auto connObj = connsArray->getObject(c_i); if (connObj->getValue<std::string>("srcId") != srcId) continue; if (connObj->getValue<std::string>("srcName") != srcName) continue; if (connObj->getValue<std::string>("dstId") != dstId) continue; if (connObj->getValue<std::string>("dstName") != dstName) continue; return true; } return false; }
/*********************************************************************** * Load a JSON block description from file and register the descriptions. * return a list of registration paths and a list of paths for blocks. **********************************************************************/ static std::vector<Pothos::PluginPath> blockDescParser(std::istream &is, std::vector<Pothos::PluginPath> &blockPaths) { std::vector<Pothos::PluginPath> entries; //parse the stream into a JSON array const auto result = Poco::JSON::Parser().parse(is); Poco::JSON::Array::Ptr arrayOut; if (result.type() == typeid(Poco::JSON::Object::Ptr)) { arrayOut = new Poco::JSON::Array(); arrayOut->add(result.extract<Poco::JSON::Object::Ptr>()); } else arrayOut = result.extract<Poco::JSON::Array::Ptr>(); for (size_t i = 0; i < arrayOut->size(); i++) { auto obj = arrayOut->getObject(i); assert(obj); std::stringstream ossJsonObj; obj->stringify(ossJsonObj); const std::string JsonObjStr(ossJsonObj.str()); std::vector<std::string> paths; paths.push_back(obj->getValue<std::string>("path")); if (obj->has("aliases")) for (const auto &alias : *obj->getArray("aliases")) { paths.push_back(alias.toString()); } //register the block description for every path for (const auto &path : paths) { const auto pluginPath = Pothos::PluginPath("/blocks/docs", path); Pothos::PluginRegistry::add(pluginPath, JsonObjStr); entries.push_back(pluginPath); blockPaths.push_back(Pothos::PluginPath("/blocks", path)); } } return entries; }
void parseModesFile() { printf("Parse\n" ); try { //Application& app = Application::instance(); /* [ { "code":1, "width":640, "height":480, "rate":60, "aspect_ratio":"4:3", "scan":"p", "3d_modes":[] } ] */ //std::string conf = std::string(" [ { \"code\":1 , \"width\":640, \"height\":480 } , { \"code\":3 , \"width\":800, \"height\":600 } ]"); std::string conf =readFileToString("data/modes.json"); Parser parser; Var result = parser.parse(conf); Poco::JSON::Array::Ptr arr = result.extract<Poco::JSON::Array::Ptr>(); for (int i=0;i<arr->size(); i++) { Object::Ptr object = arr->getObject(i); printf("w,h=%d,%d\n",object->getValue<int>("width"),object->getValue<int>("height")); } Query myQuery(result); //Var topic=myQuery.find("code"); //int value = topic.convert<int>(); //printf("Value=%d\n",value); } catch (...) { printf("Exception\n"); } }
/*********************************************************************** * make topology from JSON string - implementation **********************************************************************/ std::shared_ptr<Pothos::Topology> Pothos::Topology::make(const std::string &json) { //parse the json string/file to a JSON object const auto topObj = parseJSONStr(json); //create the proxy environment (local) and the registry auto env = Pothos::ProxyEnvironment::make("managed"); auto registry = env->findProxy("Pothos/BlockRegistry"); auto evaluator = env->findProxy("Pothos/Util/EvalEnvironment").callProxy("make"); //create thread pools std::map<std::string, Pothos::Proxy> threadPools; Poco::JSON::Object::Ptr threadPoolObj; if (topObj->isObject("threadPools")) threadPoolObj = topObj->getObject("threadPools"); std::vector<std::string> threadPoolNames; if (threadPoolObj) threadPoolObj->getNames(threadPoolNames); for (const auto &name : threadPoolNames) { std::stringstream ss; threadPoolObj->getObject(name)->stringify(ss); Pothos::ThreadPoolArgs args(ss.str()); threadPools[name] = env->findProxy("Pothos/ThreadPool").callProxy("new", args); } //create the topology and add it to the blocks //the IDs 'self', 'this', and '' can be used std::map<std::string, Pothos::Proxy> blocks; auto topology = Pothos::Topology::make(); blocks["self"] = env->makeProxy(topology); blocks["this"] = env->makeProxy(topology); blocks[""] = env->makeProxy(topology); //create the blocks Poco::JSON::Array::Ptr blockArray; if (topObj->isArray("blocks")) blockArray = topObj->getArray("blocks"); if (blockArray) for (size_t i = 0; i < blockArray->size(); i++) { if (not blockArray->isObject(i)) throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+std::to_string(i)+"] must be an object"); const auto &blockObj = blockArray->getObject(i); if (not blockObj->has("id")) throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+std::to_string(i)+"] missing 'id' field"); const auto id = blockObj->getValue<std::string>("id"); blocks[id] = makeBlock(registry, evaluator, blockObj); //set the thread pool const auto threadPoolName = blockObj->optValue<std::string>("threadPool", "default"); auto threadPoolIt = threadPools.find(threadPoolName); if (threadPoolIt != threadPools.end()) blocks[id].callVoid("setThreadPool", threadPoolIt->second); else if (threadPoolName != "default") throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+id+"] unknown threadPool = " + threadPoolName); } //create the topology and connect the blocks Poco::JSON::Array::Ptr connArray; if (topObj->isArray("connections")) connArray = topObj->getArray("connections"); if (connArray) for (size_t i = 0; i < connArray->size(); i++) { if (not connArray->isArray(i)) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] must be an array"); const auto &connArgs = connArray->getArray(i); if (connArgs->size() != 4) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] must be size 4"); //extract connection arg fields const auto srcId = connArgs->getElement<std::string>(0); const auto srcPort = connArgs->get(1).toString(); const auto dstId = connArgs->getElement<std::string>(2); const auto dstPort = connArgs->get(3).toString(); //check that the block IDs exist if (blocks.count(srcId) == 0) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] no such ID: " + srcId); if (blocks.count(dstId) == 0) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] no such ID: " + dstId); //make the connection topology->connect(blocks.at(srcId), srcPort, blocks.at(dstId), dstPort); } return topology; }