Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ set(GIT_HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/namespaces.h
${CMAKE_CURRENT_SOURCE_DIR}/parentedentity_p.h
${CMAKE_CURRENT_SOURCE_DIR}/reset_p.h
${CMAKE_CURRENT_SOURCE_DIR}/unionfind.h
${CMAKE_CURRENT_SOURCE_DIR}/units_p.h
${CMAKE_CURRENT_SOURCE_DIR}/utilities.h
${CMAKE_CURRENT_SOURCE_DIR}/variable_p.h
Expand Down
26 changes: 23 additions & 3 deletions src/internaltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@
using NodeAttributeNamespaceInfo = std::vector<std::tuple<std::string, std::string, std::string, std::string, std::string>>; /**< Type definition for attribute namespace information. */

// VariableMap
using VariableStdPair = std::pair<VariablePtr, VariablePtr>; /**< Type definition for Variable pointer pair using standard libary. */

Check failure on line 45 in src/internaltypes.h

View workflow job for this annotation

GitHub Actions / Check for spelling errors

libary ==> library
using VariableMap = std::vector<VariablePairPtr>; /**< Type definition for vector of VariablePair.*/
using VariableMapIterator = VariableMap::const_iterator; /**< Type definition of const iterator for vector of VariablePair.*/

// ComponentMap
using ComponentPair = std::pair<ComponentPtr, ComponentPtr>; /**< Type definition for Component pointer pair.*/
using ComponentMap = std::vector<ComponentPair>; /**< Type definition for vector of ComponentPair.*/
using ComponentMapIterator = ComponentMap::const_iterator; /**< Type definition of const iterator for vector of ComponentPair.*/
using ComponentStdPair = std::pair<ComponentPtr, ComponentPtr>; /**< Type definition for Component pointer pair using standard library.*/
using ComponentMap = std::vector<ComponentStdPair>; /**< Type definition for vector of ComponentStdPair.*/
using ComponentMapIterator = ComponentMap::const_iterator; /**< Type definition of const iterator for vector of ComponentStdPair.*/

using VariablePtrs = std::vector<VariablePtr>; /**< Type definition for list of variables. */

Expand Down Expand Up @@ -79,6 +80,25 @@
using ConnectionMap = std::map<VariablePtr, VariablePtr>; /**< Type definition for a connection map.*/
using NamePairList = std::vector<NamePair>; /**< Type definition for a list of a pair of names. */

using ComponentRawPtrPair = std::pair<const libcellml::Component*, const libcellml::Component*>;
using ConnectionIdMap = std::map<ComponentRawPtrPair, std::string>;

struct ComponentPair {

bool operator==(const ComponentPair& other) const {
return c1 == other.c1 && c2 == other.c2;
}

ComponentPtr c1;
ComponentPtr c2;
};

struct ComponentPairHash {
size_t operator()(const ComponentPair& p) const {
return std::hash<ComponentPtr>()(p.c1) ^ std::hash<ComponentPtr>()(p.c2);
}
};

/**
* @brief Class for defining an epoch in the history of a @ref Component or @ref Units.
*
Expand Down
4 changes: 2 additions & 2 deletions src/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ std::string printConnections(const ComponentMap &componentMap, const VariableMap
for (auto iterPair = componentMap.begin(); iterPair < componentMap.end(); ++iterPair) {
ComponentPtr currentComponent1 = iterPair->first;
ComponentPtr currentComponent2 = iterPair->second;
ComponentPair currentComponentPair = std::make_pair(currentComponent1, currentComponent2);
ComponentStdPair currentComponentPair = std::make_pair(currentComponent1, currentComponent2);
// Check whether this set of connections has already been serialised.
bool pairFound = false;
for (const auto &serialisedIterPair : serialisedComponentMap) {
Expand Down Expand Up @@ -178,7 +178,7 @@ void buildMapsForComponentsVariables(const ComponentPtr &component, ComponentMap
ComponentPtr component1 = owningComponent(variable);
ComponentPtr component2 = owningComponent(equivalentVariable);
// Also create a component map pair corresponding with the variable map pair.
ComponentPair componentPair = std::make_pair(component1, component2);
ComponentStdPair componentPair = std::make_pair(component1, component2);
componentMap.push_back(componentPair);
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/unionfind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright libCellML Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <unordered_map>

template<typename T>
class UnionFind {
public:
T find(const T& x) {
auto it = parent.find(x);
if (it == parent.end()) {
parent[x] = x;
rank[x] = 0;
return x;
}

if (it->second != x) {
it->second = find(it->second); // Path compression
}
return it->second;
}

void unite(const T& a, const T& b) {
T rootA = find(a);
T rootB = find(b);

if (rootA == rootB) return;

// Union by rank
if (rank[rootA] < rank[rootB]) {
parent[rootA] = rootB;
} else if (rank[rootA] > rank[rootB]) {
parent[rootB] = rootA;
} else {
parent[rootB] = rootA;
rank[rootA]++;
}
}

bool connected(const T& a, const T& b) {
return find(a) == find(b);
}

private:
std::unordered_map<T, T> parent;
std::unordered_map<T, int> rank;
};
108 changes: 103 additions & 5 deletions src/validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License.
#include "issue_p.h"
#include "logger_p.h"
#include "namespaces.h"
#include "unionfind.h"
#include "utilities.h"
#include "xmldoc.h"
#include "xmlutils.h"
Expand Down Expand Up @@ -639,8 +640,9 @@ class Validator::ValidatorImpl: public LoggerImpl
* @param component The component to check.
* @param idMap The IdMap object to construct.
* @param reportedConnections A set of connection identifiers to prevent duplicate reporting.
* @param connectionIds A map of connection identifiers to prevent duplicate reporting of connections.
*/
void buildComponentIdMap(const ComponentPtr &component, IdMap &idMap, std::set<std::string> &reportedConnections);
void buildComponentIdMap(const ComponentPtr &component, IdMap &idMap, std::set<std::string> &reportedConnections, const ConnectionIdMap &connectionIds);

/** @brief Utility function to add an item to the idMap.
*
Expand Down Expand Up @@ -2692,11 +2694,102 @@ void Validator::ValidatorImpl::addIdMapItem(const std::string &id, const std::st
}
}

void gatherComponents(const ComponentPtr &component, std::vector<ComponentPtr> &allComponents)
{
allComponents.push_back(component);
for (size_t c = 0; c < component->componentCount(); ++c) {
gatherComponents(component->component(c), allComponents);
}
}

IdMap Validator::ValidatorImpl::buildModelIdMap(const ModelPtr &model)
{
UnionFind<VariablePtr> uf;

// Traverse all components and variables
for (size_t c = 0; c < model->componentCount(); ++c) {
auto component = model->component(c);

for (size_t i = 0; i < component->variableCount(); ++i) {
auto v = component->variable(i);

for (size_t e = 0; e < v->equivalentVariableCount(); ++e) {
auto equiv = v->equivalentVariable(e);

if (equiv != nullptr) {
uf.unite(v, equiv);
}
}
}
}

std::unordered_map<VariablePtr, std::vector<VariablePtr>> groups;
for (size_t c = 0; c < model->componentCount(); ++c) {
auto component = model->component(c);

for (size_t i = 0; i < component->variableCount(); ++i) {
auto v = component->variable(i);
auto root = uf.find(v);

groups[root].push_back(v);
}
}

std::unordered_map<ComponentPair, std::vector<VariablePair>, ComponentPairHash> connectionMap;
// using VarPair>, ComponentPairHash> connectionMap;

// for (auto& [root, vars] : groups) {
// for (size_t i = 0; i < vars.size(); ++i) {
// for (size_t j = i + 1; j < vars.size(); ++j) {
// auto v1 = vars[i];
// auto v2 = vars[j];

// auto c1 = owningComponent(v1);
// auto c2 = owningComponent(v2);

// if (!c1 || !c2 || c1 == c2) continue;

// // Normalize ordering
// ComponentPair key = (c1 < c2)
// ? ComponentPair{c1, c2}
// : ComponentPair{c2, c1};

// connectionMap[key].emplace_back(v1, v2);
// }
// }
// }

IdMap idMap;
std::string info;
std::set<std::string> reportedConnections;

std::vector<ComponentPtr> allComponents;
for (size_t c = 0; c < model->componentCount(); ++c) {
gatherComponents(model->component(c), allComponents);
}

ConnectionIdMap connectionIds;
for (const auto &comp : allComponents) {
for (size_t i = 0; i < comp->variableCount(); ++i) {
auto item = comp->variable(i);
for (size_t e = 0; e < item->equivalentVariableCount(); ++e) {
auto equiv = item->equivalentVariable(e);
auto equivParent = owningComponent(equiv);
if (equivParent != nullptr) {
// Normalize the key order (min pointer first, max pointer second)
auto key = comp.get() < equivParent.get()
? std::make_pair(comp.get(), equivParent.get())
: std::make_pair(equivParent.get(), comp.get());

// If we haven't processed this component connection yet, do it once
if (connectionIds.find(key) == connectionIds.end()) {
connectionIds[key] = ""; //Variable::equivalenceConnectionId(item, equiv);
}
}
}
}
}

// Model.
if (!model->id().empty()) {
info = " - model '" + model->name() + "'";
Expand Down Expand Up @@ -2748,12 +2841,12 @@ IdMap Validator::ValidatorImpl::buildModelIdMap(const ModelPtr &model)

// Start recursion through encapsulation hierarchy.
for (size_t c = 0; c < model->componentCount(); ++c) {
buildComponentIdMap(model->component(c), idMap, reportedConnections);
buildComponentIdMap(model->component(c), idMap, reportedConnections, connectionIds);
}
return idMap;
}

void Validator::ValidatorImpl::buildComponentIdMap(const ComponentPtr &component, IdMap &idMap, std::set<std::string> &reportedConnections)
void Validator::ValidatorImpl::buildComponentIdMap(const ComponentPtr &component, IdMap &idMap, std::set<std::string> &reportedConnections, const ConnectionIdMap &connectionIds)
{
std::string info;

Expand Down Expand Up @@ -2807,7 +2900,12 @@ void Validator::ValidatorImpl::buildComponentIdMap(const ComponentPtr &component
addIdMapItem(mappingId, info, idMap);
}
// Connections.
auto connectionId = Variable::equivalenceConnectionId(item, equiv);
auto key = component.get() < equivParent.get()
? std::make_pair(component.get(), equivParent.get())
: std::make_pair(equivParent.get(), component.get());

auto connectionId = connectionIds.at(key);
// auto connectionId = Variable::equivalenceConnectionId(item, equiv);
std::string connection = component->name() < equivParent->name() ? component->name() + equivParent->name() : equivParent->name() + component->name();
if ((s1 < s2) && !connectionId.empty() && (reportedConnections.count(connection) == 0)) {
std::string connectionDescription =
Expand Down Expand Up @@ -2879,7 +2977,7 @@ void Validator::ValidatorImpl::buildComponentIdMap(const ComponentPtr &component

// Child components.
for (size_t c = 0; c < component->componentCount(); ++c) {
buildComponentIdMap(component->component(c), idMap, reportedConnections);
buildComponentIdMap(component->component(c), idMap, reportedConnections, connectionIds);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ include(units/tests.cmake)
include(validator/tests.cmake)
include(variable/tests.cmake)
include(version/tests.cmake)
include(investigations/tests.cmake)

set(TEST_EXPORTDEFINITIONS_H "${CMAKE_CURRENT_BINARY_DIR}/test_exportdefinitions.h")

Expand Down
Loading
Loading