Added component/bus deletion and rename

This commit is contained in:
Borna Rajković 2022-06-02 01:37:14 +02:00
parent f93a2afa63
commit 3172d7e4cf
14 changed files with 323 additions and 160 deletions

View File

@ -40,12 +40,12 @@ bool Application::loadLibrary(std::string& filename, std::ostream &errorOutput)
}
if (library.has_value()) {
errorOutput<<"Failed creating library model"<<std::endl;
return false;
} else {
libraryPath = filename;
// on library load we create a new schema
schema = new domain::Schema();
} else {
errorOutput << "Failed creating library model" << std::endl;
return false;
}
} else {
@ -55,9 +55,11 @@ bool Application::loadLibrary(std::string& filename, std::ostream &errorOutput)
return true;
}
bool Application::loadSchema(std::string &filename, std::ostream &errorOutput) {
std::pair<bool, std::vector<domain::ValidationError>> Application::loadSchema(std::string &filename, std::ostream &errorOutput) {
clear();
std::vector<domain::ValidationError> errors;
ParseContext parseContext;
auto schemaNode = load_schema_from_file(&parseContext, filename.c_str(), errorOutput);
@ -80,18 +82,37 @@ bool Application::loadSchema(std::string &filename, std::ostream &errorOutput) {
if (schema == nullptr) {
clear();
return false;
return {false, errors};
}
} else {
clear();
return false;
return {false, errors};
}
} else {
errorOutput << "Failed parsing library" << std::endl;
return false;
return {false, errors};
}
return true;
domain::ComdelValidator validator{validators};
domain::ValidationContext context;
context.instance = nullptr;
context.attribute = nullptr;
context.addressSpaces = {};
auto memoryReferenceValidation = validator.validateMemoryReferences(*schema, *library, context);
errors.insert(errors.end(), memoryReferenceValidation.begin(), memoryReferenceValidation.end());
auto nameValidation = validator.validateInstanceNames(*schema, *library, context);
errors.insert(errors.end(), nameValidation.begin(), nameValidation.end());
if(!errors.empty()) {
clear();
return {false, errors};
}
return {true, errors};
}
bool Application::generateSchema(std::ostringstream &output) {
@ -119,12 +140,15 @@ std::vector<domain::ValidationError> Application::validateSchema() {
}
auto errors = validator.validateSchema(*schema, context);
auto countValidation = validator.validateInstanceCount(*schema, *library, context);
errors.insert(errors.end(), countValidation.begin(), countValidation.end());
auto memoryReferenceValidation = validator.validateMemoryReferences(*schema, *library, context);
errors.insert(errors.end(), memoryReferenceValidation.begin(), memoryReferenceValidation.end());
auto nameValidation = validator.validateInstanceNames(*schema, *library, context);
errors.insert(errors.end(), nameValidation.begin(), nameValidation.end());
auto countValidation = validator.validateInstanceCount(*schema, *library, context);
errors.insert(errors.end(), countValidation.begin(), countValidation.end());
auto pinValidation = validator.validatePinConnections(*schema, *library, context);
errors.insert(errors.end(), pinValidation.begin(), pinValidation.end());
return errors;
@ -159,14 +183,17 @@ Application *Application::instance() {
return application;
}
std::shared_ptr<domain::ComponentInstance> Application::addComponent(domain::Component component, std::vector<domain::InstanceAttribute> attributes, int x, int y) {
std::shared_ptr<domain::ComponentInstance>
Application::addComponent(domain::Component component, std::vector<domain::InstanceAttribute> attributes, int x,
int y) {
std::set<std::string> names;
for (const auto &c: schema->componentInstances) {
names.insert(c->name);
}
std::string name = generateName(names, component.getInstanceName());
schema->componentInstances.push_back(std::make_shared<domain::ComponentInstance>(name, attributes, std::make_pair(x, y), component));
schema->componentInstances.push_back(
std::make_shared<domain::ComponentInstance>(name, attributes, std::make_pair(x, y), component));
return schema->componentInstances.back();
}
@ -197,3 +224,147 @@ std::string Application::generateName(std::set<std::string>& names, std::string
// return default value as this should never happen
return instanceName;
}
bool Application::removeComponent(std::string componentName) {
auto component = findComponentByName(componentName);
if (component == nullptr) {
return false;
}
std::set<domain::BusInstance*> automaticBusses;
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[component, &automaticBusses](const std::shared_ptr<domain::ConnectionInstance> &conn) {
auto busConnection = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConnection) {
if (busConnection->instance == component) {
return true;
}
}
auto directConnection = dynamic_cast<domain::DirectConnectionInstance *>(conn.get());
if (directConnection) {
if (directConnection->instance == component || directConnection->secondInstance == component) {
automaticBusses.insert(directConnection->bus);
return true;
}
}
return false;
}),
schema->connections.end()
);
schema->busInstances.erase(
std::remove_if(
schema->busInstances.begin(),
schema->busInstances.end(),
[&automaticBusses](const std::shared_ptr<domain::BusInstance> &bus) {
return automaticBusses.count(bus.get());
}),
schema->busInstances.end()
);
if(component->component.getType() == domain::Component::MEMORY) {
for(auto& comp: schema->componentInstances) {
if(comp->component.getType() == domain::Component::PROCESSOR) {
for(auto& attribute: comp->attributes) {
if(attribute.value.isType(domain::Value::MEMORY_REFERENCE) && attribute.value.asMemoryReference() == component->name) {
attribute.value = domain::Value::fromMemoryReference(std::nullopt);
}
}
}
}
}
for(auto iter = schema->componentInstances.begin(); iter != schema->componentInstances.end(); ++iter) {
if(iter->get() == component) {
schema->componentInstances.erase(iter);
break;
}
}
return true;
}
bool Application::removeBus(std::string busName) {
auto bus = findBusByName(busName);
if (bus == nullptr) {
return false;
}
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[bus](const std::shared_ptr<domain::ConnectionInstance> &conn) {
auto busConnection = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConnection) {
if (busConnection->bus == bus) {
return true;
}
}
return false;
}),
schema->connections.end()
);
for(auto iter = schema->busInstances.begin(); iter != schema->busInstances.end(); ++iter) {
if(iter->get() == bus) {
schema->busInstances.erase(iter);
break;
}
}
return true;
}
domain::ComponentInstance *Application::findComponentByName(std::string componentName) {
for (auto &comp: schema->componentInstances) {
if (comp->name == componentName) {
return comp.get();
}
}
return nullptr;
}
domain::BusInstance *Application::findBusByName(std::string busName) {
for (auto &bus: schema->busInstances) {
if (bus->name == busName) {
return bus.get();
}
}
return nullptr;
}
void Application::renameComponent(std::string currentName, std::string newName) {
if(currentName == newName) {
return;
}
auto component = findComponentByName(currentName);
if(component) {
component->name = newName;
if(component->component.getType() == domain::Component::MEMORY) {
for(auto& comp: schema->componentInstances) {
if(comp.get()->component.getType() == domain::Component::PROCESSOR) {
for(auto& attribute: comp.get()->attributes) {
if(attribute.value.isType(domain::Value::MEMORY_REFERENCE) && attribute.value.asMemoryReference() == currentName) {
attribute.value = domain::Value::fromMemoryReference(newName);
}
}
}
}
}
}
}
void Application::renameBus(std::string currentName, std::string newName) {
if(currentName == newName) {
return;
}
auto bus = findBusByName(currentName);
if(bus) {
bus->name = newName;
}
}

