00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00040 #include <plugins/owlexporter/CExporterOwl.h>
00041
00042
00043 namespace beliefstate {
00044 CExporterOwl::CExporterOwl() {
00045 m_strPropertyNamespace = "";
00046 m_strDefaultAnnotation = "";
00047
00048 this->setMessagePrefixLabel("owl-exporter-aux");
00049 }
00050
00051 CExporterOwl::~CExporterOwl() {
00052 }
00053
00054 void CExporterOwl::setMetaData(std::map<std::string, std::string> mapMetaData) {
00055 m_mapMetaData = mapMetaData;
00056 }
00057
00058 bool CExporterOwl::loadSemanticsDescriptorFile(std::string strFilepath) {
00059 if(this->fileExists(strFilepath)) {
00060 libconfig::Config cfgConfig;
00061
00062 try {
00063 cfgConfig.readFile(strFilepath.c_str());
00064
00065 if(cfgConfig.exists("condition-mappings")) {
00066 libconfig::Setting &sConditionMappings = cfgConfig.lookup("condition-mappings");
00067
00068 if(sConditionMappings.exists("mappings")) {
00069 libconfig::Setting &sMappings = sConditionMappings["mappings"];
00070
00071 for(int nI = 0; nI < sMappings.getLength(); nI++) {
00072 std::string strTo;
00073
00074 if(sMappings[nI].lookupValue("to", strTo)) {
00075 if(sMappings[nI].exists("from")) {
00076 libconfig::Setting &sFrom = sMappings[nI]["from"];
00077
00078 for(int nJ = 0; nJ < sFrom.getLength(); nJ++) {
00079 std::string strFrom = sFrom[nJ];
00080
00081 m_lstFailureMapping.push_back(make_pair(strFrom, strTo));
00082 }
00083 }
00084 } else {
00085 this->warn("Condition mapping without 'to' field. Ignoring.");
00086 }
00087 }
00088 }
00089 }
00090
00091 m_lstDefinedProperties.clear();
00092 m_lstAnnotationPurposeMapping.clear();
00093
00094 if(cfgConfig.exists("structure")) {
00095 libconfig::Setting &sStructure = cfgConfig.lookup("structure");
00096
00097 m_strPropertyNamespace = "";
00098 if(sStructure.exists("property-namespace")) {
00099 sStructure.lookupValue("property-namespace", m_strPropertyNamespace);
00100 }
00101
00102 if(m_strPropertyNamespace == "") {
00103 this->warn("You didn't specify the 'structure/property-namespace' parameter on the semantics descriptor file. Your OWL classes will have no namespace prepended. Is this intended?");
00104 }
00105
00106 if(sStructure.exists("defined-properties")) {
00107 libconfig::Setting &sDefinedProperties = sStructure["defined-properties"];
00108
00109 for(int nI = 0; nI < sDefinedProperties.getLength(); nI++) {
00110 std::string strProperty = sDefinedProperties[nI];
00111 m_lstDefinedProperties.push_back(m_strPropertyNamespace + strProperty);
00112 }
00113 }
00114
00115 m_strDefaultAnnotation = "";
00116 if(sStructure.exists("default-annotation-purpose")) {
00117 sStructure.lookupValue("default-annotation-purpose", m_strDefaultAnnotation);
00118 }
00119
00120 if(m_strDefaultAnnotation == "") {
00121 this->warn("You didn't specify the 'structure/default-annotation-purpose' parameter on the semantics descriptor file. Your designator attachments without a defined annotation will be empty and produce a faulty OWL file. Is this intended?");
00122 }
00123
00124 if(sStructure.exists("annotation-purposes")) {
00125 libconfig::Setting &sPurposes = sStructure["annotation-purposes"];
00126
00127 for(int nI = 0; nI < sPurposes.getLength(); nI++) {
00128 libconfig::Setting &sPurpose = sPurposes[nI];
00129
00130 std::string strFrom;
00131 std::string strTo;
00132
00133 sPurpose.lookupValue("from", strFrom);
00134 sPurpose.lookupValue("to", strTo);
00135
00136 if(strFrom != "" && strTo != "") {
00137 m_lstAnnotationPurposeMapping.push_back(make_pair(strFrom, strTo));
00138 } else {
00139 this->warn("Invalid annotation purpose mapping: '" + strFrom + "' -> '" + strTo + "'. Discarding.");
00140 }
00141 }
00142 }
00143 }
00144
00145 return true;
00146 } catch(libconfig::ParseException e) {
00147 std::stringstream sts;
00148 sts << e.getLine();
00149
00150 this->fail("Error while parsing semantics descriptor file '" + strFilepath + "': " + e.getError() + ", on line " + sts.str());
00151 } catch(...) {
00152 this->fail("Undefined error while parsing semantics descriptor file '" + strFilepath + "'");
00153 }
00154 } else {
00155 this->fail("Semantics descriptor file not found: '" + strFilepath + "'.");
00156 }
00157
00158 return false;
00159 }
00160
00161 void CExporterOwl::prepareEntities(std::string strNamespaceID, std::string strNamespace) {
00162 m_lstEntities.clear();
00163
00164 this->addEntity("owl", "http://www.w3.org/2002/07/owl#");
00165 this->addEntity("xsd", "http://www.w3.org/2001/XMLSchema#");
00166 this->addEntity("knowrob", "http://knowrob.org/kb/knowrob.owl#");
00167 this->addEntity("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
00168 this->addEntity("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
00169 this->addEntity(strNamespaceID, strNamespace + "#");
00170
00171 for(pair<std::string, std::string> prEntity : m_mapRegisteredOWLNamespaces) {
00172 this->addEntity(prEntity.first, prEntity.second);
00173 }
00174 }
00175
00176 void CExporterOwl::setRegisteredOWLNamespaces(std::map<std::string, std::string> mapRegisteredOWLNamespaces) {
00177 m_mapRegisteredOWLNamespaces = mapRegisteredOWLNamespaces;
00178 }
00179
00180 void CExporterOwl::addEntity(std::string strNickname, std::string strNamespace) {
00181 m_lstEntities.push_back(make_pair(strNickname, strNamespace));
00182 }
00183
00184 std::string CExporterOwl::generateDocTypeBlock() {
00185 std::string strDot = "<!DOCTYPE rdf:RDF [\n";
00186
00187 for(std::pair<std::string, std::string> prEntity : m_lstEntities) {
00188 strDot += " <!ENTITY " + prEntity.first + " \"" + prEntity.second + "\" >\n";
00189 }
00190
00191 strDot += "]>\n\n";
00192
00193 return strDot;
00194 }
00195
00196 std::string CExporterOwl::generateXMLNSBlock(std::string strNamespace) {
00197 std::string strDot = "<rdf:RDF xmlns=\"" + strNamespace + "#\"\n";
00198 strDot += " xml:base=\"" + strNamespace + "\"\n";
00199
00200 for(std::list< std::pair<std::string, std::string> >::iterator itPair = m_lstEntities.begin();
00201 itPair != m_lstEntities.end();
00202 itPair++) {
00203 std::pair<std::string, std::string> prEntity = *itPair;
00204
00205 if(itPair != m_lstEntities.begin()) {
00206 strDot += "\n";
00207 }
00208
00209 strDot += " xmlns:" + prEntity.first + "=\"" + prEntity.second + "\"";
00210 }
00211
00212 strDot += ">\n\n";
00213 return strDot;
00214 }
00215
00216 std::string CExporterOwl::generateOwlImports(std::string strNamespace) {
00217 std::string strDot = "";
00218 std::string strImportNamespace = "package://knowrob_common/owl/knowrob.owl";
00219
00220 strDot += " <owl:Ontology rdf:about=\"" + strNamespace + "\">\n";
00221 strDot += " <owl:imports rdf:resource=\"" + strImportNamespace + "\"/>\n";
00222 strDot += " </owl:Ontology>\n\n";
00223
00224 return strDot;
00225 }
00226
00227 std::string CExporterOwl::generatePropertyDefinitions() {
00228 std::string strDot = " <!-- Property Definitions -->\n\n";
00229
00230 for(std::string strProperty : m_lstDefinedProperties) {
00231 strDot += " <owl:ObjectProperty rdf:about=\"" + strProperty + "\"/>\n\n";
00232 }
00233
00234 return strDot;
00235 }
00236
00237 std::list<std::string> CExporterOwl::gatherClassesForNodes(std::list<Node*> lstNodes) {
00238 std::list<std::string> lstClasses;
00239
00240 for(Node* ndCurrent : lstNodes) {
00241 if(ndCurrent) {
00242 std::list<std::string> lstClassesSubnodes = this->gatherClassesForNodes(ndCurrent->subnodes());
00243
00244 if(ndCurrent->metaInformation()->stringValue("class") != "") {
00245 lstClassesSubnodes.push_back(ndCurrent->metaInformation()->stringValue("classnamespace") + ndCurrent->metaInformation()->stringValue("class"));
00246 } else {
00247 lstClassesSubnodes.push_back(this->owlClassForNode(ndCurrent));
00248 }
00249
00250 for(std::string strClassSubnode : lstClassesSubnodes) {
00251 bool bExists = false;
00252
00253 for(std::string strClassNode : lstClasses) {
00254 if(strClassSubnode == strClassNode) {
00255 bExists = true;
00256 break;
00257 }
00258 }
00259
00260 if(!bExists) {
00261 lstClasses.push_back(strClassSubnode);
00262 }
00263 }
00264 } else {
00265 this->fail("Class for invalid node requested!");
00266 }
00267 }
00268
00269 return lstClasses;
00270 }
00271
00272 std::list<std::string> CExporterOwl::gatherTimepointsForNodes(std::list<Node*> lstNodes) {
00273 std::list<std::string> lstTimepoints;
00274
00275 for(Node* ndCurrent : lstNodes) {
00276 if(ndCurrent) {
00277
00278 std::list<std::string> lstTimepointsSubnodes = this->gatherTimepointsForNodes(ndCurrent->subnodes());
00279 lstTimepointsSubnodes.push_back(ndCurrent->metaInformation()->stringValue("time-start"));
00280 lstTimepointsSubnodes.push_back(ndCurrent->metaInformation()->stringValue("time-end"));
00281
00282
00283 CKeyValuePair *ckvpFailures = ndCurrent->metaInformation()->childForKey("failures");
00284
00285 if(ckvpFailures) {
00286 std::list<CKeyValuePair*> lstFailures = ckvpFailures->children();
00287
00288 unsigned int unIndex = 0;
00289 for(CKeyValuePair* ckvpFailure : lstFailures) {
00290 lstTimepointsSubnodes.push_back(ckvpFailure->stringValue("time-fail"));
00291 }
00292 }
00293
00294
00295 CKeyValuePair *ckvpImages = ndCurrent->metaInformation()->childForKey("images");
00296
00297 if(ckvpImages) {
00298 std::list<CKeyValuePair*> lstImages = ckvpImages->children();
00299
00300 unsigned int unIndex = 0;
00301 for(CKeyValuePair* ckvpImage : lstImages) {
00302 lstTimepointsSubnodes.push_back(ckvpImage->stringValue("time-capture"));
00303 }
00304 }
00305
00306
00307 for(std::pair<std::string, std::string> prPair : m_lstDesignatorEquationTimes) {
00308 lstTimepointsSubnodes.push_back(prPair.second);
00309 }
00310
00311
00312 for(pair<std::string, CKeyValuePair*> prDesig : m_mapDesignators) {
00313 if(prDesig.second) {
00314 if(prDesig.second->childForKey("description")) {
00315 lstTimepointsSubnodes.push_back(prDesig.second->childForKey("description")->stringValue("_time_created"));
00316 }
00317 }
00318 }
00319
00320
00321 for(std::string strTimepointSubnode : lstTimepointsSubnodes) {
00322 bool bExists = false;
00323
00324 for(std::string strTimepointNode : lstTimepoints) {
00325 if(strTimepointSubnode == strTimepointNode) {
00326 bExists = true;
00327 break;
00328 }
00329 }
00330
00331 if(!bExists) {
00332 lstTimepoints.push_back(strTimepointSubnode);
00333 }
00334 }
00335 } else {
00336 this->fail("Timepoints for invalid node requested!");
00337 }
00338 }
00339
00340 return lstTimepoints;
00341 }
00342
00343 std::string CExporterOwl::generateClassDefinitions() {
00344 std::string strDot = " <!-- Class Definitions -->\n\n";
00345
00346 std::list<std::string> lstClasses = this->gatherClassesForNodes(this->nodes());
00347 lstClasses.push_back("&knowrob;TimePoint");
00348
00349 for(std::string strClass : lstClasses) {
00350 strDot += " <owl:Class rdf:about=\"" + strClass + "\"/>\n\n";
00351 }
00352
00353 return strDot;
00354 }
00355
00356 std::string CExporterOwl::nodeIDPrefix(Node* ndInQuestion, std::string strProposition) {
00357 std::string strPrefix = CExporter::nodeIDPrefix(ndInQuestion, strProposition);
00358 std::string strOwlClass = this->owlClassForNode(ndInQuestion, true);
00359
00360 if(strPrefix == "") {
00361 strPrefix = strOwlClass + "_";
00362 } else if(strPrefix == strProposition) {
00363 if(strOwlClass != "") {
00364 strPrefix = strOwlClass + "_";
00365 }
00366 }
00367
00368 return strPrefix;
00369 }
00370
00371 std::string CExporterOwl::generateEventIndividualsForNodes(std::list<Node*> lstNodes, std::string strNamespace) {
00372 std::string strDot = "";
00373
00374 m_mapDesignators.clear();
00375
00376 Node* ndLastDisplayed = NULL;
00377 for(std::list<Node*>::iterator itNode = lstNodes.begin();
00378 itNode != lstNodes.end();
00379 itNode++) {
00380 Node* ndCurrent = *itNode;
00381
00382 if(ndCurrent) {
00383 if(this->nodeDisplayable(ndCurrent)) {
00384 std::string strOwlClass;
00385
00386 if(ndCurrent->metaInformation()->stringValue("class") != "") {
00387 strOwlClass = ndCurrent->metaInformation()->stringValue("classnamespace") + ndCurrent->metaInformation()->stringValue("class");
00388 } else {
00389 strOwlClass = this->owlClassForNode(ndCurrent);
00390 }
00391
00392
00393
00394
00395
00396 strDot += this->generateEventIndividualsForNodes(ndCurrent->subnodes(), strNamespace);
00397
00398
00399
00400 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + ndCurrent->uniqueID() + "\">\n";
00401 strDot += " <rdf:type rdf:resource=\"" + strOwlClass + "\"/>\n";
00402 strDot += " <knowrob:taskContext rdf:datatype=\"&xsd;string\">" + ndCurrent->title() + "</knowrob:taskContext>\n";
00403 strDot += " <knowrob:taskSuccess rdf:datatype=\"&xsd;boolean\">" + (ndCurrent->success() ? string("true") : string("false")) + "</knowrob:taskSuccess>\n";
00404 strDot += " <knowrob:startTime rdf:resource=\"&" + strNamespace + ";timepoint_" + ndCurrent->metaInformation()->stringValue("time-start") + "\"/>\n";
00405 strDot += " <knowrob:endTime rdf:resource=\"&" + strNamespace + ";timepoint_" + ndCurrent->metaInformation()->stringValue("time-end") + "\"/>\n";
00406
00407 if(ndCurrent->title() == "GOAL-ACHIEVE") {
00408 std::list<CKeyValuePair*> lstDescription = ndCurrent->description();
00409 std::string strPattern = "";
00410
00411 for(CKeyValuePair* ckvpNow : lstDescription) {
00412 if(ckvpNow->key() == "PATTERN") {
00413 strPattern = ckvpNow->stringValue();
00414 break;
00415 }
00416 }
00417
00418 if(strPattern != "") {
00419 strDot += " <knowrob:goalContext rdf:datatype=\"&xsd;string\">" + strPattern + "</knowrob:goalContext>\n";
00420 }
00421 }
00422
00423 std::list<Node*> lstSubnodes = ndCurrent->subnodes();
00424 for(Node* ndSubnode : lstSubnodes) {
00425 if(this->nodeDisplayable(ndSubnode)) {
00426 strDot += " <knowrob:subAction rdf:resource=\"&" + strNamespace + ";" + ndSubnode->uniqueID() + "\"/>\n";
00427 }
00428 }
00429
00430 if(ndLastDisplayed) {
00431 strDot += " <knowrob:previousAction rdf:resource=\"&" + strNamespace + ";" + ndLastDisplayed->uniqueID() + "\"/>\n";
00432 }
00433
00434 std::list<Node*>::iterator itPostEvent = itNode;
00435 itPostEvent++;
00436 while(itPostEvent != lstNodes.end()) {
00437 if(this->nodeDisplayable(*itPostEvent)) {
00438 strDot += " <knowrob:nextAction rdf:resource=\"&" + strNamespace + ";" + (*itPostEvent)->uniqueID() + "\"/>\n";
00439 break;
00440 }
00441
00442 itPostEvent++;
00443 }
00444
00445
00446 CKeyValuePair* ckvpObjects = ndCurrent->metaInformation()->childForKey("objects");
00447
00448 if(ckvpObjects) {
00449 std::list<CKeyValuePair*> lstObjects = ckvpObjects->children();
00450
00451 unsigned int unIndex = 0;
00452 for(CKeyValuePair* ckvpObject : lstObjects) {
00453 std::string strDefClass = ckvpObject->stringValue("_class");
00454 std::string strDefClassNamespace = ckvpObject->stringValue("_classnamespace");
00455 std::string strDefProperty = ckvpObject->stringValue("_property");
00456
00457 if(strDefClass == "") {
00458 strDefClass = "object";
00459 }
00460
00461 if(strDefProperty == "") {
00462 if(strOwlClass == "&knowrob;VisualPerception") {
00463 strDefProperty = "knowrob:detectedObject";
00464 } else {
00465 strDefProperty = "knowrob:objectActedOn";
00466 }
00467 }
00468
00469 if(strDefClassNamespace == "") {
00470 strDefClassNamespace = "&" + strNamespace + ";";
00471 }
00472
00473 std::string strObjectID = strDefClass + "_" + ckvpObject->stringValue("__id");
00474 strDot += " <" + strDefProperty + " rdf:resource=\"" + strDefClassNamespace + strObjectID +"\"/>\n";
00475 }
00476 }
00477
00478
00479 CKeyValuePair *ckvpImages = ndCurrent->metaInformation()->childForKey("images");
00480
00481 if(ckvpImages) {
00482 std::list<CKeyValuePair*> lstImages = ckvpImages->children();
00483
00484 unsigned int unIndex = 0;
00485 for(CKeyValuePair* ckvpImage : lstImages) {
00486 std::stringstream sts;
00487 sts << ndCurrent->uniqueID() << "_image_" << unIndex;
00488
00489 strDot += " <knowrob:capturedImage rdf:resource=\"&" + strNamespace + ";" + sts.str() +"\"/>\n";
00490 }
00491 }
00492
00493
00494 CKeyValuePair *ckvpFailures = ndCurrent->metaInformation()->childForKey("failures");
00495
00496 if(ckvpFailures) {
00497 std::list<CKeyValuePair*> lstFailures = ckvpFailures->children();
00498
00499 unsigned int unIndex = 0;
00500 for(CKeyValuePair* ckvpFailure : lstFailures) {
00501 std::stringstream sts;
00502 sts << ndCurrent->uniqueID() << "_failure_" << unIndex;
00503 strDot += " <knowrob:eventFailure rdf:resource=\"&" + strNamespace + ";" + sts.str() + "\"/>\n";
00504 m_nThrowAndCatchFailureCounter++;
00505 }
00506 }
00507
00508
00509 CKeyValuePair *ckvpCaughtFailures = ndCurrent->metaInformation()->childForKey("caught_failures");
00510
00511 if(ckvpCaughtFailures) {
00512 std::list<CKeyValuePair*> lstCaughtFailures = ckvpCaughtFailures->children();
00513
00514 unsigned int unIndex = 0;
00515 for(CKeyValuePair* ckvpCaughtFailure : lstCaughtFailures) {
00516 Node* ndFailureEmitter = ndCurrent->emitterForCaughtFailure(ckvpCaughtFailure->stringValue("failure-id"), ckvpCaughtFailure->stringValue("emitter-id"), ckvpCaughtFailure->stringValue("time-catch"));
00517
00518 if(ndFailureEmitter) {
00519 std::string strCaughtFailure = ndFailureEmitter->uniqueID() + "_" + ckvpCaughtFailure->stringValue("failure-id");
00520 m_nThrowAndCatchFailureCounter--;
00521 strDot += " <knowrob:caughtFailure rdf:resource=\"&" + strNamespace + ";" + strCaughtFailure + "\"/>\n";
00522 } else {
00523 this->warn("No emitter for failure '" + ckvpCaughtFailure->stringValue("failure-id") + "'.");
00524 }
00525 }
00526 }
00527
00528
00529 CKeyValuePair* ckvpDesignators = ndCurrent->metaInformation()->childForKey("designators");
00530
00531 if(ckvpDesignators) {
00532 std::list<CKeyValuePair*> lstDesignators = ckvpDesignators->children();
00533
00534 unsigned int unIndex = 0;
00535 for(CKeyValuePair* ckvpDesignator : lstDesignators) {
00536 std::string strAnnotation = ckvpDesignator->stringValue("annotation");
00537 std::string strDesigID = ckvpDesignator->stringValue("id");
00538
00539 m_mapDesignators[strDesigID] = ckvpDesignator;
00540
00541 if(strAnnotation == "parameter-annotation") {
00542 CKeyValuePair* ckvpChildren = ckvpDesignator->childForKey("description");
00543
00544 if(ckvpChildren) {
00545 for(CKeyValuePair* ckvpChild : ckvpChildren->children()) {
00546 std::string strKey = ckvpChild->key();
00547 std::stringstream sts;
00548 bool bSupportedType = true;
00549
00550 switch(ckvpChild->type()) {
00551 case FLOAT:
00552 sts << ckvpChild->floatValue();
00553 break;
00554
00555 case STRING:
00556 sts << ckvpChild->stringValue();
00557 break;
00558
00559 default:
00560 this->warn("Unsupported parameter annotation type for key '" + strKey + "'.");
00561 bSupportedType = false;
00562 break;
00563 }
00564
00565 if(bSupportedType) {
00566 if(find(m_lstAnnotatedParameters.begin(), m_lstAnnotatedParameters.end(), strKey) == m_lstAnnotatedParameters.end()) {
00567 m_lstAnnotatedParameters.push_back(strKey);
00568 }
00569
00570 strDot += " <knowrob:" + strKey + ">" + sts.str() + "</knowrob:" + strKey + ">\n";
00571 strDot += " <knowrob:annotatedParameterType rdf:datatype=\"&xsd;string\">" + strKey + "</knowrob:annotatedParameterType>\n";
00572 }
00573 }
00574 }
00575 }
00576
00577 std::string strDesigPurpose = this->resolveDesignatorAnnotationTagName(strAnnotation);
00578 strDot += " <knowrob:" + strDesigPurpose + " rdf:resource=\"&" + strNamespace + ";" + strDesigID + "\"/>\n";
00579 }
00580 }
00581
00582 strDot += " </owl:namedIndividual>\n\n";
00583 ndLastDisplayed = ndCurrent;
00584 }
00585 } else {
00586 this->fail("Generation of event individual for node with invalid content requested!");
00587 }
00588 }
00589
00590 return strDot;
00591 }
00592
00593 std::string CExporterOwl::resolveDesignatorAnnotationTagName(std::string strAnnotation) {
00594 std::string strDesigPurpose = "";
00595
00596 for(std::pair<std::string, std::string> prMapping : m_lstAnnotationPurposeMapping) {
00597 if(prMapping.first == strAnnotation) {
00598 strDesigPurpose = prMapping.second;
00599 break;
00600 }
00601 }
00602
00603 if(strDesigPurpose == "") {
00604 strDesigPurpose = m_strDefaultAnnotation;
00605 }
00606
00607 return strDesigPurpose;
00608 }
00609
00610 std::string CExporterOwl::generateEventIndividuals(std::string strNamespace) {
00611 std::string strDot = " <!-- Event Individuals -->\n\n";
00612 strDot += this->generateEventIndividualsForNodes(this->nodes(), strNamespace);
00613
00614 return strDot;
00615 }
00616
00617 std::string CExporterOwl::owlClassForObject(CKeyValuePair *ckvpObject) {
00618 return "&knowrob;HumanScaleObject";
00619 }
00620
00621 std::string CExporterOwl::failureClassForCondition(std::string strCondition) {
00622 std::string strFailureClass = "CRAMFailure";
00623
00624 for(std::pair<std::string, std::string> prCurrent : m_lstFailureMapping) {
00625 if(strCondition == prCurrent.first) {
00626 strFailureClass = prCurrent.second;
00627 break;
00628 }
00629 }
00630
00631 return strFailureClass;
00632 }
00633
00634 std::string CExporterOwl::generateFailureIndividualsForNodes(std::list<Node*> lstNodes, std::string strNamespace) {
00635 std::string strDot = "";
00636
00637 for(Node* ndCurrent : lstNodes) {
00638 if(ndCurrent) {
00639 CKeyValuePair *ckvpFailures = ndCurrent->metaInformation()->childForKey("failures");
00640
00641 if(ckvpFailures) {
00642 std::list<CKeyValuePair*> lstFailures = ckvpFailures->children();
00643
00644 unsigned int unIndex = 0;
00645 for(CKeyValuePair* ckvpFailure : lstFailures) {
00646 std::stringstream sts;
00647 sts << ndCurrent->uniqueID() << "_failure_" << unIndex;
00648
00649 std::string strCondition = ckvpFailure->stringValue("condition");
00650 std::string strTimestamp = ckvpFailure->stringValue("time-fail");
00651
00652 std::string strFailureClass = this->failureClassForCondition(strCondition);
00653
00654 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + sts.str() + "\">\n";
00655 strDot += " <rdf:type rdf:resource=\"&knowrob;" + strFailureClass + "\"/>\n";
00656 strDot += " <rdfs:label rdf:datatype=\"&xsd;string\">" + this->owlEscapeString(strCondition) + "</rdfs:label>\n";
00657 strDot += " <knowrob:startTime rdf:resource=\"&" + strNamespace + ";timepoint_" + strTimestamp + "\"/>\n";
00658 strDot += " </owl:namedIndividual>\n\n";
00659 }
00660 }
00661
00662 strDot += this->generateFailureIndividualsForNodes(ndCurrent->subnodes(), strNamespace);
00663 } else {
00664 this->fail("Failure node with invalid content!");
00665 }
00666 }
00667
00668 return strDot;
00669 }
00670
00671 std::string CExporterOwl::generateObjectIndividualsForNodes(std::list<Node*> lstNodes, std::string strNamespace) {
00672 std::string strDot = "";
00673
00674 for(Node* ndCurrent : lstNodes) {
00675 if(ndCurrent) {
00676 CKeyValuePair* ckvpObjects = ndCurrent->metaInformation()->childForKey("objects");
00677
00678 if(ckvpObjects) {
00679 std::list<CKeyValuePair*> lstObjects = ckvpObjects->children();
00680
00681 unsigned int unIndex = 0;
00682 for(CKeyValuePair* ckvpObject : lstObjects) {
00683 std::string strDesignatorID = ckvpObject->stringValue("__id");
00684
00685 std::string strDefClass = ckvpObject->stringValue("_class");
00686 std::string strDefClassNamespace = ckvpObject->stringValue("_classnamespace");
00687
00688 std::string strOwlClass = strDefClass;
00689 if(strOwlClass == "") {
00690 strOwlClass = this->owlClassForObject(ckvpObject);
00691 } else {
00692 strOwlClass = strDefClassNamespace + strDefClass;
00693 }
00694
00695 if(strDefClass == "") {
00696 strDefClass = "object";
00697 }
00698
00699 if(strDefClassNamespace == "") {
00700 strDefClassNamespace = "&" + strNamespace + ";";
00701 }
00702
00703 std::string strObjectID = strDefClass + "_" + ckvpObject->stringValue("__id");
00704
00705 if(find(m_lstExportedObjectIndividuals.begin(), m_lstExportedObjectIndividuals.end(), strObjectID) == m_lstExportedObjectIndividuals.end()) {
00706 strDot += " <owl:namedIndividual rdf:about=\"" + strDefClassNamespace + strObjectID + "\">\n";
00707 strDot += " <knowrob:designator rdf:resource=\"&" + strNamespace + ";" + strDesignatorID + "\"/>\n";
00708 strDot += " <rdf:type rdf:resource=\"" + strOwlClass + "\"/>\n";
00709 strDot += " </owl:namedIndividual>\n\n";
00710
00711 m_lstExportedObjectIndividuals.push_back(strObjectID);
00712 }
00713 }
00714 }
00715
00716 strDot += this->generateObjectIndividualsForNodes(ndCurrent->subnodes(), strNamespace);
00717 } else {
00718 this->fail("Generation of object individual for node with invalid content requested!");
00719 }
00720 }
00721
00722 return strDot;
00723 }
00724
00725 std::string CExporterOwl::generateObjectIndividuals(std::string strNamespace) {
00726 std::string strDot = " <!-- Object Individuals -->\n\n";
00727 strDot += this->generateObjectIndividualsForNodes(this->nodes(), strNamespace);
00728
00729 return strDot;
00730 }
00731
00732 std::string CExporterOwl::generateImageIndividualsForNodes(std::list<Node*> lstNodes, std::string strNamespace) {
00733 std::string strDot = "";
00734
00735 for(Node* ndCurrent : lstNodes) {
00736 if(ndCurrent) {
00737 CKeyValuePair* ckvpImages = ndCurrent->metaInformation()->childForKey("images");
00738
00739 if(ckvpImages) {
00740 std::list<CKeyValuePair*> lstImages = ckvpImages->children();
00741
00742 unsigned int unIndex = 0;
00743 for(CKeyValuePair* ckvpImage : lstImages) {
00744 std::stringstream sts;
00745 sts << ndCurrent->uniqueID() << "_image_" << unIndex;
00746
00747 std::string strOwlClass = "&knowrob;CameraImage";
00748 std::string strFilename = ckvpImage->stringValue("filename");
00749 std::string strTopic = ckvpImage->stringValue("origin");
00750 std::string strCaptureTime = ckvpImage->stringValue("time-capture");
00751
00752 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + sts.str() + "\">\n";
00753 strDot += " <knowrob:linkToImageFile rdf:datatype=\"&xsd;string\">" + strFilename + "</knowrob:linkToImageFile>\n";
00754 strDot += " <knowrob:rosTopic rdf:datatype=\"&xsd;string\">" + strTopic + "</knowrob:rosTopic>\n";
00755 strDot += " <knowrob:captureTime rdf:resource=\"&" + strNamespace + ";timepoint_" + strCaptureTime + "\"/>\n";
00756 strDot += " <rdf:type rdf:resource=\"" + strOwlClass + "\"/>\n";
00757 strDot += " </owl:namedIndividual>\n\n";
00758 }
00759 }
00760
00761 strDot += this->generateImageIndividualsForNodes(ndCurrent->subnodes(), strNamespace);
00762 } else {
00763 this->fail("Image node with invalid content!");
00764 }
00765 }
00766
00767 return strDot;
00768 }
00769
00770 std::string CExporterOwl::generateImageIndividuals(std::string strNamespace) {
00771 std::string strDot = " <!-- Image Individuals -->\n\n";
00772
00773 strDot += this->generateImageIndividualsForNodes(this->nodes(), strNamespace);
00774
00775 return strDot;
00776 }
00777
00778 std::string CExporterOwl::generateDesignatorIndividuals(std::string strNamespace) {
00779 std::string strDot = " <!-- Designator Individuals -->\n\n";
00780
00781 std::list<std::string> lstDesigIDs = this->designatorIDs();
00782
00783 for(pair<std::string, CKeyValuePair*> prDesig : m_mapDesignators) {
00784 prDesig.second->printPair(0); std::cout << std::endl;
00785 }
00786
00787 for(std::string strID : lstDesigIDs) {
00788 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + strID + "\">\n";
00789 strDot += " <rdf:type rdf:resource=\"&knowrob;CRAMDesignator\"/>\n";
00790
00791 if(m_mapDesignators.find(strID) != m_mapDesignators.end()) {
00792 std::string strTimeCreated = m_mapDesignators[strID]->childForKey("description")->stringValue("_time_created");
00793 strDot += " <knowrob:creationTime rdf:resource=\"&" + strNamespace + ";timepoint_" + strTimeCreated + "\"/>\n";
00794 }
00795
00796 std::list<std::string> lstSuccessorIDs = this->successorDesignatorsForID(strID);
00797 for(std::string strID2 : lstSuccessorIDs) {
00798 strDot += " <knowrob:successorDesignator rdf:resource=\"&" + strNamespace + ";" + strID2 + "\"/>\n";
00799 }
00800
00801 std::string strEquationTime = this->equationTimeForSuccessorID(strID);
00802 if(strEquationTime != "") {
00803 strDot += " <knowrob:equationTime rdf:resource=\"&" + strNamespace + ";timepoint_" + strEquationTime + "\"/>\n";
00804 }
00805
00806
00807
00808
00809 if(lstSuccessorIDs.size() > 0 && strEquationTime == "") {
00810 strDot += "\n <!-- This is an index designator -->\n";
00811
00812 std::list<std::string> lstAllSuccessors = this->collectAllSuccessorDesignatorIDs(strID);
00813 for(std::string strSuccessor : lstAllSuccessors) {
00814 strDot += " <knowrob:equatedDesignator rdf:resource=\"&" + strNamespace + ";" + strSuccessor + "\"/>\n";
00815 }
00816 }
00817
00818 strDot += " </owl:namedIndividual>\n\n";
00819 }
00820
00821 return strDot;
00822 }
00823
00824 std::list<std::string> CExporterOwl::collectAllSuccessorDesignatorIDs(std::string strDesigID) {
00825 std::list<std::string> lstReturn;
00826 std::list<std::string> lstSuccessors = this->successorDesignatorsForID(strDesigID);
00827
00828 for(std::string strSuccessor : lstSuccessors) {
00829 std::list<std::string> lstSubSuccessors = this->collectAllSuccessorDesignatorIDs(strSuccessor);
00830
00831 for(std::string strSubSuccessor : lstSubSuccessors) {
00832 lstReturn.push_back(strSubSuccessor);
00833 }
00834
00835 lstReturn.push_back(strSuccessor);
00836 }
00837
00838 return lstReturn;
00839 }
00840
00841 std::string CExporterOwl::generateFailureIndividuals(std::string strNamespace) {
00842 std::string strDot = " <!-- Failure Individuals -->\n\n";
00843 strDot += this->generateFailureIndividualsForNodes(this->nodes(), strNamespace);
00844
00845 return strDot;
00846 }
00847
00848 std::string CExporterOwl::generateTimepointIndividuals(std::string strNamespace) {
00849 std::string strDot = " <!-- Timepoint Individuals -->\n\n";
00850
00851 std::list<std::string> lstTimepoints = this->gatherTimepointsForNodes(this->nodes());
00852 for(std::string strTimepoint : lstTimepoints) {
00853 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";timepoint_" + strTimepoint + "\">\n";
00854 strDot += " <rdf:type rdf:resource=\"&knowrob;TimePoint\"/>\n";
00855 strDot += " </owl:namedIndividual>\n\n";
00856 }
00857
00858 return strDot;
00859 }
00860
00861 std::string CExporterOwl::generateMetaDataIndividual(std::string strNamespace) {
00862 std::string strDot = " <!-- Meta Data Individual -->\n\n";
00863 std::string strUniqueName = this->generateUniqueID("ExperimentMetaData_", 8);
00864
00865 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + strUniqueName + "\">\n";
00866 strDot += " <rdf:type rdf:resource=\"&knowrob;ExperimentMetaData\"/>\n";
00867
00868 std::list<Node*> lstRootNodes = this->rootNodes();
00869 for(Node* ndRoot : lstRootNodes) {
00870 strDot += " <knowrob:subAction rdf:resource=\"&" + strNamespace + ";" + ndRoot->uniqueID() + "\"/>\n";
00871 }
00872
00873 for(std::pair<std::string, std::string> prEntry : m_mapMetaData) {
00874 std::string strCamelCaseKey = prEntry.first;
00875 int nCharCount = prEntry.first.length();
00876
00877 for(int nI = 0; nI < nCharCount; nI++) {
00878 if(strCamelCaseKey[nI] == '-') {
00879 std::string strTemp = strCamelCaseKey.substr(nI + 1, 1);
00880 transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::toupper);
00881 strCamelCaseKey.erase(nI, 2);
00882 strCamelCaseKey.insert(nI, strTemp);
00883 nCharCount--;
00884 }
00885 }
00886
00887 strDot += " <knowrob:" + strCamelCaseKey + " rdf:datatype=\"&xsd;string\">" + prEntry.second + "</knowrob:" + strCamelCaseKey + ">\n";
00888 }
00889 strDot += " </owl:namedIndividual>\n\n";
00890
00891 return strDot;
00892 }
00893
00894 std::string CExporterOwl::generateParameterAnnotationInformation(std::string strNamespace) {
00895 std::string strDot = " <!-- Parameter Annotation Information Individual -->\n\n";
00896 std::string strUniqueName = this->generateUniqueID("AnnotationInformation_", 8);
00897
00898 strDot += " <owl:namedIndividual rdf:about=\"&" + strNamespace + ";" + strUniqueName + "\">\n";
00899 strDot += " <rdf:type rdf:resource=\"&knowrob;AnnotationInformation\"/>\n";
00900
00901 for(std::string strParameterAnnotation : m_lstAnnotatedParameters) {
00902 strDot += " <knowrob:annotatedParameterType rdf:datatype=\"&xsd;string\">" + strParameterAnnotation + "</knowrob:annotatedParameterType>\n";
00903 }
00904
00905 strDot += " </owl:namedIndividual>\n\n";
00906
00907 return strDot;
00908 }
00909
00910 std::string CExporterOwl::owlClassForNode(Node *ndNode, bool bClassOnly, bool bPrologSyntax) {
00911 std::string strName = "";
00912
00913 if(ndNode) {
00914 strName = ndNode->title();
00915 }
00916
00917 std::string strPlainPrefix = "knowrob";
00918 std::string strPrefix = (bPrologSyntax ? strPlainPrefix + ":" : "&" + strPlainPrefix + ";");
00919 std::string strClass = "CRAMAction";
00920
00921 if(strName == "WITH-DESIGNATORS") {
00922
00923 strClass = "WithDesignators";
00924 } else if(strName == "TAG") {
00925 strClass = "Tag";
00926 } else if(strName.substr(0, 5) == "GOAL-") {
00927
00928 std::string strGoal = strName.substr(5);
00929
00930
00931
00932
00933
00934
00935
00936
00937 if(strGoal == "PERCEIVE-OBJECT") {
00938 strClass = "CRAMPerceive";
00939 } else if(strGoal == "ACHIEVE") {
00940 strClass = "CRAMAchieve";
00941 } else if(strGoal == "PERFORM") {
00942 strClass = "CRAMPerform";
00943 } else if(strGoal == "MONITOR-ACTION") {
00944 strClass = "CRAMMonitor";
00945 } else if(strGoal == "PERFORM-ON-PROCESS-MODULE") {
00946 strClass = "PerformOnProcessModule";
00947 } else {
00948 strClass = "DeclarativeGoal";
00949 }
00950 } else if(strName.substr(0, 8) == "RESOLVE-") {
00951
00952 std::string strDesigType = strName.substr(8);
00953
00954 if(strDesigType == "LOCATION-DESIGNATOR") {
00955 strClass = "ResolveLocationDesignator";
00956 } else if(strDesigType == "ACTION-DESIGNATOR") {
00957 strClass = "ResolveActionDesignator";
00958 }
00959 } else if(strName.substr(0, 21) == "REPLACEABLE-FUNCTION-") {
00960
00961 std::string strFunction = strName.substr(21);
00962
00963 if(strFunction == "NAVIGATE") {
00964 strClass = "BaseMovement";
00965 }
00966 } else if(strName.substr(0, 8) == "PERFORM-") {
00967
00968 std::string strPerformer = strName.substr(8);
00969
00970 if(strPerformer == "ACTION-DESIGNATOR") {
00971 CKeyValuePair* ckvpDescription = NULL;
00972 std::list<CKeyValuePair*> lstDesc = ndNode->description();
00973
00974 for(CKeyValuePair* ckvpCurrent : lstDesc) {
00975 if(ckvpCurrent->key() == "DESCRIPTION") {
00976 ckvpDescription = ckvpCurrent;
00977 break;
00978 }
00979 }
00980
00981 bool bSpecializedDesignator = true;
00982 if(ckvpDescription) {
00983 std::string strTo = ckvpDescription->stringValue("TO");
00984 std::string strType = ckvpDescription->stringValue("TYPE");
00985
00986 if(strTo == "GRASP") {
00987
00988 strClass = "PickingUpAnObject";
00989 } else if(strTo == "LIFT") {
00990
00991 strClass = "LiftingAnObject";
00992 } else if(strTo == "CARRY") {
00993
00994 strClass = "CarryingAnObject";
00995 } else if(strTo == "PERCEIVE") {
00996
00997 strClass = "PerceivingObjects";
00998 } else if(strTo == "PUT-DOWN") {
00999
01000 strClass = "PuttingDownAnObject";
01001 } else if(strTo == "PARK") {
01002
01003 strClass = "ParkingArms";
01004 } else if(strType == "NAVIGATION") {
01005
01006 strClass = "Navigate";
01007 } else {
01008
01009 bSpecializedDesignator = false;
01010 }
01011 } else {
01012
01013 bSpecializedDesignator = false;
01014 }
01015
01016 if(bSpecializedDesignator == false) {
01017
01018 strClass = "PerformActionDesignator";
01019 }
01020 }
01021 } else if(strName == "UIMA-PERCEIVE") {
01022 strClass = "UIMAPerception";
01023 } else if(strName == "FIND-OBJECTS") {
01024 strClass = "FindingObjects";
01025 } else if(strName == "OBJECT-IDENTITY-RESOLUTION") {
01026 strClass = "ObjectIdentityResolution";
01027 } else if(strName == "BELIEF-STATE-UPDATE") {
01028 strClass = "BeliefStateUpdate";
01029 } else if(strName == "MOTION-PLANNING") {
01030 strClass = "MotionPlanning";
01031 } else if(strName == "MOTION-EXECUTION") {
01032 strClass = "MotionExecution";
01033 } else if(strName == "AT-LOCATION") {
01034 strClass = "AtLocation";
01035 } else if(strName == "VOLUNTARY-BODY-MOVEMENT-ARMS") {
01036 strClass = "ArmMovement";
01037 } else if(strName == "VOLUNTARY-BODY-MOVEMENT-HEAD") {
01038 strClass = "HeadMovement";
01039 } else if(strName == "WITH-FAILURE-HANDLING") {
01040 strClass = "WithFailureHandling";
01041 } else if(strName == "WITH-POLICY") {
01042 strClass = "WithPolicy";
01043 }
01044
01045 return (bClassOnly ? "" : strPrefix) + (bPrologSyntax ? "'" + strClass + "'" : strClass);
01046 }
01047
01048 bool CExporterOwl::runExporter(CKeyValuePair* ckvpConfigurationOverlay) {
01049 m_lstAnnotatedParameters.clear();
01050 m_lstExportedObjectIndividuals.clear();
01051
01052 m_nThrowAndCatchFailureCounter = 0;
01053
01054 this->info("Renewing unique IDs");
01055 this->renewUniqueIDs();
01056
01057 this->info("Generating XML");
01058 if(this->outputFilename() != "") {
01059 std::string strOwl = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n";
01060
01061
01062
01063 std::string strNamespaceID = "log";
01064 std::string strNamespace = "http://ias.cs.tum.edu/kb/cram_log.owl";
01065
01066
01067 this->info(" - Preparing content");
01068 this->prepareEntities(strNamespaceID, strNamespace);
01069
01070
01071 this->info(" - Generating source");
01072 strOwl += this->generateOwlStringForNodes(this->nodes(), strNamespaceID, strNamespace);
01073
01074
01075 this->info(" - Writing file");
01076 return this->writeToFile(strOwl);
01077 } else {
01078 this->fail("No output filename was given. Cancelling.");
01079 }
01080
01081 return false;
01082 }
01083
01084 std::string CExporterOwl::owlEscapeString(std::string strValue) {
01085 return strValue;
01086 }
01087
01088 std::string CExporterOwl::generateOwlStringForNodes(std::list<Node*> lstNodes, std::string strNamespaceID, std::string strNamespace) {
01089 std::string strOwl = "";
01090
01091
01092 this->info(" - Block: DocType");
01093 strOwl += this->generateDocTypeBlock();
01094 this->info(" - Block: XMLNS");
01095 strOwl += this->generateXMLNSBlock(strNamespace);
01096 this->info(" - Block: Imports");
01097 strOwl += this->generateOwlImports(strNamespace);
01098 this->info(" - Block: PropDefs");
01099 strOwl += this->generatePropertyDefinitions();
01100 this->info(" - Block: ClassDefs");
01101 strOwl += this->generateClassDefinitions();
01102 this->info(" - Block: EvtIndivs");
01103 strOwl += this->generateEventIndividuals(strNamespaceID);
01104 this->info(" - Block: ObjIndivs");
01105 strOwl += this->generateObjectIndividuals(strNamespaceID);
01106 this->info(" - Block: ImgIndivs");
01107 strOwl += this->generateImageIndividuals(strNamespaceID);
01108 this->info(" - Block: DesigIndivs");
01109 strOwl += this->generateDesignatorIndividuals(strNamespaceID);
01110 this->info(" - Block: FailIndivs");
01111 strOwl += this->generateFailureIndividuals(strNamespaceID);
01112 this->info(" - Block: TPIndivs");
01113 strOwl += this->generateTimepointIndividuals(strNamespaceID);
01114 this->info(" - Meta Data");
01115 strOwl += this->generateMetaDataIndividual(strNamespaceID);
01116 this->info(" - Parameter Annotations");
01117 strOwl += this->generateParameterAnnotationInformation(strNamespaceID);
01118 strOwl += "</rdf:RDF>\n";
01119
01120 if(m_nThrowAndCatchFailureCounter != 0) {
01121 this->warn("Throw/Catch failure counter is != 0: '" + this->str(m_nThrowAndCatchFailureCounter) + "'");
01122 }
01123
01124 return strOwl;
01125 }
01126 }