/** An overloaded method using Expression. * @param fun :: The function * @param expr :: A parsed initialization Expression. * @param isDefault :: Is this initialization a default one? * @return A pointer to the created Constraint */ IConstraint *ConstraintFactoryImpl::createInitialized(IFunction *fun, const Expression &expr, bool isDefault) const { IConstraint *c = nullptr; if (expr.name() == "==") { c = createUnwrapped("BoundaryConstraint"); } else { c = createUnwrapped(expr.name()); } c->initialize(fun, expr, isDefault); return c; }
/** * Create a fitting function from an expression. * @param expr :: The input expression made by parsing the input string to createFitFunction(const std::string& input) */ IFitFunction* FunctionFactoryImpl::createFitFunction(const Expression& expr) const { const Expression& e = expr.bracketsRemoved(); std::string fnName = e.name(); IFitFunction* fun = createUnwrapped(fnName); if (!fun) { throw std::runtime_error("Cannot create function "+fnName); } fun->initialize(); const std::vector<Expression>& terms = e.terms(); std::vector<Expression>::const_iterator term = terms.begin(); for(;term!=terms.end();++term) {// loop over function's parameters/attributes if (term->name() == "=") { std::string parName = term->terms()[0].name(); std::string parValue = term->terms()[1].str(); if (fun->hasAttribute(parName)) {// set attribute if (parValue.size() > 1 && parValue[0] == '"') {// remove the double quotes parValue = parValue.substr(1,parValue.size()-2); } IFitFunction::Attribute att = fun->getAttribute(parName); att.fromString(parValue); fun->setAttribute(parName,att); } else if (parName.size() >= 10 && parName.substr(0,10) == "constraint") {// or it can be a list of constraints addConstraints(fun,(*term)[1]); } else if (parName == "ties") { addTies(fun,(*term)[1]); } else {// set initial parameter value fun->setParameter(parName,atof(parValue.c_str())); } } else // if the term isn't a name=value pair it could be a member function of a composite function { throw Kernel::Exception::NotImplementedError("Composite functions are not implemented yet for IFitFunction"); //CompositeFunction* cfun = dynamic_cast<CompositeFunction*>(fun); //if (!cfun) //{ // throw std::runtime_error("Cannot add a function to a non-composite function "+fnName); //} //IFitFunction* mem = createFitFunction(*term); //cfun->addFunction(mem); } }// for term return fun; }
IFitFunction* FunctionFactoryImpl::createFunction(const std::string& type) const { IFitFunction* fun = dynamic_cast<IFitFunction*>(createUnwrapped(type)); if (!fun) { throw std::runtime_error("Function "+type+" cannot be cast to IFitFunction"); } fun->initialize(); return fun; }