View File

@ -21,6 +21,9 @@ private:
std::string generateName(std::set<std::string>& names, std::string instanceName);
domain::ComponentInstance *findComponentByName(std::string componentName);
domain::BusInstance *findBusByName(std::string busName);
public:
std::optional<domain::Library> getLibrary();
domain::Schema* getSchema();
@ -28,7 +31,7 @@ public:
void clear();
bool loadLibrary(std::string& filename, std::ostream& errorOutput);
bool loadSchema(std::string& filename, std::ostream& errorOutput);
std::pair<bool, std::vector<domain::ValidationError>> loadSchema(std::string& filename, std::ostream& errorOutput);
static Application* instance();
@ -37,11 +40,18 @@ public:
std::vector<domain::ValidationError> generateComdel(std::ostringstream &output);
std::shared_ptr<domain::ComponentInstance> addComponent(domain::Component component, std::vector<domain::InstanceAttribute> attributes, int x, int y);
bool removeComponent(std::string component);
std::shared_ptr<domain::BusInstance> addBus(domain::Bus bus, int x, int y);
bool removeBus(std::string bus);
static bool hasErrors(std::vector<domain::ValidationError> empty);
~Application() = default;
void renameComponent(std::string currentName, std::string newName);
void renameBus(std::string currentName, std::string newName);
};

