コード例 #1
0
 SimpleAutomaton* SimpleAutomatonParser::parse() {
     Token token;
     expect(SimpleAutomatonToken::Automaton | SimpleAutomatonToken::Eof, token = m_tokenizer.nextToken());
     if (token.type() == SimpleAutomatonToken::Eof)
         return NULL;
     
     SimpleAutomaton* automaton = new SimpleAutomaton();
     try {
         const SimpleAutomatonToken::Type allowed = SimpleAutomatonToken::States | SimpleAutomatonToken::Transition | SimpleAutomatonToken::InitialState |SimpleAutomatonToken::FinalStates | SimpleAutomatonToken::Eof;
         expect(allowed, token = m_tokenizer.nextToken());
         
         while (token.type() != SimpleAutomatonToken::Eof) {
             if (token.type() == SimpleAutomatonToken::States)
                 parseStates(*automaton);
             else if (token.type() == SimpleAutomatonToken::Transition)
                 parseTransition(*automaton);
             else if (token.type() == SimpleAutomatonToken::InitialState)
                 parseInitialState(*automaton);
             else if (token.type() == SimpleAutomatonToken::FinalStates)
                 parseFinalStates(*automaton);
             expect(allowed, token = m_tokenizer.nextToken());
         }
     } catch (...) {
         delete automaton;
         throw;
     }
     
     return automaton;
 }
コード例 #2
0
	void* NclTransitionParser::parseTransitionBase(
		    DOMElement* parentElement, void* objGrandParent) {

		wclog << "parseTransitionBase" << endl;
		void *parentObject;
		DOMNodeList *elementNodeList;
		DOMElement *element;
		DOMNode *node;
		string elementTagName;
		void *elementObject;

		parentObject = createTransitionBase(parentElement, objGrandParent);
		if (parentObject == NULL) {
			return NULL;
		}

		elementNodeList = parentElement->getChildNodes();
		for (int i = 0; i < (int)elementNodeList->getLength(); i++) {
			node = elementNodeList->item(i);
			if (node->getNodeType()==DOMNode::ELEMENT_NODE) {
				element = (DOMElement*)node;
				elementTagName = XMLString::transcode(element->getTagName());

				if (XMLString::compareIString(
					    elementTagName.c_str(), "importBase") == 0) {

					elementObject = getImportParser()->parseImportBase(
						    element, parentObject);

					if (elementObject != NULL) {
						addImportBaseToTransitionBase(
							    parentObject, elementObject);
					}

				} else if (XMLString::compareIString(
					    elementTagName.c_str(), "transition") == 0) {

					elementObject = parseTransition(element, parentObject);
					if (elementObject != NULL) {
						addTransitionToTransitionBase(
							    parentObject, elementObject);
					}
				}
			}
		}

		return parentObject;
	}
