-
Notifications
You must be signed in to change notification settings - Fork 0
/
VLog-API.cpp
796 lines (710 loc) · 22.2 KB
/
VLog-API.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include <jni.h>
#include <vlog/concepts.h>
#include <vlog/edb.h>
#include <vlog/seminaiver.h>
#include <vlog/cycles/checker.h>
#include <vlog/reasoner.h>
#include <vlog/utils.h>
#include <kognac/utils.h>
#include <kognac/logs.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdint>
#define IS_BLANK(c) (c >= (INT64_C(1) << 40))
class VLogInfo {
public:
SemiNaiver *sn;
Program *program;
EDBLayer *layer;
VLogInfo() {
sn = NULL;
program = NULL;
layer = NULL;
}
~VLogInfo() {
if (layer != NULL) {
delete layer;
layer = NULL;
}
if (program != NULL) {
delete program;
program = NULL;
}
if (sn != NULL) {
delete sn;
sn = NULL;
}
}
};
static std::map<int, VLogInfo *> vlogMap;
static bool logLevelSet = false;
// Utility method to convert a literal id to a string.
std::string literalToString(VLogInfo *f, uint64_t literalid) {
std::string s = f->layer->getDictText(literalid);
if (s == std::string("")) {
s = "" + std::to_string(literalid >> 40) + "_"
+ std::to_string((literalid >> 32) & 0377) + "_"
+ std::to_string(literalid & 0xffffffff);
}
return s;
}
VLogInfo *getVLogInfo(int id) {
auto inf = vlogMap.find(id);
if (inf == vlogMap.end()) {
return NULL;
}
return inf->second;
}
// Utility method to throw an exception
void throwException( const char *message) {
//TODO Irina: throw std::exception/ std::runtime_error ?
throw std::exception(message);
}
// Converts a vector of Atoms into VLog representation.
std::vector<Literal> getVectorLiteral(JNIEnv *env, VLogInfo *f, jobjectArray h, Dictionary &dict) {
std::vector<Literal> result;
jsize sz = env->GetArrayLength(h);
// For all atoms:
for (int i = 0; i < sz; i++) {
// First, get the atom.
jobject atom = env->GetObjectArrayElement(h, (jsize) i);
jclass cls = env->GetObjectClass(atom);
// Get the predicate
jmethodID getPredicateMethod = env->GetMethodID(cls, "getPredicate", "()Ljava/lang/String;");
jstring jpred = (jstring) env->CallObjectMethod(atom, getPredicateMethod);
std::string predicate = jstring2string(env, jpred);
// Get the terms
jmethodID getTermsMethod = env->GetMethodID(cls, "getTerms", "()[Lkarmaresearch/vlog/Term;");
jobjectArray jterms = (jobjectArray) env->CallObjectMethod(atom, getTermsMethod);
// // Get negated
// jmethodID isNegated = env->GetMethodID(cls, "isNegated", "()Z");
// jboolean jnegated = (jboolean) env->CallBooleanMethod(atom, isNegated);
// bool negated = jnegated == JNI_TRUE;
// bool negated = false;
jsize vtuplesz = env->GetArrayLength(jterms);
// Collect conversions from terms
if (vtuplesz != (uint8_t) vtuplesz) {
throwException(("Arity of predicate " + predicate + " too large (" + std::to_string(vtuplesz) + " > 255)").c_str());
return result;
}
VTuple tuple((uint8_t) vtuplesz);
std::vector<VTerm> t;
// For each term:
for (int j = 0; j < vtuplesz; j++) {
// First, get the term
jobject jterm = env->GetObjectArrayElement(jterms, (jsize) j);
jclass termcls = env->GetObjectClass(jterm);
//
// TODO:
// // Get the name
jmethodID getNameMethod = env->GetMethodID(termcls, "getName", "()Ljava/lang/String;");
jstring jname = (jstring) env->CallObjectMethod(jterm, getNameMethod);
std::string name = jstring2string(env, jname);
// Get the type: constant or variable
jmethodID getTypeMethod = env->GetMethodID(termcls, "getTermType", "()Lkarmaresearch/vlog/Term$TermType;");
jobject jtype = env->CallObjectMethod(jterm, getTypeMethod);
jclass enumClass = env->GetObjectClass(jtype);
jmethodID getOrdinalMethod = env->GetMethodID(enumClass, "ordinal", "()I");
jint type = (jint) env->CallIntMethod(jtype, getOrdinalMethod);
// For now, we only have two choices.
if (type != 0) {
// Variable
uint64_t v = dict.getOrAdd(name);
t.push_back(VTerm((uint8_t) v, 0));
} else {
// Constant
// name = program->rewriteRDFOWLConstants(name);
uint64_t dictTerm;
f->layer->getOrAddDictNumber(name.c_str(), name.size(), dictTerm);
t.push_back(VTerm(0, dictTerm));
}
}
int pos = 0;
for (std::vector<VTerm>::iterator itr = t.begin(); itr != t.end(); ++itr) {
tuple.set(*itr, pos++);
}
uint8_t adornment = Predicate::calculateAdornment(tuple);
int64_t predid = f->program->getOrAddPredicate(predicate, (uint8_t) vtuplesz);
if (predid < 0) {
throwException(("wrong cardinality in predicate " + predicate).c_str());
return result;
}
Predicate pred((PredId_t) predid, adornment, f->layer->doesPredExists((PredId_t) predid) ? EDB : IDB, (uint8_t) vtuplesz);
Literal literal(pred, tuple, false);
result.push_back(literal);
}
return result;
}
#ifdef __cplusplus
extern "C" {
#endif
/*
* Modified JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_setLogLevel(JNIEnv *env, jobject obj, jobject level) {
* Class: karmaresearch_vlog_VLog
* Method: setLogLevel
* Signature: (Lkarmaresearch/vlog/VLog/LogLevel;)V
*/
void setLogLevel(int level) {
switch(level) {
case 0:
Logger::setMinLevel(ERRORL);
break;
case 1:
Logger::setMinLevel(WARNL);
break;
case 2:
Logger::setMinLevel(INFOL);
break;
case 3:
Logger::setMinLevel(DEBUGL);
break;
default:
Logger::setMinLevel(TRACEL);
break;
}
logLevelSet = true;
}
/*
* Modified JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_setLogFile(JNIEnv *env, jobject obj, jstring file)
* Class: karmaresearch_vlog_VLog
* Method: setLogFile
* Signature: (Ljava/lang/String;)V
*/
void setLogFile(std::string file) {
// TODO Irina NULL equivalent in C++
if (file == NULL) {
Logger::logToFile("");
return;
}
Logger::logToFile(file);
}
/*
* Modified: JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_start(JNIEnv *env, jobject obj, jstring rawconf, jboolean isfile)
* Class: karmaresearch_vlog_VLog
* Method: start
* Signature: (Ljava/lang/String;Z)V
*/
void start(int vLogId, std::string crawconf) {
VLogInfo *f = getVLogInfo(vLogId);
if (f != NULL) {
throwException("VLog is already started");
return;
}
if (! logLevelSet) {
Logger::setMinLevel(INFOL);
}
f = new VLogInfo();
try {
EDBConf conf(crawconf.c_str(), false);
try {
f->layer = new EDBLayer(conf, false);
} catch(std::string s) {
throwException(s.c_str());
return;
}
f->program = new Program(f->layer);
} catch(std::string s) {
throwException(s.c_str());
return;
} catch(char const *s) {
throwException(s);
return;
}
vlogMap[vLogId] = f;
}
/*
* Modified JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_stop(JNIEnv *env, jobject obj)
*
* Class: karmaresearch_vlog_VLog
* Method: stop
* Signature: ()V
*/
void stop(int vLogId) {
auto inf = vlogMap.find(vLogId);
if (inf == vlogMap.end()) {
return;
}
delete inf->second;
vlogMap.erase(inf);
}
/*
* Modified JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_addData(JNIEnv *env, jobject obj, jstring jpred, jobjectArray data)
* Class: karmaresearch_vlog_VLog
* Method: addData
* Signature: (Ljava/lang/String;[[Ljava/lang/String;)V
*/
void addData(JNIEnv *env, int vLogId, std::string pred, jobjectArray data) {
//TODO Irina, continue modifying, get rid of env
VLogInfo *f = getVLogInfo(vLogId);
if (f == NULL) {
f = new VLogInfo();
vlogMap[vLogId] = f;
}
if (! logLevelSet) {
Logger::setMinLevel(INFOL);
}
if (f->layer == NULL) {
EDBConf conf("", false);
f->layer = new EDBLayer(conf, false);
}
if (f->program != NULL) {
if (f->program->getNRules() > 0) {
throwException("Cannot add data if there already are rules");
return;
}
delete f->program;
f->program = NULL;
}
if (data == NULL) {
throwException("null data");
return;
}
jsize nrows = env->GetArrayLength(data);
// For all rows:
std::vector<std::vector<std::string>> values;
for (int i = 0; i < nrows; i++) {
// First, get the atom.
std::vector<std::string> value;
jobjectArray atom = (jobjectArray) env->GetObjectArrayElement(data, (jsize) i);
if (atom == NULL) {
// throwEDBConfigurationException(env, "null data");
// return;
continue; // For issue #25
}
jint arity = env->GetArrayLength(atom);
if (arity != (uint8_t) arity) {
throwException( ("Arity of " + pred + " too large (" + std::to_string(arity) + " > 255)").c_str());
return;
}
for (int j = 0; j < arity; j++) {
jstring v = (jstring) env->GetObjectArrayElement(atom, (jsize) j);
if (v == NULL) {
throwException("null data");
return;
}
value.push_back(jstring2string(env, v));
}
values.push_back(value);
}
try {
f->layer->addInmemoryTable(pred, values);
} catch(std::string s) {
throwException(s.c_str());
return;
} catch(char const *s) {
throwException(s);
return;
}
f->program = new Program(f->layer);
}
/*
* Modified JNIEXPORT jint JNICALL Java_karmaresearch_vlog_VLog_getPredicateId(JNIEnv *env, jobject obj, jstring p)
* Class: karmaresearch_vlog_VLog
* Method: getPredicateId
* Signature: (Ljava/lang/String;)I
*/
int getPredicateId(VLogInfo *f, std::string predName) {
if (f == NULL || f->program == NULL) {
throwException( "VLog is not started yet");
return -1;
}
// This might create a new predicate if it does not exist.
// There should be a way to just do a lookup???
Predicate pred = f->program->getPredicate(predName);
if (pred.getCardinality() == 0) {
// Does not exist yet.
return -1;
}
return pred.getId();
}
/*
* Modified JNIEXPORT jint JNICALL Java_karmaresearch_vlog_VLog_getPredicateArity(JNIEnv *env, jobject obj, jstring p)
* Class: karmaresearch_vlog_VLog
* Method: getPredicateArity
* Signature: (Ljava/lang/String;)I
*/
int getPredicateArity(VLogInfo *f, std::string predName ) {
if (f == NULL || f->program == NULL) {
throwNotStartedException(env, "VLog is not started yet");
return -1;
}
// TODO: fix this: this might create a new predicate if it does not exist.
// There should be a way to just do a lookup???
Predicate pred = f->program->getPredicate(predName);
if (pred.getCardinality() == 0) {
return -1;
}
return pred.getCardinality();
}
/*
* Class: karmaresearch_vlog_VLog
* Method: getPredicate
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_karmaresearch_vlog_VLog_getPredicate(JNIEnv *env, jobject obj, jint predid) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException("VLog is not started yet");
return NULL;
}
std::string pred = f->program->getPredicateName((PredId_t) predid);
if (pred == std::string("")) {
return NULL;
}
return env->NewStringUTF(pred.c_str());
}
/*
* Class: karmaresearch_vlog_VLog
* Method: getConstantId
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_karmaresearch_vlog_VLog_getConstantId(JNIEnv *env, jobject obj, jstring literal) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->layer == NULL) {
throwException("VLog is not started yet");
return (jint) -1;
}
const char *cliteral = env->GetStringUTFChars(literal, 0);
uint64_t value;
jlong retval = -1;
if (f->layer->getDictNumber(cliteral, strlen(cliteral), value)) {
retval = value;
}
env->ReleaseStringUTFChars(literal, cliteral);
return retval;
}
/*
* Class: karmaresearch_vlog_VLog
* Method: getOrAddConstantId
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_karmaresearch_vlog_VLog_getOrAddConstantId(JNIEnv *env, jobject obj, jstring literal) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->layer == NULL) {
throwException("VLog is not started yet");
return (jint) -1;
}
const char *cliteral = env->GetStringUTFChars(literal, 0);
uint64_t value;
jlong retval = -1;
if (f->layer->getOrAddDictNumber(cliteral, strlen(cliteral), value)) {
retval = value;
}
env->ReleaseStringUTFChars(literal, cliteral);
return retval;
}
/*
* Class: karmaresearch_vlog_VLog
* Method: getConstant
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_karmaresearch_vlog_VLog_getConstant(JNIEnv *env, jobject obj, jlong literalid) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException( "VLog is not started yet");
return NULL;
}
std::string s = f->layer->getDictText(literalid);
if (s == std::string("")) {
return NULL;
}
return env->NewStringUTF(s.c_str());
}
static TupleIterator *getQueryIter(JNIEnv *env, jobject obj, PredId_t p, jlongArray els, jboolean includeConstants) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException("VLog is not started yet");
return NULL;
}
// Create a VLog query from the parameters.
Predicate pred = f->program->getPredicate((PredId_t) p);
if (pred.getCardinality() == 0) {
return NULL;
}
jsize sz = env->GetArrayLength(els);
VTuple tuple((uint8_t) sz);
jlong *e = env->GetLongArrayElements(els, NULL);
for (int i = 0; i < sz; i++) {
jlong v = e[i];
uint8_t varId = 0;
uint64_t val = 0;
if (v < 0) {
varId = (uint8_t) -v;
} else {
val = v;
}
VTerm vterm(varId, val);
tuple.set(vterm, i);
}
env->ReleaseLongArrayElements(els, e, JNI_ABORT);
Literal query(pred, tuple);
// Now create an iterator over the query result.
TupleIterator *iter = NULL;
Reasoner r((uint64_t) 0);
if (pred.getType() == EDB) {
iter = r.getEDBIterator(query, NULL, NULL, *(f->layer), ! (bool) includeConstants, NULL);
} else if (f->sn != NULL) {
iter = r.getIteratorWithMaterialization(f->sn, query, ! (bool) includeConstants, NULL);
} else {
// No materialization yet, but non-EDB predicate ... so, empty.
TupleTable *table = new TupleTable(sz);
std::shared_ptr<TupleTable> pt(table);
iter = new TupleTableItr(pt);
}
return iter;
}
static int numberOfUniqueVars(JNIEnv *env, jlongArray els) {
std::vector<uint8_t> vars;
jsize sz = env->GetArrayLength(els);
jlong *e = env->GetLongArrayElements(els, NULL);
uint8_t varId;
for (int i = 0; i < sz; i++) {
if (e[i] < 0) {
varId = (uint8_t) -e[i];
if(std::find(vars.begin(), vars.end(), varId) == vars.end()) {
vars.push_back(varId);
}
}
}
return vars.size();
}
/*
* Class: karmaresearch_vlog_VLog
* Method: query
* Signature: (I[JZZ)Lkarmaresearch/vlog/QueryResultIterator;
*/
JNIEXPORT jobject JNICALL Java_karmaresearch_vlog_VLog_query(JNIEnv * env, jobject obj, jint p, jlongArray els, jboolean includeConstants, jboolean filterBlanks) {
if (p == -1) {
throwException("Query contains non-existing predicate");
return NULL;
}
TupleIterator *iter = getQueryIter(env, obj, (PredId_t) p, els, includeConstants);
if (iter == NULL) {
return NULL;
}
jclass jcls=env->FindClass("karmaresearch/vlog/QueryResultIterator");
jmethodID mID = env->GetMethodID(jcls, "<init>", "(JZ)V");
jobject jobj = env->NewObject(jcls, mID, (jlong) iter, filterBlanks);
return jobj;
}
/*
* Class: karmaresearch_vlog_VLog
* Method: nativeQuerySize
* Signature: (I[JZZ)J;
*/
JNIEXPORT jlong JNICALL Java_karmaresearch_vlog_VLog_nativeQuerySize(JNIEnv * env, jobject obj, jint p, jlongArray els, jboolean includeConstants, jboolean filterBlanks) {
jlong result = 0;
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException( "VLog is not started yet");
return result;
}
if (p == -1) {
throwException( "Query contains non-existing predicate");
return result;
}
Predicate pred = f->program->getPredicate((PredId_t) p);
if ((pred.getCardinality() == numberOfUniqueVars(env, els)) && !filterBlanks) {
if (pred.getType() == EDB) {
return f->layer->getPredSize(p);
} else if (f->sn != NULL) {
return f->sn->getSizeTable(p);
} else {
// pred.getType()==IDB and f->sn==NULL
throwException("Materialization has not run yet");
return result;
}
} else {
TupleIterator *iter = getQueryIter(env, obj, (PredId_t) p, els, (jboolean) includeConstants);
if (iter == NULL) {
return result;
}
size_t sz = iter->getTupleSize();
while (iter->hasNext()) {
iter->next();
if (filterBlanks) {
bool filterOut = false;
for (int i = 0; i < sz; i++) {
if (IS_BLANK(iter->getElementAt(i))) {
filterOut = true;
break;
}
}
if (!filterOut) {
++result;
}
}
else {
++result;
}
}
return result;
}
}
/*
* Class: karmaresearch_vlog_VLog
* Method: setRules
* Signature: ([Lkarmaresearch/vlog/Rule;Lkarmaresearch/vlog/VLog/RuleRewriteStrategy;)V
*/
JNIEXPORT void JNICALL Java_karmaresearch_vlog_VLog_setRules(JNIEnv *env, jobject obj, jobjectArray rules) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException( "VLog is not started yet");
return;
}
if (rules != NULL) {
// Create a new program, to remove any left-overs from old rule stuff
delete f->program;
f->program = new Program(f->layer);
// Get number of rules
jsize sz = env->GetArrayLength(rules);
// For each rule:
for (int i = 0; i < sz; i++) {
Dictionary dictVariables; // temporary dictionary for variables.
jobject rule = env->GetObjectArrayElement(rules, (jsize) i);
jclass cls = env->GetObjectClass(rule);
jmethodID getHeadMethod = env->GetMethodID(cls, "getHead", "()[Lkarmaresearch/vlog/Atom;");
jmethodID getBodyMethod = env->GetMethodID(cls, "getBody", "()[Lkarmaresearch/vlog/Atom;");
// Get fields: head and body.
jobjectArray head = (jobjectArray) env->CallObjectMethod(rule, getHeadMethod);
jobjectArray body = (jobjectArray) env->CallObjectMethod(rule, getBodyMethod);
// Convert them into internal VLog format.
std::vector<Literal> vhead = getVectorLiteral(env, f, head, dictVariables);
std::vector<Literal> vbody = getVectorLiteral(env, f, body, dictVariables);
// Test number of variables
uint64_t v = dictVariables.getOrAdd("___DUMMY___DUMMY___DUMMY");
if (v > 256) {
throwException( ("Too many variables in rule " + std::to_string(i)).c_str());
return;
}
for (auto lit : vhead) {
if (lit.getPredicate().getType() == EDB) {
std::string pred = f->program->getPredicateName(lit.getPredicate().getId());
if (pred == std::string("")) {
throwException( "Rule head cannot contain EDB predicate");
} else {
throwIllegalArgumentException(("Rule head cannot contain EDB predicate " + pred).c_str());
}
return;
}
}
// And add the rule.
f->program->addRule(vhead, vbody, false);
}
}
}
/*
* Class: karmaresearch_vlog_VLog
* Method: materialize
* Signature: (ZI)Z
*/
JNIEXPORT bool JNICALL Java_karmaresearch_vlog_VLog_materialize(JNIEnv *env, jobject obj) {
VLogInfo *f = getVLogInfo(env, obj);
if (f == NULL || f->program == NULL) {
throwException("VLog is not started yet");
return false;
}
if (f->sn != NULL) {
delete f->sn;
f->sn = NULL;
}
LOG(INFOL) << "Starting full materialization";
try {
f->sn = new SemiNaiver(*(f->layer), f->program, true, false, false,
TypeChase::RESTRICTED_CHASE,
-1, false, false);
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
unsigned long *p = NULL;
f->sn->run(p);
std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
LOG(INFOL) << "Runtime materialization = " << sec.count() * 1000 << " milliseconds";
} catch(std::runtime_error e) {
throwException(e.what());
if (f->sn != NULL) {
delete f->sn;
f->sn = NULL;
}
return false;
} catch(const char *e) {
throwException ( e);
if (f->sn != NULL) {
delete f->sn;
f->sn = NULL;
}
return false;
} catch(std::bad_alloc e) {
throwException( e.what());
if (f->sn != NULL) {
delete f->sn;
f->sn = NULL;
}
return false;
}
f->sn->printCountAllIDBs("");
return true;
}
/*
* Class: karmaresearch_vlog_QueryResultIterator
* Method: hasMoreElements
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_karmaresearch_vlog_QueryResultIterator_hasNext(JNIEnv *env, jobject obj, jlong ref) {
TupleIterator *iter = (TupleIterator *) ref;
if (iter == NULL) {
return (jboolean) false;
}
return (jboolean) iter->hasNext();
}
/*
* Class: karmaresearch_vlog_QueryResultIterator
* Method: nextElement
* Signature: (J)[J
*/
JNIEXPORT jlongArray JNICALL Java_karmaresearch_vlog_QueryResultIterator_next(JNIEnv *env, jobject obj, jlong ref) {
TupleIterator *iter = (TupleIterator *) ref;
if (iter == NULL) {
return NULL;
}
size_t sz = iter->getTupleSize();
iter->next();
jlong res[256];
for (int i = 0; i < sz; i++) {
res[i] = iter->getElementAt(i);
}
jlongArray outJNIArray = env->NewLongArray(sz);
if (NULL == outJNIArray) return NULL;
env->SetLongArrayRegion(outJNIArray, 0 , sz, res);
return outJNIArray;
}
/*
* Class: karmaresearch_vlog_QueryResultIterator
* Method: cleanup
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_karmaresearch_vlog_QueryResultIterator_cleanup(JNIEnv *env, jobject obj, jlong ref) {
TupleIterator *iter = (TupleIterator *) ref;
if (iter != NULL) {
delete iter;
}
}
/*
* Class: karmaresearch_vlog_QueryResultIterator
* Method: hasBlanks
* Signature: ([J)Z
*/
JNIEXPORT jboolean JNICALL Java_karmaresearch_vlog_QueryResultIterator_hasBlanks(JNIEnv * env, jobject obj, jlongArray jv) {
jsize sz = env->GetArrayLength(jv);
jlong *e = env->GetLongArrayElements(jv, NULL);
for (int i = 0; i < sz; i++) {
if (IS_BLANK(e[i])) {
env->ReleaseLongArrayElements(jv, e, JNI_ABORT);
return (jboolean) true;
}
}
env->ReleaseLongArrayElements(jv, e, JNI_ABORT);
return (jboolean) false;
}
#ifdef __cplusplus
}
#endif