Commit 623418e2 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

Merge branch 'feature/fix-python-constraints-matrix' into 'master'

Feature/fix python constraints matrix

See merge request tools/frackit!131
parents 60879a46 dbda15b9
......@@ -36,6 +36,9 @@ namespace Frackit {
class Id
{
public:
//! export underlying index type
using IndexType = std::size_t;
//! Default constructor
Id() = default;
......@@ -64,4 +67,30 @@ private:
} // end namespace Frackit
// define hash function for Id
namespace std {
template<>
struct hash<Frackit::Id>
{
using argument_type = Frackit::Id;
using result_type = std::size_t;
std::size_t operator()(const Frackit::Id& id) const noexcept
{ return hash<std::size_t>()(id.get()); }
};
template<>
struct hash<const Frackit::Id>
{
using argument_type = Frackit::Id;
using result_type = std::size_t;
std::size_t operator()(const Frackit::Id& id) const noexcept
{ return hash<std::size_t>()(id.get()); }
};
} // end namespace std
#endif // FRACKIT_ID_HH
......@@ -34,6 +34,7 @@ void registerId(py::module& module)
cls.def(py::init<std::size_t>());
cls.def("get", &Id::get, "return the id");
cls.def(py::self == Id());
cls.def(hash(py::self));
}
} // end namespace Frackit::Python
......
......@@ -43,9 +43,9 @@ class EntityNetworkConstraintsMatrix:
id1 = pair[0]
id2 = pair[1]
if self.constraintsMatrix.get(id1) == None:
self.constraintsMatrix[id1] = {id2 : [constraints]}
self.constraintsMatrix[id1] = {id2 : constraints}
elif self.constraintsMatrix.get(id1).get(id2) == None:
self.constraintsMatrix[id1][id2] = [constraints]
self.constraintsMatrix[id1][id2] = constraints
else:
raise RuntimeError("Id pair already taken!")
......@@ -67,20 +67,26 @@ class EntityNetworkConstraintsMatrix:
entity: the entity candidate for which the constraints are to be checked
id: the id of the set to which the entity candidate belongs.
"""
for setId in self.constraintsMatrix:
entitySet = entitySets.get(setId)
# no constraints registered for this id
if self.constraintsMatrix.get(id) == None:
return True
for otherSetId in self.constraintsMatrix:
entitySet = entitySets.get(otherSetId)
# set is empty, skip rest
if entitySet == None:
continue
elif self.constraintsMatrix.get(setId).get(id) != None:
# a constraint is defined between this set and the given id
# evaluate all constraints defined for this pair
constraints = self.constraintsMatrix.get(setId).get(id)
if any(not c.evaluate(entitySet, entity) for c in constraints):
elif self.constraintsMatrix.get(id).get(otherSetId) != None:
# a constraint is defined between the set with the given id
# and the set with otherSetId. Evaluate constraints for this pair.
constraints = self.constraintsMatrix.get(id).get(otherSetId)
if not constraints.evaluate(entitySet, entity):
return False
# no constraints violation detected
return True
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment