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