View File

@ -18,9 +18,13 @@ namespace display {
for(const auto &component: Application::instance()->getSchema()->componentInstances) {
names.insert(component->name);
}
auto dialog = new NameDialog(this->instance.get(), names);
auto dialog = new NameDialog(this->instance->name, names);
dialog->exec();
auto currentName = this->instance->name;
auto newName = dialog->getName();
Application::instance()->renameComponent(currentName, newName);
});
menu.addSeparator();
for (int i = 0; i < this->instance->attributes.size(); i++) {
@ -40,6 +44,13 @@ namespace display {
});
action->setEnabled(enabled);
}
menu.addSeparator();
menu.addAction(QString::fromStdString("Ukloni " + this->instance->name), [this]() {
Application::instance()->removeComponent(this->instance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
menu.exec(event->screenPos());
}
@ -103,9 +114,19 @@ namespace display {
for(const auto &component: Application::instance()->getSchema()->busInstances) {
names.insert(component->name);
}
auto dialog = new NameDialog(this->busInstance.get(), names);
auto dialog = new NameDialog(this->busInstance->name, names);
dialog->exec();
auto currentName = this->busInstance->name;
auto newName = dialog->getName();
Application::instance()->renameBus(currentName, newName);
});
menu.addSeparator();
menu.addAction(QString::fromStdString("Ukloni " + this->busInstance->name), [this]() {
Application::instance()->removeBus(this->busInstance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
menu.exec(event->screenPos());
}

View File

@ -3,6 +3,7 @@
#include <QLabel>
#include <QListWidget>
#include <QVBoxLayout>
#include <application.h>
namespace display {
@ -19,11 +20,10 @@ namespace display {
layout->addSpacing(8);
layout->addWidget(new QLabel("Buses:"));
layout->addWidget(busList, 1);
setLibrary(library);
}
void Library::setLibrary(std::optional<domain::Library> library) {
void Library::refreshContent() {
library = Application::instance()->getLibrary();
componentList->clear();
busList->clear();

View File

@ -13,7 +13,7 @@ namespace display {
public:
Library();
void setLibrary(std::optional<domain::Library> library);
void refreshContent();
private:
std::optional<domain::Library> library;

View File

@ -5,38 +5,21 @@
#include <set>
#include "name_dialog.h"
display::NameDialog::NameDialog(domain::ComponentInstance *instance, std::set<std::string>& names) : componentInstance(instance), usedNames(names) {
usedNames.erase(instance->name);
display::NameDialog::NameDialog(std::string currentName, std::set<std::string> &names): currentName(currentName) {
usedNames.erase(currentName);
auto *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Izmjeni ime", this));
edit = new QLineEdit(this);
edit->insert(instance->name.c_str());
edit->insert(currentName.c_str());
connect(edit, &QLineEdit::textChanged, this, &NameDialog::onNameUpdate);
layout->addWidget(edit);
this->setWindowTitle("Izmjeni ime");
setWindowTitle("Izmjeni ime");
button = new QPushButton("Ažuriraj", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
layout->addWidget(button);
this->setLayout(layout);
}
display::NameDialog::NameDialog(domain::BusInstance *instance, std::set<std::string>& names): busInstance(instance), usedNames(names) {
usedNames.erase(instance->name);
auto *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Izmjeni ime", this));
edit = new QLineEdit(this);
edit->insert(instance->name.c_str());
connect(edit, &QLineEdit::textChanged, this, &NameDialog::onNameUpdate);
layout->addWidget(edit);
this->setWindowTitle("Izmjeni ime");
button = new QPushButton("Ažuriraj", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
layout->addWidget(button);
this->setLayout(layout);
setLayout(layout);
}
void display::NameDialog::onNameUpdate(const QString &text) {
@ -48,12 +31,10 @@ void display::NameDialog::onNameUpdate(const QString &text) {
}
void display::NameDialog::onNameChange() {
if (componentInstance != nullptr) {
componentInstance->name = this->edit->text().toStdString();
} else if (busInstance != nullptr) {
busInstance->name = this->edit->text().toStdString();
currentName = edit->text().toStdString();
close();
}
this->close();
std::string display::NameDialog::getName() {
return currentName;
}

View File

@ -17,15 +17,13 @@ namespace display {
std::set<std::string> usedNames;
QLineEdit *edit = nullptr;
domain::ComponentInstance *componentInstance = nullptr;
domain::BusInstance *busInstance = nullptr;
std::string currentName;
QPushButton *button;
public:
NameDialog(std::string currentName, std::set<std::string>& names);
NameDialog(domain::ComponentInstance *instance, std::set<std::string>& names);
NameDialog(domain::BusInstance *instance, std::set<std::string>& names);
std::string getName();
public slots:
void onNameUpdate(const QString& text);

View File

@ -19,14 +19,15 @@ namespace display {
}
void Schema::setSchema(domain::Schema *_schema, std::optional<domain::Library> _library) {
void Schema::refreshContent() {
schema = Application::instance()->getSchema();
library = Application::instance()->getLibrary();
components.clear();
buses.clear();
scene.clear();
busConnections.clear();
this->schema = _schema;
this->library = _library;
if (schema != nullptr) {
for (auto &instance: schema->componentInstances) {
auto group = new display::ComponentGroup(instance);

View File

@ -46,14 +46,14 @@ namespace display {
std::vector<BusConnection *> busConnections;
std::vector<DirectConnection *> directConnections;
void setSchema(domain::Schema *schema, std::optional<domain::Library> library);
void updateConnections();
void removeConnectable(QPointF f);
void showConnectable(Pin *pin);
void refreshContent();
protected:
void dragEnterEvent(QDragEnterEvent *event) override;

View File

@ -153,6 +153,37 @@ namespace domain {
return nullopt;
}
std::vector<ValidationError>
ComdelValidator::validateMemoryReferences(Schema &schema, Library &library, ValidationContext context) {
std::set<std::string> memoryInstances;
for(auto& component: schema.componentInstances) {
if(component->component.getType() == Component::MEMORY) {
memoryInstances.insert(component->name);
}
}
std::vector<ValidationError> errors;
for(auto& component: schema.componentInstances) {
if(component->component.getType() == Component::PROCESSOR) {
for(auto& attribute: component->attributes) {
if(attribute.value.isType(Value::MEMORY_REFERENCE)) {
auto memoryReference = attribute.value.asMemoryReference();
if(memoryReference != nullopt) {
if(memoryInstances.count(*memoryReference) == 0) {
context.attributes["memoryReference"] = domain::Value::fromString(memoryReference.value());
auto message = populateMessage("There isn't a memory instance '{memoryReference}'", context);
errors.emplace_back(component.get(), nullptr, Action::ERROR, message);
}
}
}
}
}
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validateInstanceNames(Schema &schema, Library &library, ValidationContext context) {
std::set<std::string> names;

View File

@ -35,6 +35,8 @@ namespace domain {
std::optional<ValidationError> validateRule(Rule rule, ValidationContext context);
std::vector<ValidationError> validateMemoryReferences(Schema &schema, Library &library, ValidationContext context);
std::vector<ValidationError> validateInstanceNames(Schema &schema, Library &library, ValidationContext context);
std::vector<ValidationError> validateInstanceCount(Schema &schema, Library &library, ValidationContext context);

View File

@ -1,42 +1,14 @@
@source "frisc_library.csl"
@source "/home/bbr/Documents/Personal/FER/schema_editor/examples/simplified FRISC model/frisc_library.csl"
@schema {
@instance procesor_002 FRISC {
@position (0, 0)
@attribute _memory null
}
@instance mem Memorija {
@position (0, 250)
@attribute sinkroniziran false
@attribute brzina 1
@attribute kapacitet 1024
@attribute size 8
@attribute pocetnaAdresa 1024
}
@instance procesor_001 FRISC {
@position (-89, 74)
@attribute _memory null
}
@instance procesor_000 FRISC {
@position (-175, 195)
@attribute _memory null
@instance procesor FRISC {
@position (-177, -122)
@attribute _memory memorija2
}
@instance procesor FRISC {
@position (-195, 63)
@position (49, -97)
@attribute _memory null
}
@instance bus glavnaSabirnica {
@position (0, 200)
@size 100
}
@connection (procesor_002.glavniPin, bus) {
}
@connection (mem.glavniPin, bus) {
}
}

View File

@ -2,33 +2,13 @@
@schema {
@instance procesor FRISC {
@position (-76, -97)
@attribute _memory memorija
@position (-112, -89)
@attribute _memory memorija2
}
@instance memorija Memorija {
@position (52, 119)
@attribute sinkroniziran false
@attribute brzina 1
@attribute kapacitet 65536
@attribute size 8
@attribute pocetnaAdresa 0
@instance procesor FRISC {
@position (112, -89)
@attribute _memory null
}
@instance glavnaSabirnica glavnaSabirnica {
@position (-78, 88)
@size 100
}
@instance directRam directRam {
@position (0, 0)
@size 0
}
@connection (memorija.glavniPin, glavnaSabirnica) {
}
@connection (procesor.glavniPin, glavnaSabirnica) {
}
@connection (memorija.memDirect, directRam, procesor.memDirect) {
}
}

View File

@ -65,7 +65,7 @@ void MainWindow::onLoadLibrary() {
tr("Open library"), "/home", tr("Comdel library (*.csl)"));
if(!filename.isEmpty()) {
std::ostringstream output;
clear();
log->clear();
auto librarySource = filename.toStdString();
@ -74,8 +74,8 @@ void MainWindow::onLoadLibrary() {
log->appendPlainText(QString::fromStdString(output.str()));
}
libraryDisplay->setLibrary(instance->getLibrary());
schemaDisplay->setSchema(instance->getSchema(), instance->getLibrary());
libraryDisplay->refreshContent();
schemaDisplay->refreshContent();
}
}
@ -84,17 +84,19 @@ void MainWindow::onLoadSchema() {
tr("Open schema"), "/home", tr("Comdel schema (*.csl)"));
if(!filename.isEmpty()) {
std::ostringstream output;
clear();
log->clear();
auto schemaSource = filename.toStdString();
auto instance = Application::instance();
if(!instance->loadSchema(schemaSource, output)) {
auto result = instance->loadSchema(schemaSource, output);
if(!result.first){
formatErrors(result.second, output);
log->appendPlainText(QString::fromStdString(output.str()));
}
libraryDisplay->setLibrary(instance->getLibrary());
schemaDisplay->setSchema(instance->getSchema(), instance->getLibrary());
libraryDisplay->refreshContent();
schemaDisplay->refreshContent();
}
}
@ -176,12 +178,6 @@ void MainWindow::formatErrors(std::vector<domain::ValidationError>& errors, std:
}
}
void MainWindow::clear() {
log->clear();
this->schemaDisplay->setSchema(nullptr, std::nullopt);
this->libraryDisplay->setLibrary(std::nullopt);
}
MainWindow::~MainWindow()
{
delete ui;