/
builtins.cpp
1458 lines (1174 loc) · 40.1 KB
/
builtins.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef DHT
#include <thread> // std::thread
#include <mutex> // std::mutex, std::lock
#include <opendht.h>
string strhash(string x)
{
std::hash<std::string> fn;
size_t h = fn(x);
stringstream sss;
sss <<
/*std::showbase << std::uppercase << std::hex <<*/
h;
// "0xbab5";//h;
return sss.str();
}
/**
* Print va_list to std::ostream (used for logging).
*/
void
printDhtLog(std::ostream& s, char const* m, va_list args) {
static constexpr int BUF_SZ = 8192;
char buffer[BUF_SZ];
int ret = vsnprintf(buffer, sizeof(buffer), m, args);
if (ret < 0)
return;
s.write(buffer, std::min(ret, BUF_SZ));
if (ret >= BUF_SZ)
s << "[[TRUNCATED]]";
s.put('\n');
}
void
enableDhtLogging(dht::DhtRunner& dht)
{
dht.setLoggers(
[](char const* m, va_list args){ dout << "ERR"; printDhtLog(dout, m, args); },
[](char const* m, va_list args){ dout << "WARN"; printDhtLog(dout, m, args); },
[](char const* m, va_list args){ printDhtLog(dout, m, args); }
);
}
#endif
void build_in_dht()
{
#ifdef DHT
static bool running = false;
static mutex mut;
static dht::DhtRunner ht;
static int port = 4443;
if (!running)
{
running = true;
int port = 4443;
dout << "firing up DHT on port " << port << endl;
// Launch a dht node on a new thread, using a
// generated RSA key pair, and listen on port 4222.
ht.run(port, dht::crypto::generateIdentity(), true);
// Join the network through any running node,
// here using a known bootstrap node.
ht.bootstrap("127.0.0.1", "4444");
// put some data on the dht
std::vector<uint8_t> some_data(5, 10);
ht.put("unique_key", some_data);
// put some data on the dht, signed with our generated private key
ht.putSigned("unique_key_42", some_data);
}
auto ht_ = &ht;
EEE;
string bu = "http://idni.org/dht#put";
auto bui = dict.set(mkiri(pstr(bu)));
builtins[bui].push_back(
[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
setproc(bu);
TRACE_ENTRY;
dout <<"sssss" << endl;
switch(entry){
case 0:
x = getValue(x);
if(is_node(*x))
{
node n = dict[get_node(*x)];
string v = *n.value;
string key,val;
if (n._type == node::IRI)
{
if (has(mykb->first, v))
{
key = "root_graph_" + v;
stringstream ss;
ss << mykb->first[v];
val = ss.str();
}
else
{
string h = strhash(v);
key = "root_iri_" + h;
val = v;
}
}
else if (n._type == node::LITERAL)
{
string h = strhash(v);
key = "root_lit_" + h;
val = v;
}
else
{
dout << "nope." << endl;
DONE;
}
dout << "putting " << key << "=" << val << endl;
ht_->put(key, val);
}
else
dout << "nope." << endl;
END;
}
});
bu = "http://idni.org/dht#dbg";
bui = dict.set(mkiri(pstr(bu)));
builtins[bui].push_back(
[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
setproc(bu);
TRACE_ENTRY;
switch(entry){
case 0:
x = getValue(x);
if(is_node(*x))
{
node n = dict[get_node(*x)];
string v = *n.value;
if (v == "on")
{
MSG("dht dbg on");
enableDhtLogging(*ht_);
}
else{
MSG("dht dbg off");
ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
}
}
END;
}
});
bu = "http://idni.org/dht#setPort";
bui = dict.set(mkiri(pstr(bu)));
builtins[bui].push_back(
[bu, entry, ht_](Thing *dummy, Thing *x) mutable {
setproc(bu);
TRACE_ENTRY;
switch(entry){
case 0:
x = getValue(x);
if(is_node(*x))
{
node n = dict[get_node(*x)];
string v = *n.value;
if (v == "on")
{
MSG("dht dbg on");
enableDhtLogging(*ht_);
}
else{
MSG("dht dbg off");
ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
}
}
END;
}
});
// get data from the dht
ht_->get("other_unique_key", [](const std::vector<std::shared_ptr<dht::Value>>& values) {
// Callback called when values are found
for (const auto& value : values)
dout << "Found value: " << *value << std::endl;
return true; // return false to stop the search
});
#endif
}
//Outer vector: list
//Inner vector: triples of nodeids
void add_facts(vector<vector<nodeid>> facts)
{
//std::map<nodeid,std::vector<std::pair<Thing*,Thing*>>>
ths_t &ths = *new ths_t;
ths_garbage = &ths;///.push_back(ths);
//Map each pred to a list of it's subject/object pairs
for (auto f: facts)
ths[f[1]].push_back({
create_node(f[0]),
create_node(f[2])});
coro suc, ouc;
//For each pred in the ths:
//std::pair<nodeid,std::vector<std::pair<Thing*,Thing*>>>
for (auto ff:ths)
{
//std::vector<std::pair<Thing*, Thing*>>
auto &pairs = ff.second;
EEE; //char entry = 0;
pos_t pi = 0;
//Map each pred to a coro on it's subject/object
builtins[ff.first].push_back([suc,ouc,pi,pairs,entry](Thing *s_, Thing *o_) mutable{
switch(entry)
{
case 0:
//hrmm.. where is pi being incremented? looks
//like this just does pi=0 then exits.
/*yea looks like you found a bug
this thing looks pretty suboptimal btw...buti guess it should get the job done*/
if (pi < pairs.size())//fixed?
{
//Generate a unify coro for the subject;
//on the fly.
//Then run it.
suc = unify(s_,&pairs[pi].first);
while(suc()){
//Again for the object
ouc = unify(o_,&pairs[pi].second);
while(ouc())
{
//If both succeed then yield
//true.
//not quite
entry = LAST;
return true;
/*
entry = LAST;
so...wanna fix this?
*/
case_LAST:;
}
}
}
END;
}
});
}
}
/*
* RDF - http://www.w3.org/TR/lbase/#using - where the ?y(?x) thing comes from
*/
void build_in_facts()
{
//Is it beneficial to add the redundant facts or not?
add_facts({
//These are redundant given the rule {?x rdf:type rdfs:Class} => {?x rdfs:subClassOf rdfs:Resource}
//rdfsResource rdfssubClassOf rdfsResource
//rdfsClass rdfssubClassOf rdfsResource
//rdfsLiteral rdfssubClassOf rdfsResource
{rdfsDatatype, rdfssubClassOf, rdfsClass},
//These are redundant given the rule {?x rdf:type rdfs:Datatype} => {?x rdfs:subClassOf rdfs:Literal}
//rdflangString rdfssubClassOf rdfsLiteral //(redundant)
//rdfHTML rdfssubClassOf rdfsLiteral //(redundant)
{rdfXMLLiteral, rdfssubClassOf, rdfsLiteral}, //(redundant)
//rdfProperty rdfssubClassOf rdfsResource //(redundant)
//rdfsContainer rdfssubClassOf rdfsResource //(redundant)
{rdfAlt, rdfssubClassOf, rdfsContainer},
{rdfBag, rdfssubClassOf, rdfsContainer},
{rdfsContainerMembershipProperty, rdfssubClassOf, rdfProperty},
{rdfsDatatype, rdfssubClassOf, rdfsClass},
{rdfSeq, rdfssubClassOf, rdfsContainer},
{rdfsisDefinedBy, rdfssubPropertyOf, rdfsseeAlso},
//I think we need these:
{rdfsResource, rdftype, rdfsClass},
{rdfsClass, rdftype, rdfsClass},
{rdfsLiteral, rdftype, rdfsClass},
{rdfsDatatype, rdftype, rdfsClass},
{rdflangString, rdftype, rdfsDatatype},
{rdfHTML, rdftype, rdfsDatatype},
{rdfXMLLiteral, rdftype, rdfsDatatype},
//I think we need this one:
{rdfProperty, rdftype, rdfsClass},
//possibly redundant via their usage in rdfsdomain & rdfsrange (not sure):
//rdfsrange rdftype rdfProperty
//rdfsdomain rdftype rdfProperty
//rdftype rdftype rdfProperty
//rdfssubClassOf rdftype rdfProperty
//rdfssubPropertyOf rdftype rdfProperty
//rdfslabel rdftype rdfProperty
//rdfscomment rdftype rdfProperty
{rdfsContainer, rdftype, rdfsClass},
//rdfBag rdftype rdfsClass
//rdfSeq rdftype rdfsClass
//rdfAlt rdftype rdfsClass
//redundant due to:
/*
[rdfBag, rdfSeq, rdfAlt] rdfssubClassOf rdfsContainer
rdfssubClassOf rdfsdomain rdfsClass
{?p rdfsdomain ?c. ?s ?p ?o} => {?s rdf:type ?c}
*/
//{rdfsContainerMembershipProperty, rdftype, rdfsClass},
//redundant due to:
/*
rdfsContainerMembershipProperty rdfssubClassOf rdfProperty
rdfssubClassOf rdfsdomain rdfsClass
{?p rdfsdomain ?c. ?s ?p ?o} => {?s rdf:type ?c}
*/
//rdf:_1 rdftype rdfsContainerMembershipProperty
//rdf:_2 rdftype rdfsContainerMembershipProperty
//rdf:_3 rdftype rdfsContainerMembershipProperty
//...
//rdfsmember rdftype rdfProperty (possibly redundant due to domain & range)
{rdfList, rdftype, rdfsClass},
{rdffirst, rdftype, owlFunctionalProperty},//?
{rdfrest, rdftype, owlFunctionalProperty},
{rdfnil, rdftype, rdfList},
{rdfStatement, rdftype, rdfsClass},
//possibly redundant due to domain & range:
//rdfsubject rdftype rdfProperty
//rdfpredicate rdftype rdfProperty
//rdfobject rdftype rdfProperty
//possibly redundant due to domain & range:
//rdfsseeAlso rdftype rdfProperty
//rdfsisDefinedBy rdftype rdfProperty
//rdfvalue rdftype rdfProperty
{rdfscomment, rdfsdomain, rdfsResource},
{rdfscomment, rdfsrange, rdfsLiteral},
{rdfsdomain, rdfsdomain, rdfProperty},
{rdfsdomain, rdfsrange, rdfsClass},
{rdffirst, rdfsdomain, rdfList},
{rdffirst, rdfsrange, rdfsResource},
{rdfsisDefinedBy, rdfsdomain, rdfsResource},
{rdfsisDefinedBy, rdfsrange, rdfsResource},
{rdfslabel, rdfsdomain, rdfsResource},
{rdfslabel, rdfsrange, rdfsLiteral},
{rdfsmember, rdfsdomain, rdfsContainer},
{rdfsmember, rdfsrange, rdfsResource},
{rdfobject, rdfsdomain, rdfStatement},
{rdfobject, rdfsrange, rdfsResource},
{rdfpredicate, rdfsdomain, rdfStatement},
{rdfpredicate, rdfsrange, rdfProperty},
{rdfsrange, rdfsdomain, rdfProperty},
{rdfsrange, rdfsrange, rdfsClass},
{rdfrest, rdfsdomain, rdfList},
{rdfrest, rdfsrange, rdfList},
{rdfsseeAlso, rdfsdomain, rdfsResource},
{rdfsseeAlso, rdfsrange, rdfsResource},
{rdfssubClassOf, rdfsdomain, rdfsClass},
{rdfssubClassOf, rdfsrange, rdfsClass},
{rdfssubPropertyOf, rdfsdomain, rdfProperty},
{rdfssubPropertyOf, rdfsrange, rdfProperty},
{rdfsubject, rdfsdomain, rdfStatement},
{rdfsubject, rdfsrange, rdfsResource},
{rdftype, rdfsdomain, rdfsResource},
{rdftype, rdfsrange, rdfsClass},
{rdfvalue, rdfsdomain, rdfsResource},
{rdfvalue, rdfsrange, rdfsResource}
});
}
/*
rdfs:subPropertyOf is a partial order:
{} => {?P rdfs:subPropertyOf ?P} reflexive
{?p1 rdfs:subPropertyOf ?p2. ?p2 rdfs:subPropertyOf ?p3} => {?p1 rdfs:subPropertyOf ?p3} transitive
{?p1 rdfs:subPropertyOf ?p2. ?p2 rdfs:subPropertyOf ?p1} => {?p1 == ?p2} anti-symmetric
*/
//https://www.w3.org/TR/rdf-schema/#ch_subpropertyof
//{?P1 @is rdfs:subPropertyOf ?P2. ?S ?P1 ?O} => {?S ?P2 ?O}.
//
//{?P3 @is rdfs:subPropertyOf rdfs:subPropertyOf. ?P1 ?P3 ?P2} => {?P1 @is rdfs:subPropertyOf ?P2}
//{?P4 @is rdfs:subPropertyOf rdfs:subPropertyOf. ?P3 ?P4 rdfs:subPropertyOf} => {?P3 @is rdfs:subPropertyOf rdfs:subPropertyOf}
//fixed point
//{?P3 @is rdfs:subPropertyOf ?P1. ?S ?P3 ?O}=> {?S ?P1 ?O}
rule_t make_wildcard_rule(nodeid pr)
{
FUN;
EEE;
MSG("..")
Thing p1 = create_unbound();
Thing p2 = create_node(pr);
pred_t sub, p1wildcard, p1lambda;
nodeid p1p = 0;
//old? //thats just for assert
//hrm
//I'm a bit confused about ep-check here.
//We ep-out if we repeat a (subject, object, rule) tuple.//subject, pred, object..err..well
//you could say it both ways
//Ok so the wildcard rule will be made for each pred
//We can do that in compile_pred
//If a pred gets called during its own execution with the same s/o as originally, then we ep out
//same defined as if we can unify s with orig_s and o with orig_o; if we can then execution of
//this pred with s and o will be equivalent to execution with orig_s and orig_o, which we're already doing.
//So, each pred should have its own ep-table, and if execution of a pred ends up calling the same pred,
//then the 2nd instance should use the same ep-table as the original.
//That should handle all our ep and wildcard problems.
ep_t *ep = new_ep();//reminds me, a bug in cppout was that i forgot to pop the ep-pair before the successful unify return and push it back after. Its like..you have a query with two triples calling the same rule...when youre yielding out of the first instance of the rule you want to retract the ep-pair before
//so..this func might need checking wrt that
DBG(Thing old[2]);
return [entry,ep,DBGC(old) p1,p1p,p2,sub,p1wildcard,p1lambda](Thing *s, Thing *o) mutable {
setproc("wildcard");
TRACE_ENTRY;
return false;
/*
switch(entry){
case 0:
DBG(old[0] = *s);
// i need to figure out floobits text navigation
if (find_ep(ep, s, o))
DONE;
ep->push_back(thingthingpair(s, o));
//quite sure its there since its a rdf builtin; should be, at least. it was there last friday.
sub = ITEM(preds,rdfssubPropertyOf);
while (sub(&p1, &p2))
{
ASSERT(is_bound(p1));
{
Thing *p1v = get_thing(p1);
ASSERT(is_node(*p1v));
p1p = get_node(*p1v);
}
if (!has(preds, p1p))
preds[p1p] = make_wildcard_rule(p1p);
p1lambda = ITEM(preds, p1p);
while(p1lambda(s, o))//the recursion will happen here
{
entry = LAST;
//todo:retract ep
return true;
case_LAST:;
}
}
ASSERT(is_unbound(p1));
ASSERT(are_equal(old[0], *s));
ASSERT(ep->size());
ep->pop_back();
END;
}
*/
};
}
//under construction
void build_in_rules()
{
build_in_dht();
/*some commonly used vars*/
EEE; //char entry = 0;
coro suc, suc2, ouc;
Thing *s = nullptr, *o = nullptr;
Thing ss;
//Thing oo;
Thing *r = nullptr;
Thing a,b;
a = create_unbound();
b = create_unbound();
/*ep_t *ep = new ep_t();
eps.push_back(ep)*/
//pred_t :: function<bool(Thing*,Thing*)>
pred_t p1, p2;
/*c is for constant*/
//Thing c_rdfsType = create_node(op->make(rdftype));
Thing c_rdfsResource = create_node(rdfsResource);
Thing c_rdfsClass = create_node(rdfsClass);
//Thing c_rdfssubClassOf = create_node(op->make(rdfssubClassOf));
Thing c_rdfProperty = create_node(rdfProperty);
//rdfs:Resource(?x)
/*<HMC_a> koo7: you mean if one queries "?x a rdf:Resource" should they get every known subject as a result?
<HMC_a> the answer would be yes. :-)
<koo7> HMC_a, every known subject and object, right?
<koo7> or....every nodeid in the kb thats not in the pred position...where do we draw the line?
<HMC_a> well, when i say "known subject" i don't really mean everything in the subject position, i mean every node useable as a subject (non-literal)
<koo7> ok
<koo7> what do you mean non-literal?
<HMC_a> you wouldn't bind each int as a result, for example
<HMC_a> if you returned "0 a Resource" "1 a Resource" "2 a Resource"..... this would be a bit of a problem ;-)
<koo7> yeah, so everything that explicitly appears in the kb
<koo7> traverse lists too
<HMC_a> yes remember that lists are logically just triples as well...
<koo7> err well wouldnt that mean the bnodes that rdf lists are made up of?
<HMC_a> so any node name that appears within a list is in the object position of some rdf:first triple
<HMC_a> yes, the bnode names as well*/
/*i forgot if we are supposed to/have list triples in compiled preds...if yes this
might even produce correct results i guess*/
//so this is saying: if you pass me a constant like a node or a list, then return true, it's a resource, and if you pass me a var, then run through every pred in the kb and give me any node/list that binds to subject or object of any of these preds?yea
//maybe this should instead just iterate thru kb triples tho?
//yea probably; this works but it's basically requiring the evaluation of the entire kb when everything's already defined prior to compile
/*nvm
id rather worry about the semantic difference
anyway we should either consult euler or run with it for now
im fine with running with it; probably the fastest way to get critique from HMC :)
we need tests, hard to make a test when you don't know the should-be
:)
if we roll with it HMC will make tests for us
we should hook up eulersharp at some point though, apparently that's what we're comparing against.. apparently
*/
/* builtins[rdftype].push_back([a, b, c_rdfsResource, entry, suc, suc2, ouc, s, o, p1](Thing *s_, Thing *o_) mutable {
map<nodeid, pred_t>::iterator x;
switch (entry) {
case 0:
o = getValue(o_);
ASSERT(!is_offset(*o));
ouc = unify(o, &c_rdfsResource);
while (ouc())
{
s = getValue(s_);
if(!is_unbound(*s))
{
entry = 1;
return true;
}
case 1:
if(is_unbound(*s))
{
for ( x=preds.begin(); x!=preds.end();x++)
{
p1 = x->second;//a coro-permanent copy
while(p1(&a, &b))
suc = unify(s, &a);
while(suc())
{
entry = 2;
return true;
case 2:;
}
suc2 = unify(s, &b);
while(suc2())
{
entry = 3;
return true;
case 3:;
}
}
}
}
END
}
});
*/
//todo rdfs:Class(?y) implies (?y(?x) iff rdf:type(?x ?y))
//...nothing to implement?
//for this ^, no, but for this v :
// {?x rdf:type rdfs:Class} => {?x rdfs:subClassOf rdfs:Resource}
{
pred_t type_pred;
builtins[rdfssubClassOf].push_back([entry,c_rdfsResource,c_rdfsClass,type_pred,ouc](Thing *s, Thing *res) mutable{
setproc("type is Class implies superClass is Resource");
TRACE_ENTRY;
switch(entry){
case 0:
ouc = unify(res, &c_rdfsResource);
type_pred = ITEM(preds,rdftype);
entry = LAST;
while(ouc())
{
while(type_pred(s, &c_rdfsClass))
{
return true;
case_LAST:;
}
}
END;
}
});
}
// https://www.w3.org/TR/rdf-schema/#ch_domain
// {?Pred @has rdfs:domain ?Class. ?Instance ?Pred ?Whatever} => {?Instance a ?Class}.
// rdfs:domain(?x ?y) implies ( ?x(?u ?v)) implies ?y(?u) )
{
Thing whatever = create_unbound();
Thing pred = create_unbound();
pred_t domain_pred, pred_coro;
Thing pred_val;
nodeid pred_nodeid = 0;
/*this one might still need adding ep check*///really
//Each pred should have its own ep-table. If execution of this pred ends up calling the same pred again,
//the new instance should use the same ep-table.
//We could have a single ep-table for the whole kb.
// * Start with an empty graph.
// * Each triple in a query would make a graph-node, carrying the structure of the triple.
// * When a triple in the query gets called, it executes rules sequentially. For each rule,
// make the children of the graph-node for the triple be the triples in the body, substituted
// with the HEAD_S and HEAD_O that result from unifying the query triple with the head triple.
// * When you want to ep-check anywhere in the query, you just follow the ancestors of a node.
/* Why are we doing domain for rdftype?
builtins are just grouped by their pred
just as kb rules are grouped into pred_t's
rdftype is the predicate of the head of the rule
rule has head s rdftype o */
builtins[rdftype].push_back([entry,domain_pred,pred,pred_val,pred_nodeid,pred_coro,whatever](Thing *instance, Thing *cls) mutable {
setproc("domainImpliesType");
TRACE_ENTRY;
switch(entry){
case 0:
domain_pred = ITEM(preds,rdfsdomain);
while (domain_pred(&pred, cls))
{
{
ASSERT(is_bound(pred));
Thing *pred_val = get_thing(pred);
//how do we know it's not another bound var good q.......... i guess it wouldnt if a rule returned it
//at least i put the assert there:) we can test it
//yeah hm in the long run we should get the floobits session and tmux on one machine i guess
//so isnt that a bug
/*i dunno if we're supposed to allow rules to imply this*/
//if it's a semantic restriction it should probably be handled more fully
//but in the typesystem, not here..at least thats my guess
/*anyway good catch
* tests/rdf/domainImpliesType-tricky
*/
ASSERT(is_node(*pred_val));
nodeid pred_nodeid = get_node(*pred_val);
//(So if the pred is not there, )a subproperty of it might still satisfy,
//but we don't have a pred to run, so make a wildcard rule
if (!has(preds, pred_nodeid))
preds[pred_nodeid] = make_wildcard_rule(pred_nodeid);
//If the pred is there, use that. This will need the wildcard rule to be added to the pred during compile_kb
pred_coro = ITEM(preds, pred_nodeid);
}
ASSERT(is_unbound(whatever));
while(pred_coro(instance, &whatever))
{
entry = LAST;
return true;
case_LAST:;
}
ASSERT(is_unbound(whatever));
}
return false;
END;
}
});
}
// tests/rdf/domainImpliesType passess
//K so should we package these up into one builtin?
//hell no!
// {?Pred @has rdfs:range ?Class. ?Whatever ?Pred ?Instance} => {?Instance a ?Class}.
// rdfs:range(?x ?y) implies ( ?x(?u ?v)) implies ?y(?v) )
//Alright how does that look
//like a lot of duplicated code hehe yep lol
/* {
Thing whatever = create_unbound();
Thing pred = create_unbound();
builtins[rdftype].push_back([entry,pred,p1,p2,whatever](Thing *instance, Thing *cls) mutable {
setproc("rangeImpliesType");
TRACE_ENTRY;
switch(entry){
case 0:
range_pred = ITEM(preds,rdfsrange);
while (range_pred(&pred, cls))
{
{
ASSERT(is_bound(pred));
Thing *pred_val = get_thing(pred);
//how do we know it's not another bound var good q.......... i guess it wouldnt if a rule returned it
//at least i put the assert there:) we can test it
//yeah hm in the long run we should get the floobits session and tmux on one machine i guess
//so isnt that a bug
//if it's a semantic restriction it should probably be handled more fully
//but in the typesystem, not here..at least thats my guess
ASSERT(is_node(*pred_val));
nodeid pred_nodeid = get_node(*pred_val);
if (!has(preds, pred_nodeid))
preds[pred_nodeid] = make_wildcard_rule(pred_nodeid);
pred_coro = ITEM(preds, pred_nodeid);
}
ASSERT(is_unbound(whatever));
while(pred_coro(&whatever,instance))
{
entry = LAST;
return true;
case_LAST:;
}
ASSERT(is_unbound(whatever));
}
return false;
END;
}
});
}
*/
//rdfs:subClassOf(?x ?y) implies (forall (?u)(?x(?u) implies ?y(?u))
//{?sub rdfs:subClassOf ?sup. ?something a ?sub} => {?something a ?sup}.
{
Thing sub = create_unbound();
pred_t subclassof_pred, type_coro;
builtins[rdftype].push_back([entry,sub,subclassof_pred,type_coro](Thing *something, Thing *sup) mutable {
setproc("XasupIfXasub");
TRACE_ENTRY;
switch(entry){
case 0:
subclassof_pred = ITEM(preds,rdfssubClassOf);
while (subclassof_pred (&sub, sup))
{
ASSERT(is_bound(sub));
type_coro = ITEM(preds, rdftype);
while(type_coro(something, &sub))
{
entry = LAST;
return true;
case_LAST:;
}
}
ASSERT(is_unbound(sub));
END;
}
});
}
//rdfs:Class(?x) implies ( rdfs:subClassOf(?x ?x) and rdfs:subClassOf(?x rdfs:Resource) )
//{?x rdf:type rdfs:Class} => {?x rdfs:subClassOf ?x}
/*
{
coro suc;
pred_t type_pred;
builtins[rdfssubClassOf].push_back([entry,suc,type_pred](Thing *x1, Thing *x2){
setproc("subClass is reflexive");
TRACE_ENTRY;
switch(entry){
case 0:
suc = unify(x1,x2);
entry = LAST;
while(suc()){
type_pred = ITEM(preds,rdftype);
while(type_pred(x1,c_rdfsClass)){
return true;
case LAST:
}
}
return false;
}
});
}
*/
//{?x rdf:type rdfs:Class} => {?x rdfs:subClassOf rdfs:Resource}
//{?x rdfs:subClassOf ?y. ?y rdfs:subClassOf ?z} => {?x rdfs:subClassOf ?z}.
/*
{
Thing y = create_unbound();
pred_t sub1, sub2;
builtins[rdfssubClassOf].push_back([entry,y,sub1,sub2](Thing *x, Thing *z){
setproc("rdfssubClass transitive");
TRACE_ENTRY;
switch(entry){
case 0:
sub1 = ITEM(preds,rdfssubClassOf);
entry = LAST;
while(sub(x,y)){
sub2 = ITEM(preds,rdfssubClassOf);
while(sub(y,z)){
return true;
case LAST:
}
}
return false;
}
});
}
*/
//(rdfs:subClassOf(?x ?y) and rdfs:subClassOf(?y ?x)) implies "?x == ?y" <-- how to handle?
//{?x rdf:type rdfs:Datatype} => {?x rdfs:subClassOf rdfs:Literal}
/*
{
coro luc;
Thing l = create_unbound();
pred_t type_pred;
builtins[rdfssubClassOf].push_back([entry,l,type_pred](Thing *x, Thing *lit){
setproc("XaDatatypeThenXsubLiteral");
TRACE_ENTRY;
switch(entry){
case 0:
luc = unify(l,c_rdfsLiteral);
entry = LAST;
while(luc()){
type_pred = ITEM(
while(type_pred(x,c_rdfsDatatype)){
return true;
case LAST:
}
}
return false;
}
});
}
*/
//{?x rdf:type rdf:Property} => {?x rdfs:subPropertyOf ?x}
/*
{
coro suc;
pred_t type_pred;
builtins[rdfssubPropertyOf].push_back([entry,suc,type_pred,c_rdfProperty](Thing *x1, Thing *x2){
setproc("rdfssubPropertyOf is reflexive");
TRACE_ENTRY;
switch(entry){
case 0:
suc = unify(x1,x2);
entry = LAST;
while(suc()){
type_pred = ITEM(preds,rdftype);
while(type_pred(x1 ,c_rdfProperty)
return true;
case LAST:
}
}
return false;
};
});
}
*/
//{?x rdfs:subPropertyOf ?y. ?y rdfs:subPropertyOf ?z} => {?x rdfs:subPropertyOf ?z}
/*
{
Thing y = create_unbound();
pred_t sub1, sub2;
builtins[rdfssubPropertyOf].push_back([entry,y](Thing *x, Thing *z){
setproc("rdfssubPropertyOf is transitive");
TRACE_ENTRY;
switch(entry){
case 0:
sub1 = ITEM(preds,rdfssubPropertyOf);
entry = LAST;
while(sub1(x,y)){
sub2 = ITEM(preds,rdfssubPropertyOf);
while(sub2(y,z)){
return true;
case LAST:
}
}
return false;
}
});