| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef KONCLUDE_COMPILE_JNI_INTERFACE |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <QCoreApplication> |
| 26 |
#include <jni.h> |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "com_konclude_jnibridge_KoncludeReasonerBridge.h" |
| 31 |
#include "CJNIHandler.h" |
| 32 |
#include "CJNIInstanceManager.h" |
| 33 |
#include "CJNICommandProcessor.h" |
| 34 |
#include "CJNIOntologyRevisionData.h" |
| 35 |
#include "CJNIAxiomExpressionVisitingLoader.h" |
| 36 |
#include "CJNIQueryProcessor.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "CKoncludeInfo.h" |
| 43 |
|
| 44 |
#include "Utilities/UtilitiesSettings.h" |
| 45 |
|
| 46 |
#include "Logger/CLogger.h" |
| 47 |
#include "Logger/CConsolePrintLogObserver.h" |
| 48 |
#include "Logger/CQtDebugPrintLogObserver.h" |
| 49 |
|
| 50 |
#include "Control/Loader/CDefaultLoaderFactory.h" |
| 51 |
#include "Control/Loader/CJNICommandLoader.h" |
| 52 |
|
| 53 |
//#include "Control/Interface/CommandLine/CCommandLinePreparationTranslatorSelector.h" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
using namespace Konclude; |
| 59 |
using namespace Konclude::Logger; |
| 60 |
using namespace Konclude::Utilities; |
| 61 |
using namespace Konclude::Control::Loader; |
| 62 |
//using namespace Konclude::Interface::CommandLine; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static int qtAppargc = 1; |
| 68 |
static char* qtAppargv[] = {"Konclude", NULL}; |
| 69 |
static QCoreApplication* qtApp = NULL; |
| 70 |
static bool qtAppCreated = false; |
| 71 |
//static QThread* thread = NULL; |
| 72 |
QString qJNIConfigString; |
| 73 |
QByteArray qJNIConfigByteArray; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_initKoncludeLibraryInstance(JNIEnv * jenv, jobject bridgeObj, jstring jniConfigString) { |
| 78 |
|
| 79 |
char* defaultJNIArgumentStrings[] = { |
| 80 |
//"-CoutLogObserverLoader ", |
| 81 |
"-DefaultReasonerLoader +=Konclude.Execution.CalculationManager=Konclude.Calculation.Calculator.ConcurrentTaskCalculationManager +=Konclude.Debugging.WriteDebuggingData=TRUE ", |
| 82 |
//"-DefaultReasonerLoader +=Konclude.Execution.CalculationManager=Konclude.Calculation.Calculator.ConcurrentTaskCalculationManager ", |
| 83 |
"-JNICommandProcessorLoader ", |
| 84 |
" " |
| 85 |
}; |
| 86 |
|
| 87 |
int defaultJNIArgumentCount = 2; |
| 88 |
|
| 89 |
if (CJNIHandler::getJNIInstanceManagerBaseFieldID(jenv)) { |
| 90 |
if (CJNIHandler::setJNIInstanceManager(jenv,bridgeObj,nullptr)) { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (QCoreApplication::instance() == nullptr) { |
| 95 |
qtApp = new QCoreApplication(qtAppargc, qtAppargv); |
| 96 |
qtAppCreated = true; |
| 97 |
//qtApp->exec(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
CLogger *logger = CLogger::getInstance(); |
| 102 |
|
| 103 |
try { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
QString koncludeString(QString("%1 - %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeNameExtension())); |
| 108 |
LOG(INFO,"::Konclude::Main","Starting Konclude via JNI ...",0); |
| 109 |
LOG(INFO,"::Konclude::Main",koncludeString,0); |
| 110 |
LOG(INFO,"::Konclude::Main",QString("%1, %2, Version %3 (%4)\r\n").arg(CKoncludeInfo::getKoncludeDescription()).arg(CKoncludeInfo::getKoncludeBitPlatformString()).arg(CKoncludeInfo::getKoncludeVersionString()).arg(CKoncludeInfo::getKoncludeCompilationDateString()),0); |
| 111 |
|
| 112 |
|
| 113 |
CJNIInstanceManager* jniInstanceManager = new CJNIInstanceManager(); |
| 114 |
CJNIHandler* jniHandler = new CJNIHandler(jenv); |
| 115 |
jniInstanceManager->setJNIHandler(jniHandler); |
| 116 |
jniHandler->setJNIInstanceManager(jenv,bridgeObj,jniInstanceManager); |
| 117 |
|
| 118 |
char** jniArgumentStrings = defaultJNIArgumentStrings; |
| 119 |
int jniArgumentCount = defaultJNIArgumentCount; |
| 120 |
if (jniConfigString) { |
| 121 |
qJNIConfigString = jniHandler->getString(jenv,jniConfigString); |
| 122 |
if (!qJNIConfigString.isEmpty()) { |
| 123 |
//LOG(INFO,"::Konclude::Main",QString("Received '%1' through JNI.").arg(qJNIConfigString),0); |
| 124 |
qJNIConfigByteArray = qJNIConfigString.toLocal8Bit(); |
| 125 |
jniArgumentStrings[0] = qJNIConfigByteArray.data(); |
| 126 |
jniArgumentCount = 1; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
QStringList arguments = CJNICommandLoader::getEncodedArguments(jniArgumentCount,jniArgumentStrings,false,false); |
| 133 |
|
| 134 |
//CCommandLinePreparationTranslatorSelector transSelector(loaderFactory); |
| 135 |
//arguments = transSelector.translateArguments(arguments); |
| 136 |
// |
| 137 |
LOG(INFO,"::Konclude::Main",QString("JNI loading arguments: %1.").arg(arguments.join(", ")),0); |
| 138 |
|
| 139 |
CJNICommandLoader* jniCommandLoader = new CJNICommandLoader(jniInstanceManager,arguments); |
| 140 |
jniInstanceManager->setJNICommandLoader(jniCommandLoader); |
| 141 |
CLoaderFactory* loaderFactory = new CDefaultLoaderFactory(); |
| 142 |
jniInstanceManager->setJNILoaderFactory(loaderFactory); |
| 143 |
jniCommandLoader->init(loaderFactory); |
| 144 |
jniCommandLoader->load(); |
| 145 |
jniCommandLoader->waitSynchronization(); |
| 146 |
|
| 147 |
|
| 148 |
if (!jniInstanceManager->getJNICommandProcessor()) { |
| 149 |
LOG(WARN,"::Konclude::Main","No JNI command processor loaded!",0); |
| 150 |
} else { |
| 151 |
LOG(INFO,"::Konclude::Main","JNI command processer successfully initialised.",0); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
} catch (...) { |
| 157 |
LOG(CATASTROPHIC,"::Konclude::Main",logTr("Stop because of an unknown catastrophic error."),0); |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_closeKoncludeLibraryInstance(JNIEnv * jenv, jobject bridgeObj) { |
| 169 |
CJNIInstanceManager* jniInstanceManager = CJNIHandler::getJNIInstanceManager(jenv,bridgeObj); |
| 170 |
if (jniInstanceManager) { |
| 171 |
|
| 172 |
CJNICommandLoader* jniCommandLoader = jniInstanceManager->getJNICommandLoader(); |
| 173 |
jniCommandLoader->exit(); |
| 174 |
|
| 175 |
LOG(INFO,"::Konclude::Main",logTr("Stopping Konclude ..."),0); |
| 176 |
|
| 177 |
delete jniInstanceManager; |
| 178 |
CJNIHandler::setJNIInstanceManager(jenv,bridgeObj,nullptr); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_initAxiomExpressionVisitingBridge(JNIEnv * jenv, jobject bridgeObj, jobject builderObj) { |
| 186 |
LOG(INFO,"::Konclude::Main",logTr("Creating axiom/expression building bridge."),0); |
| 187 |
CJNIInstanceManager* jniInstanceManager = CJNIHandler::getJNIInstanceManager(jenv,bridgeObj); |
| 188 |
if (jniInstanceManager) { |
| 189 |
CJNIHandler* jniHandler = jniInstanceManager->getJNIHandler(); |
| 190 |
CJNICommandProcessor* jniProcessor = jniInstanceManager->getJNICommandProcessor(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
COntologyRevision* ontRev = nullptr; |
| 195 |
CJNIOntologyRevisionData* ontRevData = jniInstanceManager->getOntologyRevisionData(); |
| 196 |
if (!ontRevData) { |
| 197 |
LOG(INFO,"::Konclude::Main",logTr("Creating new anonomous ontology"),0); |
| 198 |
ontRev = jniProcessor->getNewlyCreatedAnonymousOntologyRevision(); |
| 199 |
} else { |
| 200 |
LOG(INFO,"::Konclude::Main",logTr("Creating new ontology revision."),0); |
| 201 |
ontRev = jniProcessor->getNewOntologyRevision(ontRevData->getOntologyRevision()); |
| 202 |
} |
| 203 |
ontRevData = new CJNIOntologyRevisionData(ontRevData); |
| 204 |
ontRevData->setOntologyRevision(ontRev); |
| 205 |
|
| 206 |
CJNIAxiomExpressionVisitingLoader* axiomExpVisitLoader = new CJNIAxiomExpressionVisitingLoader(jniInstanceManager,ontRevData); |
| 207 |
axiomExpVisitLoader->initializeBuilding(); |
| 208 |
jniHandler->setAxiomExpressionBuildingBridgeNativeData(jenv,builderObj,axiomExpVisitLoader); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_finalizeAxiomExpressionVisitingBridge(JNIEnv * jenv, jobject bridgeObj, jobject builderObj) { |
| 219 |
CJNIInstanceManager* jniInstanceManager = CJNIHandler::getJNIInstanceManager(jenv,bridgeObj); |
| 220 |
if (jniInstanceManager) { |
| 221 |
LOG(INFO,"::Konclude::Main",logTr("Finalizing axiom/expression building bridge."),0); |
| 222 |
CJNIHandler* jniHandler = jniInstanceManager->getJNIHandler(); |
| 223 |
CJNICommandProcessor* jniProcessor = jniInstanceManager->getJNICommandProcessor(); |
| 224 |
CJNIAxiomExpressionVisitingLoader* axiomExpVisitLoader = jniHandler->getAxiomExpressionBuildingBridgeNativeData(jenv,builderObj); |
| 225 |
if (axiomExpVisitLoader) { |
| 226 |
axiomExpVisitLoader->completeBuilding(); |
| 227 |
CJNIOntologyRevisionData* ontRevData = axiomExpVisitLoader->getOntologyRevisionData(); |
| 228 |
COntologyRevision* ontRev = jniProcessor->installNewOntologyRevision(ontRevData->getOntologyRevision()); |
| 229 |
// mapping hash merging? (in case several axioms/expressions are installed by different builders) |
| 230 |
ontRevData->setOntologyRevision(ontRev); |
| 231 |
jniInstanceManager->setOntologyRevisionData(ontRevData); |
| 232 |
delete axiomExpVisitLoader; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_initQueryingBridge(JNIEnv * jenv, jobject bridgeObj, jobject processorObj) { |
| 245 |
CJNIInstanceManager* jniInstanceManager = CJNIHandler::getJNIInstanceManager(jenv,bridgeObj); |
| 246 |
if (jniInstanceManager) { |
| 247 |
LOG(INFO,"::Konclude::Main",logTr("Creating querying bridge."),0); |
| 248 |
CJNIHandler* jniHandler = jniInstanceManager->getJNIHandler(); |
| 249 |
CJNICommandProcessor* jniProcessor = jniInstanceManager->getJNICommandProcessor(); |
| 250 |
|
| 251 |
|
| 252 |
CJNIOntologyRevisionData* ontRevData = jniInstanceManager->getOntologyRevisionData(); |
| 253 |
if (ontRevData) { |
| 254 |
if (!ontRevData->isQueryingPrepared()) { |
| 255 |
jniProcessor->prepareOntologyQuerying(ontRevData->getOntologyRevision()); |
| 256 |
ontRevData->setQueryingPrepared(true); |
| 257 |
} |
| 258 |
|
| 259 |
CJNIQueryProcessor* queryProcessor = new CJNIQueryProcessor(jniInstanceManager,ontRevData); |
| 260 |
jniHandler->setQueryingBridgeNativeData(jenv,processorObj,queryProcessor); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
JNIEXPORT void JNICALL Java_com_konclude_jnibridge_KoncludeReasonerBridge_finalizeQueryingBridge(JNIEnv * jenv, jobject bridgeObj, jobject processorObj) { |
| 272 |
CJNIInstanceManager* jniInstanceManager = CJNIHandler::getJNIInstanceManager(jenv,bridgeObj); |
| 273 |
if (jniInstanceManager) { |
| 274 |
LOG(INFO,"::Konclude::Main",logTr("Closing querying bridge"),0); |
| 275 |
CJNIHandler* jniHandler = jniInstanceManager->getJNIHandler(); |
| 276 |
CJNICommandProcessor* jniProcessor = jniInstanceManager->getJNICommandProcessor(); |
| 277 |
CJNIQueryProcessor* queryProcessor = jniHandler->getQueryingBridgeNativeData(jenv,processorObj); |
| 278 |
if (queryProcessor) { |
| 279 |
delete queryProcessor; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#endif |