How to add a remote namespace to OntClass with Jena - namespaces

I'm new to Jena and the Semantic web.
I'm now making a model having an OntClass "Person" using Jena. This is what I've tried:
OntModel model = ModelFactory.createOntologyModel();
String NS = http://purl.org/ontology#
model.setNsPrefix("Ont", NS);
model.setNsPrefix("foaf", "http://xmlns.com/foaf/0.1#");
OntClass person = model.createClass(NS + "Person");
person.setSameAs(FOAF.Person);
person.addProperty(FOAF.account, "account");
But it doesn't seem working because when generating the Turtle file, I receive:
Ont:Person a owl:Class ;
owl:sameAs <http://xmlns.com/foaf/0.1/Person> ;
<http://xmlns.com/foaf/0.1/account>
"account" .
instead of having:
#prefix foaf: <http://xmlns.com/foaf/spec/#> .
Ont:Person a owl:Class ;
owl:sameAs foaf:Person ;
foaf:account "account" .
So what is the right way to link a OntClass in OntModel to a remote namespace ?in this case it's to link my OntClass person to "http://xmlns.com/foaf/0.1#"
Thank you in advance for your help!

Actually I just have to replace:
model.setNsPrefix("foaf", "http://xmlns.com/foaf/0.1#");
with
model.setNsPrefix("foaf", FOAF.getURI());
and it works!

Related

converting csv file to rdf using tarql shows empty results

I m using tarql to convert a csv file to rdf the command runs correctly but i can't find the output (nothing is shown in the windows cmd line and no file is generated)
I m using tarql with windows with the following cmd
C:\tarql-master\target\appassembler\bin\tarql.bat --ntriples xx.rq xx.csv
here is my code
PREFIX dc: <http://dcontology/a#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
CONSTRUCT {
?URI owl:class dc:dataset;
dc:identifier ?identifier;
dc:title ?title;
dc:description ?description;
dc:category ?category;
dc:keywords ?keywords;
dc:PublicationDate ?PublicationDate;
dc:UpdateDate ?UpdateDate;
dc:frequencyofupdate ?frequencyofupdate;
dc:Format ?Format;
dc:License ?license
}
FROM <file:Metabase.csv>
WHERE {
BIND (URI(CONCAT('http://dcontology/dataset/', ?identifier)) AS ?URI)
BIND (xsd:integer(?identifier) AS ?identifier)
BIND (xsd:string(?title) AS ?title)
BIND (xsd:string(?description) AS ?description)
BIND (xsd:string(?category) AS ?category)
BIND (xsd:string(?keywords) AS ?keywords)
BIND (xsd:string(?PublicationDate) AS ?PublicationDate)
BIND (xsd:string(?UpdateDate) AS ?UpdateDate)
BIND (xsd:string(?FrequencyOfUpdate) AS ?FrequencyOfUpdate)
BIND (xsd:string(?format) AS ?format)
BIND (xsd:string(?license) AS ?license)
}`
and here is the csv file header`enter image description here
Case sensitivity
The matching of CSV column names and SPARQL variables is case-sensitive.
As you have, for example, "Description" as CSV column name, you need ?Description in the WHERE block of your SPARQL query.
PREFIX dc: <http://dcontology/a#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://owlontology/a#> # see bottom of this answer
CONSTRUCT {
?URI
owl:class dc:dataset ;
dc:identifier ?identifier_int ;
dc:title ?title_str ;
dc:description ?description_str ;
dc:category ?category_str ;
dc:keywords ?keywords_str ;
dc:PublicationDate ?publicationDate_str ;
dc:UpdateDate ?updateDate_str ;
dc:frequencyofupdate ?frequencyOfUpdate_str ;
dc:Format ?format_str ;
dc:License ?license_str .
}
#FROM <file:Metabase.csv>
WHERE {
BIND ( URI(CONCAT( 'http://dcontology/dataset/', ?Identifier )) AS ?URI ) .
BIND ( xsd:integer(?Identifier) AS ?identifier_int ) .
BIND ( xsd:string(?Title) AS ?title_str ) .
BIND ( xsd:string(?Description) AS ?description_str ) .
BIND ( xsd:string(?Category) AS ?category_str ) .
BIND ( xsd:string(?Keywords) AS ?keywords_str ) .
BIND ( xsd:string(?PublicationDate) AS ?publicationDate_str ) .
BIND ( xsd:string(?UpdateDate) AS ?updateDate_str ) .
BIND ( xsd:string(?FrequencyOfUpdate) AS ?frequencyOfUpdate_str ) .
BIND ( xsd:string(?Format) AS ?format_str ) .
BIND ( xsd:string(?License) AS ?license_str ) .
}
Error
If tarql doesn’t find anything to convert (e.g., because the correct column names are not included in the query), it gives no output instead of an error.
Your query, however, should give an error, because you don’t have the owl prefix defined:
Error parsing SPARQL query: Line 5, column 8: Unresolved prefixed name: owl:class
Vocabularies
If you meant to use the OWL ontology, note that there is no owl:class property defined. If you want to say that the ?URI entity belongs to a class, you can use the rdf:type property (or shorthand: a).
dc is the common prefix for DCMI Metadata Terms, which doesn’t define dc:category, dc:keywords, dc:PublicationDate, dc:UpdateDate, or dc:frequencyofupdate.
dc:Format and dc:License only exist as lowercase variants, and dc:dataset only exists as uppercase variant (by convention, lowercase terms refer to properties, and uppercase terms refer to classes).
In case you don’t use the DCMI Metadata Terms vocabulary, it’s a good practice to use a different prefix than dc, because it’s such a well-known vocabulary/prefix.

