def append_buffer_template(self): templatename = self.dlyname transitions = [] locations = [] initlocation = pyuppaal.Location(name="idle") locations.append(initlocation) delaylocation = pyuppaal.Location( name="in_transit", invariant="x<=DELAY") locations.append(delaylocation) transition = pyuppaal.Transition( source=initlocation, target=delaylocation, assignment="x:=0", synchronisation="in?") transitions.append(transition) transition = pyuppaal.Transition( source=delaylocation, target=initlocation, synchronisation="out!") transitions.append(transition) template = pyuppaal.Template( templatename, initlocation=initlocation, locations=locations, transitions=transitions, declaration="clock x;", parameter="broadcast chan &in, broadcast chan &out") template.assign_ids() template.layout() self.templates.append(template)
def create_transition(template_name, source, target, synch=""): """ Creates a new transition from given source to given target with given synchronisation. Args: source: Source location. target: Target location. synch: Synchronisation name. Returns: Index o the created transition in the current_template's transitions list. """ global _nta if synch and " " + synch[:-1] + ";" not in _nta.declaration: _nta.declaration += "chan " + synch[:-1] + ";\n" _templates[template_name].transitions.append( pyuppaal.Transition( source=_templates[template_name].get_location_by_name(source), target=_templates[template_name].get_location_by_name(target), synchronisation=synch)) return len(_templates[template_name].transitions) - 1
def add_path(template, path_source, path_target, n, li, clocks, lower_bounds, upper_bounds, final_guard): """ Add n locations from path_source to path_target starting from li (integer). Add n+1 transitions to form a path. Given a set of clocks, a period and a threshold for each clock add resets and guards such that: In particular, bounds is a list of tuples of the form clock index ind - period p - bound b: clocks[ind] is reset and checked in every p-th transition with threshold b (upper or lower) """ source = template.get_location_by_name(path_source) # Transition leaving l_0 target_name = 'l' + str(li) target = pyuppaal.Location(name=target_name) template.locations += [target] # Reset each clock except the last one on transitions leaving l_0 reset_str = ' , '.join([clocks[i] + " = 0 " for i in range(len(clocks))]) template.transitions += [ pyuppaal.Transition(source=source, target=target, guard='', assignment=reset_str) ] source = template.get_location_by_name(target_name) for i in range(1, n + 1): id = i + li target_name = 'l' + str(id) if i == n: # the target is path_target, no need to add target_name = path_target target = template.get_location_by_name(target_name) else: target = pyuppaal.Location(name=target_name) template.locations += [target] clock_ind_lower = [ ] # the indices of clocks that will be checked and reset on source to target clock_ind_upper = [] for j in range(len(lower_bounds)): if i % lower_bounds[j][1] == 0: clock_ind_lower += [j] for j in range(len(upper_bounds)): if i % upper_bounds[j][1] == 0: clock_ind_upper += [j] guard_str_lower = ' && '.join([ clocks[lower_bounds[j][0]] + " >= " + str(lower_bounds[j][2]) for j in clock_ind_lower ]) guard_str_upper = ' && '.join([ clocks[upper_bounds[j][0]] + " <= " + str(upper_bounds[j][2]) for j in clock_ind_upper ]) if guard_str_lower and guard_str_upper: guard_str = guard_str_lower + ' && ' + guard_str_upper elif guard_str_upper: guard_str = guard_str_upper elif guard_str_lower: guard_str = guard_str_lower else: guard_str = '' if i == n: guard_str = final_guard if not guard_str else final_guard + " && " + guard_str reset_indices = [lower_bounds[j][0] for j in clock_ind_lower] + \ [upper_bounds[j][0] for j in clock_ind_upper] reset_str = ' , '.join([clocks[i] + " = 0 " for i in reset_indices]) template.transitions += [ pyuppaal.Transition(source=source, target=target, guard=guard_str, assignment=reset_str) ] source = template.get_location_by_name(target_name) return template
def append_iut_template(self): templatename=self.iutname # create one location locations = [] initlocation = pyuppaal.Location(name="id0") locations.append(initlocation) #create a transition for each input with "ch<input>iut?" sync #two sets of channels exists, those used by the environment "ch<input>" and those used by the iut "ch<input>iut" #the iut channels are delayed through a buffer template to model the adapter latency transitions = [] for sym in self.inputs: #false guard transition #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_off", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, guard=sym+"==0", synchronisation="i_"+sym+"?") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) #true guard transition #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_on", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, guard=sym+"==1", synchronisation="i_"+sym+"?") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) for sym in self.outputs: #assign a true value to an output and sync #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_on", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, assignment=sym+":=1", synchronisation="o_"+sym+"!") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) #assign a false value to an output and sync #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_off", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, assignment=sym+":=0", synchronisation="o_"+sym+"!") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) template = pyuppaal.Template( templatename, initlocation=initlocation, locations=locations, transitions=transitions) template.assign_ids() template.layout() self.templates.append(template)
def append_env_template(self): templatename=self.envname # create one location locations = [] initlocation = pyuppaal.Location(name="id0") locations.append(initlocation) #create a transition for each output with "ch<output>?" sync transitions = [] for sym in self.outputs: #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_on", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, guard=sym+"==1", synchronisation="o_"+sym+"_env?") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_off", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, guard=sym+"==0", synchronisation="o_"+sym+"_env?") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) for sym in self.inputs: #assign a value to an output and sync #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_off", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, assignment=sym+":=0", synchronisation="i_"+sym+"_env!") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) #create a committed location simply to spread the graph out location = pyuppaal.Location( name=sym+"_on", committed=True) locations.append(location) transition = pyuppaal.Transition( source=initlocation, target=location, assignment=sym+":=1", synchronisation="i_"+sym+"_env!") transitions.append(transition) transition = pyuppaal.Transition( source=location, target=initlocation) transitions.append(transition) template = pyuppaal.Template( templatename, initlocation=initlocation, locations=locations, transitions=transitions) template.assign_ids() template.layout() self.templates.append(template)
def Product(ta1, ta2): productTA = pyuppaal.Template("TA_Product", declaration=ta1.declaration + "\n" + ta2.declaration) ##set of locations in the resulting TA (initially empty)## locations = {} ##Variable used to count the number of final locations in the resulting TA used for purposes such as labeling locations in the TA.## #finalLocCounter = 0 ### final locations F (F = F_TA1*F_TA2) finalLocF = [] ### final locations Fneg (intersection of two TA's, "finalLocFneg = F_TA1*neg(F_TA2)") finalLocFneg = [] ### Computing locations in the product automaton ###### for loc1 in ta1.locations: for loc2 in ta2.locations: ##Labels of accepting locations in the input TAs should start with "Final".## locID = str(loc1.name) + str(loc2.name) l_product = pyuppaal.Location(name=locID) l_product.id = locID productTA.locations.append(l_product) locations[(loc1, loc2)] = l_product if str(loc1.invariant): if str(loc2.invariant): l_product.invariant = pyuppaal.Label( "invariant", str(loc1.invariant) + ' && ' + str(loc2.invariant)) else: l_product.invariant = pyuppaal.Label( "invariant", str(loc1.invariant)) elif str(loc2.invariant): l_product.invariant = pyuppaal.Label("invariant", str(loc2.invariant)) if ta1.initlocation == loc1 and ta2.initlocation == loc2: productTA.initlocation = l_product ### Computing final location set F ### for loc in productTA.locations: if str(loc.name).find("Final") != -1 and str(loc.name).find( "Final", 6) != -1: #print "final location is.."+str(loc.name) #finalLocF.append(loc) finalLocF.append(str(loc.name)) ### Computing final location set Fneg ### for loc in productTA.locations: if str(loc.name).find("Final", 0, 6) != -1 and str(loc.name).find( "Final", 6) == -1: #print "finalneg location is.."+str(loc.name) #finalLocFneg.append(loc) finalLocFneg.append(str(loc.name)) ### Transitions in the product automaton ################# for tr1 in ta1.transitions: for tr2 in ta2.transitions: if str(tr1.synchronisation) == str(tr2.synchronisation): tr_product = pyuppaal.Transition(source=locations[(tr1.source,tr2.source)], \ target = locations[(tr1.target,tr2.target)], \ synchronisation = str(tr1.synchronisation)) if str(tr1.guard): if str(tr2.guard): tr_product.guard = pyuppaal.Label( "guard", str(tr1.guard) + ' && ' + str(tr2.guard)) else: tr_product.guard = pyuppaal.Label( "guard", str(tr1.guard)) elif str(tr2.guard): tr_product.guard = pyuppaal.Label("guard", str(tr2.guard)) if str(tr1.assignment): if str(tr2.assignment): tr_product.assignment = pyuppaal.Label( "assignment", str(tr1.assignment) + ', ' + str(tr2.assignment)) else: tr_product.assignment = pyuppaal.Label( "assignment", str(tr1.assignment)) elif str(tr2.assignment): tr_product.assignment = pyuppaal.Label( "assignment", str(tr2.assignment)) productTA.transitions.append(tr_product) return (productTA, finalLocF, finalLocFneg) ########################################################################################################### ###########################################################################################################