0

My query returns result dependent on which one of two sparql OPTIONS clauses come first for a List of Images. Although using Jena ARQ is not an option at this point, and I'd like to solve this with a pure SPARQL query, still I'd like to know how it could be solved with Jena as well. My data presentation is attached below, the data may contain a list of images. My image representation is also below. I'm attaching my query as well. The sprql query has two variables urlX, and urlY declared in the 2 OPTIONS blocks, if a List of images exists. Depending on which of the OPTIONS comes first, I get the value for that one variable, while the other one doesn't get reached. It seems the issue has to do with using OPTIONS clause. I'm not sure what else I can try instead, I'm far from being an expert on sparql queries. I want the query to do the following: if a collection of images is present, I want to see if both image sizes (dc:conformsTo) are present and get both urlX and urlY values, or get the ones that exist. Much appreciate your time.

My data representation:

@prefix dc: <http://purl.org/dc/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix lews: <http://lews.com/content/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

lews:26331340 lews:name "the Human Good Luck Charm is Back"^^xsd:token ;
        dc:created "2014-10-20T17:14:55.357-07:00"^^xsd:dateTime ;
        dc:identifier "26331340"^^xsd:int ;
        dc:modified "2016-08-04T13:43:00.897-07:00"^^xsd:dateTime ;
        dc:title "the Human Good Luck Charm is Back" ;
        dc:hasPart <http://lews.com/content/26331340#Images> ;
        dc:abstract "As the World Series gets underway..." ;
        dc:description "The super fan who rooted for the Royals is back to boost morale." ;
        dc:subject "hoping for a World Series victory".

<http://lews.com/content/26331340#Images> dc:identifier "Images"^^xsd:token ;
        rdf:first lews:26331375 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 ;
        rdf:li <http://lews.com/content/26331340#Images> , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 ;
        a rdf:List .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 rdf:first lews:26331376 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 rdf:first lews:26331377 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 rdf:first lews:26331378 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 rdf:first lews:26331379 ;
        rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 .

_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 rdf:first lews:26331380 ;
        rdf:rest rdf:nil .

My image representation:

@prefix dc: <http://purl.org/dc/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix lews: <http://abcnews.com/content/> .
@prefix mrss: <http://search.yahoo.com/mrss/> .

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

lews:26331376 lews:name "141020_wn_donvan0_704x396.jpg"^^xsd:token ;
        lews:section "wnt"^^xsd:token ;
        lews:type "Image"^^xsd:token ;
        dc:conformsTo "704x396"^^xsd:token ;
        dc:created "2014-10-20T17:15:09.637-07:00"^^xsd:dateTime ;
        dc:hasFormat <http://lews.go.com/images/WNT/141020_wn_donvan0_704x396.jpg> ;
        dc:identifier "26331376"^^xsd:int ;
        dc:isPartOf <http://lews.go.com/WNT> ;
        dc:modified "2014-10-20T17:15:09.947-07:00"^^xsd:dateTime ;
        dc:type "StillImage"^^xsd:token ;
        mrss:height "396"^^xsd:int ;
        mrss:width "704"^^xsd:int ;
        xsd:date "2014-10-20"^^xsd:date ;
        xsd:gMonthDay "--10-20"^^xsd:gMonthDay ;
        xsd:gYear "2014"^^xsd:gYear ;
        xsd:gYearMonth "2014-10"^^xsd:gYearMonth .

My query:

PREFIX dc:     <http://purl.org/dc/terms/>
PREFIX mrss:   <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName
    ?subject dc:identifier ?id.

    OPTIONAL {?subject dc:title ?title.}
    OPTIONAL {?subject dc:description ?description.}
    OPTIONAL {?subject dc:isPartOf ?section.}

    OPTIONAL {  
                ?subject dc:hasPart ?imageCol.
                ?imageCol dc:identifier "Images"^^xsd:token. 

                OPTIONAL{
                    ?imageCol rdf:li ?bnode.
                    ?bnode rdf:first ?image.
                    ?image lews:name ?imgName;                               
                           dc:conformsTo "4x3";
                           dc:hasFormat ?urlX.                                   
                }        
                OPTIONAL{
                    ?imageCol rdf:li ?bnode.
                    ?bnode rdf:first ?image.
                    ?image lews:name ?imgName;                               
                           dc:conformsTo "16x9";
                           dc:hasFormat ?urlY.                                   
                }        

    }

}
LIMIT ${limit}
user2917629
  • 659
  • 2
  • 12
  • 26
  • 1
    You could use aggregation and group by the data item and then use `SAMPLE` to pick an image sample. But at first, I think you would have to resolve the lists in a sub-select by using the funny SPARQL 1.1 property paths as explained e.g. here: http://stackoverflow.com/questions/17523804/is-it-possible-to-get-the-position-of-an-element-in-an-rdf-collection-in-sparql – UninformedUser Aug 05 '16 at 23:56
  • I took a look at the aggregation, I don't see how it would work with my data. – user2917629 Aug 25 '16 at 16:38

1 Answers1

1

If I understood correctly what it is you want, you just need to group the optionals differently:

PREFIX dc:     <http://purl.org/dc/terms/>
PREFIX mrss:   <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName {
    ?subject dc:identifier ?id.

    OPTIONAL { ?subject dc:title ?title. }
    OPTIONAL { ?subject dc:description ?description. }
    OPTIONAL { ?subject dc:isPartOf ?section. }

    OPTIONAL {
        ?subject dc:hasPart ?imageCol.
        ?imageCol dc:identifier "Images"^^xsd:token.

        OPTIONAL {
            ?imageCol rdf:li ?bnode.
            ?bnode rdf:first ?image.
            ?image lews:name ?imgName;
            # Here we optionally bind ?urlX and/or ?urlY
            OPTIONAL {
                ?image dc:conformsTo "4x3";
                    dc:hasFormat ?urlX.
            }
            OPTIONAL {
                ?image dc:conformsTo "16x9";
                    dc:hasFormat ?urlY.
            }
        }
    }
}
evsheino
  • 1,669
  • 13
  • 18