Debian Codegraph @

FaCT++.JNI/tJNICache.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 TJNICACHE_H
21 #define TJNICACHE_H
22
23 #include "JNISupport.h"
24
25 //------------------------------------------------------
26 // Keeps class names and field IDs for different Java classes in FaCT++ interface
27 //------------------------------------------------------
28
29 /// keep class, Node field and c'tor of an interface class
30 class TClassFieldMethodIDs
31 {
32 protected: // members
33 /// full qualifier for the array
34 const char* ArrayClassName;
35
36 public: // members
37 /// class name
38 jclass ClassID = nullptr;
39 /// array class type
40 jclass ArrayClassID = nullptr;
41 /// c'tor type
42 jmethodID CtorID = nullptr;
43 /// 'node' field
44 jfieldID NodeFID = nullptr;
45
46 public: // interface
47 /// c'tor: init class name
48 explicit TClassFieldMethodIDs ( const char* arrayClassName )
49 : ArrayClassName(arrayClassName)
50 {}
51 /// init values by class name
52 void init ( JNIEnv* env )
53 {
54 jclass id = env->FindClass(ArrayClassName+1);
55 if ( id == nullptr )
56 {
57 Throw ( env, "Can't get class for Pointer" );
58 return;
59 }
60 ClassID = reinterpret_cast<jclass>(env->NewGlobalRef(id));
61
62 id = env->FindClass(ArrayClassName);
63 if ( id == nullptr )
64 {
65 Throw ( env, "Can't get class for [Pointer" );
66 return;
67 }
68 ArrayClassID = reinterpret_cast<jclass>(env->NewGlobalRef(id));
69
70 CtorID = env->GetMethodID ( ClassID, "<init>", "()V" );
71 if ( CtorID == nullptr )
72 {
73 Throw ( env, "Can't get c'tor for Pointer" );
74 return;
75 }
76
77 NodeFID = env->GetFieldID ( ClassID, "node", "J" );
78 if ( NodeFID == nullptr )
79 {
80 Throw ( env, "Can't get 'node' field" );
81 return;
82 }
83 }
84 void fini ( JNIEnv* env )
85 {
86 env->DeleteGlobalRef(ClassID);
87 env->DeleteGlobalRef(ArrayClassID);
88 }
89 }; // TClassFieldMethodIDs
90
91 /// Keep all the classes cache together with ENV
92 class TJNICache
93 {
94 public: // members
95 /// copy of an ENV to work with a given kernel
96 JNIEnv* env;
97 /// link to the owned kernel
98 ReasoningKernel* K = nullptr;
99 /// attached Expression Manager
100 TExpressionManager* EM = nullptr;
101 /// cached IDs for known classes
102 TClassFieldMethodIDs
103 ClassPointer,
104 IndividualPointer,
105 ObjectPropertyPointer,
106 DataPropertyPointer,
107 DataTypePointer,
108 DataTypeExpressionPointer,
109 DataValuePointer,
110 DataTypeFacet,
111 NodePointer,
112 AxiomPointer;
113
114 protected: // methods
115 /// init all the IDs
116 void init ( void )
117 {
118 ClassPointer.init(env);
119 IndividualPointer.init(env);
120 ObjectPropertyPointer.init(env);
121 DataPropertyPointer.init(env);
122 DataTypePointer.init(env);
123 DataTypeExpressionPointer.init(env);
124 DataValuePointer.init(env);
125 DataTypeFacet.init(env);
126 NodePointer.init(env);
127 AxiomPointer.init(env);
128 }
129 /// finalise all the IDs
130 void fini ( void )
131 {
132 ClassPointer.fini(env);
133 IndividualPointer.fini(env);
134 ObjectPropertyPointer.fini(env);
135 DataPropertyPointer.fini(env);
136 DataTypePointer.fini(env);
137 DataTypeExpressionPointer.fini(env);
138 DataValuePointer.fini(env);
139 DataTypeFacet.fini(env);
140 NodePointer.fini(env);
141 AxiomPointer.fini(env);
142 }
143
144 /// get an object out of an arbitrary pointer
145 jobject retObject ( const void* pointer, const TClassFieldMethodIDs& ID )
146 {
147 if ( unlikely(pointer == nullptr) )
148 {
149 Throw ( env, "Incorrect operand by FaCT++ Kernel" );
150 return nullptr;
151 }
152
153 // create an object to return
154 jobject obj = env->NewObject ( ID.ClassID, ID.CtorID );
155
156 if ( unlikely(obj == nullptr) )
157 Throw ( env, "Can't create Pointer object" );
158 else // set the return value
159 env->SetLongField ( obj, ID.NodeFID, (jlong)pointer );
160
161 return obj;
162 }
163
164 public: // members
165 /// ctor: init all the IDs
166 explicit TJNICache ( JNIEnv* e )
167 : env(e)
168 , ClassPointer("[Luk/ac/manchester/cs/factplusplus/ClassPointer;")
169 , IndividualPointer("[Luk/ac/manchester/cs/factplusplus/IndividualPointer;")
170 , ObjectPropertyPointer("[Luk/ac/manchester/cs/factplusplus/ObjectPropertyPointer;")
171 , DataPropertyPointer("[Luk/ac/manchester/cs/factplusplus/DataPropertyPointer;")
172 , DataTypePointer("[Luk/ac/manchester/cs/factplusplus/DataTypePointer;")
173 , DataTypeExpressionPointer("[Luk/ac/manchester/cs/factplusplus/DataTypeExpressionPointer;")
174 , DataValuePointer("[Luk/ac/manchester/cs/factplusplus/DataValuePointer;")
175 , DataTypeFacet("[Luk/ac/manchester/cs/factplusplus/DataTypeFacet;")
176 , NodePointer("[Luk/ac/manchester/cs/factplusplus/NodePointer;")
177 , AxiomPointer("[Luk/ac/manchester/cs/factplusplus/AxiomPointer;")
178 { init(); }
179 /// d'tor: release all names
180 ~TJNICache() { /*fini();*/ }
181
182 /// switch the env to a new one E
183 void reset ( JNIEnv* e )
184 {
185 // fini();
186 env = e;
187 init();
188 }
189
190 /// object for class expression
191 jobject Class ( TConceptExpr* expr ) { return retObject ( expr, ClassPointer ); }
192 /// object for individual expression
193 jobject Individual ( TIndividualExpr* expr ) { return retObject ( expr, IndividualPointer ); }
194 jobject ObjectProperty ( TORoleExpr* expr ) { return retObject ( expr, ObjectPropertyPointer ); }
195 jobject ObjectComplex ( TORoleComplexExpr* expr ) { return retObject ( expr, ObjectPropertyPointer ); }
196 jobject DataProperty ( TDRoleExpr* expr ) { return retObject ( expr, DataPropertyPointer ); }
197 jobject DataType ( TDataExpr* expr ) { return retObject ( expr, DataTypePointer ); }
198 jobject DataTypeExpression ( TDataExpr* expr ) { return retObject ( expr, DataTypeExpressionPointer ); }
199 jobject DataValue ( TDataValueExpr* expr ) { return retObject ( expr, DataValuePointer ); }
200 jobject Facet ( TFacetExpr* expr ) { return retObject ( expr, DataTypeFacet ); }
201 jobject Node ( TCGNode* node ) { return retObject ( node, NodePointer ); }
202 jobject Axiom ( TDLAxiom* axiom ) { return retObject ( axiom, AxiomPointer ); }
203
204 /// create vector of Java objects defined by ID from given VEC
205 template <typename T>
206 jobjectArray buildArray ( const std::vector<T*>& vec, const TClassFieldMethodIDs& ID )
207 {
208 jobjectArray ret = env->NewObjectArray ( (jsize)vec.size(), ID.ClassID, nullptr );
209 for ( unsigned int i = 0; i < vec.size(); ++i )
210 env->SetObjectArrayElement ( ret, (jsize)i, retObject ( vec[i], ID ) );
211 return ret;
212 }
213
214 // get expressions for the names in the unified way
215
216 /// get expression for the class name
217 TConceptExpr* getCName ( const std::string& name ) { return EM->Concept(name); }
218 /// get expression for the individual name
219 TIndividualExpr* getIName ( const std::string& name ) { return EM->Individual(name); }
220 /// get expression for the object property name
221 TORoleExpr* getOName ( const std::string& name ) { return EM->ObjectRole(name); }
222 /// get expression for the data property name
223 TDRoleExpr* getDName ( const std::string& name ) { return EM->DataRole(name); }
224 }; // TJNICache
225
226 /// get JNI cache by the env and obj
227 inline
228 TJNICache* getJ ( JNIEnv* env, jobject obj )
229 {
230 TJNICache* J = getK(env,obj)->getJNICache();
231 if ( unlikely(J->env != env) )
232 J->reset(env);
233 return J;
234 }
235
236 #endif