Added count and pin validators
This commit is contained in:
1 parent
1100a9f0b9
commit
0a658a4aba
6 files changed
+138
-80
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#include "comdelvalidator.h"
|
||||
#include "library.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
@@ -15,6 +16,78 @@ std::vector<ValidationError> ComdelValidator::validateSchema(Schema &schema, Val
|
||||
return errors;
|
||||
}
|
||||
|
||||
std::vector<ValidationError> ComdelValidator::validateInstanceCount(Schema& schema, Library& library, ValidationContext context) {
|
||||
std::vector<ValidationError> errors;
|
||||
|
||||
// validate instance count
|
||||
std::map<std::string, int> instanceMap;
|
||||
for(auto& inst: schema.componentInstances) {
|
||||
instanceMap[inst->component.getName()]++;
|
||||
}
|
||||
|
||||
for(auto comp: library.getComponents()) {
|
||||
int count = instanceMap[comp.getName()];
|
||||
|
||||
context.attributes["componentName"] = Value::fromString(comp.getName());
|
||||
context.attributes["min"] = Value::fromInt(comp.getCount().first);
|
||||
context.attributes["max"] = Value::fromInt(comp.getCount().second);
|
||||
context.attributes["count"] = Value::fromInt(count);
|
||||
|
||||
if(count < comp.getCount().first) {
|
||||
auto message = populateMessage("Not enough instances of component '{componentName}' required at least {min}, found {count}", context);
|
||||
errors.push_back(ValidationError{nullptr, nullptr, Action::ERROR, message});
|
||||
} else if(count > comp.getCount().second) {
|
||||
auto message = populateMessage("To many instances of component '{componentName}' allow at most {max}, found {count}", context);
|
||||
errors.push_back(ValidationError{nullptr, nullptr, Action::ERROR, message});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// validate bus instance count
|
||||
std::map<std::string, int> busInstanceMap;
|
||||
for(auto& inst: schema.busInstances) {
|
||||
busInstanceMap[inst->bus.getName()]++;
|
||||
}
|
||||
|
||||
for(auto bus: library.getBuses()) {
|
||||
int count = busInstanceMap[bus.getName()];
|
||||
|
||||
context.attributes["busName"] = Value::fromString(bus.getName());
|
||||
context.attributes["min"] = Value::fromInt(bus.getCount().first);
|
||||
context.attributes["max"] = Value::fromInt(bus.getCount().second);
|
||||
context.attributes["count"] = Value::fromInt(count);
|
||||
|
||||
if(count < bus.getCount().first) {
|
||||
auto message = populateMessage("Not enough instances of bus '{busName}' required at least {min}, found {count}", context);
|
||||
errors.push_back(ValidationError{nullptr, nullptr, Action::ERROR, message});
|
||||
} else if(count > bus.getCount().second) {
|
||||
auto message = populateMessage("To many instances of bus '{busName}' allow at most {max}, found {count}", context);
|
||||
errors.push_back(ValidationError{nullptr, nullptr, Action::ERROR, message});
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
std::vector<ValidationError> ComdelValidator::validatePinConnections(Schema &schema, Library &library, ValidationContext context) {
|
||||
std::vector<ValidationError> errors;
|
||||
|
||||
for(auto& inst: schema.componentInstances) {
|
||||
for(auto& pin: inst->component.getPins()) {
|
||||
if(pin.getConnection().getType() == PinConnection::REQUIRED) {
|
||||
if(!connectionExists(schema, inst, pin)) {
|
||||
context.instance = inst.get();
|
||||
context.attributes["instanceName"] = Value::fromString(inst->name);
|
||||
auto message = populateMessage(pin.getConnection().getMessage(), context);
|
||||
errors.push_back(ValidationError{nullptr, nullptr, Action::ERROR, message});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstance* instance, ValidationContext context) {
|
||||
std::vector<ValidationError> errors;
|
||||
|
||||
@@ -89,4 +162,26 @@ string ComdelValidator::replacePlaceholder(string source, string key, Value valu
|
||||
return source;
|
||||
}
|
||||
|
||||
bool ComdelValidator::connectionExists(Schema &schema, shared_ptr<ComponentInstance> &component, Pin &pin) {
|
||||
for(auto conn: schema.connections) {
|
||||
auto busConnection = dynamic_cast<BusConnectionInstance*>(conn.get());
|
||||
if(busConnection != nullptr) {
|
||||
if(busConnection->instance->name == component->name && busConnection->connection.getComponent().pin == pin.getName()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
auto directConnection = dynamic_cast<DirectConnectionInstance*>(conn.get());
|
||||
if(directConnection != nullptr) {
|
||||
if(directConnection->instance->name == component->name && busConnection->connection.getComponent().pin == pin.getName()) {
|
||||
return true;
|
||||
}
|
||||
if(directConnection->secondInstance->name == component->name && busConnection->connection.getSecondComponent()->pin == pin.getName()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "instance.h"
|
||||
#include "schema.h"
|
||||
#include "library.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
@@ -29,6 +30,9 @@ public:
|
||||
std::vector<ValidationError> validateAttribute(InstanceAttribute *attribute, ValidationContext context);
|
||||
std::optional<ValidationError> validateRule(Rule rule, ValidationContext context);
|
||||
|
||||
std::vector<ValidationError> validateInstanceCount(Schema &schema, Library& library, ValidationContext context);
|
||||
std::vector<ValidationError> validatePinConnections(Schema &schema, Library& library, ValidationContext context);
|
||||
|
||||
ComdelValidator(std::vector<FunctionValidator*> validators) {
|
||||
for(auto* validator: validators) {
|
||||
validator->clear();
|
||||
@@ -42,6 +46,8 @@ private:
|
||||
std::string populateMessage(string message, ValidationContext context);
|
||||
|
||||
string replacePlaceholder(string message, const string name, Value value);
|
||||
|
||||
bool connectionExists(Schema &schema, shared_ptr <ComponentInstance> &component, Pin &pin);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user