Commit 60640b13 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

[python][constraints] add constraints matrix

parent e6ff13a8
......@@ -28,6 +28,62 @@ class EntityNetworkConstraints(_entitynetwork._EntityNetworkConstraints):
return super().evaluate(a, b)
# python implementation of the constraints matrix class
class EntityNetworkConstraintsMatrix:
def __init__(self):
self.constraintsMatrix = {}
def addConstraints(self, constraints, idPairs):
def addIdPair(pair):
if len(pair) != 2:
raise RuntimeError("Id pairs are expected to be lists of length 2")
id1 = pair[0]
id2 = pair[1]
if self.constraintsMatrix.get(id1) == None:
self.constraintsMatrix[id1] = {id2 : [constraints]}
elif self.constraintsMatrix.get(id1).get(id2) == None:
self.constraintsMatrix[id1][id2] = [constraints]
raise RuntimeError("Id pair already taken!")
if not isinstance(idPairs, list):
raise RuntimeError("This expects id pairs in the form of index lists")
if not isinstance(idPairs[0], list): # single id pair
for pair in idPairs:
def evaluate(self, entitySets, entity, id):
Evaluates the constraints for all entity sets that have constraints defined
against the entity set with the given id.
entitySets: dictionary that maps ids to lists of entities
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)
# set is empty, skip rest
if entitySet == None:
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):
return False
return True
# python implementation of the entity network builder class
class EntityNetworkBuilder(_entitynetwork._EntityNetworkBuilderWrapper):
Markdown is supported
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