Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file sparql_ms.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255(*********************************************************************************)(* OCaml-RDF *)(* *)(* Copyright (C) 2012-2024 Institut National de Recherche en Informatique *)(* et en Automatique. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License version *)(* 3 as published by the Free Software Foundation. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public License *)(* along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)openSparql_typesmoduleSMap=Sparql_types.SMapmoduleSSet=Sparql_types.SSetmoduleVMap=Dt.VMap;;exceptionIncompatible_musofstringexceptionCannot_extend_muofvarlet()=Printexc.register_printer(function|Incompatible_muss->Some(Printf.sprintf"Incompatible multisets: %s"s)|Cannot_extend_muvar->Some(Printf.sprintf"Cannot extend multiset for var %s"var.var_name)|_->None)(** A solution mapping : variable -> rdf term *)typemu={mu_bindings:Term.termSMap.t;mutablemu_bnodes:stringVMap.t;}letmu_0={mu_bindings=SMap.empty;mu_bnodes=VMap.empty}letmu_addvtmu={muwithmu_bindings=SMap.addvtmu.mu_bindings}letmu_copymu={mu_bindings=mu.mu_bindings;mu_bnodes=mu.mu_bnodes}letmuxt=mu_addxtmu_0letgen_blank_id=letcpt=ref0infun()->incrcpt;lett=Unix.gettimeofday()inPrintf.sprintf"__b_%d_%f"!cptt;;letget_bnodemuvalue=tryDt.Blank(VMap.findvaluemu.mu_bnodes)withNot_found->letlabel=gen_blank_id()inmu.mu_bnodes<-VMap.addvaluelabelmu.mu_bnodes;Dt.Blanklabel;;letmu_comparemu1mu2=SMap.compareTerm.comparemu1.mu_bindingsmu2.mu_bindings;;letmu_merge=letfvarterm1term2=matchterm1,term2with|None,x->x|x,None->x|Somet1,Somet2->matchTerm.comparet1t2with0->Somet1|_->raise(Incompatible_musvar)inletmerge_bnodesvlabel1label2=matchlabel1,label2withNone,x|x,None->x|Somel1,Somel2->Somel1(*match Stdlib.compare l1 l2 with
0 -> Some l1
| _ ->
(*dbg ~loc: "warning" ~level:2 (fun () -> "Merging mus: bnodes label maps differ");*)
Some l1*)infunmu1mu2->letmu_bindings=SMap.mergefmu1.mu_bindingsmu2.mu_bindingsinletmu_bnodes=VMap.mergemerge_bnodesmu1.mu_bnodesmu2.mu_bnodesin{mu_bindings;mu_bnodes}letmu_find_varnamenamemu=SMap.findnamemu.mu_bindingsletmu_find_varvmu=SMap.findv.var_namemu.mu_bindingsletmu_project=letfsetv_=SSet.memvsetinfunsetmu->{muwithmu_bindings=SMap.filter(fset)mu.mu_bindings}letmu_foldfmuacc=SMap.foldfmu.mu_bindingsacc;;letmu_iterfmu=SMap.iterfmu.mu_bindings;;moduleMuOrdered=structtypet=muletcompare=mu_compareendmoduleMuSet=Set.Make(MuOrdered)moduleMuNOrdered=structtypet=int*muletcompare(n1,_)(n2,_)=n1-n2endmoduleMultimu=Set.Make(MuNOrdered)(** A Multiset is a set of pairs (int, mu) *)typemultiset=Multimu.tletomega_add=letgenid=letcpt=ref0infun()->incrcpt;!cptinfunmums->Multimu.add(genid(),mu)ms;;letomega_add_if_not_present=letpredmu0(_,mu)=mu_comparemu0mu=0infunmums->ifMultimu.exists(predmu)msthenmselseomega_addmums;;letomega_0=omega_addmu_0Multimu.emptyletomegaxt=omega_add(muxt)Multimu.emptyletcard_muomegamu0=letpred(_,mu)=mu_comparemu0mu=0inlets=Multimu.filterpredomegainMultimu.cardinals;;letomega_filter=letfpred(id,mu)set=ifpredmuthenMultimu.add(id,mu)setelsesetinfunpredom->Multimu.fold(fpred)omMultimu.empty;;letomega_join=letf2predmu1(_,mu2)set=tryletmu=mu_mergemu1mu2inifpredmuthenomega_addmusetelsesetwithIncompatible_mus_->setinletfpredom2(_id1,mu1)set=Multimu.fold(f2predmu1)om2setinfun?(pred=fun_->true)om1om2->Multimu.fold(fpredom2)om1Multimu.empty;;letomega_union=Multimu.union;;letomega_diff_pred=letpredevalmu1(_,mu2)=tryletmu=mu_mergemu1mu2innot(evalmu)withIncompatible_mus_->trueinletfevalo2(_,mu1)=Multimu.for_all(predevalmu1)o2infunevalo1o2->matchMultimu.compareo2omega_0with0->o1|_->matchMultimu.compareo2Multimu.emptywith0->o1|_->Multimu.filter(fevalo2)o1;;exceptionNot_disjointletmu_disjoint_doms=letf_v1v2=matchv1,v2withSome_,Some_->raiseNot_disjoint|_->Noneinfunmu1mu2->tryignore(SMap.mergefmu1.mu_bindingsmu2.mu_bindings);truewithNot_disjoint->false;;letomega_minus=letf2mu1(_,mu2)=(mu_disjoint_domsmu1mu2)||(tryignore(mu_mergemu1mu2);falsewith_->true)inletfo2(_,mu1)=Multimu.for_all(f2mu1)o2infuno1o2->Multimu.filter(fo2)o1letomega_extend=letfevalvar(_,mu)map=letmu=tryignore(mu_find_varvarmu);raise(Cannot_extend_muvar)withNot_found->tryletv=evalmuinmu_addvar.var_namevmuwith_->muinomega_addmumapinfunevalovar->Multimu.fold(fevalvar)oMultimu.empty;;letomega_fold=letfg(_,mu)acc=gmuaccinfungoacc->Multimu.fold(fg)oacc;;letomega_iter=letfg(_,mu)=gmuinfungo->Multimu.iter(fg)o;;letomega_exists=letfpred(_,mu)=predmuinfunpredo->Multimu.exists(fpred)o;;