Skip to content

Commit f582a39

Browse files
Add DataChoice, DataArray
1 parent 2622046 commit f582a39

8 files changed

Lines changed: 195 additions & 11 deletions

File tree

CSAPI-lib/CSAPI-lib.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
<ClInclude Include="DataModels\Component\CategoryRange.h" />
138138
<ClInclude Include="DataModels\Component\Count.h" />
139139
<ClInclude Include="DataModels\Component\CountRange.h" />
140+
<ClInclude Include="DataModels\Component\DataArray.h" />
141+
<ClInclude Include="DataModels\Component\DataChoice.h" />
140142
<ClInclude Include="DataModels\Component\DataComponent.h" />
141143
<ClInclude Include="DataModels\Component\DataComponentRegistry.h" />
142144
<ClInclude Include="DataModels\Component\DataRecord.h" />

CSAPI-lib/CSAPI-lib.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,11 @@
144144
<ClInclude Include="DataModels\Component\Vector.h">
145145
<Filter>Header Files\DataModels\Component</Filter>
146146
</ClInclude>
147+
<ClInclude Include="DataModels\Component\DataArray.h">
148+
<Filter>Header Files\DataModels\Component</Filter>
149+
</ClInclude>
150+
<ClInclude Include="DataModels\Component\DataChoice.h">
151+
<Filter>Header Files\DataModels\Component</Filter>
152+
</ClInclude>
147153
</ItemGroup>
148154
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <nlohmann/json.hpp>
5+
#include "Count.h"
6+
#include "DataComponent.h"
7+
#include "DataComponentRegistry.h"
8+
9+
namespace ConnectedSystemsAPI {
10+
namespace DataModels {
11+
namespace Component {
12+
class DataArray;
13+
void to_json(nlohmann::ordered_json& j, const DataArray& v);
14+
15+
class DataArray : public DataComponent {
16+
private:
17+
Count elementCount;
18+
std::unique_ptr<DataComponent> elementType;
19+
20+
public:
21+
DataArray() = default;
22+
23+
void validate() const {
24+
DataComponent::validate();
25+
}
26+
27+
nlohmann::ordered_json toJson() const override {
28+
nlohmann::ordered_json j;
29+
to_json(j, *this);
30+
return j;
31+
}
32+
33+
/// <summary>Specifies the size of the array (i.e., the number of elements of the defined type it contains).</summary>
34+
const Count& getElementCount() const { return elementCount; }
35+
/// <summary>Specifies the size of the array (i.e., the number of elements of the defined type it contains).</summary>
36+
void setElementCount(const Count& elementCount) { this->elementCount = elementCount; }
37+
/// <summary>Defines the structure of the element that will be repeated in the array.</summary>
38+
const DataComponent* getElementType() const { return elementType.get(); }
39+
/// <summary>Defines the structure of the element that will be repeated in the array.</summary>
40+
void setElementType(std::unique_ptr<DataComponent> elementType) { this->elementType = std::move(elementType); }
41+
};
42+
43+
// Register with the DataComponentRegistry
44+
struct DataArrayRegistrar {
45+
DataArrayRegistrar() {
46+
DataComponentRegistry::registerType(
47+
"DataArray", [](const nlohmann::json& j) {
48+
return std::make_unique<DataArray>(j.get<DataArray>());
49+
}
50+
);
51+
}
52+
};
53+
static DataArrayRegistrar DdataArraytorRegistrar;
54+
55+
inline void from_json(const nlohmann::json& j, DataArray& v) {
56+
from_json(j, static_cast<DataComponent&>(v));
57+
58+
if (j.contains("elementCount"))
59+
v.setElementCount(j.at("elementCount").get<Count>());
60+
61+
if (j.contains("elementType") && j["elementType"].is_object()) {
62+
v.setElementType(DataComponentRegistry::createDataComponent(j.at("elementType")));
63+
}
64+
else {
65+
v.setElementType(nullptr);
66+
}
67+
}
68+
69+
inline void to_json(nlohmann::ordered_json& j, const DataArray& v) {
70+
to_json(j, static_cast<const DataComponent&>(v));
71+
72+
j["elementCount"] = v.getElementCount();
73+
j["elementType"] = v.getElementType()->toJson();
74+
}
75+
}
76+
}
77+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <nlohmann/json.hpp>
5+
#include "Category.h"
6+
#include "DataComponent.h"
7+
#include "DataComponentRegistry.h"
8+
9+
namespace ConnectedSystemsAPI {
10+
namespace DataModels {
11+
namespace Component {
12+
class DataChoice;
13+
void to_json(nlohmann::ordered_json& j, const DataChoice& v);
14+
15+
class DataChoice : public DataComponent {
16+
private:
17+
std::optional<Category> choiceValue;
18+
std::vector<std::unique_ptr<DataComponent>> items;
19+
20+
public:
21+
DataChoice() = default;
22+
23+
void validate() const {
24+
DataComponent::validate();
25+
}
26+
27+
nlohmann::ordered_json toJson() const override {
28+
nlohmann::ordered_json j;
29+
to_json(j, *this);
30+
return j;
31+
}
32+
33+
/// <summary>
34+
/// This category component marks the data stream element that will indicate the actual choice made.
35+
/// Possible choices are listed in the Category constraint section as an enumeration and should map to item names.
36+
/// </summary>
37+
const std::optional<Category> getChoiceValue() const { return choiceValue; }
38+
/// <summary>
39+
/// This category component marks the data stream element that will indicate the actual choice made.
40+
/// Possible choices are listed in the Category constraint section as an enumeration and should map to item names.
41+
/// </summary>
42+
void setChoiceValue(const std::optional<Category>& choiceValue) { this->choiceValue = choiceValue; }
43+
/// <summary>
44+
/// Definition of the choice items.
45+
/// Items can be of any component types.
46+
/// </summary>
47+
const std::vector<std::unique_ptr<DataComponent>>& getItems() const { return items; }
48+
/// <summary>
49+
/// Definition of the choice items.
50+
/// Items can be of any component types.
51+
/// </summary>
52+
void setItems(std::vector<std::unique_ptr<DataComponent>> f) { items = std::move(f); }
53+
};
54+
55+
// Register with the DataComponentRegistry
56+
struct DataChoiceRegistrar {
57+
DataChoiceRegistrar() {
58+
DataComponentRegistry::registerType(
59+
"DataChoice", [](const nlohmann::json& j) {
60+
return std::make_unique<DataChoice>(j.get<DataChoice>());
61+
}
62+
);
63+
}
64+
};
65+
static DataChoiceRegistrar dataChoiceRegistrar;
66+
67+
inline void from_json(const nlohmann::json& j, DataChoice& v) {
68+
from_json(j, static_cast<DataComponent&>(v));
69+
70+
if (j.contains("choiceValue"))
71+
v.setChoiceValue(j.at("choiceValue").get<Category>());
72+
73+
if (j.contains("items") && j["items"].is_array()) {
74+
std::vector<std::unique_ptr<DataComponent>> items;
75+
for (const auto& item : j["items"]) {
76+
items.push_back(DataComponentRegistry::createDataComponent(item));
77+
}
78+
v.setItems(std::move(items));
79+
}
80+
}
81+
82+
inline void to_json(nlohmann::ordered_json& j, const DataChoice& v) {
83+
to_json(j, static_cast<const DataComponent&>(v));
84+
85+
if (v.getChoiceValue())
86+
j["choiceValue"] = v.getChoiceValue().value();
87+
88+
if (!v.getItems().empty()) {
89+
j["items"] = nlohmann::ordered_json::array();
90+
for (const auto& item : v.getItems()) {
91+
j["items"].push_back(item->toJson());
92+
}
93+
}
94+
}
95+
}
96+
}
97+
}

CSAPI-lib/DataModels/Component/DataComponent.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace ConnectedSystemsAPI {
1010
private:
1111
std::string type;
1212
std::optional<std::string> id;
13-
std::string label;
13+
std::optional<std::string> label;
1414
std::optional<std::string> description;
1515
std::optional<bool> updatable;
1616
std::optional<bool> optional;
17-
std::string definition;
17+
std::optional<std::string> definition;
1818

1919
public:
2020
virtual ~DataComponent() = default;
@@ -23,8 +23,6 @@ namespace ConnectedSystemsAPI {
2323
void validate() const {
2424
if (type.empty())
2525
throw std::invalid_argument("DataComponent: type is required.");
26-
if (label.empty())
27-
throw std::invalid_argument("DataComponent: label is required.");
2826
}
2927

3028
/// <summary>Type of the component.</summary>
@@ -38,9 +36,9 @@ namespace ConnectedSystemsAPI {
3836
void setId(const std::optional<std::string> id) { this->id = id; }
3937

4038
/// <summary>Human-readable label for the object.</summary>
41-
const std::string& getLabel() const { return label; }
39+
const std::optional<std::string> getLabel() const { return label; }
4240
/// <summary>Human-readable label for the object.</summary>
43-
void setLabel(const std::string& label) { this->label = label; }
41+
void setLabel(const std::optional<std::string> label) { this->label = label; }
4442

4543
/// <summary>Human-readable description of the object.</summary>
4644
const std::optional<std::string> getDescription() const { return description; }
@@ -58,9 +56,9 @@ namespace ConnectedSystemsAPI {
5856
void setOptional(const std::optional<bool>& optional) { this->optional = optional; }
5957

6058
/// <summary>The definition of the property whose value is provided by this component (semantic link).</summary>
61-
const std::string& getDefinition() const { return definition; }
59+
const std::optional<std::string> getDefinition() const { return definition; }
6260
/// <summary>The definition of the property whose value is provided by this component (semantic link).</summary>
63-
void setDefinition(const std::string& definition) { this->definition = definition; }
61+
void setDefinition(const std::optional<std::string> definition) { this->definition = definition; }
6462
};
6563

6664
inline void from_json(const nlohmann::json& j, DataComponent& c) {
@@ -85,14 +83,16 @@ namespace ConnectedSystemsAPI {
8583
j["type"] = c.getType();
8684
if (c.getId())
8785
j["id"] = c.getId();
88-
j["label"] = c.getLabel();
86+
if (c.getLabel())
87+
j["label"] = c.getLabel();
8988
if (c.getDescription())
9089
j["description"] = c.getDescription();
9190
if (c.isUpdatable())
9291
j["updatable"] = c.isUpdatable().value();
9392
if (c.isOptional())
9493
j["optional"] = c.isOptional().value();
95-
j["definition"] = c.getDefinition();
94+
if (c.getDefinition())
95+
j["definition"] = c.getDefinition();
9696
}
9797

9898
inline std::ostream& operator<<(std::ostream& os, const DataComponent& c) {

CSAPI-lib/DataModels/Component/DataComponentRegistry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace ConnectedSystemsAPI {
2828
if (it != registry.end()) {
2929
return it->second(j);
3030
}
31+
std::cout << "Unknown component type during deserialization: " << type << std::endl;
3132
throw std::runtime_error("DataComponentRegistry: Unknown component type: " + type);
3233
}
3334

CSAPI-lib/DataModels/Component/Quantity.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ namespace ConnectedSystemsAPI {
6161
v.setValue(j.at("value").get<double>());
6262
else
6363
v.setValue(std::nullopt);
64-
std::cout << "from_json:--" << j.at("uom").dump() << "--"; // for debug
6564
v.setUnitOfMeasure(j.at("uom").get<UnitOfMeasure>());
6665
}
6766

CSAPI-lib/RegistryInit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "DataModels/Component/CategoryRange.h"
99
#include "DataModels/Component/Count.h"
1010
#include "DataModels/Component/CountRange.h"
11+
#include "DataModels/Component/DataArray.h"
12+
#include "DataModels/Component/DataChoice.h"
1113
#include "DataModels/Component/DataRecord.h"
1214
#include "DataModels/Component/Quantity.h"
1315
#include "DataModels/Component/QuantityRange.h"

0 commit comments

Comments
 (0)