#url /api/v1
#type api
#base http://localhost:8081
#method get
#title OpenCitations Meta + Index (RAMOSE federation demo)
#description Minimal RAMOSE API over OpenCitations endpoints.
#version 1.0.0
#contacts arcangelo.massari@unibo.it
#license ISC
#endpoint https://sparql.opencitations.net/meta

#url /articles/{doi}
#type operation
#doi str(.+)
#method get
#description Given a DOI, return its title from OpenCitations Meta.
#call /articles/10.1007/s11192-022-04367-w
#field_type str(doi) str(title)
#output_json [
    {
        "doi": "10.1007/s11192-022-04367-w",
        "title": "Identifying And Correcting Invalid Citations Due To DOI Errors In Crossref Data"
    }
]
#sparql PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?doi ?title WHERE {
  VALUES ?doi_value { "[[doi]]" "[[doi]]"^^xsd:string }
  ?id datacite:usesIdentifierScheme datacite:doi ;
      literal:hasLiteralValue ?doi_value .
  ?br datacite:hasIdentifier ?id ;
      dcterms:title ?title .
  BIND("[[doi]]" AS ?doi)
}

#url /citations/{doi}
#type operation
#doi str(.+)
#method get
#description Given a DOI, return its title (OpenCitations Meta) and reference count (OpenCitations Index), joined on the OMID.
#call /citations/10.1007/s11192-022-04367-w
#field_type str(doi) str(title) str(br) int(oc_reference_count)
#output_json [
    {
        "doi": "10.1007/s11192-022-04367-w",
        "title": "Identifying And Correcting Invalid Citations Due To DOI Errors In Crossref Data",
        "br": "https://w3id.org/oc/meta/br/061202127149",
        "oc_reference_count": "30"
    }
]
#sparql PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?doi ?title ?br WHERE {
  VALUES ?doi_value { "[[doi]]" "[[doi]]"^^xsd:string }
  ?id datacite:usesIdentifierScheme datacite:doi ;
      literal:hasLiteralValue ?doi_value .
  ?br datacite:hasIdentifier ?id ;
      dcterms:title ?title .
  BIND("[[doi]]" AS ?doi)
}

@@join ?br ?br type=left
@@with endpoint=https://sparql.opencitations.net/index
@@foreach ?br item wait=0
PREFIX cito: <http://purl.org/spar/cito/>

SELECT ?br (COUNT(?cited) AS ?oc_reference_count) WHERE {
  BIND(<[[item]]> AS ?br)
  ?cit a cito:Citation ;
       cito:hasCitingEntity ?br ;
       cito:hasCitedEntity  ?cited .
}
GROUP BY ?br

#url /references/{doi}
#type operation
#doi str(.+)
#method get
#description Given a DOI, resolve its OMID in OpenCitations Meta and list its references from OpenCitations Index, one cited resource per row.
#call /references/10.1007/s11192-022-04367-w
#field_type str(doi) str(br) str(cited)
#output_json [
    {
        "doi": "10.1007/s11192-022-04367-w",
        "br": "https://w3id.org/oc/meta/br/061202127149",
        "cited": "https://w3id.org/oc/meta/br/060504627"
    }
]
#sparql PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?doi ?br WHERE {
  VALUES ?doi_value { "[[doi]]" "[[doi]]"^^xsd:string }
  ?id datacite:usesIdentifierScheme datacite:doi ;
      literal:hasLiteralValue ?doi_value .
  ?br datacite:hasIdentifier ?id .
  BIND("[[doi]]" AS ?doi)
}

@@join ?br ?br type=left
@@with endpoint=https://sparql.opencitations.net/index
@@foreach ?br item wait=0
PREFIX cito: <http://purl.org/spar/cito/>

SELECT ?br ?cited WHERE {
  BIND(<[[item]]> AS ?br)
  ?cit a cito:Citation ;
       cito:hasCitingEntity ?br ;
       cito:hasCitedEntity  ?cited .
}

#url /protected/{doi}
#type operation
#doi str(.+)
#method get
#auth required
#description Given a DOI, return its title from OpenCitations Meta. Protected by a bearer token, to showcase authentication.
#call /protected/10.1007/s11192-022-04367-w
#field_type str(doi) str(title)
#output_json [
    {
        "doi": "10.1007/s11192-022-04367-w",
        "title": "Identifying And Correcting Invalid Citations Due To DOI Errors In Crossref Data"
    }
]
#sparql PREFIX datacite: <http://purl.org/spar/datacite/>
PREFIX literal: <http://www.essepuntato.it/2010/06/literalreification/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?doi ?title WHERE {
  VALUES ?doi_value { "[[doi]]" "[[doi]]"^^xsd:string }
  ?id datacite:usesIdentifierScheme datacite:doi ;
      literal:hasLiteralValue ?doi_value .
  ?br datacite:hasIdentifier ?id ;
      dcterms:title ?title .
  BIND("[[doi]]" AS ?doi)
}

#url /non-rdf/{doi}
#type operation
#doi str(.+)
#method get
#description Given a DOI, return the same article read from a non-RDF CSV file via SPARQL Anything, showing RAMOSE exposing a REST API over a non-RDF source.
#call /non-rdf/10.1007/s11192-022-04367-w
#field_type str(doi) str(title) str(venue)
#output_json [
    {
        "doi": "10.1007/s11192-022-04367-w",
        "title": "Identifying And Correcting Invalid Citations Due To DOI Errors In Crossref Data",
        "venue": "Scientometrics"
    }
]
#sparql @@with engine=sparql-anything
PREFIX fx:  <http://sparql.xyz/facade-x/ns/>
PREFIX xyz: <http://sparql.xyz/facade-x/data/>

SELECT ?doi ?title ?venue WHERE {
  SERVICE <x-sparql-anything:media-type=text/csv,csv.headers=true,location=file:///app/articles.csv> {
    ?row xyz:doi ?doi ;
         xyz:title ?title ;
         xyz:venue ?venue .
  }
  FILTER(?doi = "[[doi]]")
}
