Parse an existing Model from a file¶
The following code will read a file called sine_comparison.cellml and deserialise its contents into a Model instance.
Note that both the Parser class and its opposite number, the Printer class, deal with strings rather than files.
You’ll need to read the file into a string, and then use the string as input to the Parser item.
Parse a model from a CellML file
// Parse a CellML file into a model.
// Read the file containing the CellML model into a string.
std::string inFileName = "simulationExample.cellml";
std::ifstream inFile(inFileName);
std::stringstream inFileContents;
inFileContents << inFile.rdbuf();
std::cout << "Opening the CellML file" << std::endl;
// Create a libCellML Parser, and use it to parse the fileContents
// string and convert it into a CellML Model structure.
auto parser = libcellml::Parser::create();
auto model = parser->parseModel(inFileContents.str());
printIssues(parser);
Full context: example_simulationToolDev.cpp
# Parse the model from a CellML file.
# Create a libCellML Parser, and use it to parse the fileContents
# string and convert it into a CellML Model structure.
read_file = open("resources/simulationExample.cellml", "r")
parser = Parser()
model = parser.parseModel(read_file.read())
print_issues_to_terminal(parser)
Full context: example_simulationToolDev.py