示例#1
0
    def test_equal(self):
        v0 = Interval(0, 10)
        v1 = Interval(7, 15)

        nw = ConstraintNetwork()
        nw.set_equal(v0, v1)
        nw.minimize_network()
        assert v0 == v1
示例#2
0
def set_knowledge_base(knowledge):
    """
    Sets the knowledge base to be used in the interpretation, and updates
    the necessary global variables.
    """
    global KNOWLEDGE, _OBSERVABLES, _ABDUCIBLES, _LMAP, _EXCLUSION
    KNOWLEDGE = knowledge
    #First, we check the consistency of every single abstraction pattern
    for p in KNOWLEDGE:
        #In the Environment and Abstracted sets no repeated types can happen.
        for qset in (p.abstracted, p.environment):
            for q in qset:
                #The only coincidence must be q
                assert len(set(q.mro()) & qset) == 1
        #There should be no subclass relations between hyp. and abstractions
        for q in p.abstracted:
            assert not p.Hypothesis in q.mro()
        assert not set(p.Hypothesis.mro()) & p.abstracted
        #The abstraction transitions must be properly set.
        for q in p.abstractions:
            assert q in p.abstracted
            for tr in p.abstractions[q]:
                assert tr.observable is q
                assert tr.abstracted is ABSTRACTED
                assert tr in p.transitions
    #Organization of all the observables in abstraction levels.
    _OBSERVABLES = set.union(*(({p.Hypothesis} | p.abstracted | p.environment)
                               for p in KNOWLEDGE))
    _ABDUCIBLES = tuple(set.union(*(p.abstracted for p in KNOWLEDGE)))
    #To perform the level assignment, we use a constraint network.
    _CNET = ConstraintNetwork()
    #Mapping from observable types to abstraction levels.
    _LMAP = {}
    for q in _OBSERVABLES:
        _LMAP[q] = Variable(value=Interval(0, numpy.inf))
    #We set the restrictions, and minimize the network
    #All subclasses must have the same level than the superclasses
    for q in _OBSERVABLES:
        for sup in (set(q.mro()) & _OBSERVABLES) - {q}:
            _CNET.set_equal(_LMAP[q], _LMAP[sup])
    #Abstractions force a level increasing
    for p in KNOWLEDGE:
        for qabs in p.abstracted:
            _CNET.add_constraint(_LMAP[qabs], _LMAP[p.Hypothesis],
                                 Interval(1, numpy.inf))
    #Network minimization
    _CNET.minimize_network()
    #Now we assign to each observable the minimum of the solutions interval.
    for q in _OBSERVABLES:
        _LMAP[q] = int(_LMAP[q].start)
    #Manual definition of the exclusion relation between observables.
    _EXCLUSION = {
        Deflection: (Deflection, ),
        QRS: (QRS, ),
        TWave: (TWave, ),
        PWave: (PWave, ),
        CardiacCycle: (CardiacCycle, ),
        Cardiac_Rhythm: (Cardiac_Rhythm, )
    }
    #Automatic expansion of the exclusion relation.
    for q, qexc in _EXCLUSION.iteritems():
        _EXCLUSION[q] = tuple(
            (q2 for q2 in _OBSERVABLES if issubclass(q2, qexc)))
    for q in sorted(_OBSERVABLES, key=_LMAP.get, reverse=True):
        _EXCLUSION[q] = tuple(
            set.union(*(set(_EXCLUSION[q2]) for q2 in _EXCLUSION
                        if issubclass(q, q2))))