Esempio n. 1
0
void Authors::initialize() {
    QFile file(":/authors.xml");
    if (!file.open(QIODevice::ReadOnly))
        throw Exception("Cannot fint embedded file 'authors.xml");
    reader->setDevice(&file);

    nextToken();
    if (!reader->isStartElement() && elementNameEquals("authors"))
        throw Exception(message("Expected <authors> element'"));

    nextToken();
    while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("author")) {
            readAuthor();
        } else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"));
        }
        if(!reader->isEndElement())
            throw Exception(message("Expected end element </author>"));
        nextToken();
    }

    //nextToken();
    if(!reader->isEndElement())
        throw Exception(message("Expected end element </authors>"));
}
Esempio n. 2
0
void Authors::readAuthor() {
    Author author;
    nextToken();
    while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("id")) {
            author.id = readText();
        }
        else if (elementNameEquals("name")) {
            author.name = readText();
        }
        else if (elementNameEquals("address")) {
            author.address = readText();
        }
        else if (elementNameEquals("email")) {
            author.email = readText();
        }
        else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"));
        }
    }
    if (author.id.label().isEmpty())
        throw Exception(message("Missing author id"));
    if (author.name.isEmpty())
        throw Exception(message("Missing author name"));
    if (author.address.isEmpty())
        throw Exception(message("Missing author address"));
    if (theCollection.contains(author.id))
        throw Exception(message("Author id occurs twice"));
    theCollection[author.id] = author;
}
Esempio n. 3
0
bool SimulationMaker::readOutputElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement() && parent);
	

    QString objectName = attributeValue("name");
	if (objectName.isEmpty()) objectName = "anonymous";
    QString type = attributeValue("type").toLower();

    Output *output;
    try {
        output = OutputMaker::create(type, objectName, parent);
    }
    catch (Exception &ex) {
        throw Exception(message(ex.message()));
    }

	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("parameter")) {
            readParameterElement(output);
        }
        else if (elementNameEquals("variable")) {
            readVariableElement(output);
        }
        else {
            throw Exception(message(
                    "Unknown element in 'output' element: " + elementName()));
		}
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
    return output;
}	
Esempio n. 4
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;
}
Esempio n. 5
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;
}
Esempio n. 6
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;
}
Esempio n. 7
0
bool SimulationMaker::readOutputElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement() && parent);
	

    QString objectName = attributeValue("name", "anonymous");
    QString type = attributeValue("type", parent);
    QString summary = attributeValue("summary", "");

    Output *output;
    try {
        output = OutputMaker::create(type, objectName, parent);
        if (!summary.isEmpty()) {
            bool isSummary = UniSim::stringToValue<bool>(summary);
            output->setIsSummary(isSummary);
        }
    }
    catch (Exception &ex) {
        throw Exception(message(ex.message()));
    }

	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("parameter")) {
            readParameterElement(output);
        }
        else if (elementNameEquals("read-parameter")) {
            readOutputParameterElement(output);
        }
        else if (elementNameEquals("variable")) {
            readOutputVariableElement(output);
        }
        else if (elementNameEquals("data")) {
            readOutputDataElement(output);
        }
        else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"), parent);
        }
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
    return output;
}	
Esempio n. 8
0
bool SimulationMaker::readIntegratorElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement() && parent);
	
    QString type = attributeValue("type");
	if (type.isEmpty())
        throw Exception(message("Missing 'type' attribute for 'integrator' element"));
 	
    QString name = attributeValue("name");
	if (name.isEmpty()) 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(dynamic_cast<Parameters*>(integrator));
        }
        else {
            throw Exception(message(
                           "Unknown element in 'model' element: " + elementName()));
		}
	}	
	Q_ASSERT(reader->isEndElement());
	nextElementDelim();
	
	return integrator;
}	
Esempio n. 9
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;
}	
Esempio n. 10
0
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;
}
Esempio n. 11
0
bool SimulationMaker::nextElementDelim()
{
    QString myTest = elementName();
    QXmlStreamReader::TokenType myType;
    bool unusedElement;
    if (moreToRead()) {
		do {
			reader->readNext();
            myTest = elementName();
            myType = reader->tokenType();
            bool commonElement = reader->isStartElement() && elementNameEquals("common");

            unusedElement = commonElement || !(reader->isStartElement() || reader->isEndElement());
            if (commonElement)
                ignoreElement();
        } while (unusedElement && moreToRead());
	}
	return reader->isStartElement() || reader->isEndElement();
}
Esempio n. 12
0
void SimulationMaker::readDatasetElement(QObject* parent)
{
    Q_ASSERT(reader->isStartElement());

    QString objectName = attributeValue("name", parent);

    Dataset *dataset = new Dataset(objectName, parent);

    nextElementDelim();
    while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("parameter")){
            readParameterElement(dataset);
        }
        else {
            throw Exception(message("Unexpected element: '" + elementName() + "'"), parent);
        }
    }
    Q_ASSERT(reader->isEndElement());
    nextElementDelim();
}
Esempio n. 13
0
void SimulationMaker::readSequenceElement(QObject* parent)
{
	Q_ASSERT(reader->isStartElement() && parent);

	nextElementDelim();
	while (!reader->hasError() && reader->isStartElement()) {
        if (elementNameEquals("model")) {
            QString model = attributeValue("name", parent);
            _sequence.append(model);
        }
        else {
            throw Exception(message(
                           "Unknown child element of 'sequence' element: " + elementName()), parent);
		}
		nextElementDelim();
		Q_ASSERT(reader->isEndElement());
		nextElementDelim();
	}	
	Q_ASSERT(reader->isEndElement());

	nextElementDelim();
}