コード例 #1
0
ファイル: simulation_maker.cpp プロジェクト: HawkLane/UniSim
Simulation* SimulationMaker::parse(QString fileName_)
{
	QString simName, simVersion;

    redirectedParameters.clear();
    outputVariableParam.clear();
    outputParameterParam.clear();
    outputDataParam.clear();

    emit beginExpansion();
    fileName = compileToFile(fileName_);
    emit endExpansion();

    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) throw Exception(message("Cannot open file: '"+fileName+"' for reading."));
	reader->setDevice(&file);

    if (!nextElementDelim()) throw Exception(message("File is not in valid XML format"));
    if (elementNameNotEquals("simulation")) throw Exception(message("Root element must be 'simulation'"));

    simName = attributeValue("name", "anonymous");
    simVersion = attributeValue("version", "1.0");
	
	Simulation *sim = new Simulation(simName, simVersion);
    UniSim::setSimulationObject(sim);

	nextElementDelim();
    int iCon=0, iMod=0, iOut=0;
    while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("integrator") || elementNameEquals("controller")) {
			if (readIntegratorElement(sim)) ++iCon;
        } else if (elementNameEquals("model")) {
			if (readModelElement(sim)) ++iMod;
        } else if (elementNameEquals("output")) {
			if (readOutputElement(sim)) ++iOut;
		} else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"), sim);


		}
	}
	Q_ASSERT(reader->isEndElement());
    if (reader->hasError()) throw Exception(message(""));
    if (iCon==0) throw Exception(message("Missing 'integrator' element in 'simulation'"));
    else if (iCon>1) throw Exception(message("Only one 'integrator' element allowed in 'simulation'"));
    if (iMod==0) throw Exception(message("Missing 'model' element in 'simulation'"));
    if (iOut==0) throw Exception(message("Missing 'output' element in 'simulation'"));

    redirectParameters();

    reader->clear();
    emit beginInitialization();
    sim->initialize(_sequence, this);
    emit endInitialization();

    return sim;
}
コード例 #2
0
Simulation* SimulationMaker::parse(QString fileName_)
{
    fileName = fileName_;
	QString simName, simVersion;

    XmlExpander expander(fileName, "_expanded");
	emit beginExpansion();
	expander.expand();
	emit endExpansion();	
    fileName = expander.newFileName();
	
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) throw Exception(message("Cannot open file: '"+fileName+"' for reading."));
	reader->setDevice(&file);

    if (!nextElementDelim()) throw Exception(message("File is not in valid XML format"));
    if (elementNameNotEquals("simulation")) throw Exception(message("Root element must be 'simulation'"));

    simName = attributeValue("name");
	if (simName.isEmpty()) simName = "anonymous";
    simVersion = attributeValue("version");
	if (simVersion.isEmpty()) simVersion = "1.0";
	
	Simulation *sim = new Simulation(simName, simVersion);
    UniSim::setSimulationObject(sim);

	nextElementDelim();
    int iCon=0, iMod=0, iOut=0;
    while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("integrator") || elementNameEquals("controller")) {
			if (readIntegratorElement(sim)) ++iCon;
        } else if (elementNameEquals("model")) {
			if (readModelElement(sim)) ++iMod;
        } else if (elementNameEquals("output")) {
			if (readOutputElement(sim)) ++iOut;
		} else {
            throw Exception(message("Unknown element in 'simulation' element: " + elementName()));
		}
	}
	Q_ASSERT(reader->isEndElement());
    if (reader->hasError()) throw Exception(message(""));
    if (iCon==0) throw Exception(message("Missing 'integrator' element in 'simulation'"));
    else if (iCon>1) throw Exception(message("Only one 'integrator' element allowed in 'simulation'"));
    if (iMod==0) throw Exception(message("Missing 'model' element in 'simulation'"));
    if (iOut==0) throw Exception(message("Missing 'output' element in 'simulation'"));

	reader->clear();
    emit beginInitialization();
    sim->initialize(_sequence);
    emit endInitialization();

    return sim;
}