Example #1
0
 def __init__(self, actions):
     for i, a in enumerate(actions):
         for a2 in actions[i + 1:]:
             assert a is not a2
             assert a2 not in a.mutex
             assert a not in a2.mutex
     self.actions = actions
     self.must_bits = BitMask.all_union([a.must_bits for a in actions])
     self.then_bits = BitMask.all_union([a.then_bits for a in actions])
Example #2
0
def findButton(msgGroup, sequence_len, timeout):
    if len(msgGroup) < sequence_len:
        return

    for byte_n in range(msgGroup.byte_n):
        mask_queue = deque([BitMask(0xFF)])

        while (mask_queue):
            mask = mask_queue.popleft()
            values = (msg[byte_n] & mask.get() for msg in msgGroup.data)

            timeline = []
            pair = deque(maxlen=2)

            for i, gr in enumerate(groupby(zip(values, msgGroup.timeline),
                                           lambda x: x[0]),
                                   start=1):
                val, period = next(gr[1])

                if i > 2 and val not in pair:
                    mask_queue.extend(mask.split())
                    break

                pair.appendleft(val)
                timeline.append(period)
            else:
                check_timeout = map(lambda prev, curr, t: (curr - prev) > t,
                                    timeline, timeline[1:], cycle(timeout))

                if i == sequence_len and all(check_timeout):
                    yield msgGroup.ID, mask.get(), byte_n, pair, timeline
Example #3
0
    def score_accomplishment_pool(self, pool, goal, start):
        """Scores an ActionPool, given a goal and initial state.

        This returns a tuple, with each item being a different tie-breaker"""
        remaining = goal.unset_from_action(pool.then_bits)
        requirement = pool.must_bits
        union = BitMask.all_union([remaining, requirement])
        if start:
            start_score = start.difference(union).count_set()
        else:
            start_score = 0
        if hasattr(pool, "action_count"):
            count_score = pool.action_count()
        else:
            count_score = 0
        score = (remaining.count_set(), union.count_set(), start_score, count_score)
        return score
Example #4
0
 def _parse_state_strings(self, state_strings):
     options = {}
     if isinstance(state_strings, str):
         state_strings = [
           line.strip()
           for line in state_strings.splitlines()
           if line.strip() and not line.strip().startswith("#")]
     init_state = BitMask.null(len(self.states))
     for set_state in state_strings:
         if set_state == "default_false":
             options['default_false'] = True
             continue
         bit = 1
         if set_state.startswith("not "):
             bit = 0
             set_state = set_state[4:]
         set_state_bit = self.state_bits[set_state]
         if init_state.is_set(set_state_bit):
             raise Exception("Tried to set state twice: %s" % set_state)
         init_state = init_state.add(set_state_bit, bit)
     return init_state, options
Example #5
0
 def __init__(self, name, exprs, bindings):
     self.name = name
     self.bindings = bindings
     self.constraints = [
       item
       for expr in exprs['constraints']
       for item in expr.expand(bindings)
     ]
     self.actions = [
       item
       for expr in exprs['actions']
       for item in expr.expand(bindings)
     ]
     self.states = [
       item
       for expr in exprs['states']
       for item in expr.expand(bindings)
     ]
     if not self.states:
         # Must be implied states, since none were given
         implied_states = set()
         for action in self.actions:
             old_implied = set(implied_states)
             implied_states.update([
                 state[4:] if state.startswith("not ") else state
                 for state in action.must + action.then])
             #print("Updated", action, "must", action.must, "then", action.then,
             #    "before", len(old_implied), "now", len(implied_states),
             #    "added", implied_states - old_implied)
         self.states = [State(name) for name in implied_states]
     self.states.sort(key=lambda x: x.name)
     for i, state in enumerate(self.states):
         state.domain = self
         state.bit = 1 << i
         state.bit_mask = BitMask(state.bit, state.bit, len(self.states))
     self.state_bits = dict((state.name, state.bit) for state in self.states)
     self.actions.sort(key=lambda x: x.action)
     self.null = BitMask.null(len(self.states))
     for action in self.actions:
         action.domain = self
         action.must_bits = BitMask.null(len(self.states))
         for must in action.must:
             bit = 1
             if must.startswith("not "):
                 bit = 0
                 must = must[4:]
             action.must_bits = action.must_bits.add(self.state_bits[must], bit)
         action.must_bits = self.apply_constraints(action.must_bits)
         action.then_bits = self.null
         for then in action.then:
             bit = 1
             if then.startswith("not "):
                 bit = 0
                 then = then[4:]
             action.then_bits = action.then_bits.add(self.state_bits[then], bit)
         try:
             action.then_bits = self.apply_constraints(action.then_bits)
         except IncompatibleConstraints as e:
             e.action = action
             raise
         action.then_bits = action.then_bits.carry_forward(action.must_bits)
     for action in self.actions:
         action.mutex = set()
         for other_action in self.actions:
             if other_action is action:
                 continue
             if action.is_mutex(other_action):
                 action.mutex.add(other_action)
Example #6
0
 def __init__(self, goal):
     self.must_bits = goal
     self.then_bits = BitMask.null(goal._width)