OwlReader.py
Go to the documentation of this file.
00001 from xml.dom.minidom import parse, parseString
00002 import pickle
00003 import os, sys
00004 import time
00005 
00006 
00007 class OwlIndividual:
00008     def __init__(self, domIndividual):
00009         self.domIndividual = domIndividual
00010 
00011     def name(self, bSplitNamespace = True):
00012         strName = self.domIndividual.getAttribute("rdf:about")
00013         if bSplitNamespace:
00014             return strName.split("#")[1]
00015         else:
00016             return strName
00017 
00018     def tagAttributeValues(self, strTagName, strAttributeName):
00019         arrAttributeValues = []
00020         domTags = self.domIndividual.getElementsByTagName(strTagName)
00021 
00022         for domTag in domTags:
00023             strAttributeValue = domTag.getAttribute(strAttributeName)
00024 
00025             if strAttributeValue:
00026                 arrAttributeValues.append(strAttributeValue)
00027 
00028         return arrAttributeValues
00029     
00030     def tagNodeValues(self, strTagName):
00031         arrNodeValues = []
00032         domTags = self.domIndividual.getElementsByTagName(strTagName)
00033         
00034         for domTag in domTags:
00035             strNodeValue = domTag.firstChild.nodeValue
00036             arrNodeValues.append(strNodeValue)
00037         
00038         return arrNodeValues
00039     
00040     def type(self, bSplitNamespace = True):
00041         arrTypes = self.tagAttributeValues("rdf:type", "rdf:resource")
00042         if len(arrTypes) > 0:
00043             strType = arrTypes[0]
00044 
00045             if bSplitNamespace:
00046                 return strType.split("#")[1]
00047             else:
00048                 return strType
00049     
00050     def annotatedParameterTypes(self):
00051         return self.tagNodeValues("knowrob:annotatedParameterType")
00052     
00053     def annotatedParameters(self, bSingularParameters = False):
00054         arrParams = {}
00055         params = self.annotatedParameterTypes()
00056         
00057         for param in params:
00058             if bSingularParameters: 
00059                 arrParams[param] = self.annotatedParameterValue(param)
00060             else:
00061                 arrParams[param] = self.annotatedParameterValues(param)
00062         
00063         return arrParams
00064     
00065     def subActions(self):
00066         subs = self.tagAttributeValues("knowrob:subAction", "rdf:resource")
00067         arrSubs = []
00068         for sub in subs:
00069             arrSubs.append(sub.split("#")[1])
00070         
00071         return arrSub
00072     
00073     def annotatedParameterValues(self, strParam):
00074         return self.tagNodeValues("knowrob:" + strParam)
00075     
00076     def annotatedParameterValue(self, strParam):
00077         values = self.annotatedParameterValues(strParam)
00078         
00079         if len(values) > 0:
00080             return values[0]
00081         
00082         return None
00083     
00084     def taskContext(self):
00085         tnv = self.tagNodeValues("knowrob:taskContext")
00086         
00087         if tnv:
00088             return tnv[0]
00089         
00090         return None
00091     
00092     def failures(self):
00093         failures_pre = self.tagAttributeValues("knowrob:eventFailure", "rdf:resource")
00094         arrFailures = []
00095         for failure in failures_pre:
00096             arrFailures.append(failure.split("#")[1])
00097         
00098         return arrFailures
00099     
00100     def timeSpan(self):
00101         timeStart = self.tagAttributeValues("knowrob:startTime", "rdf:resource")
00102         timeEnd = self.tagAttributeValues("knowrob:endTime", "rdf:resource")
00103 
00104         if len(timeStart) > 0 and len(timeEnd) > 0:
00105             return [timeStart[0].split("#")[1].split("_")[1], timeEnd[0].split("#")[1].split("_")[1]]
00106 
00107     def time(self):
00108         arrTimespan = self.timeSpan()
00109 
00110         if arrTimespan:
00111             if len(arrTimespan) == 2:
00112                 return int(arrTimespan[1]) - int(arrTimespan[0])
00113 
00114         return 0
00115 
00116     def subActions(self, bSplitNamespace = True):
00117         arrReturn = []
00118         arrSubActions = self.tagAttributeValues("knowrob:subAction", "rdf:resource")
00119 
00120         for strSubAction in arrSubActions:
00121             if bSplitNamespace:
00122                 arrReturn.append(strSubAction.split("#")[1])
00123             else:
00124                 arrReturn.append(strSubAction)
00125 
00126         return arrReturn
00127 
00128 
00129 class OwlReader:
00130     def __init__(self):
00131         pass
00132     
00133     def loadOwl(self, strFile):
00134         return self.crawlOwl(parse(strFile))
00135     
00136     def crawlOwl(self, domOwl):
00137         arrIndividuals = domOwl.getElementsByTagName("owl:namedIndividual")
00138 
00139         arrOwlTaskTreeIndividuals = {}
00140         arrOwlDesignatorIndividuals = {}
00141         arrOwlAuxIndividuals = {}
00142         owlAnnotation = None
00143         owlMetaData = None
00144         
00145         for domIndividual in arrIndividuals:
00146             owlIndividual = OwlIndividual(domIndividual)
00147             
00148             if owlIndividual.type() == "CRAMDesignator":
00149                 arrOwlDesignatorIndividuals[owlIndividual.name()] = owlIndividual
00150             elif owlIndividual.type() == "CameraImage" or owlIndividual.type() == "HumanScaleObject":
00151                 arrOwlAuxIndividuals[owlIndividual.name()] = owlIndividual
00152             elif owlIndividual.type() == "AnnotationInformation":
00153                 owlAnnotation = owlIndividual
00154             elif owlIndividual.type() == "ExperimentMetaData":
00155                 owlMetaData = owlIndividual
00156             else:
00157                 arrOwlTaskTreeIndividuals[owlIndividual.name()] = owlIndividual
00158         
00159         #dicTaskTree = self.createTaskTree(arrOwlTaskTreeIndividuals)
00160         
00161         return {#"task-tree": dicTaskTree,
00162                 "task-tree-individuals": arrOwlTaskTreeIndividuals,
00163                 "designator-individuals": arrOwlDesignatorIndividuals,
00164                 "aux-individuals": arrOwlAuxIndividuals,
00165                 "annotation": owlAnnotation,
00166                 "metadata": owlMetaData}
00167     
00168     def createTaskTree(self, arrOwlTaskTreeIndividuals):
00169         arrToplevelIndividuals = {}
00170 
00171         for strIndividualName in arrOwlTaskTreeIndividuals:
00172             bFound = False
00173 
00174             for strIndividualNameCheckAgainst in arrOwlTaskTreeIndividuals:
00175                 owlCheckAgainst = arrOwlTaskTreeIndividuals[strIndividualNameCheckAgainst]
00176 
00177                 if not strIndividualName == strIndividualNameCheckAgainst:
00178                     arrSubActions = owlCheckAgainst.subActions()
00179 
00180                     if strIndividualName in arrSubActions:
00181                         bFound = True
00182                         break
00183 
00184             if not bFound:
00185                 arrToplevelIndividuals[strIndividualName] = arrOwlTaskTreeIndividuals[strIndividualName]
00186 
00187         arrTaskTrees = {}
00188         for strToplevelIndividualName in arrToplevelIndividuals:
00189             arrTaskTrees[strToplevelIndividualName] = self.createSubTaskTree(arrOwlTaskTreeIndividuals, strToplevelIndividualName)
00190 
00191         return arrTaskTrees
00192 
00193     def createSubTaskTree(self, arrOwlTaskTreeIndividuals, strParent):
00194         arrTree = {"children": {}}
00195 
00196         if strParent in arrOwlTaskTreeIndividuals:
00197             owlParent = arrOwlTaskTreeIndividuals[strParent]
00198             arrTree["time"] = owlParent.time()
00199 
00200             for strSubAction in owlParent.subActions():
00201                 arrTree["children"][strSubAction] = self.createSubTaskTree(arrOwlTaskTreeIndividuals, strSubAction)
00202 
00203         return arrTree


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15