diff --git a/lib/ontologies_linked_data/models/resource.rb b/lib/ontologies_linked_data/models/resource.rb index 9bccc785..6caf601e 100644 --- a/lib/ontologies_linked_data/models/resource.rb +++ b/lib/ontologies_linked_data/models/resource.rb @@ -177,6 +177,7 @@ def get_type(value) end def namespace_predicate(property_url) + return nil if property_url.is_a?(RDF::Literal) || !URI.regexp.match?(property_url) regex = /^(?.*[\/#])(?[^\/#]+)$/ match = regex.match(property_url.to_s) [match[:namespace], match[:id]] if match diff --git a/test/models/test_resource.rb b/test/models/test_resource.rb index b409ddb1..63d51f66 100644 --- a/test/models/test_resource.rb +++ b/test/models/test_resource.rb @@ -16,6 +16,11 @@ def self.before_suite . _:blanknode1 . _:blanknode2 . + "test/bonjour"@fr . + "Person#human"@en . + "2024-11-29"^^ . + "http://person1.org/test" . + _:blanknode1 "Jane Smith" . _:blanknode1 "25"^^ . _:blanknode1 "female" . @@ -72,6 +77,8 @@ def test_generate_model if property.to_sym == :knows assert_equal hash_value.map{|x| x.is_a?(Hash) ? x.values : x}.flatten.map(&:to_s).sort, object_value.map{|x| x.is_a?(String) ? x : x.to_h.values}.flatten.map(&:to_s).sort + elsif property.to_sym == :birthday + assert_equal Array(hash_value).map(&:object), Array(object_value) else assert_equal Array(hash_value).map(&:to_s), Array(object_value).map(&:to_s) end @@ -94,20 +101,24 @@ def test_resource_fetch_related_triples "http://xmlns.com/foaf/0.1/gender" => "male", "http://xmlns.com/foaf/0.1/hasInterest" => %w[Cooking Hiking], "http://xmlns.com/foaf/0.1/age" => "30", + "http://xmlns.com/foaf/0.1/birthday"=>"2024-11-29", "http://xmlns.com/foaf/0.1/email" => "mailto:john@example.com", + "http://www.w3.org/2004/02/skos/core#altLabel" => "test/bonjour", + "http://www.w3.org/2004/02/skos/core#prefLabel" => "Person#human", "http://xmlns.com/foaf/0.1/knows" => ["http://example.org/person3", - { - "http://xmlns.com/foaf/0.1/gender" => "female", - "http://xmlns.com/foaf/0.1/age" => "25", - "http://xmlns.com/foaf/0.1/email" => "mailto:jane@example.com", - "http://xmlns.com/foaf/0.1/name" => "Jane Smith" - }, - { - "http://xmlns.com/foaf/0.1/name" => "Jane Smith 2" - } + { + "http://xmlns.com/foaf/0.1/gender" => "female", + "http://xmlns.com/foaf/0.1/age" => "25", + "http://xmlns.com/foaf/0.1/email" => "mailto:jane@example.com", + "http://xmlns.com/foaf/0.1/name" => "Jane Smith" + }, + { + "http://xmlns.com/foaf/0.1/name" => "Jane Smith 2" + } ], "http://xmlns.com/foaf/0.1/name" => "John Doe", + "http://xmlns.com/foaf/0.1/homepage"=>"http://person1.org/test", "reverse" => { "http://example2.org/person2" => "http://xmlns.com/foaf/0.1/mother", "http://example2.org/person5" => ["http://xmlns.com/foaf/0.1/brother", "http://xmlns.com/foaf/0.1/friend"] @@ -125,26 +136,30 @@ def test_resource_serialization_json refute_empty result expected_result = %( { - "@context": {"ns0": "http://example.org/", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "foaf": "http://xmlns.com/foaf/0.1/", "ns1": "http://example2.org/"}, - "@graph": [ - { - "@id": "ns0:person1", - "@type": "foaf:Person", - "foaf:name": "John Doe", - "foaf:age": {"@type": "http://www.w3.org/2001/XMLSchema#integer", "@value": "30"}, - "foaf:email": {"@id": "mailto:john@example.com"}, - "foaf:gender": "male", - "foaf:hasInterest": ["Cooking", "Hiking"], - "foaf:knows": [{"@id": "ns0:person3"}, {"@id": "_:g445960"}, {"@id": "_:g445980"}] - }, - { - "@id": "_:g445960", + "@context": {"ns0": "http://example.org/", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "foaf": "http://xmlns.com/foaf/0.1/", "ns1": "http://example2.org/", "skos":"http://www.w3.org/2004/02/skos/core#"}, + "@graph": + [ + { + "@id": "ns0:person1", + "@type": "foaf:Person", + "foaf:name": "John Doe", + "foaf:birthday":{"@value":"2024-11-29", "@type":"http://www.w3.org/2001/XMLSchema#date"}, + "foaf:hasInterest":["Cooking", "Hiking"], + "foaf:age": {"@type": "http://www.w3.org/2001/XMLSchema#integer", "@value": "30"}, + "foaf:email": {"@id": "mailto:john@example.com"}, + "foaf:gender": "male", + "skos:altLabel":{"@value":"test/bonjour", "@language":"fr"}, + "skos:prefLabel":{"@value":"Person#human", "@language":"en"}, + "foaf:knows": [{"@id": "ns0:person3"}, {"@id": "_:g447140"}, {"@id": "_:g447160"}], + "foaf:homepage":"http://person1.org/test"}, + { + "@id": "_:g447140", "foaf:name": "Jane Smith", "foaf:age": {"@type": "http://www.w3.org/2001/XMLSchema#integer", "@value": "25"}, "foaf:email": {"@id": "mailto:jane@example.com"}, "foaf:gender": "female" }, - {"@id": "_:g445980", "foaf:name": "Jane Smith 2"}, + {"@id": "_:g447160", "foaf:name": "Jane Smith 2"}, {"@id": "ns1:person5", "foaf:friend": {"@id": "ns0:person1"}, "foaf:brother": {"@id": "ns0:person1"}}, {"@id": "ns1:person2", "foaf:mother": {"@id": "ns0:person1"}} ] @@ -164,16 +179,19 @@ def test_resource_serialization_xml refute_empty result expected_result = %( - + + 2024-11-29 male Cooking Hiking 30 + test/bonjour + Person#human - + female 25 @@ -181,11 +199,12 @@ def test_resource_serialization_xml - + Jane Smith 2 John Doe + http://person1.org/test @@ -194,7 +213,7 @@ def test_resource_serialization_xml - + ) a = result.gsub(' ', '').gsub(/rdf:nodeID="[^"]*"/, '').split("\n").reject(&:empty?) b = expected_result.gsub(' ', '').gsub(/rdf:nodeID="[^"]*"/, '').split("\n").reject(&:empty?) @@ -208,24 +227,28 @@ def test_resource_serialization_ntriples refute_empty result expected_result = %( - . - "male" . - "Cooking" . - "Hiking" . - "30"^^ . - . - . - _:g445940 "female" . - _:g445940 "25"^^ . - _:g445940 . - _:g445940 "Jane Smith" . - _:g445940 . - _:g445960 "Jane Smith 2" . - _:g445960 . - "John Doe" . - . - . - . + . + "2024-11-29"^^ . + "male" . + "Cooking" . + "Hiking" . + "30"^^ . + . + "test/bonjour"@fr . + "Person#human"@en . + . + _:g447260 "female" . + _:g447260 "25"^^ . + _:g447260 . + _:g447260 "Jane Smith" . + _:g447260 . + _:g447280 "Jane Smith 2" . + _:g447280 . + "John Doe" . + "http://person1.org/test" . + . + . + . ) a = result.gsub(' ', '').gsub(/_:g\d+/, 'blanke_nodes').split("\n").reject(&:empty?) b = expected_result.gsub(' ', '').gsub(/_:g\d+/, 'blanke_nodes').split("\n").reject(&:empty?) @@ -237,33 +260,38 @@ def test_resource_serialization_turtle result = @@resource1.to_turtle refute_empty result expected_result = %( - @prefix rdf: . - @prefix ns0: . - @prefix foaf: . - @prefix ns1: . - - ns0:person1 - a foaf:Person ; - foaf:age 30 ; - foaf:email ; - foaf:gender "male" ; - foaf:hasInterest "Cooking", "Hiking" ; - foaf:knows ns0:person3, [ - foaf:age 25 ; - foaf:email ; - foaf:gender "female" ; - foaf:name "Jane Smith" - ], [ - foaf:name "Jane Smith 2" - ] ; - foaf:name "John Doe" . - - ns1:person2 - foaf:mother ns0:person1 . - - ns1:person5 - foaf:brother ns0:person1 ; - foaf:friend ns0:person1 . + @prefix rdf: . + @prefix ns0: . + @prefix foaf: . + @prefix skos: . + @prefix ns1: . + + ns0:person1 + a foaf:Person ; + skos:altLabel "test/bonjour"@fr ; + skos:prefLabel "Person#human"@en ; + foaf:age 30 ; + foaf:birthday "2024-11-29"^^ ; + foaf:email ; + foaf:gender "male" ; + foaf:hasInterest "Cooking", "Hiking" ; + foaf:homepage "http://person1.org/test" ; + foaf:knows ns0:person3, [ + foaf:age 25 ; + foaf:email ; + foaf:gender "female" ; + foaf:name "Jane Smith" + ], [ + foaf:name "Jane Smith 2" + ] ; + foaf:name "John Doe" . + + ns1:person2 + foaf:mother ns0:person1 . + + ns1:person5 + foaf:brother ns0:person1 ; + foaf:friend ns0:person1 . ) a = result.gsub(' ', '').split("\n").reject(&:empty?) b = expected_result.gsub(' ', '').split("\n").reject(&:empty?)