| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License |
| 7 | as published by the Free Software Foundation; either version 2 |
| 8 | of the License, or (at your option) any later version. |
| 9 | |
| 10 | This program 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 |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifndef PARSER_H |
| 21 | #define PARSER_H |
| 22 | |
| 23 | #include "scanner.h" |
| 24 | #define GenericToken LispToken |
| 25 | #include "comparser.h" |
| 26 | #include "Kernel.h" |
| 27 | |
| 28 | /// class for parsing LISP-like ontologies |
| 29 | class DLLispParser: public CommonParser<TsScanner> |
| 30 | { |
| 31 | protected: // typedefs |
| 32 | /// general expression |
| 33 | typedef ReasoningKernel::TExpr TExpr; |
| 34 | /// concept expression |
| 35 | typedef ReasoningKernel::TConceptExpr TConceptExpr; |
| 36 | /// individual expression |
| 37 | typedef ReasoningKernel::TIndividualExpr TIndividualExpr; |
| 38 | /// role expression |
| 39 | typedef ReasoningKernel::TRoleExpr TRoleExpr; |
| 40 | /// object role complex expression (including role chains and projections) |
| 41 | typedef ReasoningKernel::TORoleComplexExpr TORoleComplexExpr; |
| 42 | /// object role expression |
| 43 | typedef ReasoningKernel::TORoleExpr TORoleExpr; |
| 44 | /// data role expression |
| 45 | typedef ReasoningKernel::TDRoleExpr TDRoleExpr; |
| 46 | /// data expression |
| 47 | typedef ReasoningKernel::TDataExpr TDataExpr; |
| 48 | /// data type expression |
| 49 | typedef ReasoningKernel::TDataTypeExpr TDataTypeExpr; |
| 50 | /// data value expression |
| 51 | typedef ReasoningKernel::TDataValueExpr TDataValueExpr; |
| 52 | |
| 53 | protected: // members |
| 54 | /// Kernel to be filled |
| 55 | ReasoningKernel* Kernel; |
| 56 | /// expression manager to be used |
| 57 | TExpressionManager* EManager; |
| 58 | /// set of known data role names |
| 59 | std::set<std::string> DataRoles; |
| 60 | |
| 61 | protected: // methods |
| 62 | /// error by given exception |
| 63 | void errorByException ( const EFPPCantRegName& ex ) const { parseError(ex.what()); } |
| 64 | |
| 65 | /// @return concept-like Id of just scanned name |
| 66 | TConceptExpr* getConcept ( void ) |
| 67 | { |
| 68 | TConceptExpr* ret = EManager->Concept(scan.GetName()); |
| 69 | NextLex(); |
| 70 | return ret; |
| 71 | } |
| 72 | /// @return singleton Id of just scanned name |
| 73 | TIndividualExpr* getSingleton ( void ) |
| 74 | { |
| 75 | TIndividualExpr* ret = EManager->Individual(scan.GetName()); |
| 76 | NextLex(); |
| 77 | return ret; |
| 78 | } |
| 79 | /// @return data- or object role build from just scanned name |
| 80 | TRoleExpr* getRole ( void ) |
| 81 | { |
| 82 | TRoleExpr* ret; |
| 83 | if ( DataRoles.find(scan.GetName()) != DataRoles.end() ) |
| 84 | ret = EManager->DataRole(scan.GetName()); // found data role |
| 85 | else // object role |
| 86 | ret = EManager->ObjectRole(scan.GetName()); |
| 87 | NextLex(); |
| 88 | return ret; |
| 89 | } |
| 90 | /// @return data role build from just scanned name |
| 91 | TDRoleExpr* getDataRole ( void ) |
| 92 | { |
| 93 | DataRoles.insert(scan.GetName()); |
| 94 | TDRoleExpr* ret = EManager->DataRole(scan.GetName()); |
| 95 | NextLex(); |
| 96 | return ret; |
| 97 | } |
| 98 | /// @return object role build from just scanned name |
| 99 | TORoleExpr* getObjectRole ( void ) |
| 100 | { |
| 101 | TORoleExpr* ret = EManager->ObjectRole(scan.GetName()); |
| 102 | NextLex(); |
| 103 | return ret; |
| 104 | } |
| 105 | /// @return datavalue of a data type TYPE with an Id of a just scanned name |
| 106 | TDataValueExpr* getDTValue ( TDataTypeExpr* type ) |
| 107 | { |
| 108 | TDataValueExpr* ret = EManager->DataValue ( scan.GetName(), type ); |
| 109 | NextLex(); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | /// check whether expression R is data role |
| 114 | bool isDataRole ( const TRoleExpr* R ) const { return dynamic_cast<const TDRoleExpr*>(R) != nullptr; } |
| 115 | /// generate object role axiom between R and S according to the operation TAG |
| 116 | void tellRoleAxiom ( LispToken tag, TORoleExpr* R, TORoleExpr* S ); |
| 117 | /// generate data role axiom between R and S according to the operation TAG |
| 118 | void tellRoleAxiom ( LispToken tag, TDRoleExpr* R, TDRoleExpr* S ); |
| 119 | /// get role expression, ie (data)role or its inverse |
| 120 | TRoleExpr* getRoleExpression ( void ); |
| 121 | /// get object role expression, ie object role, OR constant or their inverse |
| 122 | TORoleExpr* getORoleExpression ( void ); |
| 123 | /// get data role expression, ie data role or DR constant |
| 124 | TDRoleExpr* getDRoleExpression ( void ); |
| 125 | /// get simple role expression or role projection or chain |
| 126 | TRoleExpr* getComplexRoleExpression ( void ); |
| 127 | /// parse simple DL command |
| 128 | void parseCommand ( void ); |
| 129 | /// parse role arguments if defprimrole command |
| 130 | void parseRoleArguments ( TORoleExpr* role ); |
| 131 | /// parse list of concept expressions (in disjoint-like commands) |
| 132 | void parseConceptList ( bool singletonsOnly ); |
| 133 | /// get concept-like expression for simple variants |
| 134 | TConceptExpr* getConceptExpression ( void ); |
| 135 | /// get concept-like expression for complex constructors |
| 136 | TConceptExpr* getComplexConceptExpression ( void ); |
| 137 | /// get data expression |
| 138 | TDataExpr* getDataExpression ( void ); |
| 139 | |
| 140 | public: // interface |
| 141 | /// the only c'tor |
| 142 | DLLispParser ( std::istream* in, ReasoningKernel* kernel ) |
| 143 | : CommonParser<TsScanner>(in) |
| 144 | , Kernel (kernel) |
| 145 | , EManager(kernel->getExpressionManager()) |
| 146 | { |
| 147 | // locally register Top/Bottom data properties |
| 148 | DataRoles.insert("*UDROLE*"); |
| 149 | DataRoles.insert("*EDROLE*"); |
| 150 | } |
| 151 | |
| 152 | /// main parsing method |
| 153 | void Parse ( void ); |
| 154 | }; // DLLispParser |
| 155 | |
| 156 | #endif |