Beispiel #1
0
void Callback(JSONNode & test, void * ide){
	assertEquals(ide, (void*)0xDEADBEEF);
    ++counter;
    switch(counter){
	   case 1:
		  assertEquals(test.type(), JSON_NODE);
		  assertTrue(test.empty());
		  break;
	   case 2:
		  assertEquals(test.type(), JSON_ARRAY);
		  assertTrue(test.empty());
		  break;
	   case 3:
		  assertEquals(test.type(), JSON_NODE);
		  assertEquals(test.size(), 1);
		  assertEquals(test[0].name(), JSON_TEXT("hello"));
		  assertEquals(test[0].as_int(), 1);
		  break;
	   case 4:
		  assertEquals(test.type(), JSON_ARRAY);
		  assertEquals(test.size(), 3);
		  break;
	   case 5:
		  assertEquals(test.type(), JSON_NODE);
		  assertEquals(test.size(), 1);
		  assertEquals(test[0].name(), JSON_TEXT("hi"));
		  assertEquals(test[0].size(), 1)
		  assertEquals(test[0][0].type(), JSON_NUMBER);
		  assertEquals(test[0][0].name(), JSON_TEXT("one"));
		  assertEquals(test[0][0].as_int(), 1);
		  break;
    }
}
void write_formatted(JSONNode n, const char *filename){
  if(n.type()!=JSON_NULL){
    std::string jc = n.write_formatted();
    std::ofstream out(filename);
    out << jc;
    out.close();
  }
}
Beispiel #3
0
bool USB_device::processMessage(ClientConn& client, string& cmd, JSONNode& n){
	if (cmd == "controlTransfer"){
		unsigned id = jsonIntProp(n, "id", 0);
		uint8_t bmRequestType = jsonIntProp(n, "bmRequestType", 0xC0);
		uint8_t bRequest = jsonIntProp(n, "bRequest");
		uint16_t wValue = jsonIntProp(n, "wValue", 0);
		uint16_t wIndex = jsonIntProp(n, "wIndex", 0);
	
		bool isIn = bmRequestType & 0x80;
		
		JSONNode reply(JSON_NODE);
		reply.push_back(JSONNode("_action", "return"));
		reply.push_back(JSONNode("id", id));
		
		int ret = -1000;
		
		if (isIn){
			uint16_t wLength = jsonIntProp(n, "wLength", 64);
			if (wLength > 64) wLength = 64;
			if (wLength < 0) wLength = 0;
		
			uint8_t data[wLength];
			ret = controlTransfer(bmRequestType, bRequest, wValue, wIndex, data, wLength);
			
			if (ret >= 0){
				JSONNode data_arr(JSON_ARRAY);
				for (int i=0; i<ret && i<wLength; i++){
					data_arr.push_back(JSONNode("", data[i]));
				}
				data_arr.set_name("data");
				reply.push_back(data_arr);
			}
		}else{
			string datastr;
			JSONNode data = n.at("data");
			if (data.type() == JSON_ARRAY){
				for(JSONNode::iterator i=data.begin(); i!=data.end(); i++){
					datastr.push_back(i->as_int());
				}
			}else{
				datastr = data.as_string();
			}
			ret = controlTransfer(bmRequestType, bRequest, wValue, wIndex, (uint8_t *)datastr.data(), datastr.size());
		}
		
		reply.push_back(JSONNode("status", ret));
	
		client.sendJSON(reply);
	}else if(cmd == "enterBootloader"){
		std::cout << "enterBootloader: ";
		int r = controlTransfer(0x40|0x80, 0xBB, 0, 0, NULL, 100);
		std::cout << "return " <<  r << std::endl;
	}else{
		return false;
	}
	return true;
}
Beispiel #4
0
HQLOperand::HQLOperand(const JSONNode &n, OPERAND_TYPE ot)
{
    // ot == ID -> auto
    switch(n.type()){
    case JSON_NULL:
        type = NIL;
        break;
    case JSON_STRING:
        {
            if(ot == ID){
                type = ID;
                data.str = new string(n.as_string());
            }else{
                type = STRING;
                data.str = new string(string("\"") + n.as_string() + "\"");
            }
            break;
        }
    case JSON_NUMBER:
        {
            type = NUM;
            data.num = n.as_float();
            break;
        }
    case JSON_BOOL:
        {
            type = BOOL;
            data.boolean = n.as_bool();
            break;
        }
    case JSON_ARRAY:
    case JSON_NODE:
        {
            //TODO
            break;
        }
    default:
        type = NIL;
    }
}
Beispiel #5
0
void xmm::Label::from_json(JSONNode root)
{
    try {
        if (root.type() != JSON_NODE)
            throw JSONException("Wrong type: was expecting 'JSON_NODE'", root.name());
        JSONNode::const_iterator root_it = root.begin();
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->name() != "type")
            throw JSONException("Wrong name: was expecting 'type'", root_it->name());
        if (root_it->type() != JSON_STRING)
            throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name());
        if (root_it->as_string() == "INT")
            type = INT;
        else
            type = SYM;
        ++root_it;
        
        if (root_it == root.end())
            throw JSONException("JSON Node is incomplete", root_it->name());
        if (root_it->name() != "value")
            throw JSONException("Wrong name: was expecting 'value'", root_it->name());
        if (type == INT) {
            if (root_it->type() != JSON_NUMBER)
                throw JSONException("Wrong type: was expecting 'JSON_NUMBER'", root_it->name());
            intLabel_ = static_cast<int>(root_it->as_int());
        } else {
            if (root_it->type() != JSON_STRING)
                throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name());
            symLabel_ = root_it->as_string();
        }
    } catch (JSONException &e) {
        throw JSONException(e, root.name());
    } catch (std::exception &e) {
        throw JSONException(e, root.name());
    }
}
void TestSuite::TestInspectors(void){
    UnitTest::SetPrefix("TestInspectors.cpp - Inspectors");
    JSONNode test = JSONNode(JSON_NULL);
    #ifdef JSON_CASTABLE
	   assertEquals(test.as_string(), JSON_TEXT(""));
	   assertEquals(test.as_int(), 0);
	   assertEquals(test.as_float(), 0.0f);
	   assertEquals(test.as_bool(), false);
    #endif

    test = 15.5f;
    assertEquals(test.type(), JSON_NUMBER);
  #ifdef JSON_CASTABLE
    assertEquals(test.as_string(), JSON_TEXT("15.5"));
  #endif
    assertEquals(test.as_int(), 15);
    assertEquals(test.as_float(), 15.5f);
    #ifdef JSON_CASTABLE
	   assertEquals(test.as_bool(), true);
    #endif

    test = 0.0f;
    assertEquals(test.type(), JSON_NUMBER);
  #ifdef JSON_CASTABLE
    assertEquals(test.as_string(), JSON_TEXT("0"));
  #endif
    assertEquals(test.as_int(), 0);
    assertEquals(test.as_float(), 0.0f);
    #ifdef JSON_CASTABLE
	   assertEquals(test.as_bool(), false);
    #endif

    test = true;
    assertEquals(test.type(), JSON_BOOL);
    #ifdef JSON_CASTABLE
	   assertEquals(test.as_string(), JSON_TEXT("true"));
	   assertEquals(test.as_int(), 1);
	   assertEquals(test.as_float(), 1.0f);
    #endif
    assertEquals(test.as_bool(), true);

    test = false;
    assertEquals(test.type(), JSON_BOOL);
    #ifdef JSON_CASTABLE
	   assertEquals(test.as_string(), JSON_TEXT("false"));
	   assertEquals(test.as_int(), 0);
	   assertEquals(test.as_float(), 0.0f);
    #endif
    assertEquals(test.as_bool(), false);

    #ifdef JSON_CASTABLE
	   test.cast(JSON_NODE);
    #else
	   test = JSONNode(JSON_NODE);
    #endif
    assertEquals(test.type(), JSON_NODE);
    assertEquals(test.size(), 0);
    test.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world")));
    test.push_back(JSONNode(JSON_TEXT("hello"), JSON_TEXT("mars")));
    test.push_back(JSONNode(JSON_TEXT("salut"), JSON_TEXT("france")));
    assertEquals(test.size(), 3);
    TestSuite::testParsingItself(test);

    #ifdef JSON_CASTABLE
	   JSONNode casted = test.as_array();
	   #ifdef JSON_UNIT_TEST
		  assertNotEquals(casted.internal, test.internal);
	   #endif
	   assertEquals(casted.type(), JSON_ARRAY);
	   assertEquals(test.type(), JSON_NODE);
	   assertEquals(test.size(), 3);
	   assertEquals(casted.size(), 3);
	   TestSuite::testParsingItself(casted);
    #endif

    UnitTest::SetPrefix("TestInspectors.cpp - Location");

    try {
	   #ifdef JSON_CASTABLE
		  assertEquals(casted.at(0), JSON_TEXT("world"));
		  assertEquals(casted.at(1), JSON_TEXT("mars"));
		  assertEquals(casted.at(2), JSON_TEXT("france"));
		  assertEquals(casted.at(0).name(), JSON_TEXT(""));
		  assertEquals(casted.at(1).name(), JSON_TEXT(""));
		  assertEquals(casted.at(2).name(), JSON_TEXT(""));
	   #endif
	   assertEquals(test.at(0), JSON_TEXT("world"));
	   assertEquals(test.at(1), JSON_TEXT("mars"));
	   assertEquals(test.at(2), JSON_TEXT("france"));
	   assertEquals(test.at(0).name(), JSON_TEXT("hi"));
	   assertEquals(test.at(1).name(), JSON_TEXT("hello"));
	   assertEquals(test.at(2).name(), JSON_TEXT("salut"));
    } catch (std::out_of_range){
	   FAIL("exception caught");
    }

    try {
	   assertEquals(test.at(JSON_TEXT("hi")), JSON_TEXT("world"));
	   assertEquals(test.at(JSON_TEXT("hello")), JSON_TEXT("mars"));
	   assertEquals(test.at(JSON_TEXT("salut")), JSON_TEXT("france"));
	   #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
		  assertEquals(test.at_nocase(JSON_TEXT("SALUT")), JSON_TEXT("france"));
		  assertEquals(test.at_nocase(JSON_TEXT("HELLO")), JSON_TEXT("mars"));
		  assertEquals(test.at_nocase(JSON_TEXT("HI")), JSON_TEXT("world"));
	   #endif
    } catch (std::out_of_range){
	   FAIL("exception caught");
    }

    assertException(test.at(JSON_TEXT("meh")), std::out_of_range);
    #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
	   assertException(test.at_nocase(JSON_TEXT("meh")), std::out_of_range);
    #endif

    assertEquals(test[JSON_TEXT("hi")], json_string(JSON_TEXT("world")));
    assertEquals(test[JSON_TEXT("hello")], json_string(JSON_TEXT("mars")));
    assertEquals(test[JSON_TEXT("salut")], json_string(JSON_TEXT("france")));
    assertEquals(test[0], JSON_TEXT("world"));
    assertEquals(test[1], JSON_TEXT("mars"));
    assertEquals(test[2], JSON_TEXT("france"));

    #ifdef JSON_ITERATORS
	  #ifdef JSON_CASTABLE
	   UnitTest::SetPrefix("TestInspectors.cpp - Iterators");
	   for(JSONNode::iterator it = casted.begin(), end = casted.end(); it != end; ++it){
		  assertEquals((*it).name(), JSON_TEXT(""));
	   }
	  #endif
    #endif

    #ifdef JSON_BINARY
	   UnitTest::SetPrefix("TestInspectors.cpp - Binary");
	   test.set_binary((const unsigned char *)"Hello World", 11);
	   assertEquals(test.type(), JSON_STRING);
	   assertEquals(test.as_string(), JSON_TEXT("SGVsbG8gV29ybGQ="));
	   assertEquals(test.as_binary(), "Hello World");
	   assertEquals(test.as_binary().size(), 11);

	   test = JSON_TEXT("Hello World");
	   assertEquals(test.type(), JSON_STRING);
	   assertEquals(test.as_string(), JSON_TEXT("Hello World"));
	   #ifdef JSON_SAFE
		  assertEquals(test.as_binary(), "");
	   #endif
    #endif

   #ifdef JSON_READ_PRIORITY
	  //This is a regression test for a bug in at()
	  json_string buffer(JSON_TEXT("{ \"myValue1\" : \"foo\", \"myValue2\" : \"bar\"}"));
	  JSONNode current = libjson::parse(buffer);
	  try {
		  JSONNode & value1 = current[JSON_TEXT("myValue1")];
		  assertEquals(value1.as_string(), JSON_TEXT("foo"));
		  JSONNode & value2 = current[JSON_TEXT("myValue2")];
		  assertEquals(value2.as_string(), JSON_TEXT("bar"));
	  } catch (...){
		  assertTrue(false);
	  }
  #endif
}
TEST_F(OffsetTableUnittest, TableToJSON)
{
    Savepoint sp0, sp1;
    sp0.Init("FastWavesUnittest.Divergence-in");
    sp0.AddMetainfo("LargeTimeStep", 1);
    sp0.AddMetainfo("RKStageNumber", 2);
    sp0.AddMetainfo("ldyn_bbc", false);
    sp1.Init("DycoreUnittest.DoStep-out");
    sp1.AddMetainfo("LargeTimeStep", 2);
    sp1.AddMetainfo("hd", (Real).5);

    // Just for the sake of getting some valid checksums
    double somedata[] = { 1.1, 2.2, 3.3, 4.4 };

    // Fill table
    OffsetTable table;
    ASSERT_NO_THROW(table.AddNewSavepoint(sp0, 0));
    ASSERT_NO_THROW(table.AddNewSavepoint(sp1, 1));
    ASSERT_NO_THROW(table.AddFieldRecord(sp0, "Field1",   0, computeChecksum(somedata, 4)));
    ASSERT_NO_THROW(table.AddFieldRecord(  0, "Field2",   0, computeChecksum(somedata, 8)));
    ASSERT_NO_THROW(table.AddFieldRecord(  1, "Field1", 100, computeChecksum(somedata, 12)));
    ASSERT_NO_THROW(table.AddFieldRecord(sp1, "Field2", 100, computeChecksum(somedata, 16)));

    //Generate table JSON
    JSONNode tableNode = table.TableToJSON();
    ASSERT_EQ(JSON_ARRAY, tableNode.type());
    ASSERT_EQ(2, tableNode.size());

    // Check first savepoint
    ASSERT_EQ(std::string("FastWavesUnittest.Divergence-in"), tableNode[0]["__name"].as_string());
    ASSERT_EQ(0, tableNode[0]["__id"].as_int());
    ASSERT_EQ(1, tableNode[0]["LargeTimeStep"].as_int());
    ASSERT_EQ(2, tableNode[0]["RKStageNumber"].as_int());
    ASSERT_FALSE(tableNode[0]["ldyn_bbc"].as_bool());
    ASSERT_EQ((int)JSON_ARRAY, (int)tableNode[0]["__offsets"][0].type());
    ASSERT_EQ(std::string("Field1"), tableNode[0]["__offsets"][0].name());
    ASSERT_EQ(0, tableNode[0]["__offsets"][0][0].as_int());
    ASSERT_EQ(computeChecksum(somedata, 4), tableNode[0]["__offsets"][0][1].as_string());
    ASSERT_EQ(0, tableNode[0]["__offsets"][1][0].as_int());
    ASSERT_EQ(computeChecksum(somedata, 8), tableNode[0]["__offsets"][1][1].as_string());

    // Check second savepoint
    ASSERT_EQ(std::string("DycoreUnittest.DoStep-out"), tableNode[1]["__name"].as_string());
    ASSERT_EQ(1, tableNode[1]["__id"].as_int());
    ASSERT_EQ(2, tableNode[1]["LargeTimeStep"].as_int());
    ASSERT_EQ(0.5, tableNode[1]["hd"].as_float());
    ASSERT_EQ((int)JSON_ARRAY, (int)tableNode[1]["__offsets"][0].type());
    ASSERT_EQ(std::string("Field1"), tableNode[1]["__offsets"][0].name());
    ASSERT_EQ(100, tableNode[1]["__offsets"][0][0].as_int());
    ASSERT_EQ(computeChecksum(somedata, 12), tableNode[1]["__offsets"][0][1].as_string());
    ASSERT_EQ(100, tableNode[1]["__offsets"][1][0].as_int());
    ASSERT_EQ(computeChecksum(somedata, 16), tableNode[1]["__offsets"][1][1].as_string());

    // Interpret JSON for table
    OffsetTable table2;
    table2.TableFromJSON(tableNode);

    // Check savepoints
    std::vector<Savepoint> const & sp = GetSavepoints(table2);
    ASSERT_EQ(2, sp.size());
    ASSERT_EQ(sp0, sp[0]);
    ASSERT_EQ(sp1, sp[1]);
    ASSERT_EQ(0, table2.GetSavepointID(sp[0]));
    ASSERT_EQ(1, table2.GetSavepointID(sp[1]));

    // Check methods
    ASSERT_EQ(  0, table2.GetOffset(sp0, "Field1"));
    ASSERT_EQ(  0, table2.GetOffset(sp0, "Field2"));
    ASSERT_EQ(100, table2.GetOffset(sp1, "Field1"));
    ASSERT_EQ(100, table2.GetOffset(sp1, "Field2"));
    ASSERT_EQ(  0, table2.AlreadySerialized("Field1", computeChecksum(somedata, 4)));
    ASSERT_EQ(100, table2.AlreadySerialized("Field1", computeChecksum(somedata, 12)));
    ASSERT_EQ(  0, table2.AlreadySerialized("Field2", computeChecksum(somedata, 8)));
    ASSERT_EQ(100, table2.AlreadySerialized("Field2", computeChecksum(somedata, 16)));
    ASSERT_EQ( -1, table2.AlreadySerialized("Field1", computeChecksum(somedata, 8)));
    ASSERT_EQ( -1, table2.AlreadySerialized("Field2", computeChecksum(somedata, 4)));
}
TEST_F(FieldsTableUnittest, Serilalize)
{
    // Filling some info
    DataFieldInfo info1, info2;

    info1.Init(
            "Field1", "double", 8, 3,
            38, 24, 60, 1,
            3, 3, 3, 3, 0, 0, 0, 0);
    info1.AddMetainfo("ADV", true);

    info2.Init(
            "Field2", "double", 8, 3,
            38, 24, 61, 1,
            3, 3, 3, 3, 0, 1, 0, 0);
    info2.AddMetainfo("Init", 7.4);

    // Registering fields into table
    table.RegisterField(info1);
    table.RegisterField(info2);

    // Checking table
    ASSERT_EQ(2, getTableSize());
    ASSERT_TRUE(table.HasField("Field1"));
    ASSERT_TRUE(table.HasField("Field2"));
    ASSERT_FALSE(table.HasField("Field3"));

    ASSERT_TRUE(info1 == table.Find("Field1"));
    ASSERT_TRUE(info2 == table.Find("Field2"));

    JSONNode node = table.TableToJSON();

    ASSERT_EQ(node.name(), "FieldsTable");
    ASSERT_EQ(node.type(), JSON_ARRAY);
    ASSERT_EQ(node.size(), 2);

    // Check first field info
    ASSERT_EQ(node[0].name(), "DataFieldInfo");
    ASSERT_EQ(node[0]["__name"].as_string(), "Field1");
    ASSERT_EQ(node[0]["__id"].as_int(), 0);
    ASSERT_EQ(node[0]["__isize"].as_int(), 38);
    ASSERT_EQ(node[0]["__jsize"].as_int(), 24);
    ASSERT_EQ(node[0]["__ksize"].as_int(), 60);
    ASSERT_EQ(node[0]["__iminushalosize"].as_int(), 3);
    ASSERT_EQ(node[0]["__iplushalosize"].as_int(), 3);
    ASSERT_EQ(node[0]["__jminushalosize"].as_int(), 3);
    ASSERT_EQ(node[0]["__jplushalosize"].as_int(), 3);
    ASSERT_EQ(node[0]["__kminushalosize"].as_int(), 0);
    ASSERT_EQ(node[0]["__kplushalosize"].as_int(), 0);
    ASSERT_EQ(node[0]["ADV"].as_bool(), true);

    // Check second field info
    ASSERT_EQ(node[1].name(), "DataFieldInfo");
    ASSERT_EQ(node[1]["__name"].as_string(), "Field2");
    ASSERT_EQ(node[1]["__id"].as_int(), 1);
    ASSERT_EQ(node[1]["__isize"].as_int(), 38);
    ASSERT_EQ(node[1]["__jsize"].as_int(), 24);
    ASSERT_EQ(node[1]["__ksize"].as_int(), 61);
    ASSERT_EQ(node[1]["__iminushalosize"].as_int(), 3);
    ASSERT_EQ(node[1]["__iplushalosize"].as_int(), 3);
    ASSERT_EQ(node[1]["__jminushalosize"].as_int(), 3);
    ASSERT_EQ(node[1]["__jplushalosize"].as_int(), 3);
    ASSERT_EQ(node[1]["__kminushalosize"].as_int(), 0);
    ASSERT_EQ(node[1]["__kplushalosize"].as_int(), 1);
    ASSERT_EQ(node[1]["Init"].as_float(), 7.4);
}
Beispiel #9
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());
    }
}
Beispiel #10
0
bool HQLOperand::predicate(const HQLOperand &o, const JSONNode &n)
{
    if(type != COOP){
        return false;
    }

    switch(data.coop){
    case COND_OPER::EQ:
        return CompareHQLOperandAndJSONNode<COND_OPER::EQ>(o, n);
        break;
    case COND_OPER::GT:
        return CompareHQLOperandAndJSONNode<COND_OPER::GT>(o, n);
        break;
    case COND_OPER::LT:
        return CompareHQLOperandAndJSONNode<COND_OPER::LT>(o, n);
        break;
    case COND_OPER::GE:
        return CompareHQLOperandAndJSONNode<COND_OPER::GE>(o, n);
        break;
    case COND_OPER::LE:
        return CompareHQLOperandAndJSONNode<COND_OPER::LE>(o, n);
        break;
    case COND_OPER::IN:
        {
            if(o.get_type()==NUM_ARRAY){
                if(n.type()==JSON_NUMBER){
                    string ns=num2str(n.as_int());
                    vector<string>::const_iterator it=o.as_str_array()->begin();
                    for(;it!=o.as_str_array()->end();it++){
                        if(ns==*it)return true;
                    }
                    return false;
                }
                return false;
            }else if(o.get_type()==STR_ARRAY){
                if(n.type()==JSON_STRING){
                    string ns=n.as_string();
                    vector<string>::const_iterator it=o.as_str_array()->begin();
                    for(;it!=o.as_str_array()->end();it++){
                        if(ns==*it)return true;
                    }
                    return false;
                }
                return false;
            }
            return false;
            break;
        }
    case COND_OPER::CONTAINS:
        {
            string val = o.as_str_array()->at(0);
            string sep = o.as_str_array()->at(1);
            string ns=n.as_string();
            string unit;
            string::size_type start = 0, end = 0;
            while(end!=string::npos){
                end = ns.find_first_of(sep, start);
                if(end==string::npos){
                    unit = ns.substr(start);
                }else{
                    unit = ns.substr(start, end-start);
                }
                start = end + 1;
                if(val == "\"" + unit + "\""){
                    return true;
                }
            }
            return false;
            break;
        }
    case COND_OPER::TIME_IN:
        {
            if(n.type()!=JSON_NUMBER) return false;
            if(o.get_type()!=NUM) return false;
            time_t now = time(NULL);
            return now-o.as_num() <= n.as_float();
            break;
        }
    default:
        return false;
    }
    return false;
}
Beispiel #11
0
const map<uint64_t, set<string> > SLFKCondNode::match(const Model &m, ModelGetter *getter)
{
    map<uint64_t, set<string> > ret;
    if(m.attr<bool>("deleted")) return ret;
    //uint64_t fn = m.fullname();
    uint64_t gn = m.attr<uint64_t>(*operands[1].as_str());
    string type = m.type();

    string attr = *operands[3].as_str();
    JSONNode v = m.attr<JSONNode>(attr);
    if(v.type()==JSON_NODE && v.size()<=0){
        return ret;
    }

    if(!TypeConfig::is_subtype(type, *operands[0].as_str())){
        // this will not happens
        return ret;
    }

    if(TypeConfig::type_id(*operands[0].as_str())==0){
        gn = FULLNAME_SET_CAT(gn);
    }

    if(this->has_semantic_each()){
        switch(operands[4].as_coop_type()){
        case COND_OPER::EQ:
            {
                HQLOperand o = HQLOperand(v);
                SLFKCondNode n = SLFKCondNode(type,
                                              *operands[1].as_str(),
                                              etype,
                                              attr,
                                              operands[4],
                                              o);
                ret[gn].insert(n.cache_key());
                break;
            }
        case COND_OPER::CONTAINS:
            {
                string sep = operands[5].as_str_array()->at(1);
                string val = v.as_string();
                string unit;
                string::size_type start = 0, end = 0;
                while(end!=string::npos){
                    end = val.find_first_of(sep, start);
                    if(end==string::npos){
                        unit = val.substr(start);
                    }else{
                        unit = val.substr(start, end-start);
                    }
                    start = end + 1;
                    if(unit.size()<1) continue;
                    vector<string> *arg = new vector<string>;//({unit, sep});
                    arg->push_back("\"" + unit + "\"");
                    arg->push_back(sep);
                    HQLOperand o = HQLOperand(arg, HQLOperand::CONTAINS_ARG);

                    SLFKCondNode n = SLFKCondNode(type,
                                                  *operands[1].as_str(),
                                                  etype,
                                                  attr,
                                                  operands[4],
                                                  o);
                    ret[gn].insert(n.cache_key());
                }
                break;
            }
        default:
            {
                //this will not happens
                return ret;
            }
        }
    }else{
        if(operands[4].predicate(operands[5], v)){
            ret[gn].insert(this->cache_key());
        }
    }

    return ret;
}