def _direct_left_corner(elem: iAntlr4GramElem, dlc: SortedSet, allow_eps_in_sel=False): """ Collect all possible symbols which can appear on the beggining """ if isinstance(elem, Antlr4Symbol): dlc.add(elem.symbol) return False elif isinstance(elem, Antlr4Iteration): _direct_left_corner( elem.body, dlc, allow_eps_in_sel=allow_eps_in_sel) return not elem.positive elif isinstance(elem, Antlr4Sequence): for e in elem: if not _direct_left_corner( e, dlc, allow_eps_in_sel=allow_eps_in_sel): break return True elif isinstance(elem, Antlr4Selection): for e in elem: can_be_eps = _direct_left_corner( e, dlc, allow_eps_in_sel=allow_eps_in_sel) if not allow_eps_in_sel: assert not can_be_eps, elem elif isinstance(elem, Antlr4Option): _direct_left_corner( elem.body, dlc, allow_eps_in_sel=allow_eps_in_sel) return True elif isinstance(elem, (Antlr4Indent, Antlr4Newline)): return True else: raise TypeError(elem)
class VoteDatabase(object): def __init__(self): self.batches = SortedSet(key=lambda b: b.id) pass def record(self, vote_batch): self.batches.add(vote_batch) @property def votes(self): return reduce(lambda a, b: a.votes.union(b.votes), self.batches, set())
def _bugs(self): covered_bugs = reduce(lambda a, b: a.union(b), map(lambda c: frozenset(c.bugs), self.chunks), set()) result = SortedSet(key=lambda b: b.fully_qualified_name) for bug in SortedSet(covered_bugs, key=lambda b: b.fully_qualified_name): rand = Random() bug_test_hash = hash((self.fully_qualified_name, bug.fully_qualified_name)) rand.seed(bug_test_hash) p = rand.random() if p <= self.effectiveness: result.add(bug) return result
def index_search_terms(word, hotel_details): ''' :param word: :param hotel_details: ''' try: if DataStore().indexed_search_terms.get(word) is not None: DataStore().indexed_search_terms[word.lower()].append(hotel_details.hotel_id) else: hotel_names = SortedSet() hotel_names.add(word.lower()) word_len = len(word) DataStore().indexed_search_terms[word.lower()[0:(word_len if word_len <= 1 else 2)]] = word except Exception, ex: logger.exception(ex) raise ex
def chunk_indexes(self): """ The indexes to chunks in the sorted set of chunks of this tests's parent feature that this tests touches. """ shuffled_indexes = range(0, self.feature.size) shuffler = Random(self.logical_name) shuffler.shuffle(shuffled_indexes) result = SortedSet() rand = Random(self.logical_name) for chunk_index in shuffled_indexes: p = rand.random() if p <= self.efficiency ** (len(result)): result.add(chunk_index) return result
def chunk_indexes(self): """ The indexes to chunks in the sorted set of chunks of this tests's parent feature that this tests touches. """ shuffled_indexes = range(0, self.feature.size) shuffler = Random(self.logical_name) shuffler.shuffle(shuffled_indexes) result = SortedSet() rand = Random(self.logical_name) for chunk_index in shuffled_indexes: p = rand.random() if p <= self.efficiency**(len(result)): result.add(chunk_index) return result
def _bugs(self): covered_bugs = reduce(lambda a, b: a.union(b), map(lambda c: frozenset(c.bugs), self.chunks), set()) result = SortedSet(key=lambda b: b.fully_qualified_name) for bug in SortedSet(covered_bugs, key=lambda b: b.fully_qualified_name): rand = Random() bug_test_hash = hash( (self.fully_qualified_name, bug.fully_qualified_name)) rand.seed(bug_test_hash) p = rand.random() if p <= self.effectiveness: result.add(bug) return result
def simple_visual(data_set): events = prepare_events(data_set) state = SortedSet() # (point, event, [skey]) all_lines = LinesCollection(data_set, color='gray') yield Scene([], [all_lines]) def get_neighbours(segment): index = state.index(segment) return ( state[index - 1].segment if index > 0 else None, #above state[index + 1].segment if index < len(state) - 1 else None ) #bellow def check_if_intersection_exists(segment, neighbour, orientation): if neighbour and segment: point = get_intersection_point(segment, neighbour, orientation) return point def check_intersections(state, skey): above, below = get_neighbours(skey) return check_if_intersection_exists(skey.segment, above, BELOW) or \ check_if_intersection_exists(skey.segment, below, ABOVE) for point, event, [skey] in iter_events(events): if event == START: state.add(skey) ipoint = check_intersections(state, skey) elif event == END: above, below = get_neighbours(skey) state.remove(skey) ipoint = check_if_intersection_exists(above, below, ABOVE) yield Scene([ PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue') ]) if ipoint: break yield Scene([ PointsCollection([ipoint], color='green'), ], [all_lines])
class Actor(): state = None environment = None heuristic = None frontier = None explored = None c = 0 f = 0 actions = [] def __init__(self, position, environment): self.state = State(position, position) self.environment = environment self.heuristic = manhattan_distance self.frontier = SortedSet(key = self.frontier_order) self.explored = set() self.reset() def update(self, action): self.state = action self.state.parent = None def move(self, position): self.state.target = position def can_act(self): return len(self.actions) def reset(self): self.actions = [] self.frontier.clear() self.explored.clear() self.explored.add(self.state) self.c = 0 self.f = 0 def frontier_order(self, state): return -state.path_cost def act(self): # Recalculate when bumping #if sensors[BUMPED] == 1: # del self.actions # No action is needed if we are at the target if self.state.goal(): return None result = True if not self.can_act(): # Reset values self.reset() # Think result = self.think(self.state) # If a route is already calculated, return next action if (result): return self.actions.pop() else: print "No solution found" return None def think(self, state): # Define the initial frontier self.expand_frontier(state) frontier_size = 1 while frontier_size: self.c += 1 # Get lowest valued frontier state state = self.frontier.pop() # Check for goal if state.goal(): self.recreate_actions(state) return True # Add current state to explored self.explored.add(state.as_tuple()) # Expand frontier self.expand_frontier(state) frontier_size = len(self.frontier) # DEBUG """s = '' for i in self.frontier: s += "{}:({},{}) ".format(i.cost, i.row(), i.column()) print s""" # DEBUG return False def expand_frontier(self, state): for row in (-1, 0, 1): for col in (-1, 0, 1): # Only allow adjacent non-diagonal moves #if row != 0 and col != 0: # continue # Get the new position position = Position(state.row() + row, state.column() + col) # Rule out invalid positions if position.row() < 0 or position.column() < 0 or \ position.row() >= self.environment.height or position.column() >= self.environment.width: return p = position.as_tuple() # If not an obstacle and not explored, then add to frontier if p not in self.environment.obstacles and p not in self.explored: self.f += 1 # Create the new state new_state = State(position, state.target, state.cost + 1, state) # Update state path cost new_state.path_cost = new_state.cost + self.heuristic(new_state) # Add to frontier self.frontier.add(new_state) def recreate_actions(self, state): while state is not None: self.actions.append(state) state = state.parent
class SoftwareSystem(object): def __init__(self, probability_gain_feature_dependency=0.5, probability_lose_feature_dependency=0.25, probability_gain_system_dependency=0.1, probability_lose_system_dependency=0.25, probability_new_bug=0.5, probability_debug_known=0.9, probability_debug_unknown=0.01, probability_failure_on_demand=0.01, test_effectiveness=0.5, test_efficiency=0.5 ): self.probability_gain_feature_dependency = probability_gain_feature_dependency self.probability_lose_feature_dependency = probability_lose_feature_dependency self.probability_gain_system_dependency = probability_gain_system_dependency self.probability_lose_system_dependency = probability_lose_system_dependency self.probability_new_bug = probability_new_bug self.probability_debug_known = probability_debug_known self.probability_debug_unknown = probability_debug_unknown self.probability_failure_on_demand = probability_failure_on_demand self.test_effectiveness = test_effectiveness self.test_efficiency = test_efficiency self.features = SortedSet(key=lambda f: f.logical_name) self.successful_operations = list() def add_feature(self, logical_name, size): feature = Feature(self, logical_name, size) self.features.add(feature) return feature def get_feature(self, logical_name): result = filter(lambda f: f.logical_name == logical_name, self.features) if len(result) is 0: return None else: return result[0] def get_chunk(self, fully_qualified_name): result = filter(lambda chunk: chunk.fully_qualified_name == fully_qualified_name, self.chunks) if len(result) is 0: return None else: return result[0] @property def chunks(self): chunk_sets = map(lambda f: frozenset(f.chunks), self.features) return reduce(lambda a, b: a.union(b), chunk_sets, SortedSet(key=lambda c: c.fully_qualified_name)) @property def chunk_names(self): return map(lambda c: c.fully_qualified_name, self.chunks) @property def chunk_contents(self): return map(lambda c: c.local_content, self.chunks) @property def tests(self): test_sets = map(lambda f: frozenset(f.tests), self.features) return reduce(lambda a, b: a.union(b), test_sets, SortedSet(key=lambda test: test.fully_qualified_name)) @property def bugs(self): bug_sets = map(lambda c: frozenset(c.bugs), self.chunks) return reduce(lambda a, b: a.union(b), bug_sets, SortedSet(key=lambda bug: bug.fully_qualified_name)) def operate(self, random, limit=sys.maxint): current_operations = list() self.successful_operations.append(current_operations) if len(self.features) == 0: return while len(current_operations) < limit: next_feature = random.choice(self.features) next_feature.operate(random) current_operations.append(next_feature) @property def last_trace(self): """ :return : the last sequence of successful operations called by operate. """ last_trace_index = len(self.successful_operations) - 1 return None if last_trace_index < 0 else self.successful_operations[last_trace_index] @property def mean_operations_to_failure(self): total_operations = reduce(lambda x, y: x + y, map(lambda l: len(l), self.successful_operations), 0) if len(self.successful_operations) is 0: return 0 else: return total_operations / len(self.successful_operations) def __str__(self): result = [] for feature in self.features: result.append(" ") result.append(repr(feature)) result.append("[\n") for chunk in feature.chunks: result.append(" ") result.append(str(chunk)) result.append("\n") result.append("]\n") result.append("[\n") for test in self.tests: result.append(" ") result.append(str(test)) result.append("\n") result.append("]") return "".join(result)
def algo_visual(data_set): events = prepare_events(data_set) state = SortedSet() # (point, event, [skey]) intersections = [] points = [] all_lines = LinesCollection(data_set, color='gray') yield Scene([], [all_lines]) def get_neighbours(segment): index = state.index(segment) return ( state[index - 1].segment if index > 0 else None, #above state[index + 1].segment if index < len(state) - 1 else None ) #bellow def swap_on(point, segments): above, below = segments state.remove(above) state.remove(below) above.op = below.op = point state.add(above) state.add(below) def add_intersection_if_exists(segment, neighbour, orientation): if neighbour and segment: point = get_intersection_point(segment, neighbour, orientation) if point: inter = tuple(sorted([segment, neighbour])) if inter not in intersections: intersections.append(inter) points.append(point) events.put( (point, INTERSECT, [Key(segment), Key(neighbour)])) def add_intersections(state, skey): above, below = get_neighbours(skey) add_intersection_if_exists(skey.segment, above, BELOW) add_intersection_if_exists(skey.segment, below, ABOVE) for point, event, segments in iter_events(events): if event == START: [skey] = segments state.add(skey) add_intersections(state, skey) above, below = get_neighbours(skey) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), *([LinesCollection([above], color='red')] if above else []), *([LinesCollection([below], color='green')] if below else []) ]) elif event == END: [skey] = segments above, below = get_neighbours(skey) state.remove(skey) add_intersection_if_exists(above, below, ABOVE) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), *([LinesCollection([above], color='red')] if above else []), *([LinesCollection([below], color='green')] if below else []), ]) else: above, below = segments swap_on(point, segments) add_intersections(state, above) add_intersections(state, below) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), LinesCollection([below.segment], color='red'), LinesCollection([above.segment], color='green') ]) yield Scene([PointsCollection(points[:], color='green')], [all_lines])