14#include "arcane/CaseOptionSimple.h"
16#include "arcane/utils/ValueConvert.h"
17#include "arcane/utils/ITraceMng.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/StringBuilder.h"
20#include "arcane/utils/internal/ParameterCaseOption.h"
22#include "arcane/core/CaseOptionException.h"
23#include "arcane/core/CaseOptionBuildInfo.h"
24#include "arcane/core/XmlNodeList.h"
25#include "arcane/core/ICaseFunction.h"
26#include "arcane/core/ICaseMng.h"
27#include "arcane/core/ICaseDocument.h"
28#include "arcane/core/CaseNodeNames.h"
29#include "arcane/core/CaseOptionError.h"
30#include "arcane/core/IPhysicalUnitConverter.h"
31#include "arcane/core/IPhysicalUnitSystem.h"
32#include "arcane/core/IStandardFunction.h"
33#include "arcane/core/ICaseDocumentVisitor.h"
34#include "arcane/core/internal/StringVariableReplace.h"
35#include "arcane/core/internal/ICaseMngInternal.h"
46template <
typename T>
void
47_copyCaseOptionValue(T& out,
const T& in);
49template <>
void _copyCaseOptionValue(
String& out,
const String& in)
53template <>
void _copyCaseOptionValue(
bool& out,
const bool& in)
57template <>
void _copyCaseOptionValue(
Real& out,
const Real& in)
61template <>
void _copyCaseOptionValue(
Int16& out,
const Int16& in)
65template <>
void _copyCaseOptionValue(
Int32& out,
const Int32& in)
69template <>
void _copyCaseOptionValue(
Int64& out,
const Int64& in)
73template <>
void _copyCaseOptionValue(
Real2& out,
const Real2& in)
77template <>
void _copyCaseOptionValue(
Real3& out,
const Real3& in)
81template <>
void _copyCaseOptionValue(
Real2x2& out,
const Real2x2& in)
85template <>
void _copyCaseOptionValue(
Real3x3& out,
const Real3x3& in)
90template <
typename T>
void
96template <
typename T>
void
102template <
typename T>
void
114, m_is_optional(cob.isOptional())
115, m_has_valid_value(true)
123CaseOptionSimple(
const CaseOptionBuildInfo& cob,
const String& physical_unit)
125, m_is_optional(cob.isOptional())
126, m_has_valid_value(true)
127, m_default_physical_unit(physical_unit)
137 delete m_unit_converter;
143void CaseOptionSimple::
144_search(
bool is_phase1)
150 const String& velem_name = name();
151 XmlNodeList velems = rootElement().children(velem_name);
153 Integer nb_elem = velems.size();
154 ICaseDocumentFragment* doc = caseDocumentFragment();
158 CaseOptionError::addWarning(doc, A_FUNCINFO, velem.xpathFullName(),
159 String::format(
"Only one token of the element is allowed (nb_occur={0})",
166 const ParameterListWithCaseOption& params = caseMng()->_internalImpl()->parameters();
167 const ParameterCaseOption pco{ params.getParameterCaseOption(doc->language()) };
169 String reference_input = pco.getParameterOrNull(String::format(
"{0}/{1}", rootElement().xpathFullName(), velem_name), 1,
false);
170 if (!reference_input.null()) {
174 velem = rootElement().createElement(name());
176 velem.setValue(reference_input);
179 velem.setValue(StringVariableReplace::replaceWithCmdLineArgs(params, velem.value(),
true));
186 _searchFunction(velem);
188 String physical_unit = m_element.attrValue(
"unit");
189 if (!physical_unit.null()) {
190 _setPhysicalUnit(physical_unit);
191 if (_allowPhysicalUnit()) {
193 m_unit_converter = caseMng()->physicalUnitSystem()->createConverter(physical_unit, defaultPhysicalUnit());
196 CaseOptionError::addError(doc, A_FUNCINFO, velem.xpathFullName(),
197 String::format(
"Usage of a physic unit ('{0}') is not allowed for this kind of option",
205void CaseOptionSimple::
206_setPhysicalUnit(
const String& value)
208 m_physical_unit = value;
217 return m_physical_unit;
223void CaseOptionSimple::
230 String fname = caseDocumentFragment()->caseNodeNames()->function_ref;
232 if (func_name.
null())
238 msg->
pfatal() <<
"In element <" << velem.
name()
239 <<
">: no function named <" << func_name <<
">";
243 IStandardFunction* sf =
dynamic_cast<IStandardFunction*
>(func);
245 msg->
info() <<
"Use standard function: " << func_name;
246 m_standard_function = sf;
249 msg->
info() <<
"Use function: " << func_name;
257void CaseOptionSimple::
258_setChangedSinceLastIteration(
bool has_changed)
260 m_changed_since_last_iteration = has_changed;
266bool CaseOptionSimple::
267hasChangedSinceLastIteration()
const
269 return m_changed_since_last_iteration;
278 if (!m_element.null())
279 return m_element.xpathFullName();
288defaultPhysicalUnit()
const
290 return m_default_physical_unit;
296void CaseOptionSimple::
299 visitor->applyVisitor(
this);
317template <
typename DataType>
static void
320 ARCANE_UNUSED(converter);
321 ARCANE_UNUSED(value);
325_checkPhysicalConvert(IPhysicalUnitConverter* converter, Real& value)
328 Real new_value = converter->convert(value);
334_checkPhysicalConvert(IPhysicalUnitConverter* converter, RealUniqueArray& values)
339 converter->convert(input_values, values);
343template <
typename DataType>
static bool
344_allowConvert(
const DataType& value)
346 ARCANE_UNUSED(value);
351_allowConvert(
const Real& value)
353 ARCANE_UNUSED(value);
358_allowConvert(
const RealUniqueArray& value)
360 ARCANE_UNUSED(value);
367template <
typename T> CaseOptionSimpleT<T>::
368CaseOptionSimpleT(
const CaseOptionBuildInfo& cob)
369: CaseOptionSimple(cob)
371 _copyCaseOptionValue(m_value, Type());
381 _copyCaseOptionValue(m_value, Type());
390 using Type =
typename CaseOptionTraitsT<T>::ContainerType;
391 return _allowConvert(Type());
400 template <
typename Type>
401 class StringCollapser
405 static String collapse(
const String& str)
411 class StringCollapser<
String>
415 static String collapse(
const String& str)
436 CaseOptionSimple::_search(is_phase1);
444 bool has_valid_value =
true;
445 if (str_val.
null()) {
452 has_valid_value =
false;
454 _setHasValidValue(has_valid_value);
455 if (has_valid_value) {
457 str_val = StringCollapser<Type>::collapse(str_val);
458 bool is_bad = builtInGetValue(val, str_val);
482 if (_isInitialized())
497 template <
typename T>
498 class FunctionConverterT
506 ARCANE_UNUSED(value);
512 class FunctionConverterT<
Real>
516 void convert(ICaseFunction& tbl,
Real t,
Real& value)
523 class FunctionConverterT<
Real3>
527 void convert(ICaseFunction& tbl,
Real t, Real3& value)
534 class FunctionConverterT<bool>
538 void convert(ICaseFunction& tbl,
Real t,
bool& value)
545 class FunctionConverterT<
Integer>
549 void convert(ICaseFunction& tbl,
Real t,
Integer& value)
556 class FunctionConverterT<
String>
560 void convert(ICaseFunction& tbl,
Real t, String& value)
569 template <
typename ParamType,
typename ValueType>
570 class ComputeFunctionValue
574 static void convert(ICaseFunction* func, ParamType t, ValueType& new_value)
576 FunctionConverterT<ValueType>().convert(*func, t, new_value);
589 Type new_value(m_value);
591 ComputeFunctionValue<Real, T>::convert(func, t, new_value);
604 Type new_value(m_value);
606 ComputeFunctionValue<Integer, T>::convert(func, t, new_value);
618 _checkIsInitialized();
622 Type new_value(m_value);
625 ComputeFunctionValue<Real, T>::convert(func, current_time, new_value);
628 ComputeFunctionValue<Integer, T>::convert(func, current_iteration, new_value);
634 this->_setChangedSinceLastIteration(m_value != new_value);
636 msg->
debug() <<
"New value for option <" <<
name() <<
"> " << new_value;
637 _copyCaseOptionValue(m_value, new_value);
647 _checkIsInitialized();
669template <
typename T> CaseOptionMultiSimpleT<T>::
670CaseOptionMultiSimpleT(
const CaseOptionBuildInfo& cob,
672: CaseOptionMultiSimple(cob)
682 const T* avalue = m_view.
data();
692 using Type =
typename CaseOptionTraitsT<T>::ContainerType;
693 return _allowConvert(Type());
714 const ParameterListWithCaseOption& params =
caseMng()->_internalImpl()->parameters();
715 const ParameterCaseOption pco{ params.getParameterCaseOption(
caseDocumentFragment()->language()) };
720 pco.indexesInParam(full_xpath, option_in_param,
false);
723 Integer asize = elem_list.size();
727 if (asize == 0 && option_in_param.
empty() && is_optional) {
736 if (!option_in_param.
empty()) {
737 max_in_param = option_in_param[0];
738 for (
Integer index : option_in_param) {
739 if (index > max_in_param)
740 max_in_param = index;
742 if (max_occurs >= 0) {
743 if (max_in_param > max_occurs) {
744 StringBuilder msg =
"Bad number of occurences in command line (greater than max)";
745 msg +=
" index_max_in_param=";
747 msg +=
" max_occur=";
756 if (max_occurs >= 0) {
757 if (asize > max_occurs) {
758 StringBuilder msg =
"Bad number of occurences (greater than max)";
761 msg +=
" max_occur=";
770 Integer final_size = std::max(asize, std::max(min_occurs, max_in_param));
772 const Type* old_value = m_view.data();
774 using Type =
typename CaseOptionTraitsT<T>::ContainerType;
775 Type* ptr_value =
new Type[final_size];
786 for (
Integer i = 0; i < final_size; ++i) {
790 if (option_in_param.
contains(i + 1)) {
791 str_val = pco.getParameterOrNull(full_xpath, i + 1,
false);
795 else if (i < asize) {
798 str_val = velem.
value();
803 if (str_val.
null()) {
808 str_val = StringVariableReplace::replaceWithCmdLineArgs(params, str_val,
true);
817 str_val = StringCollapser<Type>::collapse(str_val);
818 bool is_bad = builtInGetValue(val, str_val);
824 _copyCaseOptionValue(ptr_value[i], val);
835 for (
Integer i = 0; i < this->size(); ++i)
836 o << this->
_ptr()[i] <<
" ";
845 visitor->applyVisitor(
this);
855 ComputeFunctionValue<Real, String>::convert(func, t, v);
862String CaseOptionSimple::
863_convertFunctionIntegerToString(ICaseFunction* func,
Integer t)
866 ComputeFunctionValue<Integer, String>::convert(func, t, v);
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
bool empty() const
Capacity (number of allocated elements) of the vector.
bool contains(ConstReferenceType v) const
True if the array contains the value element v.
constexpr pointer _ptr() noexcept
Returns a pointer to the array.
constexpr const_pointer data() const noexcept
Pointer to the start of the view.
void _setArray(pointer v, Integer s) noexcept
Modifies the pointer and size of the array.
Base class for 1D data vectors.
Base class for a data set option.
String name() const
Returns the option name corresponding to the data set language.
String _defaultValue() const
Returns the default value of the option or 0 if none exists.
ICaseMng * caseMng() const
Case manager.
ICaseDocumentFragment * caseDocumentFragment() const
Returns the document associated with this option.
bool isOptional() const
Allows knowing if an option is optional.
Integer maxOccurs() const
Maximum number of occurrences (for a multiple option) (-1 == unbounded).
XmlNode rootElement() const
Returns the root element of the DOM.
Integer minOccurs() const
Minimum number of occurrences (for a multiple option).
static void addInvalidTypeError(ICaseDocumentFragment *document, const TraceInfo &where, const String &node_name, const XmlNode &parent, const String &value, const String &expected_type)
Error when a dataset value is not of the correct type. This error is collective.
static void addOptionNotFoundError(ICaseDocumentFragment *document, const TraceInfo &where, const String &node_name, const XmlNode &parent)
Error when a dataset option is not found. This error is collective.
Exception related to the dataset.
Data set option of simple type list (real, integer, boolean, ...).
typename CaseOptionTraitsT< T >::ContainerType Type
Type of the option value.
typename CaseOptionTraitsT< T >::ArrayViewType ArrayViewType
Type of the view on the option values.
void print(const String &lang, std::ostream &o) const override
Prints the option value in the language lang, to the stream o.
void visit(ICaseDocumentVisitor *visitor) const override
Applies the visitor to this option.
void _search(bool is_phase1) override
Searches for the option value in the dataset.
Simple data set option (real, integer, boolean, ...).
virtual void _search(bool is_phase1)
Searches for the option value in the data set.
Type valueAtParameter(Real t) const
Returns the value of the option for the real parameter t.
CaseOptionTraitsT< T >::ContainerType Type
Option type.
virtual void print(const String &lang, std::ostream &o) const
Prints the option value in the language lang, to the stream o.
virtual void updateFromFunction(Real current_time, Integer current_iteration)
void setDefaultValue(const Type &def_value)
Sets the default value of the option.
Base class for simple options (single value).
bool hasValidValue() const
Indicates if the option has an invalid value.
ICaseFunction * function() const override
Function associated with this option (0 if none).
bool isOptional() const
Indicates if the option is optional.
IPhysicalUnitConverter * physicalUnitConverter() const
Physical unit converter.
bool isPresent() const
Returns true if the option is present.
Visitor interface for a dataset option.
@ ParamUnknown
Unknown parameter type.
@ ParamReal
Real type parameter.
@ ParamInteger
Integer type parameter.
virtual eParamType paramType() const =0
Function parameter type.
virtual TraceMessage pfatal()=0
Stream for a parallel fatal error message.
virtual TraceMessage info()=0
Stream for an information message.
virtual TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium)=0
Stream for a debug message.
Class managing a 2-dimensional real vector.
Class managing a 2x2 matrix of reals.
Class managing a 3-dimensional real vector.
Class managing a 3x3 real matrix.
Unicode character string constructor.
String toString() const
Returns the constructed character string.
Unicode character string.
bool null() const
Returns true if the string is null.
static String collapseWhiteSpace(const String &rhs)
Performs whitespace character normalization.
1D data vector with value semantics (STL style).
List of nodes of a DOM tree.
String attrValue(const String &name, bool throw_exception=false) const
Value of attribute name.
String value() const
Node value.
bool null() const
True if the node is null.
String name() const
Node name.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool builtInPutValue(const String &v, String &s)
Converti la valeur v dans la chaîne s.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
UniqueArray< Real > RealUniqueArray
Dynamic 1D array of reals.
std::int16_t Int16
Signed integer type of 16 bits.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.