-1

I'm trying to create a SPARQL query to construct or insert graphs, following the BIBFRAME 2.0 Model, using a personal database with a lot of datas. I want to get a result like this:

Subject a bf:Topic, madsrdf:ComplexSubject ;
rdfs:label "Subject" ;
madsrdf:componentList [ a madsrdf:Topic ;
            madsrdf:authoritativeLabel "FirstSubject" ] ;

But I do not know how to do it in SPARQL. I tryed with this query, but I always get a lot of blank nodes (as much as registers with empty "?Subject" fields I have in my database):

PREFIX bf: <http://id.loc.gov/ontologies/bibframe/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix madsrdf: <http://www.loc.gov/mads/rdf/v1#>

CONSTRUCT{

    ?subject a bf:Topic, madsrdf:ComplexSubject ;
        rdfs:label ?subject;
        madsrdf:componentList [ a madsrdf:Topic ;
                madsrdf:authoritativeLabel ?firstsubject ];

} where{ service <http://localhost:.......> {
        ?registerRow a <urn:Row> ;
        OPTIONAL{?registerRow <urn:col:Subject> ?subject ;}
        OPTIONAL{?registerRow <urn:col:FirstSubject> ?firstsubject ;}
                     }
}
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Wences
  • 71
  • 8
  • The first part of the CONSTRUCT query is done for each RDF subgraph that matches the pattern in the WHERE part. So sure, it will be done for each row. – UninformedUser May 12 '17 at 00:17
  • And in the `OPTIONAL` parts, you use a different variable `?registroRow` instead of `?registerRow`, thus it's not connected, thus, cross-product with a lots of entries. – UninformedUser May 12 '17 at 00:19
  • Sorry, both of them are `?registerRow`. The problem keeps. – Wences May 12 '17 at 08:12
  • What do you mean by problem? Can you show some sample data + the corresponding result. This would make the understanding of the problem easier - at least for me :D – UninformedUser May 12 '17 at 08:16
  • I have a database in http://localhost:....... with a lot of columns (two of them are Subject and FirstSubject) and 1200 rows, however in Subject and FirstSubject there are only 4 rows with values and 1196 empty rows (for this reason they appear in `OPTIONAL`). When i execute my query in SPARQL i get the graphs i want it, relative to the 4 rows with values, but also there a lot of graphs with blank nodes from the empty rows. – Wences May 12 '17 at 08:42
  • Then why do you use `OPTIONAL` patterns? Have you tried regular patterns instead? Or your backend returns blank nodes as `?subject`'s? Then you have to use `FILTER isBlank(?subject)`. – Stanislav Kralin May 13 '17 at 07:06
  • @Wences, excuse me, I mean `FILTER (! isBlank(?subject))`. – Stanislav Kralin May 13 '17 at 20:01
  • Also, `?subject rdfs:label ?subject` is strange. – Stanislav Kralin May 13 '17 at 20:47
  • I use`OPTIONAL` because there are more lines in the original query. If i run this query without `OPTIONAL` y get the graphs i want it, but when i use `OPTIONAL` not, and i need it. The problem is in `madsrdf:componentList [ a madsrdf:Topic` but i don't know how solve it. – Wences May 14 '17 at 16:27
  • @Wences, are you using GraphDB? Are your another problem is similar to http://stackoverflow.com/questions/43497670/sparql-construct-does-implicit-subject-object-influence-the-result ? – Stanislav Kralin May 14 '17 at 16:57
  • Yes, i'm using GraphDB 8.1, but my problem isn't the same. – Wences May 14 '17 at 17:50

1 Answers1

0

@Wences, AKSW answered you, please read more carefully. You don't use ?registerRow in the CONSTRUCT part, that's why it is executed once for each row.

Vladimir Alexiev
  • 2,477
  • 1
  • 20
  • 31