コード例 #3
0
ファイル: xmlengine.cpp プロジェクト: Romanow/petri-nets
bool XMLEngine::parseTransitions(QXmlStreamReader * reader, StateList * states, TransitionList * transitions)
{
	bool result = true;
	if (reader->readNextStartElement() && compare(reader->name(), "transitions"))
	{
		while (reader->readNextStartElement() && compare(reader->name(), "transition"))
		{
			QString id = reader->attributes().value("id").toString();
			Transition * transition = transitions->find(id).first();
			if (transition != 0)
				result &= parseTransition(reader, transition, states);
			else
				result = false;
		}
	}

	return result & !reader->hasError();
}
コード例 #4
0
ファイル: FSMDescrip.cpp プロジェクト: MengeCrowdSim/Menge
bool FSMDescrip::loadFromXML(const std::string& xmlName, bool verbose) {
  logger << Logger::INFO_MSG << "Loading behavior from xml: " << xmlName;
  TiXmlDocument xml(xmlName);
  bool loadOkay = xml.LoadFile();

  if (!loadOkay) {
    logger << Logger::ERR_MSG << "Could not load behavior configuration xml (";
    logger << xmlName << ") due to xml syntax errors.\n";
    logger << "\t" << xml.ErrorDesc();
    return false;
  }

  TiXmlElement* popNode = xml.RootElement();
  if (!popNode) {
    logger << Logger::ERR_MSG << "Root element does not exist.";
    return false;
  }
  if (popNode->ValueStr() != "BFSM") {
    logger << Logger::ERR_MSG << "Root element value should be \"BFSM\".";
    return false;
  }

  std::string absPath;
  os::path::absPath(xmlName, absPath);
  std::string junk;
  os::path::split(absPath, _behaviorFldr, junk);
  logger << Logger::INFO_MSG << "Behavior root: " << _behaviorFldr;

  TiXmlElement* child;
  for (child = popNode->FirstChildElement(); child; child = child->NextSiblingElement()) {
    if (child->ValueStr() == "GoalSet") {
      int i;
      if (!child->Attribute("id", &i)) {
        logger << Logger::ERR_MSG << "GoalSet requires an \"id\" property.";
        return false;
      }
      size_t setID = static_cast<size_t>(i);
      // confirm that the id doesn't already exist
      if (_goalSets.find(setID) != _goalSets.end()) {
        logger << Logger::WARN_MSG << "Found multiple GoalSets with the same id: ";
        logger << setID << ".\n\tGoal definitions will be merged!";
      } else {
        _goalSets[setID] = new GoalSet();
      }
      TiXmlElement* goalNode;
      for (goalNode = child->FirstChildElement(); goalNode;
           goalNode = goalNode->NextSiblingElement()) {
        if (goalNode->ValueStr() == "Goal") {
          Goal* goal = parseGoal(goalNode, _behaviorFldr);
          if (goal == 0x0) {
            logger << Logger::ERR_MSG << "Error parsing a goal description.";
            return false;
          }
          // Make sure that this goal doesn't duplicate previous goal ids
          if (!_goalSets[setID]->addGoal(goal->getID(), goal)) {
            logger << Logger::ERR_MSG << "GoalSet " << setID;
            logger << " has two goals with the identifier: " << goal->getID();
            logger << " (second appears on line " << goalNode->Row() << ").";
            return false;
          }
        } else {
          logger << Logger::WARN_MSG
                 << "Found a child tag of the GoalSet that "
                    "is not a \"Goal\" tag on line "
                 << goalNode->Row()
                 << ". "
                    "It will be ignored.";
        }
      }

    } else if (child->ValueStr() == "State") {
      if (!parseState(child, _behaviorFldr, _states)) {
        return false;
      }
    } else if (child->ValueStr() == "Transition") {
      std::string from;
      Transition* trans = parseTransition(child, _behaviorFldr, from);
      if (trans == 0x0) {
        return false;
      }

      addTransition(from, trans);
    } else if (child->ValueStr() == "VelModifier") {
      VelModifier* vel = parseVelModifier(child, _behaviorFldr);
      if (vel == 0x0) {
        return false;
      } else {
        _velModifiers.push_back(vel);
      }
    } else if (child->ValueStr() == "Task") {
      Task* task = parseTask(child, _behaviorFldr);
      if (task == 0x0) {
        logger << Logger::WARN_MSG << "User-specified Task on line ";
        logger << child->Row()
               << " couldn't be instantiated.  "
                  "It is being ignored.";
      } else {
        _tasks.push_back(task);
      }
    } else if (child->ValueStr() == "EventSystem") {
      if (!EVENT_SYSTEM->parseEvents(child, _behaviorFldr)) {
        return false;
      }
    } else {
      logger << Logger::ERR_MSG << "Unrecognized tag as child of <Population>: <";
      logger << child->ValueStr() << ">.";
      return false;
    }
  }

  return true;
}
コード例 #5
0
ファイル: parser.cpp プロジェクト: iosphere/mapbox-gl-native
StyleParseResult Parser::parse(const std::string& json) {
    rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document;
    document.Parse<0>(json.c_str());

    if (document.HasParseError()) {
        std::stringstream message;
        message <<  document.GetErrorOffset() << " - "
            << rapidjson::GetParseError_En(document.GetParseError());

        return std::make_exception_ptr(std::runtime_error(message.str()));
    }

    if (!document.IsObject()) {
        return std::make_exception_ptr(std::runtime_error("style must be an object"));
    }

    if (document.HasMember("version")) {
        const JSValue& versionValue = document["version"];
        const int version = versionValue.IsNumber() ? versionValue.GetInt() : 0;
        if (version != 8) {
            Log::Warning(Event::ParseStyle, "current renderer implementation only supports style spec version 8; using an outdated style will cause rendering errors");
        }
    }

    if (document.HasMember("name")) {
        const JSValue& value = document["name"];
        if (value.IsString()) {
            name = { value.GetString(), value.GetStringLength() };
        }
    }

    if (document.HasMember("center")) {
        const JSValue& value = document["center"];
        conversion::Error error;
        auto convertedLatLng = conversion::convert<LatLng>(value, error);
        if (convertedLatLng) {
            latLng = *convertedLatLng;
        } else {
            Log::Warning(Event::ParseStyle, "center coordinate must be a longitude, latitude pair");
        }
    }

    if (document.HasMember("zoom")) {
        const JSValue& value = document["zoom"];
        if (value.IsNumber()) {
            zoom = value.GetDouble();
        }
    }

    if (document.HasMember("bearing")) {
        const JSValue& value = document["bearing"];
        if (value.IsNumber()) {
            bearing = value.GetDouble();
        }
    }

    if (document.HasMember("pitch")) {
        const JSValue& value = document["pitch"];
        if (value.IsNumber()) {
            pitch = value.GetDouble();
        }
    }

    if (document.HasMember("transition")) {
        parseTransition(document["transition"]);
    }

    if (document.HasMember("light")) {
        parseLight(document["light"]);
    }

    if (document.HasMember("sources")) {
        parseSources(document["sources"]);
    }

    if (document.HasMember("layers")) {
        parseLayers(document["layers"]);
    }

    if (document.HasMember("sprite")) {
        const JSValue& sprite = document["sprite"];
        if (sprite.IsString()) {
            spriteURL = { sprite.GetString(), sprite.GetStringLength() };
        }
    }

    if (document.HasMember("glyphs")) {
        const JSValue& glyphs = document["glyphs"];
        if (glyphs.IsString()) {
            glyphURL = { glyphs.GetString(), glyphs.GetStringLength() };
        }
    }

    return nullptr;
}