Debian Codegraph @

FaCT++.JNI/JNISupport.h

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2006-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 JNISUPPORT_H
21 #define JNISUPPORT_H
22
23 #include <jni.h>
24
25 // switch tracing on
26 //#define JNI_TRACING
27
28 #ifdef ENABLE_CHECKING
29 # define JNI_TRACING
30 #endif
31
32 #ifdef JNI_TRACING
33 # include <iostream>
34 # define TRACE_JNI(func) std::cerr << "JNI " << env << " Kernel " << getK(env,obj) << " Call " << func << "\n"
35 # define TRACE_ARG(env,obj,arg) do { \
36 std::cerr << " arg "; \
37 TExpr* expr=getROExpr(env,arg); \
38 const TNamedEntity* ne = dynamic_cast<const TNamedEntity*>(expr); \
39 if ( ne != nullptr ) std::cerr << ne->getName(); \
40 std::cerr << "\n"; } while (false)
41 # define TRACE_STR(env,str) std::cerr << " string arg " << JString(env,str)() << "\n"
42 #else
43 # define TRACE_JNI(func) (void)NULL
44 # define TRACE_ARG(env,obj,arg) (void)NULL
45 # define TRACE_STR(env,str) (void)NULL
46 #endif
47
48 //-------------------------------------------------------------
49 // Expression typedefs
50 //-------------------------------------------------------------
51
52 /// general expression
53 typedef ReasoningKernel::TExpr TExpr;
54 /// concept expression
55 typedef ReasoningKernel::TConceptExpr TConceptExpr;
56 /// individual expression
57 typedef ReasoningKernel::TIndividualExpr TIndividualExpr;
58 /// role expression
59 typedef ReasoningKernel::TRoleExpr TRoleExpr;
60 /// object role complex expression (including role chains and projections)
61 typedef ReasoningKernel::TORoleComplexExpr TORoleComplexExpr;
62 /// object role expression
63 typedef ReasoningKernel::TORoleExpr TORoleExpr;
64 /// data role expression
65 typedef ReasoningKernel::TDRoleExpr TDRoleExpr;
66 /// data expression
67 typedef ReasoningKernel::TDataExpr TDataExpr;
68 /// data type expression
69 typedef ReasoningKernel::TDataTypeExpr TDataTypeExpr;
70 /// data value expression
71 typedef ReasoningKernel::TDataValueExpr TDataValueExpr;
72 /// data facet expression
73 typedef const TDLFacetExpression TFacetExpr;
74 /// completion tree node
75 typedef const ReasoningKernel::TCGNode TCGNode;
76
77 //-------------------------------------------------------------
78 // Support functions
79 //-------------------------------------------------------------
80
81 // class for easy dealing with Java strings
82 class JString
83 {
84 private:
85 JNIEnv* env;
86 jstring str;
87 const char* buf;
88
89 public:
90 JString ( JNIEnv* e, jstring s ) : env(e), str(s) { buf = env->GetStringUTFChars(str, nullptr); }
91 // prevent copy
92 JString ( const JString& ) = delete;
93 JString& operator = ( const JString& ) = delete;
94 ~JString() { env->ReleaseStringUTFChars(str, buf); }
95 const char* operator() ( void ) const { return buf; }
96 }; // JString
97
98 /// throw exception with a given signature
99 inline
100 void ThrowExc ( JNIEnv * env, const char* reason, const char* className )
101 {
102 jclass cls = env->FindClass(className);
103 env->ThrowNew ( cls, reason );
104 }
105
106 /// throw exception with an empty c'tor and a given signature
107 inline
108 void ThrowExc ( JNIEnv * env, const char* className )
109 {
110 jclass cls = env->FindClass(className);
111 jmethodID CtorID = env->GetMethodID ( cls, "<init>", "()V" );
112 jobject obj = env->NewObject ( cls, CtorID );
113 env->Throw((jthrowable)obj);
114 }
115
116 /// throw general Java exception
117 inline
118 void ThrowGen ( JNIEnv* env, const char* reason )
119 {
120 ThrowExc ( env, reason, "Ljava/lang/Exception;" );
121 }
122
123 /// throw general FaCT++ exception
124 inline
125 void Throw ( JNIEnv* env, const char* reason )
126 {
127 ThrowExc ( env, reason, "Lorg/semanticweb/owlapi/reasoner/ReasonerInternalException;" );
128 }
129
130 /// throw Inconsistent Ontology exception
131 inline
132 void ThrowICO ( JNIEnv* env )
133 {
134 ThrowExc ( env, "Lorg/semanticweb/owlapi/reasoner/InconsistentOntologyException;" );
135 }
136
137 /// throw CR for non-simple role exception
138 inline
139 void ThrowNSR ( JNIEnv* env, const char* reason )
140 {
141 std::string msg ("Non-simple object property '");
142 msg += reason;
143 msg += "' is used as a simple one";
144 ThrowExc ( env, msg.c_str(), "Lorg/semanticweb/owlapi/reasoner/OWLReasonerRuntimeException;");
145
146 // not correct because does not have enough information
147 // jclass ceNotProfile = env->FindClass("Lorg/semanticweb/owlapi/reasoner/ClassExpressionNotInProfileException;");
148 // if ( ceNotProfile == 0 )
149 // {
150 // Throw ( env, "Can't get class for Pointer" );
151 // return ;
152 // }
153 //
154 // jmethodID CtorID = env->GetMethodID ( ceNotProfile, "<init>", "(Lorg/semanticweb/owlapi/model/OWLClassExpression;Lorg/semanticweb/owlapi/profiles/OWLProfile;)V" );
155 //
156 // // create an object to return
157 // jobject obj = env->NewObject ( ceNotProfile, CtorID, nullptr, nullptr );
158 // env->Throw((jthrowable)obj);
159 }
160
161 /// throw Role Inclusion Cycle exception
162 inline
163 void ThrowRIC ( JNIEnv* env, const char* reason )
164 {
165 ThrowExc ( env, reason, "Lorg/semanticweb/owlapi/reasoner/AxiomNotInProfileException;" );
166 }
167
168 /// throw Role Inclusion Cycle exception
169 inline
170 void ThrowTO ( JNIEnv* env )
171 {
172 ThrowExc ( env, "Lorg/semanticweb/owlapi/reasoner/TimeOutException;" );
173 }
174
175 /// field for Kernel's ID
176 extern "C" jfieldID KernelFID;
177
178 /// get Kernel local to given object
179 // as a side effect sets up curKernel
180 inline
181 ReasoningKernel* getK ( JNIEnv * env, jobject obj )
182 {
183 jlong id = env->GetLongField ( obj, KernelFID );
184
185 // ID corresponds to a pointer -- should not be NULL
186 if ( unlikely(id == 0) )
187 Throw ( env, "Uninitialized FaCT++ kernel found" );
188
189 return (ReasoningKernel*)id;
190 }
191
192 // helper for getTree which extracts a JLONG from a given object
193 inline
194 jlong getPointer ( JNIEnv * env, jobject obj )
195 {
196 jclass classThis = env->GetObjectClass(obj);
197
198 if ( unlikely(classThis == 0) )
199 {
200 Throw ( env, "Can't get class of 'this'" );
201 return 0;
202 }
203
204 jfieldID fid = env->GetFieldID ( classThis, "node", "J" );
205
206 if ( unlikely(fid == 0) )
207 {
208 Throw ( env, "Can't get 'node' field" );
209 return 0;
210 }
211
212 return env->GetLongField ( obj, fid );
213 }
214
215 // macro to expand into the accessor function that transforms pointer into appropriate type
216 #define ACCESSOR(Name) \
217 inline T ## Name* get ## Name ( JNIEnv * env, jobject obj ) { \
218 return dynamic_cast<T ## Name*>((TExpr*)getPointer(env,obj)); }
219
220 // accessors for different expression types
221 ACCESSOR(Expr)
222 ACCESSOR(ConceptExpr)
223 ACCESSOR(IndividualExpr)
224 ACCESSOR(RoleExpr)
225 ACCESSOR(ORoleComplexExpr)
226 ACCESSOR(ORoleExpr)
227 ACCESSOR(DRoleExpr)
228 ACCESSOR(DataExpr)
229 ACCESSOR(DataValueExpr)
230 ACCESSOR(FacetExpr)
231
232 // ACCESSOR(DataTypeExpr) -- doesn't work as DTE is not a const typedef
233 inline TDataTypeExpr* getDataTypeExpr ( JNIEnv * env, jobject obj )
234 { return const_cast<TDataTypeExpr*>(dynamic_cast<const TDataTypeExpr*>((TExpr*)getPointer(env,obj))); }
235
236 #undef ACCESSOR
237
238 // macro to expand into the RO accessor function that transforms pointer into appropriate type
239 #define ACCESSOR(Name) \
240 inline const T ## Name* getRO ## Name ( JNIEnv * env, jobject obj ) { \
241 return dynamic_cast<const T ## Name*>((const TExpr*)getPointer(env,obj)); }
242
243 // accessors for different expression types
244 ACCESSOR(Expr)
245 ACCESSOR(ConceptExpr)
246 ACCESSOR(IndividualExpr)
247 ACCESSOR(RoleExpr)
248 ACCESSOR(ORoleComplexExpr)
249 ACCESSOR(ORoleExpr)
250 ACCESSOR(DRoleExpr)
251 ACCESSOR(DataExpr)
252 ACCESSOR(DataTypeExpr)
253 ACCESSOR(DataValueExpr)
254 ACCESSOR(FacetExpr)
255
256 // ACCESSOR(NodeExpr) -- doesn't work as the type is not a TExpr's descendant
257 inline TCGNode* getRONode ( JNIEnv * env, jobject obj )
258 { return (TCGNode*)getPointer(env,obj); }
259
260 #undef ACCESSOR
261
262 inline
263 TDLAxiom* getAxiom ( JNIEnv * env, jobject obj )
264 {
265 return (TDLAxiom*)getPointer(env,obj);
266 }
267
268 inline
269 TCGNode* getNode ( JNIEnv * env, jobject obj )
270 {
271 return (TCGNode*)getPointer(env,obj);
272 }
273
274 #endif