Exemple #1
0
bool SimulationMaker::readModelElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement());
	
    QString modelType = attributeValue("type", "anonymous");
    QString objectName = attributeValue("name", "anonymous");
    QString hide = attributeValue("hide", "");
    QString instancesStr = attributeValue("instances", "");

    bool manyInstances = !instancesStr.isEmpty();
    int instances = 1;
    if (manyInstances) {
        bool ok(true);
        instances = instancesStr.toInt(&ok);
        if (!ok || instances <= 0)
            throw Exception("instances must a number larger than zero");
    }

    Model *model;
    try {
        for (int i = 0; i < instances; ++i) {
            QString objectInstanceName = objectName;
            if (manyInstances)
                objectInstanceName += "(" + QString::number(i+1) + ")";

            model = ModelMaker::create(modelType, objectInstanceName, parent);

            if (!hide.isEmpty()) {
                bool isHidden = UniSim::stringToValue<bool>(hide);
                model->setHide(isHidden);
            }
        }
    }
    catch (Exception &ex) {
        throw Exception(message(ex.message()), parent);
    }

	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("model")) {
			readModelElement(model);
        }
        else if (elementNameEquals("dataset")){
            readDatasetElement(model);
        }
        else if (elementNameEquals("parameter")){
            readParameterElement(model);
        }
        else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"), parent);
        }
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
	
	return model;
}
Exemple #2
0
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;
}
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;
}
Exemple #4
0
bool SimulationMaker::readIntegratorElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement() && parent);
	
    QString type = attributeValue("type", parent);
    QString name = attributeValue("name", "anonymous");

    Integrator *integrator;
    try {
        integrator = IntegratorMaker::create(type, name, parent);
    }
    catch (Exception &ex) {
        throw Exception(message(ex.message()));
    }
	
	_sequence.clear();
	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("sequence")) {
			readSequenceElement(integrator);
        }
        else if (elementNameEquals("parameter")){
            readParameterElement(integrator);
        }
        else if (elementNameEquals("model")){
            readModelElement(integrator);
        }
        else {
            throw Exception(message(
                           "Unexpected element: '" + elementName() + "'"), parent);
		}
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
	
	return integrator;
}	
bool SimulationMaker::readModelElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement());
	
    QString modelType = attributeValue("type");
	if (modelType.isEmpty()) modelType = "anonymous";
	
    QString objectName = attributeValue("name");
	if (objectName.isEmpty()) objectName = "anonymous";

    Model *model;
    try {
        model = ModelMaker::create(modelType, objectName, parent);
    }
    catch (Exception &ex) {
        throw Exception(message(ex.message()));
    }

	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("model")) {
			readModelElement(model);
        }
        else if (elementNameEquals("parameter")){
			readParameterElement(dynamic_cast<Parameters*>(model));
        }
        else {
            throw Exception(message(
                           "Unknown element in 'model' element: " + elementName()));
		}
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
	
	return model;
}