Search code examples
rdfsemantic-webinferencen3eulersharp

n3 inferencing applying str:contains to xsd:string in EulerSharp


I have a dataset that contains a large number of resources I need to reconcile against various existing data. The most straight forward approach is to do some simple string comparisons between various literals.

Unfortunately the literals are typed xsd:string, and the EulerSharp builtin str:contains isn't working on typed literals.

I have read everything I can find on the eulersharp builtins and the closest I could come is to try to cast the xsd:string to a PlainLiteral using the rdf:PlainLiteral predicate from RIF; however, this does not appear to be supported by EulerSharp.

How can I manipulate and compare Literals of type xsd:string?

Or, should I resort to preprocessing the data to strip off the datatypes?

The following .n3 file demonstrates the problem:

@prefix : <http://local#> .

@prefix str: <http://www.w3.org/2000/10/swap/string#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Bob :name "Bob Smith" .
:Rob :name "Rob Smith"^^xsd:string .

{ ?P :name ?N .
  ?N str:contains "Smith" } => { ?P :bingo ?N } .

And the result of running the above file:

$ eye --swipl test.n3 --pass --nope
Id: euler.yap 5974 2013-02-12 00:29:00Z josd
SWI-Prolog 5.10.4 (amd64): Dec 27 2011, 08:54:16
starting 80 [msec cputime] 78 [msec walltime]
GET file:///home/ubuntu/src/test/test.n3 SC=4
networking 0 [msec cputime] 2 [msec walltime]
#Processed by Id: euler.yap 5974 2013-02-12 00:29:00Z josd
#eye --swipl test.n3 --pass --nope

@prefix : <http://local#>.
@prefix str: <http://www.w3.org/2000/10/swap/string#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix var: <http://localhost/var#>.
@prefix e: <http://eulersharp.sourceforge.net/2003/03swap/log-rules#>.
@prefix r: <http://www.w3.org/2000/10/swap/reason#>.
@prefix n3: <http://www.w3.org/2004/06/rei#>.

:Bob :name "Bob Smith".
:Rob :name "Rob Smith"^^xsd:string.
:Bob :bingo "Bob Smith".

TC=4 TP=8 BC=0 BP=5 PM=0 CM=0 FM=0 AM=0
reasoning 0 [msec cputime] 4 [msec walltime]

#ENDS 0 [msec] TC=4 TP=8 BC=0 BP=5 PM=0 CM=0 FM=0 AM=0

Solution

  • I asked on the EulerSharp list and received this reply:

    You can get the literal from a datatyped literal via log:dtlit.

    The extra rule would make it work.

    Not that instead of ?dt you could also use xsd:string

    @prefix : <http://local#> .
    
    @prefix str: <http://www.w3.org/2000/10/swap/string#> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix log: <http://www.w3.org/2000/10/swap/log#>.
    
    
    :Bob :name "Bob Smith" .
    :Rob :name "Rob Smith"^^xsd:string .
    
    { ?P :name ?N .
      ?N str:contains "Smith" } => { ?P :bingo ?N } .
    
    { ?P :name ?N .
      (?lit ?dt) log:dtlit ?N.
      ?lit str:contains "Smith" } => { ?P :bingo ?N } .
    

    Running this gives the desired result:

    $ eye --swipl test.n3 --pass --nope
    Id: euler.yap 5974 2013-02-12 00:29:00Z josd
    SWI-Prolog 5.10.4 (amd64): Dec 27 2011, 08:54:16
    starting 50 [msec cputime] 62 [msec walltime]
    #Processed by Id: euler.yap 5974 2013-02-12 00:29:00Z josd
    #eye --swipl test.n3 --pass --nope
    
    GET file:///home/ubuntu/src/test/test.n3 SC=4
    networking 10 [msec cputime] 2 [msec walltime]
    @prefix : <http://local#>.
    @prefix str: <http://www.w3.org/2000/10/swap/string#>.
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
    @prefix log: <http://www.w3.org/2000/10/swap/log#>.
    @prefix var: <http://localhost/var#>.
    @prefix e: <http://eulersharp.sourceforge.net/2003/03swap/log-rules#>.
    @prefix r: <http://www.w3.org/2000/10/swap/reason#>.
    @prefix n3: <http://www.w3.org/2004/06/rei#>.
    
    :Bob :name "Bob Smith".
    :Rob :name "Rob Smith"^^xsd:string.
    :Bob :bingo "Bob Smith".
    :Rob :bingo "Rob Smith"^^xsd:string.
    
    #ENDS 0 [msec] TC=6 TP=12 BC=0 BP=7 PM=0 CM=0 FM=0 AM=0
    
    TC=6 TP=12 BC=0 BP=7 PM=0 CM=0 FM=0 AM=0
    reasoning 0 [msec cputime] 2 [msec walltime]