BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
Attribute.cpp
Go to the documentation of this file.
1#include "Attribute.h"
3#include "Parameter.h"
4#include "Expression.h"
7
8using namespace BPMNOS::Model;
9
11 : element(attribute)
12 , category(category)
13 , index(std::numeric_limits<size_t>::max())
14 , id(attribute->id.value.value)
15 , expression(getExpression(attribute->name.value.value,attributeRegistry))
16 , name(getName(attribute->name.value.value))
17{
18//std::cerr << "Attribute: " << name << std::endl;
19 attributeRegistry.add(this);
20 if ( expression ) {
21 // expression requires pointer to target attribute
22 const_cast<Expression*>(expression.get())->target = std::make_optional<const Attribute*>(this);
23 }
24
25 if ( attribute->type.value.value == "boolean" ) {
26 type = ValueType::BOOLEAN;
27 }
28 else if ( attribute->type.value.value == "integer" ) {
29 type = ValueType::INTEGER;
30 }
31 else if ( attribute->type.value.value == "decimal" ) {
32 type = ValueType::DECIMAL;
33 }
34 else if ( attribute->type.value.value == "string" ) {
35 type = ValueType::STRING;
36 }
37 else if ( attribute->type.value.value == "collection" ) {
38 type = ValueType::COLLECTION;
39 }
40
41 if ( attribute->weight.has_value() ) {
42 if ( attribute->objective.has_value() && attribute->objective->get().value.value == "maximize" ) {
43 weight = (double)attribute->weight->get().value;
44 }
45 else if ( attribute->objective.has_value() && attribute->objective->get().value.value == "minimize" ) {
46 weight = -(double)attribute->weight->get().value;
47 }
48 else {
49 throw std::runtime_error("Attribute: illegal objective of attribute '" + id + "'");
50 }
51 }
52 else {
53 if ( attribute->objective.has_value() && attribute->objective->get().value.value != "none" ) {
54 throw std::runtime_error("Attribute: required objective weight missing for attribute '" + id + "'");
55 }
56 weight = 0;
57 }
58
59 isImmutable = (id != Keyword::Timestamp);
60}
61
62std::unique_ptr<const Expression> Attribute::getExpression(std::string& input, AttributeRegistry& attributeRegistry) {
63 if ( !input.contains(":=") ) {
64 return nullptr;
65 }
66
67 auto expression = std::make_unique<const Expression>(encodeCollection(encodeQuotedStrings(input)),attributeRegistry,true);
68 auto& root = expression->compiled.getRoot();
69 assert( root.operands.size() == 1 );
70 assert( root.type == LIMEX::Type::group );
71 auto& node = std::get< LIMEX::Node<double> >(root.operands[0]);
72 if ( node.type != LIMEX::Type::assign ) {
73 throw std::runtime_error("Attribute: illegal initialization '" + input + "' for attribute '" + id + "'");
74 }
75 return expression;
76}
77
78std::string Attribute::getName(std::string& input) {
79 if ( expression ) {
80 assert( expression->compiled.getTarget().has_value() );
81 return expression->compiled.getTarget().value();
82 }
83 return input;
84}
85
86
std::unique_ptr< const Expression > expression
Definition Attribute.h:31
Attribute(XML::bpmnos::tAttribute *attribute, Attribute::Category category, AttributeRegistry &attributeRegistry)
Definition Attribute.cpp:10
Class representing a mathematical expression.
Definition Expression.h:17
std::optional< const Attribute * > target
Definition Expression.h:25
Attribute & type
Attribute value can be expected to be of type 'std::string'.
Definition tAttribute.h:54
std::optional< std::reference_wrapper< Attribute > > weight
Attribute value can be expected to be of type 'double'.
Definition tAttribute.h:56
std::optional< std::reference_wrapper< Attribute > > objective
Attribute value can be expected to be of type 'std::string'.
Definition tAttribute.h:55
const std::string Timestamp
Definition Keywords.h:12
std::string encodeQuotedStrings(std::string text)
std::string encodeCollection(std::string text)
Function to replace a collection that is not preceded by an alphanumeric or underscore.
STL namespace.
Value value
Definition bpmn++.h:78
std::string value
Definition bpmn++.h:50