Debian Codegraph @

FaCT++/FaCT.cpp

raw symbols SCIP
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 #include <iostream>
21 #include <iomanip>
22 #include <fstream>
23 #include <sstream>
24
25 #include "procTimer.h"
26 #include "parser.h"
27 #include "configure.h"
28 #include "logging.h"
29
30 #include "Kernel.h"
31 #include "cpm.h"
32
33 TsProcTimer totalTimer, wTimer;
34 Configuration Config;
35 ReasoningKernel Kernel;
36 std::ofstream Out;
37
38 // defined in AD.cpp
39 void CreateAD ( TOntology* O, ModuleMethod moduleMethod );
40
41 // local methods
42 inline void Usage ( void )
43 {
44 std::cerr << "\nUsage:\tFaCT++ <Conf file> or\n\tFaCT++ -get-default-options\n\n";
45 exit (1);
46 }
47
48 inline void error ( const char* mes )
49 {
50 std::cerr << mes << "\n";
51 exit(2);
52 }
53
54 inline void OutTime ( std::ostream& o )
55 {
56 o << "Working time = " << totalTimer << " seconds\n";
57 }
58
59 //----------------------------------------------------------------------------------
60 // SAT/SUB queries
61 //----------------------------------------------------------------------------------
62
63 std::string Query[2];
64
65 /// fill query target names by configure
66 static void
67 fillSatSubQuery ( void )
68 {
69 // founds a target for checking
70 if ( !Config.checkValue ( "Query", "Target" ) )
71 Query[0] = Config.getString();
72
73 if ( !Config.checkValue ( "Query", "Target2" ) )
74 Query[1] = Config.getString();
75 }
76
77 static ReasoningKernel::TConceptExpr*
78 getNextName ( TsScanner& sc, TExpressionManager* pEM )
79 {
80 for (;;)
81 {
82 if ( sc.GetLex() == LEXEOF )
83 return nullptr;
84 LispToken t = sc.getNameKeyword();
85
86 if ( t != ID )
87 {
88 if ( t == L_TOP )
89 return pEM->Top();
90 else
91 return pEM->Bottom();
92 }
93 try
94 {
95 return pEM->Concept(sc.GetName());
96 }
97 catch ( const EFPPCantRegName& )
98 {
99 try
100 {
101 return pEM->OneOf(pEM->Individual(sc.GetName()));
102 }
103 catch ( const EFPPCantRegName& )
104 {
105 std::cout << "Query name " << sc.GetName() << " is undefined in TBox\n";
106 }
107 }
108 }
109 }
110
111 static const char*
112 getConceptName ( ReasoningKernel::TConceptExpr* C )
113 {
114 if ( const TDLConceptName* name = dynamic_cast<const TDLConceptName*>(C) )
115 return name->getName();
116 if ( dynamic_cast<const TDLConceptTop*>(C) )
117 return "*TOP*";
118 if ( dynamic_cast<const TDLConceptBottom*>(C) )
119 return "*BOTTOM*";
120 return "concept expression";
121 }
122
123 /// try to do a reasoning; react if exception was thrown
124 #define TryReasoning(action) \
125 do { \
126 try { action; } \
127 catch ( const EFPPInconsistentKB& ) {} \
128 catch ( const EFPPCantRegName& crn ) \
129 { std::cout << "Query name " << crn.getName() \
130 << " is undefined in TBox\n"; } \
131 catch ( const EFPPNonSimpleRole& nsr ) \
132 { std::cerr << "WARNING: KB is incorrect: " \
133 << nsr.what() << ". Query is NOT processed\n"; \
134 exit(0); } \
135 catch ( const EFPPCycleInRIA& cir ) \
136 { std::cerr << "WARNING: KB is incorrect: " \
137 << cir.what() << ". Query is NOT processed\n"; \
138 exit(0); } \
139 } while (false)
140
141 static void
142 testSat ( const std::string& names, ReasoningKernel& Kernel )
143 {
144 std::stringstream s(names);
145 TsScanner sc(&s);
146 ReasoningKernel::TConceptExpr* sat;
147
148 while ( (sat = getNextName(sc,Kernel.getExpressionManager())) != nullptr )
149 {
150 bool result = false;
151 if ( dynamic_cast<const TDLConceptTop*>(sat) != nullptr )
152 result = Kernel.isKBConsistent();
153 else
154 TryReasoning ( result = Kernel.isSatisfiable(sat) );
155
156 std::cout << "The '" << getConceptName(sat) << "' concept is ";
157 if ( !result )
158 std::cout << "un";
159 std::cout << "satisfiable w.r.t. TBox\n";
160 }
161 }
162
163 static void
164 testSub ( const std::string& names1, const std::string& names2, ReasoningKernel& Kernel )
165 {
166 std::stringstream s1(names1), s2(names2);
167 TsScanner sc1(&s1), sc2(&s2);
168 ReasoningKernel::TConceptExpr *sub, *sup;
169 TExpressionManager* pEM = Kernel.getExpressionManager();
170
171 while ( (sub = getNextName(sc1,pEM)) != nullptr )
172 {
173 sc2.ReSet();
174 while ( (sup = getNextName(sc2,pEM)) != nullptr )
175 {
176 bool result = false;
177 TryReasoning ( result = Kernel.isSubsumedBy ( sub, sup ) );
178
179 std::cout << "The '" << getConceptName(sub) << " [= " << getConceptName(sup) << "' subsumption does";
180 if ( !result )
181 std::cout << " NOT";
182 std::cout << " holds w.r.t. TBox\n";
183 }
184 }
185 }
186
187 static void
188 doReasoningQuery ( void )
189 {
190 if ( Query[0].empty() )
191 {
192 if ( Query[1].empty() )
193 TryReasoning(Kernel.realiseKB());
194 else
195 error ( "Query: Incorrect options" );
196 }
197 else
198 {
199 if ( Query[1].empty() ) // sat
200 testSat ( Query[0], Kernel );
201 else
202 testSub ( Query[0], Query[1], Kernel );
203 }
204 }
205
206
207 //********************** Main function ************************************
208 int main ( int argc, char *argv[] )
209 {
210 try{
211
212 totalTimer. Start ();
213
214 Kernel.setTopBottomRoleNames ( "*UROLE*", "*EROLE*", "*UDROLE*", "*EDROLE*" );
215
216 // parse options
217 if ( argc > 3 || argc < 2 )
218 Usage ();
219
220 // test if we asked for default options
221 if ( !strcmp ( argv[1], "-get-default-options" ) )
222 { // print Kernel's option set
223 Kernel.getOptions()->printConfig ( std::cout );
224 exit (0);
225 }
226
227 // loading config file...
228 if ( Config. Load ( argv [1] ) )
229 error ( "Cannot load Config file" );
230 else
231 {
232 argv++;
233 argc--;
234 }
235
236 // fills option values by Config
237 if ( Kernel.getOptions()->initByConfigure ( Config, "Tuning" ) )
238 error ( "Cannot fill options value by config file" );
239
240 // getting TBox file name
241 const char* tBoxName = nullptr;
242 if ( Config. checkValue ( "Query", "TBox" ) )
243 error ( "Config: no TBox file defined" );
244 else
245 tBoxName = Config. getString ();
246
247 // Open input file for TBox and so on...
248 std::ifstream iTBox ( tBoxName );
249
250 if ( iTBox.fail () )
251 error ( "Cannot open input TBox file" );
252
253 // output file...
254 Out.open ( argc == 3 ? argv [2] : "dl.res" );
255
256 if ( Out.fail () )
257 error ( "Cannot open output file" );
258
259 if ( USE_LOGGING )
260 {
261 // initialize LeveLogger only if not AD
262 if ( !Kernel.getOptions()->getBool("checkAD") )
263 if ( LLM.initLogger(Config) )
264 error ( "LeveLogger: couldn't open logging file" );
265 }
266
267 // init timeout option
268 unsigned long testTimeout = (unsigned long)Kernel.getOptions()->getInt("testTimeout");
269 Kernel.setOperationTimeout(testTimeout);
270 if ( LLM.isWritable(llAlways) )
271 LL << "Init testTimeout = " << testTimeout << "\n";
272
273 // init undefined names
274 Kernel.setUseUndefinedNames(false);
275
276 // dump ontology if requested
277 Kernel.setDumpOntology(Kernel.getOptions()->getBool("dumpOntology"));
278
279 // Load the ontology
280 DLLispParser TBoxParser ( &iTBox, &Kernel );
281 Kernel.setVerboseOutput(true);
282 TProgressMonitor* pMon = new ConsoleProgressMonitor;
283 Kernel.setProgressMonitor(pMon);
284
285 // parsing input TBox
286 std::cerr << "Loading KB...";
287 wTimer.Start ();
288 TBoxParser.Parse ();
289 wTimer.Stop ();
290 std::cerr << " done in " << wTimer << " seconds\n";
291
292 Out << "loading time " << wTimer << " seconds\n";
293
294 if ( Kernel.getOptions()->getBool("checkAD") ) // check atomic decomposition and exit
295 {
296 if ( argc < 2 )
297 {
298 std::cerr << "Atomic Decomposition option missed: use s,c,n for syntactic, counting or semantic locality checking\n";
299 exit(1);
300 }
301 char c = argv[1][0];
302 ModuleMethod moduleMethod;
303 switch ( c )
304 {
305 case 's' : moduleMethod = SYN_LOC_STD; break;
306 case 'c' : moduleMethod = SYN_LOC_COUNT; break;
307 case 'n' : moduleMethod = SEM_LOC; break;
308 default:
309 std::cerr << "Wrong AD option: " << c << "\n";
310 exit(1);
311 }
312
313 CreateAD(&Kernel.getOntology(), moduleMethod);
314 return 0;
315 }
316
317 TsProcTimer pt;
318 pt.Start();
319
320 // parsing query targets
321 fillSatSubQuery();
322
323 TryReasoning(Kernel.preprocessKB());
324
325 // do preprocessing
326 if ( !Kernel.isKBConsistent() )
327 std::cerr << "WARNING: KB is inconsistent. Query is NOT processed\n";
328 else // perform reasoning
329 doReasoningQuery();
330
331 pt.Stop();
332
333 // save final TBox
334 Kernel.writeReasoningResult ( Out, pt );
335
336 // finish
337 totalTimer.Stop ();
338 OutTime (std::cout);
339 OutTime (Out);
340 delete pMon;
341
342 }
343 catch ( const EFaCTPlusPlus& e )
344 {
345 std::cerr << "\n" << e.what() << "\n";
346 exit(1);
347 }
348 return 0;
349 }