<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <issn pub-type="ppub">1613-0073</issn>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>N3.js Reasoner: Implementing reasoning in N3.js</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Wright</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Jesse</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Computer Science Department, University of Oxford</institution>
          ,
          <country country="UK">UK</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Reasoner</institution>
          ,
          <addr-line>Inference, RDFJS, RDF, JavaScript, TypeScript, Web, Browser, Solid</addr-line>
        </aff>
      </contrib-group>
      <abstract>
        <p>RDF reasoning typically does not occur in browser-based applications despite being critical to the next generation of Web technologies. The primary constraint is that client-side reasoners do not meet the performance requirements of modern applications and the technicalities of using reasoners make them inaccessible to front-end developers.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>CEUR
ceur-ws.org</p>
    </sec>
    <sec id="sec-2">
      <title>1. Introduction</title>
      <p>
        RDF reasoning has been integral to the Semantic Web since its inception in 2001 [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. Most RDF
reasoners have been developed in Java or C++ for desktop or server environments [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. However,
server-side reasoning alone is inadequate for a decentralised Semantic Web like Solid [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], where
data are distributed across many sources, including local files, Solid Pods, and public knowledge
graphs. Client-side reasoners are necessary to handle inferences from multiple sources and to
apply inferences to local documents or results from less-intelligent servers.
      </p>
      <p>
        N3.js [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] is a widely-used JavaScript library in the Semantic Web ecosystem. It has 589 stars
on GitHub, 336 downstream packages, including Wikidata [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], and 2605 dependent GitHub
repositories. Since N3.js implements the RDF JS model specification [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] and the RDF JS dataset
specification [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ], our reasoning engine is compatible with RDF JS libraries and applications out
of the box.
      </p>
    </sec>
    <sec id="sec-3">
      <title>2. Implementation</title>
      <p>
        We implement Horn Logic [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] reasoning in N3.js by applying the semi-naive reasoning
algorithm [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ] alongside a bespoke technique for indexing rules in-memory to optimise rule
evaluation against the 3-layered index of N3.js - which we detail in the remainder of this section.
      </p>
      <p>First, we describe the existing N3.js store index. For each triple in the store, the subject,
predicate and object are converted to a canonical string. An internal record maintains a mapping
https://www.cs.ox.ac.uk/people/jesse.wright/ (W. Jesse)</p>
      <p>© 2024 Copyright for this paper by its authors. Use permitted under Creative Commons License Attribution 4.0 International (CC BY 4.0).
between these canonical strings and a numerical ID. Triples are stored by adding these numerical
IDs as keys in nested indexes (dictionaries). The N3.js store uses three, three-layered indexes to
store data. The depth of these indexes reflects the fact that a triple has three elements: subject
(s), predicate (p) and object (o). The three indexes are ordered as osp, spo and pos.</p>
      <p>We assume that most applications have a limited number of rules, i.e. less than 100 as with
RDFS and OWL2RL inference. Consequently our reasoner implementation1 optimises to reduce
the time complexity of reasoning with respect to dataset size. The first step of reasoning in
N3.js is re-writing rules to: (1) convert each non-variable term in the premise and conclusion
into the internal ID used by the N3.js store index; (2) convert each variable into a pointer to
a shared memory location into which concrete values for the variable will be substituted (c.f.
Figure 2a); (3) identify matching patterns between a rule’s conclusion and the premise(s) of the
same or other rules (c.f. Figure 3a) to establish which rules need to be evaluated next when
new implicit data is discovered; and (4) precompute which of the 3 N3.js store indexes should
be used for looking up triples matching a rule premise. Figure 1b shows how the rule {?s a ?o
. ?o rdfs:subClassOf ?o2} → {?s a ?o2} (R1) is stored in memory with respect to the index
mapping in Table 1a. Figure 3a displays the dependencies between {?o1 rdfs:subClassOf ?o2 .
?o2 rdfs:subClassOf ?o3 } → {?o1 rdfs:subClassOf ?o3} (R2) and R1.</p>
      <p>Rules are then evaluated as follows: (1) perform a nested loop join on the premises, the
outermost loop iterates through triples matching the first premise - substituting bindings
into the variable memory locations (c.f. Figure 2a), subsequent nested loops iterate over the
remaining premises and iterate through all triples matching the partially-bound pattern (c.f.
Figure 2b); (2) new conclusion triples found added to the three-layered indexes; (3) for each new
conclusion, identify matching premises from the same, or other, rules (c.f. Figure 3a); and (4)
perform the subsequent reasoning run using rules with these premises pre-filled (c.f. Figure 3b)
to avoid re-evaluating the same patterns across reasoning runs.
1https://github.com/rdfjs/N3.js/pull/296
(3a) The dependency between R2 and R1 is stored (3b) Matches to the first premise of R1 can then
and can be used to pre-fill variables in R1 after R2 be iterated through to produce new derivations on
has been evaluated. subsequent reasoning runs.</p>
    </sec>
    <sec id="sec-4">
      <title>3. Performance Results and Conclusion</title>
      <p>
        We evaluate this N3.js reasoner against the only other known browser-available reasoning
engines: EYE JS [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ], a WebAssembly port of EYE [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ], and the HyLAR Reasoner [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ]. The
results in Table 1 and Table 2 were collected at https://github.com/jeswr/demo-perf-tests/ on a
GitHub Actions runner with 2 cores and 7GB of memory, running Ubuntu 22.04. NodeJS results
were collected from commit a51bc3e. Chrome and Firefox were run headless using Selenium2
and the performance results are collected from commit 33cbfb4.
      </p>
      <p>
        Table 1 presents the time taken to perform RDFS materialisation on Tim Berners-Lee’s profile
card and the FOAF ontology [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ]. The N3.js reasoner outperforms the others, completing the
task in under 0.1s, which is within the threshold for an instantaneous user perception [
        <xref ref-type="bibr" rid="ref14">14</xref>
        ].
      </p>
      <p>Table 2 presents the time taken to materialise all inferences for the the Data Deep Taxonomy
Benchmark (DTB) using the N3.js Reasoner, EYE JS, and HyLAR. This modification of the deep
taxonomy benchmark encodes subclasses as facts (:N0 rdfs:subClassOf :N1) rather than rules
(?X rdf:type :N0 → ?X rdf:type :N1), as the N3.js reasoner is optimised to handle a large
number of facts. The depth variable indicates the number of nested subclasses in the dataset.
The extended (ext) taxonomy benchmark scales the number of instances of the class :N0 with
the depth; consequently the number of implicit facts scales quadratically with depth.</p>
      <p>The N3.js Reasoner primarily outperforms HyLAR because (1) HyLAR does not index triples
or rules; consequently, HyLAR iterates through all facts when matching each premise taking
() time (2) HyLAR does not create internal representations for terms and triples - resulting in
(2a) costly string and object comparison operations for rule matching where N3.js is comparing
integers and (2b) costly object creation where N3.js is adding integers to an existing index.</p>
      <p>These results show that N3.js can eficiently reason over moderately-sized datasets in the
browser, proving that we can now perform RDF inference in the client.</p>
    </sec>
    <sec id="sec-5">
      <title>4. Usage</title>
      <p>The reasoner is now distributed with N3.js; making it easily accessible to
JavaScript developers via NPM. Documentation can be found in the README.</p>
    </sec>
    <sec id="sec-6">
      <title>Acknowledgements</title>
      <p>Jesse Wright is funded by the Department of Computer Science, University of
Oxford.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>T.</given-names>
            <surname>Berners-Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Hendler</surname>
          </string-name>
          ,
          <string-name>
            <surname>O. Lassila,</surname>
          </string-name>
          <article-title>The semantic web</article-title>
          ,
          <source>Scientific american 284</source>
          (
          <year>2001</year>
          )
          <fpage>34</fpage>
          -
          <lpage>43</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>A.</given-names>
            <surname>Khamparia</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Pandey</surname>
          </string-name>
          ,
          <article-title>Comprehensive analysis of semantic web reasoners and tools: a survey</article-title>
          ,
          <source>Education and Information Technologies</source>
          <volume>22</volume>
          (
          <year>2017</year>
          )
          <fpage>3121</fpage>
          -
          <lpage>3145</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>S.</given-names>
            <surname>Capadisli</surname>
          </string-name>
          ,
          <string-name>
            <given-names>T.</given-names>
            <surname>Berners-Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Verborgh</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Kjernsmo</surname>
          </string-name>
          , Solid protocol, https://solidproject. org/TR/2021/protocol-20211217 (
          <year>2021</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>R.</given-names>
            <surname>Verborgh</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Taelman</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Wright</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S. V.</given-names>
            <surname>Braeckel</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Rietveld</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Hurlburt</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Woudt</surname>
          </string-name>
          , T. Bergwinkl, Vincent,
          <string-name>
            <given-names>T.</given-names>
            <surname>Tanon</surname>
          </string-name>
          ,
          <string-name>
            <surname>S. ROZE</surname>
          </string-name>
          ,
          <string-name>
            <given-names>N. D.</given-names>
            <surname>Martin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Smart</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Maillard</surname>
          </string-name>
          , Martin,
          <string-name>
            <given-names>M.</given-names>
            <surname>Fathi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Colpaert</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Heyvaert</surname>
          </string-name>
          , Ruben, Shawn, W. Turner, alxflam, elf Pavlik,
          <string-name>
            <given-names>L.</given-names>
            <surname>Roy</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. D.</given-names>
            <surname>Smet</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Scazzosi</surname>
          </string-name>
          , I. Aaronsohn,
          <string-name>
            <given-names>H.</given-names>
            <surname>Zuo</surname>
          </string-name>
          , G. Middell,
          <source>rdfjs/n3.js: v1.17.3</source>
          ,
          <year>2024</year>
          . URL: https://doi.org/10.5281/zenodo.10866356. doi:
          <volume>10</volume>
          .5281/zenodo.10866356.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>D.</given-names>
            <surname>Vrandečić</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Krötzsch</surname>
          </string-name>
          ,
          <article-title>Wikidata: a free collaborative knowledgebase</article-title>
          ,
          <source>Communications of the ACM</source>
          <volume>57</volume>
          (
          <year>2014</year>
          )
          <fpage>78</fpage>
          -
          <lpage>85</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>T.</given-names>
            <surname>Bergwinkl</surname>
          </string-name>
          , M. Luggen, elf
          <string-name>
            <surname>Pavlik</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          <string-name>
            <surname>Regalia</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          <string-name>
            <surname>Savastano</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          <string-name>
            <surname>Verborgh</surname>
          </string-name>
          , RDF/JS:
          <article-title>Data model specification</article-title>
          ,
          <source>W3C Community Group Draft Report, W3C</source>
          ,
          <year>2023</year>
          . http://rdf.js.org
          <article-title>/ data-model-spec/.</article-title>
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>T.</given-names>
            <surname>Bergwinkl</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Regalia</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Felder</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Taelman</surname>
          </string-name>
          ,
          <source>RDF/JS: Dataset specification 1.0, W3C Community Group Final Report, W3C</source>
          ,
          <year>2019</year>
          . https://rdf.js.org/dataset-spec/.
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>B. N.</given-names>
            <surname>Grosof</surname>
          </string-name>
          ,
          <string-name>
            <surname>I. Horrocks</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Volz</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Decker</surname>
          </string-name>
          ,
          <article-title>Description logic programs: Combining logic programs with description logic</article-title>
          ,
          <source>in: Proceedings of the 12th international conference on World Wide Web</source>
          ,
          <year>2003</year>
          , pp.
          <fpage>48</fpage>
          -
          <lpage>57</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>S.</given-names>
            <surname>Abiteboul</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Hull</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Vianu</surname>
          </string-name>
          , Foundations of databases, volume
          <volume>8</volume>
          ,
          <string-name>
            <surname>Addison-Wesley</surname>
            <given-names>Reading</given-names>
          </string-name>
          ,
          <year>1995</year>
          , pp.
          <fpage>312</fpage>
          -
          <lpage>316</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>J.</given-names>
            <surname>Wright</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. D.</given-names>
            <surname>Roo</surname>
          </string-name>
          ,
          <string-name>
            <surname>I. Smessaert</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Hochstenbach</surname>
          </string-name>
          , W. Slabbinck,
          <source>eyereasoner/eyejs: v16.22.0</source>
          ,
          <year>2024</year>
          . URL: https://doi.org/10.5281/zenodo.13632329. doi:
          <volume>10</volume>
          .5281/zenodo. 13632329.
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>R.</given-names>
            <surname>Verborgh</surname>
          </string-name>
          , J. De Roo,
          <article-title>Drawing conclusions from linked data on the web: The eye reasoner</article-title>
          ,
          <source>IEEE Software 32</source>
          (
          <year>2015</year>
          )
          <fpage>23</fpage>
          -
          <lpage>27</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [12]
          <string-name>
            <given-names>M.</given-names>
            <surname>Terdjimi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Médini</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Mrissa</surname>
          </string-name>
          , Hylar:
          <article-title>Hybrid location-agnostic reasoning</article-title>
          ,
          <source>in: ESWC Developers Workshop</source>
          <year>2015</year>
          ,
          <year>2015</year>
          , p.
          <fpage>1</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>D.</given-names>
            <surname>Brickley</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Miller</surname>
          </string-name>
          ,
          <source>Foaf vocabulary specification 0</source>
          .91,
          <year>2007</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>F. F.-H.</given-names>
            <surname>Nah</surname>
          </string-name>
          ,
          <article-title>A study on tolerable waiting time: how long are web users willing to wait?</article-title>
          ,
          <source>Behaviour &amp; Information Technology</source>
          <volume>23</volume>
          (
          <year>2004</year>
          )
          <fpage>153</fpage>
          -
          <lpage>163</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>