| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2010-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef TEXPRESSIONPRINTERLISP_H |
| 21 | #define TEXPRESSIONPRINTERLISP_H |
| 22 | |
| 23 | #include <ostream> |
| 24 | |
| 25 | #include "tDLExpression.h" |
| 26 | |
| 27 | class TLISPExpressionPrinter: public DLExpressionVisitor |
| 28 | { |
| 29 | protected: // members |
| 30 | /// main stream |
| 31 | std::ostream& o; |
| 32 | /// define str-str map |
| 33 | typedef std::map<std::string, std::string> SSMap; |
| 34 | /// map between OWL datatype names and FaCT++ ones |
| 35 | SSMap DTNames; |
| 36 | /// helper class for brackets |
| 37 | class BR |
| 38 | { |
| 39 | protected: |
| 40 | std::ostream& o; |
| 41 | public: |
| 42 | BR ( std::ostream& o_, const char* command ) : o(o_) { o << " (" << command; } |
| 43 | ~BR() { o << ")"; } |
| 44 | }; // BR |
| 45 | protected: // methods |
| 46 | /// array helper |
| 47 | template <typename Argument> |
| 48 | void printArray ( const TDLNAryExpression<Argument>& expr ) |
| 49 | { |
| 50 | for ( typename TDLNAryExpression<Argument>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 51 | (*p)->accept(*this); |
| 52 | } |
| 53 | /// datatype helper to get a LISP datatype name by a OWL one |
| 54 | const char* getDTName ( const char* owlName ) const |
| 55 | { |
| 56 | SSMap::const_iterator p = DTNames.find(owlName); |
| 57 | if ( p != DTNames.end() ) // known name |
| 58 | return p->second.c_str(); |
| 59 | return owlName; |
| 60 | } |
| 61 | |
| 62 | public: // interface |
| 63 | /// init c'tor |
| 64 | explicit TLISPExpressionPrinter ( std::ostream& o_ ) : o(o_) |
| 65 | { |
| 66 | DTNames["http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral"] = "string"; |
| 67 | DTNames["http://www.w3.org/2001/XMLSchema#string"] = "string"; |
| 68 | DTNames["http://www.w3.org/2001/XMLSchema#anyURI"] = "string"; |
| 69 | |
| 70 | DTNames["http://www.w3.org/2001/XMLSchema#integer"] = "number"; |
| 71 | DTNames["http://www.w3.org/2001/XMLSchema#int"] = "number"; |
| 72 | |
| 73 | DTNames["http://www.w3.org/2001/XMLSchema#float"] = "real"; |
| 74 | DTNames["http://www.w3.org/2001/XMLSchema#double"] = "real"; |
| 75 | DTNames["http://www.w3.org/2001/XMLSchema#real"] = "real"; |
| 76 | } |
| 77 | |
| 78 | public: // visitor interface |
| 79 | // concept expressions |
| 80 | void visit ( const TDLConceptTop& ) override { o << " *TOP*"; } |
| 81 | void visit ( const TDLConceptBottom& ) override { o << " *BOTTOM*"; } |
| 82 | void visit ( const TDLConceptName& expr ) override { o << " " << expr.getName(); } |
| 83 | void visit ( const TDLConceptNot& expr ) override { BR b(o,"not"); expr.getC()->accept(*this); } |
| 84 | void visit ( const TDLConceptAnd& expr ) override { BR b(o,"and"); printArray(expr); } |
| 85 | void visit ( const TDLConceptOr& expr ) override { BR b(o,"or"); printArray(expr); } |
| 86 | void visit ( const TDLConceptOneOf& expr ) override { BR b(o,"one-of"); printArray(expr); } |
| 87 | void visit ( const TDLConceptObjectSelf& expr ) override { BR b(o,"self-ref"); expr.getOR()->accept(*this); } |
| 88 | void visit ( const TDLConceptObjectValue& expr ) override { BR b(o,"some"); expr.getOR()->accept(*this); BR i(o,"one-of"); expr.getI()->accept(*this); } |
| 89 | void visit ( const TDLConceptObjectExists& expr ) override { BR b(o,"some"); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 90 | void visit ( const TDLConceptObjectForall& expr ) override { BR b(o,"all"); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 91 | void visit ( const TDLConceptObjectMinCardinality& expr ) override |
| 92 | { BR b(o,"atleast"); o << " " << expr.getNumber(); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 93 | void visit ( const TDLConceptObjectMaxCardinality& expr ) override |
| 94 | { BR b(o,"atmost"); o << " " << expr.getNumber(); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 95 | void visit ( const TDLConceptObjectExactCardinality& expr ) override |
| 96 | { |
| 97 | BR a(o,"and"); |
| 98 | { BR b(o,"atleast"); o << " " << expr.getNumber(); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 99 | { BR b(o,"atmost"); o << " " << expr.getNumber(); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 100 | } |
| 101 | void visit ( const TDLConceptDataValue& expr ) override { BR b(o,"some"); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 102 | void visit ( const TDLConceptDataExists& expr ) override { BR b(o,"some"); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 103 | void visit ( const TDLConceptDataForall& expr ) override { BR b(o,"all"); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 104 | void visit ( const TDLConceptDataMinCardinality& expr ) override |
| 105 | { BR b(o,"atleast"); o << " " << expr.getNumber(); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 106 | void visit ( const TDLConceptDataMaxCardinality& expr ) override |
| 107 | { BR b(o,"atmost"); o << " " << expr.getNumber(); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 108 | void visit ( const TDLConceptDataExactCardinality& expr ) override |
| 109 | { |
| 110 | BR a(o,"and"); |
| 111 | { BR b(o,"atleast"); o << " " << expr.getNumber(); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 112 | { BR b(o,"atmost"); o << " " << expr.getNumber(); expr.getDR()->accept(*this); expr.getExpr()->accept(*this); } |
| 113 | } |
| 114 | |
| 115 | // individual expressions |
| 116 | void visit ( const TDLIndividualName& expr ) override { o << " " << expr.getName(); } |
| 117 | |
| 118 | // object role expressions |
| 119 | void visit ( const TDLObjectRoleTop& ) override { o << " *UROLE*"; } |
| 120 | void visit ( const TDLObjectRoleBottom& ) override { o << " *EROLE*"; } |
| 121 | void visit ( const TDLObjectRoleName& expr ) override { o << " " << expr.getName(); } |
| 122 | void visit ( const TDLObjectRoleInverse& expr ) override { BR b(o,"inv"); expr.getOR()->accept(*this); } |
| 123 | void visit ( const TDLObjectRoleChain& expr ) override { BR b(o,"compose"); printArray(expr); } |
| 124 | void visit ( const TDLObjectRoleProjectionFrom& expr ) override |
| 125 | { BR b(o,"project_from"); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 126 | void visit ( const TDLObjectRoleProjectionInto& expr ) override |
| 127 | { BR b(o,"project_into"); expr.getOR()->accept(*this); expr.getC()->accept(*this); } |
| 128 | |
| 129 | // data role expressions |
| 130 | void visit ( const TDLDataRoleTop& ) override { o << " *UDROLE*"; } |
| 131 | void visit ( const TDLDataRoleBottom& ) override { o << " *EDROLE*"; } |
| 132 | void visit ( const TDLDataRoleName& expr ) override { o << " " << expr.getName(); } |
| 133 | |
| 134 | // data expressions |
| 135 | void visit ( const TDLDataTop& ) override { o << " *TOP*"; } |
| 136 | void visit ( const TDLDataBottom& ) override { o << " *BOTTOM*"; } |
| 137 | void visit ( const TDLDataTypeName& expr ) override { o << " (" << getDTName(expr.getName()) << ")"; } |
| 138 | // no need to use a type of a restriction here, as all contains in constants |
| 139 | void visit ( const TDLDataTypeRestriction& expr ) override { BR b(o,"and"); printArray(expr); } |
| 140 | void visit ( const TDLDataValue& expr ) override |
| 141 | { o << " (" << getDTName(getBasicDataType(const_cast<TDLDataTypeExpression*>(expr.getExpr()))->getName()) << " " << expr.getName() << ")"; } |
| 142 | void visit ( const TDLDataNot& expr ) override { BR b(o,"not"); expr.getExpr()->accept(*this); } |
| 143 | void visit ( const TDLDataAnd& expr ) override { BR b(o,"and"); printArray(expr); } |
| 144 | void visit ( const TDLDataOr& expr ) override { BR b(o,"or"); printArray(expr); } |
| 145 | void visit ( const TDLDataOneOf& expr ) override { BR b(o,"d-one-of"); printArray(expr); } |
| 146 | |
| 147 | // facets |
| 148 | void visit ( const TDLFacetMinInclusive& expr ) override { BR b(o,"ge"); expr.getExpr()->accept(*this); } |
| 149 | void visit ( const TDLFacetMinExclusive& expr ) override { BR b(o,"gt"); expr.getExpr()->accept(*this); } |
| 150 | void visit ( const TDLFacetMaxInclusive& expr ) override { BR b(o,"le"); expr.getExpr()->accept(*this); } |
| 151 | void visit ( const TDLFacetMaxExclusive& expr ) override { BR b(o,"lt"); expr.getExpr()->accept(*this); } |
| 152 | }; // TLISPExpressionPrinter |
| 153 | |
| 154 | #endif |