def initDomain(self): skills = self._wmi.resolve_elements(wmi.Element(":Skill")) for skill in skills: params = {} preconds = [] holdconds = [] postconds = [] # Note: Only skills with pre AND post conditions are considered for planning for p in skill.getRelations(pred="skiros:hasParam"): e = self._wmi.get_element(p['dst']) params[e._label] = e.getProperty("skiros:DataType").value for p in skill.getRelations(pred="skiros:hasPreCondition"): e = self._wmi.get_element(p['dst']) if e.type.find("ConditionRelation") != -1 or e.type == "skiros:ConditionProperty" or e.type == "skiros:ConditionHasProperty": preconds.append(pddl.Predicate(e, params, e.type.find("Abs") != -1)) for p in skill.getRelations(pred="skiros:hasHoldCondition"): e = self._wmi.get_element(p['dst']) if e.type.find("ConditionRelation") != -1 or e.type == "skiros:ConditionProperty" or e.type == "skiros:ConditionHasProperty": holdconds.append(pddl.Predicate(e, params, e.type.find("Abs") != -1)) for p in skill.getRelations(pred="skiros:hasPostCondition"): e = self._wmi.get_element(p['dst']) if e.type.find("ConditionRelation") != -1 or e.type == "skiros:ConditionProperty" or e.type == "skiros:ConditionHasProperty": postconds.append(pddl.Predicate(e, params, e.type.find("Abs") != -1)) self._pddl_interface.addAction(pddl.Action(skill, params, preconds, holdconds, postconds)) if self._verbose: log.info("[Domain]", self._pddl_interface.printDomain(False))
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))