How to use __dfa_changed method in hypothesis

Best Python code snippet using hypothesis

lstar.py

Source:lstar.py Github

copy

Full Screen

...121 # end up searching to find a suitable candidate. By putting states in122 # a self-organisating list we ideally minimise the number of lookups.123 self.__self_organising_states = SelfOrganisingList(self.__states)124 self.start = 0125 self.__dfa_changed()126 def __dfa_changed(self):127 """Note that something has changed, updating the generation128 and resetting any cached state."""129 self.__generation += 1130 self.dfa = LearnedDFA(self)131 def is_accepting(self, i):132 """Equivalent to ``self.dfa.is_accepting(i)``"""133 return self.__states[i].accepting134 def label(self, i):135 """Returns the string label for state ``i``."""136 return self.__states[i].label137 def transition(self, i, c):138 """Equivalent to ``self.dfa.transition(i, c)```"""139 c = self.normalizer.normalize(c)140 state = self.__states[i]141 try:142 return state.transitions[c]143 except KeyError:144 pass145 # The state that we transition to when reading ``c`` is reached by146 # this string, because this state is reached by state.label. We thus147 # want our candidate for the transition to be some state with a label148 # equivalent to this string.149 #150 # We find such a state by looking for one such that all of its listed151 # experiments agree on the result for its state label and this string.152 string = state.label + bytes([c])153 # We keep track of some useful experiments for distinguishing this154 # string from other states, as this both allows us to more accurately155 # select the state to map to and, if necessary, create the new state156 # that this string corresponds to with a decent set of starting157 # experiments.158 accumulated = {}159 counts = Counter()160 def equivalent(t):161 """Checks if ``string`` could possibly lead to state ``t``."""162 for e, expected in accumulated.items():163 if self.member(t.label + e) != expected:164 counts[e] += 1165 return False166 for e, expected in t.experiments.items():167 result = self.member(string + e)168 if result != expected:169 # We expect most experiments to return False so if we add170 # only True ones to our collection of essential experiments171 # we keep the size way down and select only ones that are172 # likely to provide useful information in future.173 if result:174 accumulated[e] = result175 return False176 return True177 try:178 destination = self.__self_organising_states.find(equivalent)179 except NotFound:180 i = len(self.__states)181 destination = DistinguishedState(182 index=i,183 label=string,184 experiments=accumulated,185 accepting=self.member(string),186 )187 self.__states.append(destination)188 self.__self_organising_states.add(destination)189 state.transitions[c] = destination.index190 return destination.index191 def member(self, s):192 """Check whether this string is a member of the language193 to be learned."""194 try:195 return self.__member_cache[s]196 except KeyError:197 result = self.__member(s)198 self.__member_cache[s] = result199 return result200 @property201 def generation(self):202 """Return an integer value that will be incremented203 every time the DFA we predict changes."""204 return self.__generation205 def learn(self, string):206 """Learn to give the correct answer on this string.207 That is, after this method completes we will have208 ``self.dfa.matches(s) == self.member(s)``.209 Note that we do not guarantee that this will remain210 true in the event that learn is called again with211 a different string. It is in principle possible that212 future learning will cause us to make a mistake on213 this string. However, repeatedly calling learn on214 each of a set of strings until the generation stops215 changing is guaranteed to terminate.216 """217 string = bytes(string)218 correct_outcome = self.member(string)219 # We don't want to check this inside the loop because it potentially220 # causes us to evaluate more of the states than we actually need to,221 # but if our model is mostly correct then this will be faster because222 # we only need to evaluate strings that are of the form223 # ``state + experiment``, which will generally be cached and/or needed224 # later.225 if self.dfa.matches(string) == correct_outcome:226 return227 # In the papers they assume that we only run this process228 # once, but this is silly - often when you've got a messy229 # string it will be wrong for many different reasons.230 #231 # Thus we iterate this to a fixed point where we repair232 # the DFA by repeatedly adding experiments until the DFA233 # agrees with the membership function on this string.234 # First we make sure that normalization is not the source of the235 # failure to match.236 while True:237 normalized = bytes([self.normalizer.normalize(c) for c in string])238 # We can correctly replace the string with its normalized version239 # so normalization is not the problem here.240 if self.member(normalized) == correct_outcome:241 string = normalized242 break243 alphabet = sorted(set(string), reverse=True)244 target = string245 for a in alphabet:246 def replace(b):247 if a == b:248 return target249 return bytes([b if c == a else c for c in target])250 self.normalizer.distinguish(a, lambda x: self.member(replace(x)))251 target = replace(self.normalizer.normalize(a))252 assert self.member(target) == correct_outcome253 assert target != normalized254 self.__dfa_changed()255 if self.dfa.matches(string) == correct_outcome:256 return257 # Now we know normalization is correct we can attempt to determine if258 # any of our transitions are wrong.259 while True:260 dfa = self.dfa261 states = [dfa.start]262 def seems_right(n):263 """After reading n characters from s, do we seem to be264 in the right state?265 We determine this by replacing the first n characters266 of s with the label of the state we expect to be in.267 If we are in the right state, that will replace a substring268 with an equivalent one so must produce the same answer.269 """270 if n > len(string):271 return False272 # Populate enough of the states list to know where we are.273 while n >= len(states):274 states.append(dfa.transition(states[-1], string[len(states) - 1]))275 return self.member(dfa.label(states[n]) + string[n:]) == correct_outcome276 assert seems_right(0)277 n = find_integer(seems_right)278 # We got to the end without ever finding ourself in a bad279 # state, so we must correctly match this string.280 if n == len(string):281 assert dfa.matches(string) == correct_outcome282 break283 # Reading n characters does not put us in a bad state but284 # reading n + 1 does. This means that the remainder of285 # the string that we have not read yet is an experiment286 # that allows us to distinguish the state that we ended287 # up in from the state that we should have ended up in.288 source = states[n]289 character = string[n]290 wrong_destination = states[n + 1]291 # We've made an error in transitioning from ``source`` to292 # ``wrong_destination`` via ``character``. We now need to update293 # the DFA so that this transition no longer occurs. Note that we294 # do not guarantee that the transition is *correct* after this,295 # only that we don't make this particular error.296 assert self.transition(source, character) == wrong_destination297 labels_wrong_destination = self.dfa.label(wrong_destination)298 labels_correct_destination = self.dfa.label(source) + bytes([character])299 ex = string[n + 1 :]300 assert self.member(labels_wrong_destination + ex) != self.member(301 labels_correct_destination + ex302 )303 # Adding this experiment causes us to distinguish the wrong304 # destination from the correct one.305 self.__states[wrong_destination].experiments[ex] = self.member(306 labels_wrong_destination + ex307 )308 # We now clear the cached details that caused us to make this error309 # so that when we recalculate this transition we get to a310 # (hopefully now correct) different state.311 del self.__states[source].transitions[character]312 self.__dfa_changed()313 # We immediately recalculate the transition so that we can check314 # that it has changed as we expect it to have.315 new_destination = self.transition(source, string[n])316 assert new_destination != wrong_destination317class LearnedDFA(DFA):318 """This implements a lazily calculated DFA where states319 are labelled by some string that reaches them, and are320 distinguished by a membership test and a set of experiments."""321 def __init__(self, lstar):322 DFA.__init__(self)323 self.__lstar = lstar324 self.__generation = lstar.generation325 def __check_changed(self):326 if self.__generation != self.__lstar.generation:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful