Best Python code snippet using avocado_python
compiler.py
Source:compiler.py
1#! /usr/bin/env python2import os, sys, itertools3import pddl, pddl_parser4import fdtask_to_pddl5class PlanStep:6 def __init__(self, n, o, st, b, d, op, ap):7 self.name = n8 self.operator = o9 self.stime = st10 self.block = b 11 self.durations = d12 self.oparams = op13 self.aparams = ap14 def __str__(self):15 str_out = ""16 str_out = str_out + "action:\n"17 str_out = str_out + self.name + "\n" 18 str_out = str_out + "operator:\n"19 str_out = str_out + self.operator.name + "\n" 20 str_out = str_out + "start_time:\n"21 str_out = str_out + str(self.stime + self.block) + "\n" 22 str_out = str_out + "durations:\n"23 str_out = str_out + str(self.durations).replace("[","").replace("]","").replace(",","") + "\n"24 25 str_out = str_out + "precs:\n"26 str_precs = fdtask_to_pddl.format_condition(self.operator.precondition)27 for i in range(len(self.oparams)):28 str_precs = str_precs.replace(self.oparams[i]+" ",self.aparams[i]+" ")29 str_precs = str_precs.replace(self.oparams[i]+")",self.aparams[i]+")")30 str_out = str_out + format_string_literals(str_precs.replace("(and","").split(")("),1) + "\n"31 32 str_out = str_out + "effs:\n"33 str_effs = ""34 for item in self.operator.effects:35 str_effs = str_effs + fdtask_to_pddl.format_effect(item)36 for i in range(len(self.oparams)): 37 str_effs = str_effs.replace(self.oparams[i]+" ",self.aparams[i]+" ")38 str_effs = str_effs.replace(self.oparams[i]+")",self.aparams[i]+")")39 if isinstance(self.operator.precondition, pddl.conditions.Conjunction):40 npres = len(self.operator.precondition.parts)41 elif isinstance(self.operator.precondition, pddl.conditions.Atom):42 npres = 143 else:44 npres = 045 46 str_out = str_out + format_string_literals(str_effs.split(")("), npres+1) 47 48 return str_out.lower()49 50def filter_literal_string(str_in):51 str_in = str_in.replace("(not","not-").replace("not (","not-(").replace(" ","_").replace("_(","(").replace("))",")")52 return str_in53 54def format_string_literals(str_literals, offset):55 str_out = str(offset) +" "+ filter_literal_string(str_literals[0]) + ")"56 for i in range(1,len(str_literals)):57 str_out = str_out + " & " + str(offset+i) + " (" + filter_literal_string(str_literals[i]) + ")"58 return str_out.replace("))",")").replace("(not-(","not-(")59# **************************************#60# MAIN61# **************************************#62try:63 if "-n" in sys.argv:64 index = sys.argv.index("-n")65 sys.argv.pop(index)66 bneginit = True67 else:68 bneginit = False69 if "-e" in sys.argv:70 index = sys.argv.index("-e")71 sys.argv.pop(index)72 btrunk = True73 else:74 btrunk = False 75 76 domain_filename = sys.argv[1]77 problem_filename = sys.argv[2]78 plan_filename = sys.argv[3]79 output_filename = sys.argv[4] 80except:81 print "Usage:"82 print sys.argv[0] + " [-n negative facts at initial state, -e trunk the last end line] <domain file name> <problem file name> <plan file name> <output file name>"83 sys.exit(-1)84str_out = ""85# Creating a FD task with the domain and the problem files86fd_domain = pddl_parser.pddl_file.parse_pddl_file("domain", domain_filename)87fd_problem = pddl_parser.pddl_file.parse_pddl_file("task", problem_filename)88fd_task = pddl_parser.pddl_file.parsing_functions.parse_task(fd_domain, fd_problem)89# Domain and problem name90str_out = str_out + "domain:\n"91str_out = str_out + domain_filename + "\n"92str_out = str_out + "\n"93str_out = str_out + "problem:\n"94str_out = str_out + problem_filename + "\n"95str_out = str_out + "\n"96# Init and goals97str_out = str_out + "init:\n"98formattedinit1 = fdtask_to_pddl.format_condition([i for i in fd_task.init if i.predicate!="="])99formattedinit2 = format_string_literals(formattedinit1.split(") ("),1).replace(")_)",")")100npositive = len(formattedinit1.split(") (")) 101atoms = ""102if bneginit==True: # Completing initial state with negated literals103 for p in fd_task.predicates:104 candidate_objects = []105 for arg in p.arguments:106 candidate_pos=[]107 for o in sorted(set(fd_task.objects)):108 aux = [t for t in fd_task.types if t.name==o.type_name]109 supernames = [str(t.name) for t in aux] + [str(t) for t in aux[0].supertype_names]110 if arg.type_name in supernames:111 candidate_pos = candidate_pos + [str(o.name)]112 candidate_objects.append(candidate_pos)113 combinations = list(itertools.product(*candidate_objects))114 for item in combinations:115 atom = "(" + p.name + "_" + "_".join(map(str,item)) + ")"116 if not atom in formattedinit2:117 npositive = npositive + 1118 atoms = atoms + " & " + str(npositive) + str(" not-(" + p.name + "_" + "_".join(map(str,item)) + ")") 119 120str_out = str_out + formattedinit2 + atoms + "\n"121str_out = str_out + "\n"122str_out = str_out + "goals:\n"123formattedgoal = fdtask_to_pddl.format_condition(fd_task.goal)124str_out = str_out + format_string_literals(formattedgoal.replace("(and ","")[:-1].split(")("),1)+ "\n"125# Reading plan126steps = []127makespan=0128timestamps = set([])129plan_file = open(plan_filename, 'r')130nactions=0131for line in plan_file:132 if not ";" in line and ":" in line:133 nactions = nactions + 1134 135 # Creating a plan step136 start_time = int(line.split(".")[0])137 timestamps.add(start_time)138 139 duration = float(line.split("[")[1].replace("]",""))140 aname = line.split(": ")[1].split(" [")[0].replace(" (","(").replace(") ",")").replace(" ","_").replace("_(","(")141 aparams = line.split("(")[1].split(")")[0].split(" ")[1:]142 clean_aname=aname.lower().replace("(","").replace(")","").split("_")[0] 143 operator = [o for o in fd_task.actions if clean_aname==o.name.lower()][0]144 oparams = [str(p.name) for p in operator.parameters]145 146 steps.append(PlanStep(aname,operator, start_time, -1, [] , oparams, aparams))147 makespan = max(makespan, int(start_time + duration))148plan_file.close()149# Adding the last timestamp150timestamps.add(makespan)151# Makespan152str_out = str_out + "makespan:\n"153str_out = str_out + str(makespan + len(timestamps))+ "\n"154str_out = str_out + "\n"155# Plan steps156steps.sort(key=lambda x: x.stime)157for s in steps:158 aux_timestamps = sorted(timestamps)159 s.block = aux_timestamps.index(s.stime) + 1 160 s.durations = [item - s.stime for item in aux_timestamps[aux_timestamps.index(s.stime)+1:]]161 str_out = str_out + str(s)+ "\n"162 str_out = str_out + "\n"163if not btrunk: 164 str_out = str_out + "end:\n"165if nactions > 0:166 output_file = open(output_filename, 'w')167 output_file.write(str_out)168 output_file.close()169 ...
fdtask_to_pddl.py
Source:fdtask_to_pddl.py
1import sys2import utils,pddl3def format_problem(task,domain):4 str_out = "(define (problem " + task.task_name + ")\n"5 str_out = str_out + " (:domain "+ task.domain_name + ")\n"6 str_out = str_out + " (:objects "7 for i in sorted(set(task.objects)):8 str_out = str_out + str(i).replace(":"," - ") + " "9 10 str_out = str_out + ")\n"11 str_out = str_out + " (:init " + format_condition([i for i in task.init if i.predicate!="="])+")\n"12 str_out = str_out + " (:goal " + format_condition(task.goal)+")"13 str_out = str_out + ")"14 return str_out15def format_domain(task,domain):16 str_out = "(define (domain " + task.domain_name + ")\n"17 str_out = str_out + " (:requirements " + str(task.requirements).replace(",","").replace(":non-deterministic","") + ")\n"18 str_out = str_out + " (:types "19 for i in task.types:20 str_out = str_out + i.name + " - " + str(i.basetype_name) + " "21 str_out = str_out + ")\n"22 23 constants=utils.compute_constants(task,domain)24 constants_str = list()25 if len(constants)>0:26 str_out = str_out + " (:constants "27 for i in sorted(set(task.objects)):28 aux = str(i).replace(":"," - ")29 constants_str.append(aux)30 constants_str = sorted(constants_str)31 str_out += " ".join(constants_str) + ")\n"32 str_out = str_out + " (:predicates "33 predicates_str = list()34 for i in task.predicates:35 if i.name != "=":36 aux = "(" + i.name37 for j in i.arguments:38 aux += " " + j.name + " - " + j.type_name39 aux += ")"40 predicates_str.append(aux)41 predicates_str = sorted(predicates_str)42 str_out += " ".join(predicates_str) + ")\n"43 # for axiom in task.axioms:44 # str_out = str_out + " (:derived (" + axiom.name + ")\n"45 # str_out = str_out + format_condition(axiom.condition)46 # str_out = str_out + ")\n"47 str_out=str_out+"\n"48 for a in task.actions:49 str_out=str_out + format_action(a,task)50 str_out=str_out + ")"51 return str_out 52def format_action(a,task):53 str_out=""54 str_out=str_out + " (:action " + a.name +"\n"55 str_out=str_out + " :parameters (" + " ".join(map(str, a.parameters)).replace(":"," -") + ")\n"56 str_out=str_out + " :precondition " + format_condition(a.precondition) +"\n"57 str_out=str_out + " :effect (and "58 for item in a.effects:59 str_out = str_out + format_effect(item)60 str_out = str_out + "))\n\n" 61 return str_out62def format_condition(c):63 str_out=""64 65 if isinstance(c,list):66 for item in c:67 str_out = str_out + format_condition(item) +" "68 if isinstance(c,pddl.conditions.Conjunction):69 str_out=str_out+"(and "70 for item in c.parts:71 str_out=str_out+format_condition(item) 72 str_out=str_out+")"73 if isinstance(c,pddl.conditions.Disjunction):74 str_out=str_out+"(or "75 for item in c.parts:76 str_out=str_out+format_condition(item) 77 str_out=str_out+")"78 if isinstance(c,pddl.conditions.UniversalCondition):79 str_out = str_out + "(forall ("80 if isinstance(c,pddl.conditions.ExistentialCondition): 81 str_out = str_out + "(exists ("82 if isinstance(c,pddl.conditions.UniversalCondition) or isinstance(c,pddl.conditions.ExistentialCondition):83 for p in c.parameters:84 str_out = str_out + p.name + " - " + p.type_name +" "85 str_out = str_out + ")" + format_condition(list(c.parts)) + ")"86 if isinstance(c,pddl.conditions.NegatedAtom):87 str_out=str_out+"(not ("+str(c.predicate)+" "+" ".join(map(str, c.args))+"))"88 if isinstance(c,pddl.conditions.Atom):89 str_out=str_out+"("+str(c.predicate)+" "+" ".join(map(str, c.args))+")"90 return str_out91def format_effect(e):92 str_out=""93 if isinstance(e,pddl.effects.Effect):94 if e.parameters:95 str_out=str_out+"(forall (" + " ".join(map(str, e.parameters)).replace(":"," - ")+")"96 97 if e.condition != pddl.conditions.Truth():98 str_out=str_out+"(when "+format_condition(e.condition)99 if e.literal.negated:100 str_out=str_out+"(not ("+str(e.literal.predicate)+" "+" ".join(map(str, e.literal.args))+"))"101 else:102 str_out=str_out+"("+str(e.literal.predicate)+" "+" ".join(map(str, e.literal.args))+")"103 104 if e.condition != pddl.conditions.Truth():105 str_out=str_out+")"106 if e.parameters:107 str_out=str_out+")"...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!