How to create a SHACL rule to infer rdf:type from rdfs:subClassOf

In order to validate my RDF graph against my SHACL validation shapes V, I want to infer some triples to keep my shapes simple. In particular, one of the rule I need to implement is (in pseudo code):
(?s, rdf:type, :X) <-- (?s, rdfs:subClassOf, :Y)
I was trying several implementations, ending up with this triple rule (and its variants):
#prefix sh: <http://www.w3.org/ns/shacl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix : <http://example.com/ex#> .
:s
a sh:NodeShape ;
sh:targetClass rdfs:Resource ;
sh:rule [
a sh:TripleRule ;
sh:subject sh:this ;
sh:predicate rdf:type ;
sh:object :X ;
sh:condition [ sh:property [ sh:path rdfs:subClassOf ;
sh:hasValue :Y ] ]
] .
However the rule does not infer :A rdf:type :X . for data graph
:A rdfs:subClassOf :Y .
(Executing against https://github.com/TopQuadrant/shacl). It is possible to solve this issue with a SPARQL rule, so my question is whether there is an option to do it through Triple rule as well. Thanks for hints!
Why don't you keep the inference rules and the validation separate, as you've noted is possible using SHACL + SPARQL, as this will keep things simpler?
You could use pySHACL and put rules into an ontology file since pySHACL can run ontology rules/inference before applying SHACL validators (see the -i and -e options).
Given the "MAY" in following quote, the advice in previous answer by #NicholasCar is solid IMO.
Purpose of answering here, is just to corroborate and expand with recent experience.
The 2017 W3C SHACL docs regarding Relationship between SHACL and RDFS inferencing:
SHACL implementations MAY, but are not required to, support entailment
regimes. If a shapes graph contains any triple with the predicate
sh:entailment and object E and the SHACL processor does not support E
as an entailment regime for the given data graph then the processor
MUST signal a failure.
(AFAICT the phrase "entailment regime" only refers to SPARQL as standard)
Looking at the section on Property Paths:
SPARQL Property path: rdf:type/rdfs:subClassOf*
SHACL Property path: (rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ] )
In most of the SHACL implementations I've played with basic rdfs type entailment works (obv IFF the rdf:type/rdfs:subClassOf* path is visible to the SHACL validator), so (rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ]) isn't needed explicitly.
The problem comes when you try to stuff advanced paths into the shapes - e.g. following this example to enforce graph contains at least one instance of an abstract type:
sh:path [ sh:inversePath ( rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ] ) ] ;
... isn't working for me in a number of SHACL validation implementations.

SPARQL construct/insert query and blank nodes

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 ;}
}
}
#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.

How to express "all members of containers of class C must be of class M" in rdfs?

I have these triples (expressed in turtle):
:C rdf:subClassOf rdfs:Container.
:M a rdfs:Class.
How do i specify that only instances of :M can be members of :C? I looked through this, but couldn't find the answer.
You can't express this with an RDFS ontology (that is, as an RDF graph interpreted according to the RDFS entailment regime). You can't express this with an OWL DL ontology (that is, an OWL ontology interpreted according to the OWL direct semantics). However, it can be expressed with OWL Full (that is, as an RDF graph interpreted according to the OWL RDF-based semantics). In Turtle:
[
a owl:Restriction;
owl:onProperty rdfs:member;
owl:someValuesFrom :C
]
rdfs:subClassOf :M .
If you wan't to make it compatible with OWL DL, you must not use RDF containers but you can make your own class of containers:
:Container a owl:Class .
:C rdfs:subClassOf :Container .
:M a owl:Class .
:member a owl:ObjectProperty .
[
a owl:Restriction;
owl:onProperty :member;
owl:someValuesFrom :C
]
rdfs:subClassOf :M .

How do I replace entity references with character references?

I'm looking for a way in Ruby or Rails to replace entity references ( ) in a file with their character reference equivalents ( ).
is the main offender, but I'd like to do the replacement systematically rather than just hand coding a bunch of gsubs.
You can use the HtmlEntities gem:
gem install htmlentieties
require 'htmlentities'
decoded = HTMLEntities.new.decode ' Hello'
decoded[0].ord #=> 160
As Stefan mentioned in the comment, if you want to encode it back using reference numbers, just decode the string and encode it with the :decimal flag:
require 'htmlentities'
text = ' Hello'
coder = HTMLEntities.new
final_text = coder.encode coder.decode(' Hello'), :decimal
p final_text #=>  Hello
"Max Williams".html_safe => "Max Williams"
This is functionality of Rails's Active Support.