本文整理汇总了C++中ObjRef::generateCode方法的典型用法代码示例。如果您正苦于以下问题:C++ ObjRef::generateCode方法的具体用法?C++ ObjRef::generateCode怎么用?C++ ObjRef::generateCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjRef
的用法示例。
在下文中一共展示了ObjRef::generateCode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: retrieveOdeCodeInformation
void CellmlFileRuntime::retrieveOdeCodeInformation(iface::cellml_api::Model *pModel)
{
// Get a code generator bootstrap and create an ODE code generator
ObjRef<iface::cellml_services::CodeGeneratorBootstrap> codeGeneratorBootstrap = CreateCodeGeneratorBootstrap();
ObjRef<iface::cellml_services::CodeGenerator> codeGenerator = codeGeneratorBootstrap->createCodeGenerator();
// Generate some code for the model
try {
mOdeCodeInformation = codeGenerator->generateCode(pModel);
// Check that the code generation went fine
checkCodeInformation(mOdeCodeInformation);
} catch (iface::cellml_api::CellMLException &exception) {
couldNotGenerateModelCodeIssue(Core::formatMessage(QString::fromStdWString(exception.explanation)));
} catch (...) {
unknownProblemDuringModelCodeGenerationIssue();
}
// Check the outcome of the ODE code generation
if (mIssues.count())
resetOdeCodeInformation();
}
示例2: instantiateCellmlApiObjects
int CellmlModelDefinition::instantiateCellmlApiObjects()
{
try
{
// create an annotation set to manage our variable usages
ObjRef<iface::cellml_services::AnnotationToolService> ats = CreateAnnotationToolService();
ObjRef<iface::cellml_services::AnnotationSet> as = ats->createAnnotationSet();
mCapi->annotations = as;
// mapping the connections between variables is a very expensive operation, so we want to
// only do it once and keep hold of the mapping (tracker item 3294)
ObjRef<iface::cellml_services::CeVASBootstrap> cvbs = CreateCeVASBootstrap();
ObjRef<iface::cellml_services::CeVAS> cevas = cvbs->createCeVASForModel(mCapi->model);
std::wstring msg = cevas->modelError();
if (msg != L"")
{
std::cerr << "loadModel: Error creating CellML Variable Association Service: "
<< ws2s(msg) << std::endl;
return -2;
}
mCapi->cevas = cevas;
// now check we can generate code and grab hold of the initial code information
ObjRef<iface::cellml_services::CodeGeneratorBootstrap> cgb = CreateCodeGeneratorBootstrap();
ObjRef<iface::cellml_services::CodeGenerator> cg = cgb->createCodeGenerator();
try
{
cg->useCeVAS(cevas);
ObjRef<iface::cellml_services::CodeInformation> cci = cg->generateCode(mCapi->model);
msg = cci->errorMessage();
if (msg != L"")
{
std::cerr << "CellmlModelDefintion::loadModel: Error generating code: "
<< ws2s(msg) << std::endl;
return -4;
}
// TODO: we are only interested in models we can work with?
if (cci->constraintLevel() != iface::cellml_services::CORRECTLY_CONSTRAINED)
{
std::cerr << "CellmlModelDefintion::loadModel: Model is not correctly constrained: "
<< std::endl;
return -5;
}
mCapi->codeInformation = cci;
// always flag all state variables and the variable of integration
ObjRef<iface::cellml_services::ComputationTargetIterator> cti = cci->iterateTargets();
while (true)
{
ObjRef<iface::cellml_services::ComputationTarget> ct = cti->nextComputationTarget();
if (ct == NULL) break;
if (ct->degree() > 0) break; // only want to initialise the base variables not the differential
ObjRef<iface::cellml_api::CellMLVariable> v(ct->variable());
if (ct->type() == iface::cellml_services::STATE_VARIABLE)
{
mVariableTypes[getVariableUniqueId(v)] = csim::StateType;
mVariableIndices[getVariableUniqueId(v)][csim::StateType] = mStateCounter;
mStateCounter++;
}
else if (ct->type() == iface::cellml_services::VARIABLE_OF_INTEGRATION)
{
mVariableTypes[getVariableUniqueId(v)] = csim::IndependentType;
mNumberOfIndependentVariables++;
}
else
{
// need to initialise the variable type
mVariableTypes[getVariableUniqueId(v)] = csim::UndefinedType;
}
}
}
catch (...)
{
std::cerr << "loadModel: Error generating the code information for the model" << std::endl;
return -3;
}
// if we get to here, everything worked.
mModelLoaded = true;
}
catch (...)
{
std::wcerr << L"Error instantiating CellML API objects." << std::endl;
return -1;
}
return csim::CSIM_OK;
}
示例3: generateCodeForModel
std::string generateCodeForModel(CellmlApiObjects* capi,
std::map<std::string, unsigned char>& variableTypes,
std::map<std::string, std::map<unsigned char, int> >& variableIndices,
int numberOfInputs, int numberOfStates)
{
std::stringstream code;
std::string codeString;
if (! (capi && capi->codeInformation))
{
std::cerr << "CellML Model Definition::generateCodeForModel: missing model implementation?" << std::endl;
return "";
}
// We need to regenerate the code information to make use of the flagged variables.
ObjRef<iface::cellml_services::CodeGeneratorBootstrap> cgb = CreateCodeGeneratorBootstrap();
ObjRef<iface::cellml_services::CodeGenerator> cg = cgb->createCodeGenerator();
// catch any exceptions from the CellML API
try
{
// annotate the source variables in the model with the code-names based on existing annotations
for (unsigned int i=0; i < capi->cevas->length(); i++)
{
ObjRef<iface::cellml_services::ConnectedVariableSet> cvs = capi->cevas->getVariableSet(i);
ObjRef<iface::cellml_api::CellMLVariable> sv = cvs->sourceVariable();
std::string currentId = getVariableUniqueId(sv);
std::map<std::string, unsigned char>::iterator typeit(variableTypes.find(currentId));
if (typeit != variableTypes.end())
{
std::wstringstream ename;
// here we assign an array and index based on the "primary" purpose of the variable. Later
// we will add in secondary purposes.
unsigned char vType = typeit->second;
if (vType & csim::StateType)
{
ename << L"CSIM_STATE[" << variableIndices[currentId][csim::StateType] << L"]";
}
else if (vType & csim::IndependentType)
{
// do nothing, but stop input and output annotations
}
else if (vType & csim::InputType)
{
ename << L"CSIM_INPUT[" << variableIndices[currentId][csim::InputType] << L"]";
}
else if (vType & csim::OutputType)
{
ename << L"CSIM_OUTPUT[" << variableIndices[currentId][csim::OutputType] << L"]";
}
capi->annotations->setStringAnnotation(sv, L"expression", ename.str());
if (vType & csim::StateType)
{
ename.str(L"");
ename.clear();
ename << L"CSIM_RATE[" << variableIndices[currentId][csim::StateType] << L"]";
capi->annotations->setStringAnnotation(sv, L"expression_d1", ename.str());
}
}
}
cg->useCeVAS(capi->cevas);
cg->useAnnoSet(capi->annotations);
ObjRef<iface::cellml_services::CodeInformation> cci = cg->generateCode(capi->model);
std::wstring m = cci->errorMessage();
if (m != L"")
{
std::cerr << "CellML Model Definition::generateCodeForModel: error generating code?" << std::endl;
return "";
}
iface::cellml_services::ModelConstraintLevel mcl = cci->constraintLevel();
if (mcl == iface::cellml_services::UNDERCONSTRAINED)
{
std::cerr << "Model is underconstrained" << std::endl;
return "";
}
else if (mcl == iface::cellml_services::OVERCONSTRAINED)
{
std::cerr << "Model is overconstrained" << std::endl;
return "";
}
else if (mcl == iface::cellml_services::UNSUITABLY_CONSTRAINED)
{
std::cerr << "Model is unsuitably constrained" << std::endl;
return "";
}
std::cout << "Model is correctly constrained" << std::endl;
// create the code in the format we know how to handle
code << "//#include <math.h>\n"
/* required functions */
<< "double fabs(double x);\n"
<< "double acos(double x);\n"
<< "double acosh(double x);\n"
<< "double atan(double x);\n"
<< "double atanh(double x);\n"
<< "double asin(double x);\n"
<< "double asinh(double x);\n"
<< "double acos(double x);\n"
<< "double acosh(double x);\n"
<< "double asin(double x);\n"
<< "double asinh(double x);\n"
<< "double atan(double x);\n"
<< "double atanh(double x);\n"
//.........这里部分代码省略.........
示例4: exportTo
bool CellmlFile::exportTo(const QString &pFileName,
const QString &pUserDefinedFormatFileName)
{
// Export the model to the required format, after loading it if necessary
if (load()) {
// Check that the user-defined format file actually exists
if (!QFile::exists(pUserDefinedFormatFileName)) {
mIssues << CellmlFileIssue(CellmlFileIssue::Error,
QObject::tr("the user-defined format file does not exist"));
return false;
}
// Make sure that the user-defined format file is valid XML
// Note: you would normally expect CeLEDSExporter to check this, but all
// it does in case of an invalid XML file is crash...
QString userDefinedFormatFileContents;
if (!Core::readTextFromFile(pUserDefinedFormatFileName, userDefinedFormatFileContents)) {
mIssues << CellmlFileIssue(CellmlFileIssue::Error,
QObject::tr("the user-defined format file could not be read"));
return false;
}
QDomDocument domDocument;
if (!domDocument.setContent(userDefinedFormatFileContents)) {
mIssues << CellmlFileIssue(CellmlFileIssue::Error,
QObject::tr("the user-defined format file is not a valid XML file"));
return false;
}
// Fully instantiate all the imports
if (!fullyInstantiateImports(mModel, mIssues))
return false;
// Do the actual export
ObjRef<iface::cellml_services::CeLEDSExporterBootstrap> celedsExporterBootstrap = CreateCeLEDSExporterBootstrap();
ObjRef<iface::cellml_services::CodeExporter> codeExporter = celedsExporterBootstrap->createExporterFromText(userDefinedFormatFileContents.toStdWString());
if (celedsExporterBootstrap->loadError().length()) {
mIssues << CellmlFileIssue(CellmlFileIssue::Error,
QObject::tr("the user-defined format file could not be loaded"));
return false;
}
// Save the export
if (!Core::writeTextToFile(pFileName, QString::fromStdWString(codeExporter->generateCode(mModel)))) {
mIssues << CellmlFileIssue(CellmlFileIssue::Error,
QObject::tr("the output file could not be saved"));
return false;
}
return true;
} else {
return false;
}
}