def beamStep(self, re): newRegexes = list() # generate merged steps for stateID1 in list((s.ID_) for s in re.states_.values()): for stateID2 in list((s.ID_) for s in re.states_.values()): if stateID1 == stateID2: continue newRe = re.copy() newRe.mergeRandom(stateID1, stateID2) newRegexes.append((newRe, self.likelihood(newRe))) # generate wildcard steps for stateID1 in list((s.ID_) for s in re.states_.values()): for wildcard in ['S', 'N', 'A']: for k, s in re.states_[stateID1].next_: if keysOverlap(k, wildcard) and keyMinus(wildcard, k) != '': newRe = re.copy() newRe.wildcardize(stateID1, wildcard) newRegexes.append((newRe, self.likelihood(newRe))) # only replace one of the transitions for a wildcard break return newRegexes
def subsetConstruction(self): # copy old old = self.copy() # clear values self.mergeQueue_ = list() self.states_ = dict() self.lastStateID_ = -1 # initialize DFA self.start_ = State(regex=self, start=True) self.start_.accept_ = old.start_.accept_ stateMapping = {0: [0]} stateQueue = [0] while len(stateQueue) > 0: stateID = stateQueue.pop(0) state = self.states_[stateID] if DEBUG: print "\nstate", stateID, "=", stateMapping[stateID] # create distinct transitons newNext = list() for mappedID in stateMapping[stateID]: for key1, nextStateID in list( (key, s.ID_) for key, s in old.states_[mappedID].next_): add = True newNext2 = list() for key2, nextStateIDs in newNext: # if there is overlap, break transitions into pieces if keysOverlap(key1, key2): newNext2.append((keyIntersect(key1, key2), nextStateIDs + [nextStateID])) if DEBUG: print "intersect leads to", (keyIntersect( key1, key2), nextStateIDs + [nextStateID]) temp = keyMinus(key2, key1) if len(temp) > 0: if DEBUG: print "modified to lead to", (temp, nextStateIDs) newNext2.append((temp, nextStateIDs)) key1 = keyMinus(key1, key2) # otherwise, keep the transition else: newNext2.append((key2, nextStateIDs)) if len(key1) > 0: if DEBUG: print "added to lead to", (key1, [nextStateID]) newNext2.append((key1, [nextStateID])) newNext = newNext2 if DEBUG: print "adding states", newNext # add state mappings for key, nextStateIDs1 in newNext: addSet = True # map to preexising state if there is one for nextStateID2, nextStateIDs2 in stateMapping.items(): if set(nextStateIDs1) == set(nextStateIDs2): addSet = False state.nextIs(key, self.states_[nextStateID2]) break # otherwise, create one if addSet: newState = State(self) state.nextIs(key, newState) stateMapping[newState.ID_] = nextStateIDs1 for mappedID in nextStateIDs1: newState.accept_ = old.states_[mappedID].accept_ if newState.accept_: break stateQueue.append(newState.ID_)
def subsetConstruction(self): # copy old old = self.copy() # clear values self.mergeQueue_ = list() self.states_ = dict() self.lastStateID_ = -1 # initialize DFA self.start_ = State(regex=self, start=True) self.start_.accept_ = old.start_.accept_ stateMapping = {0 : [0]} stateQueue = [0] while len(stateQueue) > 0: stateID = stateQueue.pop(0) state = self.states_[stateID] if DEBUG: print "\nstate", stateID, "=", stateMapping[stateID] # create distinct transitons newNext = list() for mappedID in stateMapping[stateID]: for key1, nextStateID in list((key, s.ID_) for key,s in old.states_[mappedID].next_): add = True newNext2 = list() for key2, nextStateIDs in newNext: # if there is overlap, break transitions into pieces if keysOverlap(key1, key2): newNext2.append((keyIntersect(key1, key2), nextStateIDs + [nextStateID])) if DEBUG: print "intersect leads to", (keyIntersect(key1, key2), nextStateIDs + [nextStateID]) temp = keyMinus(key2, key1) if len(temp) > 0: if DEBUG: print "modified to lead to", (temp, nextStateIDs) newNext2.append((temp, nextStateIDs)) key1 = keyMinus(key1, key2) # otherwise, keep the transition else: newNext2.append((key2, nextStateIDs)) if len(key1) > 0: if DEBUG: print "added to lead to", (key1, [nextStateID]) newNext2.append((key1, [nextStateID])) newNext = newNext2 if DEBUG: print "adding states", newNext # add state mappings for key, nextStateIDs1 in newNext: addSet = True # map to preexising state if there is one for nextStateID2, nextStateIDs2 in stateMapping.items(): if set(nextStateIDs1) == set(nextStateIDs2): addSet = False state.nextIs(key, self.states_[nextStateID2]) break # otherwise, create one if addSet: newState = State(self) state.nextIs(key, newState) stateMapping[newState.ID_] = nextStateIDs1 for mappedID in nextStateIDs1: newState.accept_ = old.states_[mappedID].accept_ if newState.accept_: break stateQueue.append(newState.ID_)