def setGoal(self, goal): try: goals = goal.split(",") for g in goals: if not g: continue if g.find("forall") != -1: self._pddl_interface.addGoal(pddl.ForallPredicate(g)) else: g = g[1:-1] tokens = g.split(" ") if len(tokens) == 3: self._pddl_interface.addGoal( pddl.GroundPredicate(tokens[0], [tokens[1], tokens[2]])) if tokens[1].find("-") == -1: # If isAbstractObject self._abstract_objects.append( self._wmi.get_template_element(tokens[1])) if tokens[2].find("-") == -1: # If isAbstractObject self._abstract_objects.append( self._wmi.get_template_element(tokens[2])) else: self._pddl_interface.addGoal( pddl.GroundPredicate(tokens[0], [tokens[1]])) if tokens[1].find("-") == -1: # If isAbstractObject self._abstract_objects.append( self._wmi.get_template_element(tokens[1])) except BaseException: raise Exception("Error while parsing input goal: {}".format(goal))
def initProblem(self): objects = {} elements = {} self._elements = {} # Find objects for objType in self._pddl_interface._types._types["thing"]: temp = self._wmi.resolve_elements(wmi.Element(objType)) elements[objType] = temp if len(temp) > 0: objects[objType] = [] for e in temp: objects[objType].append(e.id) self._elements[e.id] = e self._elements[e.id.lower()] = e for e in self._abstract_objects: ctype = self._wmi.get_super_class(e.type) if ctype not in objects: objects[ctype] = [] elements[ctype] = [] e._id = e.label if not e.label in objects[ctype]: # Avoids duplicates objects[ctype].append(e._label) elements[ctype].append(e) self._elements[e.id] = e self._elements[e.id.lower()] = e self._pddl_interface.setObjects(objects) # Evaluate inital state for supertype, types in self._pddl_interface._types._types.items(): elements[supertype] = [] for t in types: elements[supertype] += elements[t] params = skirosp.ParamHandler() params.addParam("x", Element(), skirosp.ParamTypes.Required) params.addParam("y", Element(), skirosp.ParamTypes.Required) for p in self._pddl_interface._predicates: if self._wmi.get_reasoner(p.name) is not None: # The predicate is handled by a reasoner xtype = p.params[0]["valueType"] ytype = p.params[1]["valueType"] for xe in elements[xtype]: for ye in elements[ytype]: relations = self._wmi.get_reasoner( p.name).computeRelations(xe, ye) if self._verbose: log.info( "[Checking {}-{}-{}]".format( xe.id, p.name, ye.id), " Got: {}".format(relations)) if p.name in relations: self._pddl_interface.addInitState( pddl.GroundPredicate(p.name, [xe.id, ye.id])) else: # The predicate is handled normally if len(p.params) == 1: if p.value != None: c = cond.ConditionProperty("", p.name, "x", p.operator, p.value, True) else: c = cond.ConditionHasProperty("", p.name, "x", True) xtype = p.params[0]["valueType"] for xe in elements[xtype]: params.specify("x", xe) if c.evaluate(params, self._wmi): self._pddl_interface.addInitState( pddl.GroundPredicate(p.name, [xe._id], p.operator, p.value)) else: xtype = p.params[0]["valueType"] ytype = p.params[1]["valueType"] subx = [xtype] if self._pddl_interface.getSubTypes( xtype) is None else self._pddl_interface.getSubTypes( xtype) suby = [ytype] if self._pddl_interface.getSubTypes( ytype) is None else self._pddl_interface.getSubTypes( ytype) if p.abstracts: query_str_template = """ SELECT ?x ?y WHERE {{ {{ ?xtypes rdfs:subClassOf* {xtype}. }} UNION {{ {xtype} rdfs:subClassOf* ?xtypes. }} {{ ?ytypes rdfs:subClassOf* {ytype}. }} UNION {{ {ytype} rdfs:subClassOf* ?ytypes. }} ?xtypes rdfs:subClassOf ?restriction . ?restriction owl:onProperty {relation}. ?restriction ?quantity ?ytypes. ?x rdf:type/rdfs:subClassOf* ?xtypes. ?y rdf:type/rdfs:subClassOf* ?ytypes. }}""" else: query_str_template = """ SELECT ?x ?y WHERE {{ {{ ?x {relation} ?y. ?x rdf:type/rdfs:subClassOf* {xtype}. ?y rdf:type/rdfs:subClassOf* {ytype}.}} UNION {{?t {relation} ?z. ?t rdf:type/rdfs:subClassOf* {xtype}. ?z rdf:type/rdfs:subClassOf* {ytype}. ?t skiros:hasTemplate ?x. ?z skiros:hasTemplate ?y. }} UNION {{?t {relation} ?y. ?t rdf:type/rdfs:subClassOf* {xtype}. ?y rdf:type/rdfs:subClassOf* {ytype}. ?t skiros:hasTemplate ?x.}} UNION {{?x {relation} ?z. ?x rdf:type/rdfs:subClassOf* {xtype}. ?z rdf:type/rdfs:subClassOf* {ytype}. ?z skiros:hasTemplate ?y.}} }}""" for x in subx: for y in suby: query_str = query_str_template.format( relation=p.name, xtype=x, ytype=y) answer = self._wmi.query_ontology(query_str) for line in answer: tokens = line.strip().split(" ") self._pddl_interface.addInitState( pddl.GroundPredicate(p.name, tokens)) for p in self._pddl_interface._functions: c = cond.ConditionProperty("", p.name, "x", p.operator, p.value, True) xtype = p.params[0]["valueType"] for xe in elements[xtype]: params.specify("x", xe) if c.evaluate(params, self._wmi): self._pddl_interface.addInitState( pddl.GroundPredicate(p.name, [xe._id], p.operator, p.value)) if self._verbose: log.info("[Problem]", self._pddl_interface.printProblem(False))