def get_object_list(problem_text): """ This function return the object list in problem PDDL :param problem_text: problem text :return: object list """ block_list = Parser_Functions.get_bracket(problem_text, 2) for block in block_list: if "objects" in block: objects = Parser_Functions.parse_objects(block) return objects
def parse_visual(text_to_parse, result): """ This function is used to parse visual block in animation pddl :param text_to_parse: text contain one visual text block :param result: animation dictionary :return: updated animation dictionary """ # -------------------------------------------- # Patterns that are going to be used for Visual pattern_visual = ":visual" pattern_type = ":type" pattern_objects = ":objects" pattern_properties = ":properties" # Get the value of the visual temp_visual_block = text_to_parse[text_to_parse.index(pattern_visual):] temp_visual_block = Parser_Functions.get_one_block(temp_visual_block) temp_visual_pattern = re.compile("(?i)" + pattern_visual + "\s[\w\-]+", re.IGNORECASE) temp_subshape, temp_subshape_value = temp_visual_pattern.findall( temp_visual_block)[0].split() sublist = {} # Get the value of type temp_regex_pattern = re.compile(pattern_type + "\s[\w\-]+", re.IGNORECASE) temp_subelement, temp_subelement_value = temp_regex_pattern.search( temp_visual_block)[0].split() if "objects" in temp_visual_block: objects_list = parseObjectLine(pattern_objects, temp_visual_block) result["objects"][temp_subelement_value][ temp_subshape_value] = objects_list elif (temp_subelement_value == "default"): result["objects"][temp_subelement_value] = temp_subshape_value # Get the value of properties temp_property_block = temp_visual_block[temp_visual_block. index(pattern_properties) + len(pattern_properties):] temp_property_block = Parser_Functions.get_one_block(temp_property_block) temp_properties_pattern = re.compile( "\([a-zA-Z0-9_.-]*\s[#a-zA-Z0-9_.-]*\)") temp_properties = temp_properties_pattern.findall(temp_property_block) for x in temp_properties: x, y = x.replace('(', '').replace(')', '').split() sublist[x] = y result["visual"][temp_subshape_value] = sublist return result
def parse_predicate(text_to_parse, result): """ This function is used to parse predicate text block :param text_to_parse: test contain one predicate text block :param result: animation profile dictionary :return: """ pattern_predicate = ":predicate" pattern_parameters = ":parameters" pattern_custom = ":custom" pattern_priority = ":priority" pattern_effect = ":effect" # Get the value of the predicate temp_visual_block = text_to_parse[text_to_parse.index(pattern_predicate):] temp_visual_block = Parser_Functions.get_one_block(temp_visual_block) temp_visual_pattern = re.compile(pattern_predicate + "\s[\w\-]+", re.IGNORECASE) temp_subshape, temp_subshape_value = temp_visual_pattern.findall( temp_visual_block)[0].split() if "priority" in temp_visual_block: priority_pattern = re.compile(pattern_priority + "\s*(\(\d+\)|[\d]+)", re.IGNORECASE) priority = priority_pattern.findall(temp_visual_block) # Get the value of parameters temp_regex_pattern = re.compile(pattern_parameters + " " + "\((.*?)\)", re.IGNORECASE) objectList = temp_regex_pattern.findall(temp_visual_block)[0].split() customObjectList = parseObjectLine(pattern_custom, temp_visual_block) # Get the value of effect temp_effect_block = temp_visual_block[temp_visual_block. index(pattern_effect) + len(pattern_effect):] temp_effect_block = Parser_Functions.get_one_block(temp_effect_block) require_dic = {} result["predicates_rules"][temp_subshape_value] = parse_rules( temp_effect_block, require_dic) result["predicates_rules"][temp_subshape_value]["require"] = require_dic result["predicates_rules"][temp_subshape_value]["objects"] = objectList if "priority" in temp_visual_block: result["predicates_rules"][temp_subshape_value]["priority"] = priority[ 0] if len(customObjectList) > 0: result["predicates_rules"][temp_subshape_value][ "custom_obj"] = customObjectList
def get_problem_dic(problem_text, predicates_lists): """ The function will parse the problem pddl and get the Initial predicates and and goal predicates :param problem_text: problem pddl text :param predicates_lists: all the available predicate from domain pddl :return: a dictionary contains INIT and GOAL states(predicates). """ # Prepare REGEX for each predicate try: get_regex_list(predicates_lists) except: raise ValueError("Empty predicates found") text_blocks = Parser_Functions.get_bracket(problem_text, 2) result = [] for block in text_blocks: if "init" in block: init_object_list = get_state_list(predicates_lists, block) result.append({"init": init_object_list}) elif "goal" in block: goal_object_list = get_state_list(predicates_lists, block) result.append({ "goal": goal_object_list, "goal-condition": ["and"] }) return result
def parse_add(text, require_dic): """ This function parse the add text block into dictionary format :param text: text contain one add text block :param require_dic: an dictionary which will record the required objects and its parameters to do the addition :return: a dictionary contain add information """ template = {"add": []} reference = Parser_Functions.get_bracket(text, 2) if reference: for item in reference: name, value = Parser_Functions.parse_objects(item) template["add"].append({name: value}) update_require(require_dic, name, value) digital_pattern = re.compile(r'[-\d]+') digital_list = re.findall(digital_pattern, text) if digital_list: for item in digital_list: template["add"].append(int(item)) return template
def parse_rule(rule, require_dic): """ This function is used to parse one particular animation rule :param rule: text contain one rule :param require_dic: an dictionary which will record the required objects and its parameters to apply this rule :return: an rule in dictionary format """ template = {"left": {}, "value": {}} rulePattern = re.compile(r'\((\w+)\s+(\([^)]+\))\s*(\(.*\)|[\d\w#-]+)\)') divide_rule = re.search(rulePattern, rule) rule_type = divide_rule.group(1) left_object = divide_rule.group(2) right_value = divide_rule.group(3) middle = Parser_Functions.parse_objects(left_object) template["left"][middle[0]] = middle[1:] if "function" in rule: template["value"] = parse_function(right_value, require_dic) elif "(" not in right_value: value_pattern = re.compile(r'\(equal\s+\([^)]+\)\s*([\d\w#-]+)\)') searchValue = re.search(value_pattern, rule) value = searchValue.group(1) template["value"]["equal"] = value elif "add" in right_value: template["value"] = parse_add(right_value, require_dic) elif "(" in right_value: try: name, value = Parser_Functions.parse_objects(right_value) except: print("right value: ") print(right_value) print() for i in range(len(right_value)): print(right_value[i]) print() template["value"]["equal"] = {name: value} update_require(require_dic, name, value) return template
def parse_function(text, require_dic): """ This function is used to parse function text block into dictionary format :param text: text contain function text block :param require_dic: an dictionary which will record the required objects and its parameters to run the function :return: a function dictionary """ template = {"fname": "", "obj_indexs": [], "settings": {}} name_pattern = re.compile(r'\(function\s*(\w+)\s*\(.*') searchName = re.search(name_pattern, text) name = searchName.group(1) template["fname"] = name # (objects a b c) objects_pattern = re.compile(r'\(objects\s+([^)]+)\)') searchObj = re.search(objects_pattern, text) objects = re.split(r'\s+', searchObj.group(1)) template["obj_indexs"] = objects require = Custom_functions.customf_controller(name, None, None, None, None, True)["require"] for key, value in require.items(): index = int(key) for item in value: obj_name = objects[index] update_require(require_dic, obj_name, item) if "settings" in text: settings_pattern = re.compile(r'\(settings\s+(\s*\([^)]*\)\s*)+\)') searchSetting = re.search(settings_pattern, text) SettingStr = searchSetting.group(0) settingList = Parser_Functions.get_bracket(SettingStr, 2) for setting in settingList: sname, svalue = Parser_Functions.parse_objects(setting) template["settings"][sname] = svalue return {"function": template}
def parse(text, result): """ :param text: whole text file :param result: animation dictionary :return: updated animation dictionary All the text are case insensitive, except the image name and value. """ text_blocks = Parser_Functions.get_bracket(text, 2) for text_block in text_blocks: if "visual" in text_block.lower(): parse_visual(text_block.lower(), result) elif "predicate" in text_block.lower(): parse_predicate(text_block.lower(), result) elif "image" in text_block: parse_image(text_block, result)
def parse_image(text_to_parse, result): """ This function is used to parse Image text block :param text_to_parse: text contain one image text block :param result: animation profile dictionary :return: """ pattern_image = ":image" temp_image_block = text_to_parse[text_to_parse.index(pattern_image):] temp_image_block = Parser_Functions.get_one_block(temp_image_block) patternPare = re.compile(r'\((.*?)\)') imagePareList = patternPare.findall(temp_image_block) for imagePare in imagePareList: name, value = imagePare.split() result["imageTable"]["m_keys"].append(name) result["imageTable"]["m_values"].append(value)
def parseObjectLine(pattern, text): """ :param pattern: line start with an pattern :param text: one line of text :return: an array of objects """ temp_objects_pattern = re.compile(pattern + "\s*(\([^\)]+\)|[\w\-\%]+)", re.IGNORECASE) try: objectsStr = temp_objects_pattern.search(text).group(1) except: return [] if "(" in objectsStr: return Parser_Functions.parse_objects(objectsStr) else: return [objectsStr]
def parse_rules(text, require_dic): """ This function is used to parse all the predicate rules in the effect part of predicate text block :param text: text contain several rules :param require_dic: a dictionary contain all the require objects and its properties to solve a predicate. :return: a dictionary contain all the rules """ template = {"rules": []} rules = Parser_Functions.get_bracket(text, 2) for i, rule in enumerate(rules): newrule = "rule" + str(i + 1) template["rules"].append(newrule) if "equal" in rule or "assign" in rule: template[newrule] = parse_rule(rule, require_dic) elif "action" in rule: template[newrule] = parse_actionrule(rule, require_dic) return template