예제 #1
0
파일: jsonreader.cpp 프로젝트: SSE4/vmf-1
bool JSONReader::parseAll(const std::string& text, IdType& nextId, std::string& filepath, std::string& checksum,
    std::vector<std::shared_ptr<MetadataStream::VideoSegment>>& segments,
    std::vector<std::shared_ptr<MetadataSchema>>& schemas,
    std::vector<std::shared_ptr<MetadataInternal>>& metadata)
{
    if(text.empty())
    {
        VMF_LOG_ERROR("Empty input JSON string");
        return false;
    }

    schemas.clear();
    metadata.clear();

    JSONNode root;
    try
    {
        root = libjson::parse(text);
    }
    catch(...)
    {
        VMF_LOG_ERROR("Can't get JSON root");
        return false;
    }

    if(root.size() != 1)
    {
        VMF_LOG_ERROR("More than one JSON root");
        return false;
    }

    JSONNode localRootNode = root[0];

    if( localRootNode.name() == TAG_VMF )
    {
        auto nextIdIter = localRootNode.find(ATTR_VMF_NEXTID);
        if(nextIdIter != localRootNode.end() )
            nextId = nextIdIter->as_int();
        auto filepathIter = localRootNode.find(ATTR_VMF_FILEPATH);
        if(filepathIter != localRootNode.end() )
            filepath = filepathIter->as_string();
        auto checksumIter = localRootNode.find(ATTR_VMF_CHECKSUM);
        if(checksumIter != localRootNode.end() )
            checksum = checksumIter->as_string();

	if(!parseVideoSegments(text, segments))
	    return false;
        if(!parseSchemas(text, schemas))
            return false;
        if(!parseMetadata(text, schemas, metadata))
            return false;
    }
    else
    {
        VMF_LOG_ERROR("Root JSON element isn't 'vmf'");
        return false;
    }

    return true;
}
예제 #2
0
bool Programmer::loadJSON(JSONNode data) {
  auto devices = data.find("devices");
  if (devices == data.end()) {
    Logger::log(ERR, "No devices found in Programmer");
    return false;
  }

  auto it = devices->begin();
  while (it != devices->end()) {
    Device* device = new Device(it->name(), *it);

    // We're overwriting data here, so make sure to actually delete the old data.
    // if there is any
    if (m_devices.count(device->getId()) > 0)
      delete m_devices[device->getId()];

    m_devices[device->getId()] = device;
    it++;
  }

  auto c = data.find("captured");
  if (c == data.end()) {
    Logger::log(WARN, "No captured set found in Programmer");
    captured = DeviceSet(m_rig);
  }
  else {
    captured = DeviceSet(m_rig, *c);
  }

  return true;
}
예제 #3
0
void Rig::loadJSON(JSONNode root) {
  //JSONNode::const_iterator i = root.begin();
  JSONNode::iterator i = root.begin();

  auto version = root.find("version");
  if (version == root.end()) {
    Logger::log(ERR, "No version specified for input file. Aborting load.");
    return;
  }
  else {
    stringstream ss;
    stringstream ss2(version->as_string());

    ss << LumiverseCore_VERSION_MAJOR << "." << LumiverseCore_VERSION_MINOR;
    
    float libVer;
    float fileVer;

    ss >> libVer;
    ss2 >> fileVer;

    if (fileVer < libVer) {
      // Friendly warning if you're loading an old file.
      Logger::log(WARN, "File created against earlier version of Lumiverse. Check logs for any load problems.");
    }
    else if (fileVer > libVer) {
      // Loading newer file with older library.
      Logger::log(WARN, "File created against newer version of Lumiverse. Check logs for any load problems.");
    }
  }

  while (i != root.end()){
    // get the node name and value as a string
    std::string nodeName = i->name();

    if (nodeName == "devices") {
      loadDevices(*i);
      Logger::log(INFO, "Device load complete");
    }
    else if (nodeName == "patches") {
	  i->push_back(*root.find("jsonPath"));
      loadPatches(*i);
      Logger::log(INFO, "Patch load complete");
    }
    else if (nodeName == "refreshRate") {
      setRefreshRate(i->as_int());
    }

    //increment the iterator
    ++i;
  }
}
예제 #4
0
bool Condition::Test(JSONNode row, bool oldValue)
{
	bool result;
	string operand1Value = operand1IsConstant ? operand1 : row.find(operand1)->as_string();
	string operand2Value = operand2IsConstant ? operand2 : row.find(operand2)->as_string();
	//cout << "operand1 " << operand1Value << endl;
	//cout << "operand2 " << operand2Value << endl;
	//cout << "mode " << (mode == 0 ? "and" : "or") << endl;
	//cout << "operator " << (operatorMode == 0 ? "==" : "!=") << endl;
	switch (operatorMode)
	{
	case 0 :
		result = operand1Value == operand2Value;
		break;
	case 1 :
		result = operand1Value != operand2Value;
		break;
	}
	switch (mode)
	{
	case 0 :
		return oldValue && result;
	case 1 :
		return oldValue || result;
	}
	return false;
}
예제 #5
0
Keyframe::Keyframe(JSONNode node) {
  auto jt = node.find("t");
  if (jt == node.end()) {
    Logger::log(ERR, "Keyframe has no assigned time. Defaulting to 0.");
    t = 0;
  }
  else {
    t = jt->as_int();
  }

  auto v = node.find("val");
  if (v == node.end()) {
    val = nullptr;
  }
  else {
    val = shared_ptr<LumiverseType>(LumiverseTypeUtils::loadFromJSON(*v));
  }

  auto ucs = node.find("useCurrentState");
  if (ucs == node.end()) {
    useCurrentState = false;
  }
  else {
    useCurrentState = ucs->as_bool();
  }

  auto tid = node.find("timelineID");
  if (tid == node.end()) {
    timelineID = "";
  }
  else {
    timelineID = tid->as_string();
  }

  auto to = node.find("timelineOffset");
  if (to == node.end()) {
    timelineOffset = 0;
  }
  else {
    timelineOffset = to->as_int();
  }
}
예제 #6
0
파일: jsonreader.cpp 프로젝트: SSE4/vmf-1
static std::shared_ptr<MetadataStream::VideoSegment> parseVideoSegmentFromNode(JSONNode& segmentNode)
{
    auto segmentTitleIter = segmentNode.find(ATTR_SEGMENT_TITLE);
    auto segmentFPSIter = segmentNode.find(ATTR_SEGMENT_FPS);
    auto segmentTimeIter = segmentNode.find(ATTR_SEGMENT_TIME);
    auto segmentDurationIter = segmentNode.find(ATTR_SEGMENT_DURATION);
    auto segmentWidthIter = segmentNode.find(ATTR_SEGMENT_WIDTH);
    auto segmentHeightIter = segmentNode.find(ATTR_SEGMENT_HEIGHT);

    if(segmentTitleIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no title");
    if(segmentFPSIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no fps value");
    if(segmentTimeIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no time value");

    std::string title = segmentTitleIter->as_string();
    double fps = segmentFPSIter->as_float();
    long long timestamp = segmentTimeIter->as_int();

    if(title.empty())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid title");
    if(fps <= 0)
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid fps value");
    if(timestamp < 0)
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid time value");

    std::shared_ptr<MetadataStream::VideoSegment> spSegment(new MetadataStream::VideoSegment(title, fps, timestamp));

    if(segmentDurationIter != segmentNode.end())
    {
	long long duration = segmentDurationIter->as_int();
	if(duration > 0)
	    spSegment->setDuration(duration);
    }
    if(segmentWidthIter != segmentNode.end() && segmentHeightIter != segmentNode.end())
    {
	long width = segmentWidthIter->as_int(), height = segmentHeightIter->as_int();
	if(width > 0 && height > 0)
	    spSegment->setResolution(width, height);
    }

    return spSegment;

}
예제 #7
0
파일: jsonreader.cpp 프로젝트: SSE4/vmf-1
static std::shared_ptr<MetadataSchema> parseSchemaFromNode(JSONNode& schemaNode)
{
    std::shared_ptr<vmf::MetadataSchema> spSchema;
    auto schemaNameIter = schemaNode.find(ATTR_NAME);
    auto schemaAuthorIter = schemaNode.find(ATTR_SCHEMA_AUTHOR);
    if(schemaNameIter != schemaNode.end())
    {
        if(schemaAuthorIter != schemaNode.end())
            spSchema = std::make_shared<vmf::MetadataSchema>(schemaNameIter->as_string(), schemaAuthorIter->as_string());
        else
            spSchema = std::make_shared<vmf::MetadataSchema>(schemaNameIter->as_string());
    }
    else
        VMF_EXCEPTION(IncorrectParamException, "Schema has no name");

    auto descsArrayIter = schemaNode.find(TAG_DESCRIPTIONS_ARRAY);
    if(descsArrayIter == schemaNode.end())
        VMF_EXCEPTION(IncorrectParamException, "Can't find descriptions-array JSON node");

    std::shared_ptr<vmf::MetadataDesc> spDesc;
    for(auto descNode = descsArrayIter->begin(); descNode != descsArrayIter->end(); descNode++)
    {
        auto descNameIter = descNode->find(ATTR_NAME);
        if(descNameIter == descNode->end())
            VMF_EXCEPTION(IncorrectParamException, "Description has no name");

        auto fieldsArrayIter = descNode->find(TAG_FIELDS_ARRAY);
        if(fieldsArrayIter == descNode->end())
            VMF_EXCEPTION(IncorrectParamException, "Description has no fields array");

        std::vector<FieldDesc> vFields;
        for(auto fieldNode = fieldsArrayIter->begin(); fieldNode != fieldsArrayIter->end(); fieldNode++)
        {
            auto fieldNameIter = fieldNode->find(ATTR_NAME);
            auto fieldTypeIter = fieldNode->find(ATTR_FIELD_TYPE);
            if(fieldNameIter == fieldNode->end() || fieldTypeIter == fieldNode->end() )
                VMF_EXCEPTION(IncorrectParamException, "Field has no 'name' or 'type' attribute");
            vmf::Variant::Type field_type = Variant::typeFromString(fieldTypeIter->as_string());
            bool field_optional = false;
            auto fieldsOptionalityIter = fieldNode->find(ATTR_FIELD_OPTIONAL);
            if(fieldsOptionalityIter != fieldNode->end())
            {
                if(fieldsOptionalityIter->as_string() == "true")
                    field_optional = true;
                else if (fieldsOptionalityIter->as_string() == "false")
                    field_optional = false;
                else
                    VMF_EXCEPTION(vmf::IncorrectParamException, "Invalid value of boolean attribute 'optional'");
            }
            vFields.push_back(FieldDesc(fieldNameIter->as_string(), field_type, field_optional));
        }

        auto refsArrayIter = descNode->find(TAG_METADATA_REFERENCES_ARRAY);
        if (refsArrayIter == descNode->end())
            VMF_EXCEPTION(IncorrectParamException, "Description has no references array");

        std::vector<std::shared_ptr<ReferenceDesc>> vReferences;
        for (auto refNode = refsArrayIter->begin(); refNode != refsArrayIter->end(); refNode++)
        {
            auto refNameIter = refNode->find(ATTR_NAME);
            if (refNameIter == refNode->end())
                VMF_EXCEPTION(IncorrectParamException, "Field has no 'name' attribute");

            auto refUniqueIter = refNode->find(ATTR_REFERENCE_UNIQUE);
            bool isUnique = false;
            if (refUniqueIter != refNode->end())
            {
                if (refUniqueIter->as_string() == "true")
                    isUnique = true;
                else if (refUniqueIter->as_string() == "false")
                    isUnique = false;
                else
                    VMF_EXCEPTION(vmf::IncorrectParamException, "Invalid value of boolean attribute 'isUnique'");
            }

            auto refCustomIter = refNode->find(ATTR_REFERENCE_CUSTOM);
            bool isCustom = false;
            if (refCustomIter != refNode->end())
            {
                if (refCustomIter->as_string() == "true")
                    isCustom = true;
                else if (refCustomIter->as_string() == "false")
                    isCustom = false;
                else
                    VMF_EXCEPTION(vmf::IncorrectParamException, "Invalid value of boolean attribute 'isCustom'");
            }

            vReferences.emplace_back(std::make_shared<ReferenceDesc>(refNameIter->as_string(), isUnique, isCustom));
        }

        spDesc = std::make_shared<vmf::MetadataDesc>(descNameIter->as_string(), vFields, vReferences);
        spSchema->add(spDesc);
    }

    return spSchema;
}
예제 #8
0
파일: jsonreader.cpp 프로젝트: SSE4/vmf-1
static std::shared_ptr<MetadataInternal> parseMetadataFromNode(JSONNode& metadataNode, const std::vector<std::shared_ptr<MetadataSchema>>& schemas)
{
    auto schemaIter = metadataNode.find(ATTR_METADATA_SCHEMA);
    auto descIter = metadataNode.find(ATTR_METADATA_DESCRIPTION);
    auto idIter = metadataNode.find(ATTR_ID);
    if(schemaIter == metadataNode.end() || descIter == metadataNode.end() || idIter == metadataNode.end())
        VMF_EXCEPTION(vmf::IncorrectParamException, "Metadata item has no schema name, description name or id");

    auto schema = schemas.begin();
    for(; schema != schemas.end(); schema++)
        if((*schema)->getName() == schemaIter->as_string())
            break;

    if(schema == schemas.end())
        VMF_EXCEPTION(vmf::IncorrectParamException, "Unknown schema for metadata item");

    std::shared_ptr<MetadataDesc> spDesc;
    if( (spDesc = (*schema)->findMetadataDesc(descIter->as_string())) == nullptr)
        VMF_EXCEPTION(vmf::IncorrectParamException, "Unknown description for metadata item");

    std::shared_ptr<MetadataInternal> spMetadataInternal(new MetadataInternal(spDesc));
    spMetadataInternal->setId(idIter->as_int());

    auto frameIdxLoIter = metadataNode.find(ATTR_METADATA_FRAME_IDX_LO);
    auto frameIdxHiIter = metadataNode.find(ATTR_METADATA_FRAME_IDX_HI);
    auto nFramesLoIter = metadataNode.find(ATTR_METADATA_NFRAMES_LO);
    auto nFramesHiIter = metadataNode.find(ATTR_METADATA_NFRAMES_HI);
    auto timeLoIter = metadataNode.find(ATTR_METADATA_TIMESTAMP_LO);
    auto timeHiIter = metadataNode.find(ATTR_METADATA_TIMESTAMP_HI);
    auto durationLoIter = metadataNode.find(ATTR_METADATA_DURATION_LO);
    auto durationHiIter = metadataNode.find(ATTR_METADATA_DURATION_HI);

    if (frameIdxLoIter != metadataNode.end() && frameIdxHiIter != metadataNode.end())
    {
        unsigned long lo = frameIdxLoIter->as_int();
        unsigned long hi = frameIdxHiIter->as_int();
        long long frmIdx = ((long long)hi << 32) | lo;

        if (nFramesLoIter != metadataNode.end() && nFramesHiIter != metadataNode.end())
        {
            unsigned long lo = nFramesLoIter->as_int();
            unsigned long hi = nFramesHiIter->as_int();
            long long numFrm = ((long long)hi << 32) | lo;
            spMetadataInternal->setFrameIndex(frmIdx, numFrm);
        }
        else
            spMetadataInternal->setFrameIndex(frmIdx);
    }
    if (timeLoIter != metadataNode.end() && timeHiIter != metadataNode.end())
    {
        unsigned long lo = timeLoIter->as_int();
        unsigned long hi = timeHiIter->as_int();
        long long time = ((long long)hi << 32) | lo;

        if (durationLoIter != metadataNode.end() && durationHiIter != metadataNode.end())
        {
            unsigned long lo = durationLoIter->as_int();
            unsigned long hi = durationHiIter->as_int();
            long long dur = ((long long)hi << 32) | lo;
            spMetadataInternal->setTimestamp(time, dur);
        }
        else
            spMetadataInternal->setTimestamp(time);
    }

    auto metadataFieldsArrayIter = metadataNode.find(TAG_FIELDS_ARRAY);
    if(metadataFieldsArrayIter == metadataNode.end())
        VMF_EXCEPTION(vmf::IncorrectParamException, "No metadata fields array");

    for(auto fieldNode = metadataFieldsArrayIter->begin(); fieldNode != metadataFieldsArrayIter->end(); fieldNode++)
    {
        FieldDesc fieldDesc;
        auto fieldNameIter = fieldNode->find(ATTR_NAME);
        auto fieldValueIter = fieldNode->find(ATTR_VALUE);
        if(fieldNameIter == fieldNode->end() || fieldValueIter == fieldNode->end() )
            VMF_EXCEPTION(vmf::IncorrectParamException, "Missing field name or field value");

        vmf::Variant field_value;
        spDesc->getFieldDesc(fieldDesc, fieldNameIter->as_string());
        field_value.fromString(fieldDesc.type, fieldValueIter->as_string());
        spMetadataInternal->setFieldValue(fieldNameIter->as_string(), field_value);
    }

    auto referencesArrayIter = metadataNode.find(TAG_METADATA_REFERENCES_ARRAY);
    if(referencesArrayIter != metadataNode.end())
    {
        for(auto referenceNode = referencesArrayIter->begin(); referenceNode != referencesArrayIter->end(); referenceNode++)
        {
            auto referenceIdIter = referenceNode->find(ATTR_ID);
            if(referenceIdIter == referenceNode->end()) VMF_EXCEPTION(vmf::IncorrectParamException, "Missing reference 'id'");

            auto referenceNameIter = referenceNode->find(ATTR_NAME);
            if(referenceNameIter == referenceNode->end()) VMF_EXCEPTION(vmf::IncorrectParamException, "Missing reference 'name'");

            spMetadataInternal->vRefs.push_back(std::make_pair(IdType(referenceIdIter->as_int()), referenceNameIter->as_string()));
        }
    }

    return spMetadataInternal;
}
예제 #9
0
inline bool jsonBoolProp(JSONNode &n, const char* prop){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_BOOL) return i->as_bool();
	else throw ErrorStringException(string("JSON missing bool property: ") + prop);
}
예제 #10
0
listener_ptr makeStreamListener(StreamingDevice* dev, ClientConn* client, JSONNode &n){
	std::auto_ptr<WSStreamListener> listener(new WSStreamListener());

	listener->id = jsonIntProp(n, "id");
	listener->device = dev;
	listener->client = client;
	
	listener->decimateFactor = jsonIntProp(n, "decimateFactor", 1);
	
	// Prevent divide by 0
	if (listener->decimateFactor == 0) listener->decimateFactor = 1; 

	int start = jsonIntProp(n, "start", -1);
	if (start < 0){ // Negative indexes are relative to latest sample
		start = (dev->buffer_max()) + start + 1;
	}

	if (start < 0) listener->index = 0;
	else listener->index = start;
	
	listener->count = jsonIntProp(n, "count");
	
	JSONNode j_streams = n.at("streams");
	for(JSONNode::iterator i=j_streams.begin(); i!=j_streams.end(); i++){
		listener->streams.push_back(
			dev->findStream(
				jsonStringProp(*i, "channel"),
				jsonStringProp(*i, "stream")));
	}
	
	JSONNode::iterator t = n.find("trigger");
	if (t != n.end() && (t->type()) == JSON_NODE){
		JSONNode &trigger = *t;
		
		string type = jsonStringProp(trigger, "type", "in");
		if (type == "in"){
			listener->triggerType = INSTREAM;
			listener->triggerLevel = jsonFloatProp(trigger, "level");
			listener->triggerStream = dev->findStream(
				jsonStringProp(trigger, "channel"),
				jsonStringProp(trigger, "stream"));
		}else if (type == "out"){
			listener->triggerType = OUTSOURCE;
			listener->triggerChannel = dev->channelById(jsonStringProp(trigger, "channel"));
			if (!listener->triggerChannel) throw ErrorStringException("Trigger channel not found");
		}else{
			throw ErrorStringException("Invalid trigger type");
		}
			
		listener->triggerRepeat = jsonBoolProp(trigger, "repeat", true);
		listener->triggerHoldoff = jsonIntProp(trigger, "holdoff", 0);
		listener->triggerOffset = jsonIntProp(trigger, "offset", 0);
		
		if (listener->triggerOffset<0 && -listener->triggerOffset >= listener->triggerHoldoff){
			// Prevent big negative offsets that could cause infinite loops
			listener->triggerHoldoff = -listener->triggerOffset;
		}
		listener->triggerForce = jsonIntProp(trigger, "force", 0);
		listener->triggerForceIndex = listener->index + listener->triggerForce;
	}
		
		
	return listener_ptr(listener.release());
}
예제 #11
0
inline double jsonFloatProp(JSONNode &n, const char* prop, double def){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_NUMBER) return i->as_float();
	else return def;
}
예제 #12
0
inline bool jsonBoolProp(JSONNode &n, const char* prop, bool def){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_BOOL) return i->as_bool();
	else return def;
}
예제 #13
0
inline int jsonIntProp(JSONNode &n, const char* prop, int def){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_NUMBER) return i->as_int();
	else return def;
}
예제 #14
0
inline string jsonStringProp(JSONNode &n, const char* prop, string def){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_STRING) return i->as_string();
	else return def;
}
예제 #15
0
inline double jsonFloatProp(JSONNode &n, const char* prop){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_NUMBER) return i->as_float();
	else throw ErrorStringException(string("JSON missing float property: ") + prop);
}
예제 #16
0
void Rig::loadPatches(JSONNode root) {
  JSONNode::iterator i = root.begin();

  // for this we want to iterate through all children and have the device class
  // parse the sub-element.
  while (i != root.end()){
	if (i->name() == "jsonPath") {
		i++;
		continue;
	}

    // get the node name and value as a string
    std::string nodeName = i->name();

    stringstream ss;
    ss << "Loading patch " << nodeName;
    Logger::log(INFO, ss.str());

    Patch* patch;
    
    auto type = i->find("type");
    if (type == i->end()) {
      stringstream ss;
      ss << "Unable to determine Patch type for " << nodeName << ". Patch not loaded.";
      Logger::log(WARN, ss.str());
    }

    string patchType = type->as_string();

    // New patch types will need new seralization definitions.
    if (patchType == "DMXPatch") {
      patch = (Patch*) new DMXPatch(*i);
      addPatch(nodeName, patch);
    }
#ifdef USE_ARNOLD
	else if (patchType == "PhotoPatch") {
		patch = (Patch*) new PhotoPatch(*i);
		addPatch(nodeName, patch);

		Device::DeviceCallbackFunction callback = std::bind(&PhotoPatch::onDeviceChanged,
			(PhotoPatch*)patch,
			std::placeholders::_1);
		for (Device *d : getDeviceRaw()) {
			d->addParameterChangedCallback(callback);
			d->addMetadataChangedCallback(callback);
		}
	}
	else if (patchType == "PhotoAnimationPatch") {
		patch = (Patch*) new PhotoAnimationPatch(*i);
		addPatch(nodeName, patch);

		Device::DeviceCallbackFunction callback = std::bind(&PhotoAnimationPatch::onDeviceChanged,
			(PhotoAnimationPatch*)patch,
			std::placeholders::_1);
		for (Device *d : getDeviceRaw()) {
			d->addParameterChangedCallback(callback);
			d->addMetadataChangedCallback(callback);
		}
	}
    else if (patchType == "ArnoldAnimationPatch") {
	  i->push_back(*root.find("jsonPath"));
      patch = (Patch*) new ArnoldAnimationPatch(*i);
      addPatch(nodeName, patch);
      Device::DeviceCallbackFunction callback = std::bind(&ArnoldAnimationPatch::onDeviceChanged,
                                                            (ArnoldAnimationPatch*)patch,
                                                            std::placeholders::_1);
      for (Device *d : getDeviceRaw()) {
          d->addParameterChangedCallback(callback);
          d->addMetadataChangedCallback(callback);
      }
    }
    else if (patchType == "ArnoldPatch") {
	  i->push_back(*i->find("jsonPath"));
      patch = (Patch*) new ArnoldPatch(*i);
      addPatch(nodeName, patch);
        
        Device::DeviceCallbackFunction callback = std::bind(&ArnoldPatch::onDeviceChanged,
                                                            (ArnoldPatch*)patch,
                                                            std::placeholders::_1);
        for (Device *d : getDeviceRaw()) {
            d->addParameterChangedCallback(callback);
            d->addMetadataChangedCallback(callback);
        }
    }
#endif
    else {
      stringstream ss;
      ss << "Unknown Patch type " << patchType << " in Patch ID " << nodeName << "Patch not loaded.";
      Logger::log(WARN, ss.str());
    }

    //increment the iterator
    ++i;
  }
}
예제 #17
0
void xmm::TrainingSet::from_json(JSONNode root)
{
    if (!owns_data)
        throw std::runtime_error("Cannot read Training Set with Shared memory");
    
    try {
        if (root.type() != JSON_NODE)
            throw JSONException("Wrong type: was expecting 'JSON_NODE'", root.name());
        JSONNode::const_iterator root_it = root.begin();
        
        // Get Number of modalities
        root_it = root.find("bimodal");
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->type() != JSON_BOOL)
            throw JSONException("Wrong type for node 'bimodal': was expecting 'JSON_BOOL'", root_it->name());
        if(bimodal_ != root_it->as_bool()) {
            if (bimodal_)
                throw JSONException("Trying to read an unimodal model in a bimodal model.", root.name());
            else
                throw JSONException("Trying to read a bimodal model in an unimodal model.", root.name());
        }
        
        // Get Dimension
        root_it = root.find("dimension");
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->type() != JSON_NUMBER)
            throw JSONException("Wrong type for node 'dimension': was expecting 'JSON_NUMBER'", root_it->name());
        dimension_ = static_cast<unsigned int>(root_it->as_int());
        
        // Get Input Dimension if bimodal_
        if (bimodal_){
            root_it = root.find("dimension_input");
            if (root_it == root.end())
                throw JSONException("JSON Node is incomplete", root_it->name());
            if (root_it->type() != JSON_NUMBER)
                throw JSONException("Wrong type for node 'dimension_input': was expecting 'JSON_NUMBER'", root_it->name());
            dimension_input_ = static_cast<unsigned int>(root_it->as_int());
        }
        
        // Get Column Names
        column_names_.assign(dimension_, "");
        root_it = root.find("column_names");
        if (root_it != root.end()) {
            if (root_it->type() != JSON_ARRAY)
                throw JSONException("Wrong type for node 'column_names': was expecting 'JSON_ARRAY'", root_it->name());
            json2vector(*root_it, column_names_, dimension_);
        }
        
        // Get Size: Number of Phrases
        root_it = root.find("size");
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->type() != JSON_NUMBER)
            throw JSONException("Wrong type for node 'size': was expecting 'JSON_NUMBER'", root_it->name());
        unsigned int ts_size = static_cast<unsigned int>(root_it->as_int());
        
        // Get Default label
        root_it = root.find("defaultlabel");
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->type() != JSON_NODE)
            throw JSONException("Wrong type for node 'defaultlabel': was expecting 'JSON_NODE'", root_it->name());
        defaultLabel_.from_json(*root_it);
        
        // Get Phrases
        phrases.clear();
        phraseLabels.clear();
        root_it = root.find("phrases");
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->type() != JSON_ARRAY)
            throw JSONException("Wrong type for node 'phrases': was expecting 'JSON_ARRAY'", root_it->name());
        for (unsigned int i=0 ; i<ts_size ; i++)
        {
            JSONNode::const_iterator array_it = (*root_it)[i].end();
            // Get Index
            array_it = (*root_it)[i].find("index");
            if (array_it == (*root_it)[i].end())
                throw JSONException("JSON Node is incomplete", array_it->name());
            if (array_it->type() != JSON_NUMBER)
                throw JSONException("Wrong type for node 'index': was expecting 'JSON_NUMBER'", array_it->name());
            unsigned int phraseIndex = static_cast<unsigned int>(array_it->as_int());
            
            // Get Label
            array_it = (*root_it)[i].find("label");
            if (array_it == (*root_it)[i].end())
                throw JSONException("JSON Node is incomplete", array_it->name());
            if (array_it->type() != JSON_NODE)
                throw JSONException("Wrong type for node 'label': was expecting 'JSON_NODE'", array_it->name());
            phraseLabels[phraseIndex].from_json(*array_it);
            updateLabelList();
            
            // Get Phrase Content
            array_it = (*root_it)[i].find("Phrase");
            if (array_it == (*root_it)[i].end())
                throw JSONException("JSON Node is incomplete", array_it->name());
            if (array_it->type() != JSON_NODE)
                throw JSONException("Wrong type for node 'Phrase': was expecting 'JSON_NODE'", array_it->name());
            phrases[phraseIndex] = new Phrase(flags_, dimension_, dimension_input_);
            phrases[phraseIndex]->from_json(*array_it);
        }
        
        if (ts_size != phrases.size())
            throw JSONException("Number of phrases does not match", root_it->name());
        has_changed_ = true;
        
    } catch (JSONException &e) {
        throw JSONException(e, root.name());
    } catch (std::exception &e) {
        throw JSONException(e, root.name());
    }
}
예제 #18
0
inline string jsonStringProp(JSONNode &n, const char* prop){
	JSONNode::iterator i = n.find(prop);
	if (i != n.end() && i->type() == JSON_STRING) return i->as_string();
	else throw ErrorStringException(string("JSON missing string property: ") + prop);
}