Debian Codegraph @

FaCT++.JNI/JNIActor.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 JNIACTOR_H
21 #define JNIACTOR_H
22
23 #include "tJNICache.h"
24
25 /// class for acting with concept taxonomy
26 template <typename AccessPolicy>
27 class JTaxonomyActor: public WalkerInterface
28 {
29 protected: // types
30 /// array of TNEs
31 typedef std::vector<TExpr*> SynVector;
32 /// array for a set of taxonomy vertices
33 typedef std::vector<SynVector> SetOfNodes;
34
35 protected: // members
36 /// JNI environment
37 TJNICache* J;
38 /// 2D array to return
39 SetOfNodes acc;
40 /// 1D array to return
41 SynVector plain;
42 /// temporary vector to keep synonyms
43 SynVector syn;
44
45 protected: // methods
46 /// create vector of Java objects by given SynVector
47 jobjectArray getArray ( const SynVector& vec ) const
48 { return J->buildArray ( vec, AccessPolicy::getIDs(J) ); }
49 /// try current entry
50 void tryEntry ( const ClassifiableEntry* p )
51 {
52 if ( p->isSystem() )
53 return;
54 if ( AccessPolicy::applicable(p) )
55 syn.push_back(AccessPolicy::buildTree(J,p));
56 }
57
58 public: // interface
59 /// c'tor
60 explicit JTaxonomyActor ( TJNICache* cache ) : J(cache) {}
61
62 void clear ( void ) { acc.clear(); plain.clear(); }
63 // return values
64
65 /// get single vector of synonyms (necessary for Equivalents, for example)
66 jobjectArray getSynonyms ( void ) const { return getArray ( acc.empty() ? SynVector() : acc[0] ); }
67 /// get 2D array of all required elements of the taxonomy
68 jobjectArray getElements ( void ) const
69 {
70 if ( AccessPolicy::needPlain() )
71 return getArray(plain);
72 jobjectArray ret = J->env->NewObjectArray ( (jsize)acc.size(), AccessPolicy::getIDs(J).ArrayClassID, nullptr );
73 for ( unsigned int i = 0; i < acc.size(); ++i )
74 J->env->SetObjectArrayElement ( ret, (jsize)i, getArray(acc[i]) );
75 return ret;
76 }
77
78 /// taxonomy walking method.
79 /// @return true if node was processed, and there is no need to go further
80 /// @return false if node can not be processed in current settings
81 bool apply ( const TaxonomyVertex& v ) override
82 {
83 syn.clear();
84 tryEntry(v.getPrimer());
85
86 for ( auto synonym: v.synonyms() )
87 tryEntry(synonym);
88
89 /// no applicable elements were found
90 if ( syn.empty() )
91 return false;
92
93 if ( AccessPolicy::needPlain() )
94 plain.insert ( plain.end(), syn.begin(), syn.end() );
95 else
96 acc.push_back(syn);
97 return true;
98 }
99 }; // JTaxonomyActor
100
101 // policy elements
102
103 /// policy for concepts
104 class ClassPolicy
105 {
106 public:
107 static const TClassFieldMethodIDs& getIDs ( const TJNICache* J ) { return J->ClassPointer; }
108 static bool applicable ( const ClassifiableEntry* p )
109 { return !static_cast<const TConcept*>(p)->isSingleton(); }
110 static bool needPlain ( void ) { return false; }
111 static TExpr* buildTree ( TJNICache* J, const ClassifiableEntry* p )
112 {
113 if ( p->getId() >= 0 )
114 return J->getCName(p->getName());
115
116 // top or bottom
117 std::string name(p->getName());
118
119 if ( name == std::string("TOP") )
120 return J->EM->Top();
121 else if ( name == std::string("BOTTOM") )
122 return J->EM->Bottom();
123 else // error
124 return nullptr;
125 }
126 }; // ClassPolicy
127
128 /// policy for individuals
129 template<bool plain>
130 class IndividualPolicy
131 {
132 public:
133 static const TClassFieldMethodIDs& getIDs ( const TJNICache* J ) { return J->IndividualPointer; }
134 static bool applicable ( const ClassifiableEntry* p )
135 { return static_cast<const TConcept*>(p)->isSingleton(); }
136 static bool needPlain ( void ) { return plain; }
137 static TExpr* buildTree ( TJNICache* J, const ClassifiableEntry* p )
138 { return J->getIName(p->getName()); }
139 }; // IndividualPolicy
140
141 /// policy for object properties
142 class ObjectPropertyPolicy
143 {
144 public:
145 static const TClassFieldMethodIDs& getIDs ( const TJNICache* J ) { return J->ObjectPropertyPointer; }
146 static bool applicable ( const ClassifiableEntry* ) { return true; }
147 static bool needPlain ( void ) { return false; }
148 static TExpr* buildTree ( TJNICache* J, const ClassifiableEntry* p )
149 {
150 return p->getId() >= 0 ?
151 J->getOName(p->getName()) :
152 J->EM->Inverse(J->getOName(static_cast<const TRole*>(p)->realInverse()->getName()));
153 }
154 }; // ObjectPropertyPolicy
155
156 /// policy for data properties
157 class DataPropertyPolicy
158 {
159 public:
160 static const TClassFieldMethodIDs& getIDs ( const TJNICache* J ) { return J->DataPropertyPointer; }
161 static bool applicable ( const ClassifiableEntry* p ) { return p->getId() >= 0; }
162 static bool needPlain ( void ) { return false; }
163 static TExpr* buildTree ( TJNICache* J, const ClassifiableEntry* p )
164 { return J->getDName(p->getName()); }
165 }; // DataPropertyPolicy
166
167 #endif