FunctionInterface.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2009 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/FunctionInterface.h
10
11#ifndef __MEAL_FunctionInterface_h
12#define __MEAL_FunctionInterface_h
13
14#include "MEAL/Function.h"
15#include "TextInterface.h"
16
17#include <algorithm>
18
19namespace MEAL
20{
21
23 template<class T>
25 {
26 std::string name;
27 std::string description;
28
29 public:
30
32 Interface ( T* = 0 );
33
35 std::string get_interface_name() const { return name; }
36
38 std::string get_interface_description () const { return description; }
39
40 };
41
43 class Parameter : public Reference::Able
44 {
46 unsigned index;
47
48 public:
49
50 Parameter (ParameterPolicy* policy, unsigned i) : parameter_policy(policy)
51 { index = i; }
52
53 // Text interface to a Parameter instance
54 class Interface : public TextInterface::To<Parameter>
55 {
56 public:
57 Interface( Parameter *s_instance = NULL );
58 };
59
61 std::string get_description () const
62 { return parameter_policy->get_description (index); }
63
65 double get_param () const
66 { return parameter_policy->get_param (index); }
67
69 void set_param (double value)
70 { parameter_policy->set_param (index,value); }
71
73 double get_variance () const
74 { return parameter_policy->get_variance (index); }
75
77 void set_variance (double value)
78 { parameter_policy->set_variance (index,value); }
79
81 bool get_infit () const
82 { return parameter_policy->get_infit (index); }
83
85 void set_infit (bool flag)
86 { parameter_policy->set_infit (index,flag); }
87 };
88}
89
90
91template<typename T>
93{
94 if (c)
95 {
96 name = c->get_name();
97 description = c->get_description();
98
99 this->set_instance (c);
100 }
101
102 this->add( &Function::get_nparam, "nparam", "Number of parameters" );
103
104 {
105 typename TextInterface::To<T>::template VGenerator<std::string> generator;
106
107 this->add_value(generator( "name", std::string("Parameter name"),
110
111 this->add_value(generator( "help", std::string("Parameter description"),
114 }
115
116 {
117 typename TextInterface::To<T>::template VGenerator<double> generator;
118
119 this->add_value(generator( "val", std::string("Parameter value"),
123
124 this->add_value(generator( "var", std::string("Parameter variance"),
128 }
129
130 {
131 typename TextInterface::To<T>::template VGenerator<bool> generator;
132
133 this->add_value(generator( "fit", std::string("Fit flag"),
137 }
138
139 if (!c)
140 return;
141
142 if (!c->has_parameter_policy())
143 return;
144
145 ParameterPolicy* policy = c->get_parameter_policy();
146
147 unsigned nparam = c->get_nparam();
148
149 // check for duplicate names
150
151 std::vector<std::string> names (nparam);
152 for (unsigned iparam=0; iparam < nparam; iparam++)
153 names[iparam] = c->get_param_name (iparam);
154
155 std::sort (names.begin(), names.end());
156 if (std::adjacent_find(names.begin(), names.end()) != names.end())
157 return;
158
159 // there are no duplicate names
160
161 for (unsigned iparam=0; iparam < nparam; iparam++)
162 {
163 std::string name = c->get_param_name (iparam);
164 Parameter* parameter = new Parameter (policy, iparam);
165 this->insert( name, new Parameter::Interface (parameter) );
166 }
167
168#if 0
169 add( &Function::get_,
170 &Function::set_,
171 "", "" );
172#endif
173
174}
175
176#endif
177
Textual interface to Function attributes.
Definition FunctionInterface.h:25
std::string get_interface_name() const
Get the interface name.
Definition FunctionInterface.h:35
Interface(T *=0)
Default constructor that takes an optional instance.
Definition FunctionInterface.h:92
std::string get_interface_description() const
Get a short description of this interface.
Definition FunctionInterface.h:38
void set_param(unsigned index, double value)
Set the value of the specified parameter.
Definition Function.h:131
bool get_infit(unsigned index) const
Return true if parameter at index is to be fitted.
Definition Function.h:143
std::string get_param_description(unsigned index) const
Return the description of the specified parameter.
Definition Function.h:123
double get_param(unsigned index) const
Return the value of the specified parameter.
Definition Function.h:127
void set_variance(unsigned index, double value)
Set the variance of the specified parameter.
Definition Function.h:139
void set_infit(unsigned index, bool flag)
Set flag for parameter at index to be fitted.
Definition Function.h:147
unsigned get_nparam() const
Return the number of parameters.
Definition Function.h:115
double get_variance(unsigned index) const
Return the variance of the specified parameter.
Definition Function.h:135
std::string get_param_name(unsigned index) const
Return the name of the specified parameter.
Definition Function.h:119
Managers of Function parameters.
Definition ParameterPolicy.h:19
Provides name-based access to parameter attributes in Function::Interface.
Definition FunctionInterface.h:44
std::string get_description() const
Description of the parameter.
Definition FunctionInterface.h:61
double get_variance() const
Return the variance of the specified parameter.
Definition FunctionInterface.h:73
void set_infit(bool flag)
Set flag for parameter at index to be fitted.
Definition FunctionInterface.h:85
bool get_infit() const
Return true if parameter at index is to be fitted.
Definition FunctionInterface.h:81
void set_param(double value)
Set the value of the specified parameter.
Definition FunctionInterface.h:69
void set_variance(double value)
Set the variance of the specified parameter.
Definition FunctionInterface.h:77
double get_param() const
Return the value of the specified parameter.
Definition FunctionInterface.h:65
void insert(Parser *)
void add_value(Value *value)
void add(T(P::*get)() const, const char *name, const char *description=0)
virtual void set_instance(C *c)
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0