PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl:  <http://www.w3.org/2002/07/owl#>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rpubl: <http://rinfo.lagrummet.se/ns/2008/11/rinfo/publ#>

CONSTRUCT {

    ?s ?p ?o .
    ?current ?curr_p ?curr_o .

    ?relitem ?relprop ?relobj .

    ?reldoc rpubl:paragraf ?relitem .
    ?relchapter rpubl:paragraf ?relitem .
    ?reldoc rpubl:kapitel ?relchapter .
    ?reldoc a ?reldoctype;
        dct:identifier ?reldoc_label .


    ?revdoc a ?revdoctype;
        dct:identifier ?revdoc_id .

    ?revitem a ?revtype;
        dct:publisher ?revpublisher;
        ?rev ?current;
        ?revprop ?revtext .

    ?revrevitem a ?revrevtype;
        ?revrev ?revitem;
        ?revrevprop ?revrevtext .

    ?revdoc rpubl:paragraf ?revitem .
    ?revchapter rpubl:paragraf ?revitem .
    ?revdoc rpubl:kapitel ?revchapter .


    ?inrefitem a rpubl:Forfattningsreferens;
        rpubl:angerGrundforfattning ?current;
        ?refprop ?reftext .

    ?revdoc ?refRev ?inrefitem .
    ?revdoc rpubl:kapitel ?revchapter .
    ?revchapter rpubl:paragraf ?revpara .
    ?revdoc rpubl:paragraf ?revpara .
    ?revpara ?paraRefRev ?inrefitem .

} WHERE {

    # NOTE: if support for sameAs of documents is needed, this will work
    # (provided that all occurrences of ?current in the construct above this
    # and the query below this are replaced with ?actual).
    #OPTIONAL { ?actual owl:sameAs ?current }
    #FILTER(BOUND(?actual) || ?actual = ?current)

    {

        ?g foaf:primaryTopic ?current .
        GRAPH ?g { ?s ?p ?o }

    } UNION {

        ?current ?curr_p ?curr_o .

    } UNION {

        ?current ?rel ?relitem .
        ?relitem ?relprop ?relobj .
        FILTER(?relprop in (rdf:type, dct:publisher) || isLiteral(?relobj))

        # IMPROVE: generalize...
        # NOTE: this part is a bit slow:
        OPTIONAL {
            {
                ?reldoc rpubl:paragraf ?relitem .
            } UNION {
                ?relchapter rpubl:paragraf ?relitem .
                ?reldoc rpubl:kapitel ?relchapter .
            # TODO: currect construct for this
            #} UNION {
            #    ?relitem a rpubl:Forfattningsreferens;
            #        rpubl:angerGrundforfattning ?reldoc .
            }
            ?reldoc a ?reldoctype;
                dct:identifier ?reldoc_label .
        }

    } UNION {

        # Skip if ?current is of a ?type known to have lots of revs
        ?current a ?type .
        FILTER NOT EXISTS {
            ?type rdfs:subClassOf* ?basetype .
            FILTER(?basetype in (foaf:Organization, bibo:Collection))
        }

        ?revitem ?rev ?current .

        {
            ?revitem ?revprop ?revtext .
            FILTER(isLiteral(?revtext))
            OPTIONAL { ?revitem a ?revtype . }
            OPTIONAL { ?revitem dct:publisher ?revpublisher . }
        } UNION {
            ?revrevitem ?revrev ?revitem;
                ?revrevprop ?revrevtext .
            FILTER (?revrev = rpubl:referatAvDomstolsavgorande &&
                    isLiteral(?revrevtext) )
            OPTIONAL { ?revrevitem a ?revrevtype . }
        }

        # IMPROVE: generalize to follow any revitems upward to "containers" and
        # describe those.
        # NOTE: this part is a bit slow:
        OPTIONAL {
            {
                ?revdoc rpubl:paragraf ?revitem .
            } UNION {
                ?revchapter rpubl:paragraf ?revitem .
                ?revdoc rpubl:kapitel ?revchapter .
            }
            ?revdoc a ?revdoctype;
                dct:identifier ?revdoc_label .
        }

    } UNION {

        ?inrefitem a rpubl:Forfattningsreferens;
            rpubl:angerGrundforfattning ?current;
            ?refprop ?reftext .
        {
            ?revdoc rpubl:kapitel ?revchapter .
            ?revchapter rpubl:paragraf ?revpara .
            ?revpara ?paraRefRev ?inrefitem .
        } UNION {
            ?revdoc rpubl:paragraf ?revpara .
            ?revpara ?paraRefRev ?inrefitem .
        } UNION {
            ?revdoc ?refRev ?inrefitem .
        }

        ?revdoc a ?revdoctype;
            dct:identifier ?revdoc_id .

    }

}