odrl-ruby 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +16 -16
- data/CHANGELOG.md +5 -5
- data/Gemfile +12 -12
- data/Gemfile.lock +249 -249
- data/LICENSE.txt +21 -21
- data/README.md +128 -127
- data/Rakefile +8 -8
- data/bin/console +15 -15
- data/bin/setup +8 -8
- data/examples/biohackathon-output.ttl +63 -63
- data/examples/build_profile.rb +48 -47
- data/examples/create-biohackathon-offer.rb +44 -44
- data/examples/create-nagoya-es-offer.rb +54 -54
- data/examples/nagoya-output.ttl +66 -66
- data/launch.json +10 -10
- data/lib/odrl/action.rb +106 -106
- data/lib/odrl/asset.rb +77 -77
- data/lib/odrl/base.rb +230 -230
- data/lib/odrl/constraint.rb +110 -110
- data/lib/odrl/odrl/version.rb +7 -7
- data/lib/odrl/party.rb +97 -97
- data/lib/odrl/policy.rb +97 -97
- data/lib/odrl/profile/builder.rb +348 -186
- data/lib/odrl/ruby.rb +26 -26
- data/lib/odrl/rule.rb +116 -116
- metadata +8 -6
data/lib/odrl/asset.rb
CHANGED
@@ -1,77 +1,77 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ODRL
|
4
|
-
# ODRL::Action
|
5
|
-
# Describes an action like "use"
|
6
|
-
#
|
7
|
-
# @author Mark D Wilkinson
|
8
|
-
class Asset < Base
|
9
|
-
attr_accessor :uid, :hasPolicy, :refinements, :partOf
|
10
|
-
|
11
|
-
def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
|
12
|
-
@uid = uid
|
13
|
-
self.uid = Base.baseURI + "#asset_" + Base.getuuid unless @uid
|
14
|
-
super(type: type, uid: @uid, **args)
|
15
|
-
|
16
|
-
@partOf = partOf
|
17
|
-
@hasPolicy = hasPolicy
|
18
|
-
|
19
|
-
if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
|
20
|
-
raise "The policy of an Asset must be of type ODRL::Policy. The provided value will be discarded"
|
21
|
-
@hasPolicy = nil
|
22
|
-
end
|
23
|
-
if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
|
24
|
-
raise "The parent collection of an Asset must be of type ODRL::AssetCollection. The provided value will be discarded"
|
25
|
-
@partOf = nil
|
26
|
-
end
|
27
|
-
|
28
|
-
@refinements = {}
|
29
|
-
refinements = [refinements] unless refinements.is_a? Array
|
30
|
-
return if refinements.first.nil?
|
31
|
-
|
32
|
-
refinements.each do |c|
|
33
|
-
addRefinement(refinement: c)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def addPart(part: args)
|
38
|
-
raise "Asset cannot be added as part of something that is not an asset collection" unless is_a?(AssetCollection)
|
39
|
-
raise "Only Assets can be added as part of asset collections" unless part.is_a?(Asset)
|
40
|
-
|
41
|
-
part.partOf[uid] = [PPARTOF, self]
|
42
|
-
end
|
43
|
-
|
44
|
-
def addRefinement(refinement: args)
|
45
|
-
raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
|
46
|
-
|
47
|
-
refinements[refinement.uid] = [PREFINEMENT, refinement]
|
48
|
-
end
|
49
|
-
|
50
|
-
def load_graph
|
51
|
-
super
|
52
|
-
# TODO: This is bad DRY!! Put the bulk of this method into the base object
|
53
|
-
%i[refinements partOf hasPolicy].each do |connected_object_type|
|
54
|
-
next unless send(connected_object_type)
|
55
|
-
|
56
|
-
send(connected_object_type).each do |_uid, typedconnection|
|
57
|
-
predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
|
58
|
-
object = odrlobject.uid
|
59
|
-
subject = uid
|
60
|
-
repo = repository
|
61
|
-
triplify(subject, predicate, object, repo)
|
62
|
-
odrlobject.load_graph # start the cascade
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def serialize(format:)
|
68
|
-
super
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
class AssetCollection < Asset
|
73
|
-
def initialize(type: CASSETCOLLECTION, **args)
|
74
|
-
super(type: type, **args)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ODRL
|
4
|
+
# ODRL::Action
|
5
|
+
# Describes an action like "use"
|
6
|
+
#
|
7
|
+
# @author Mark D Wilkinson
|
8
|
+
class Asset < Base
|
9
|
+
attr_accessor :uid, :hasPolicy, :refinements, :partOf
|
10
|
+
|
11
|
+
def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
|
12
|
+
@uid = uid
|
13
|
+
self.uid = Base.baseURI + "#asset_" + Base.getuuid unless @uid
|
14
|
+
super(type: type, uid: @uid, **args)
|
15
|
+
|
16
|
+
@partOf = partOf
|
17
|
+
@hasPolicy = hasPolicy
|
18
|
+
|
19
|
+
if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
|
20
|
+
raise "The policy of an Asset must be of type ODRL::Policy. The provided value will be discarded"
|
21
|
+
@hasPolicy = nil
|
22
|
+
end
|
23
|
+
if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
|
24
|
+
raise "The parent collection of an Asset must be of type ODRL::AssetCollection. The provided value will be discarded"
|
25
|
+
@partOf = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
@refinements = {}
|
29
|
+
refinements = [refinements] unless refinements.is_a? Array
|
30
|
+
return if refinements.first.nil?
|
31
|
+
|
32
|
+
refinements.each do |c|
|
33
|
+
addRefinement(refinement: c)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def addPart(part: args)
|
38
|
+
raise "Asset cannot be added as part of something that is not an asset collection" unless is_a?(AssetCollection)
|
39
|
+
raise "Only Assets can be added as part of asset collections" unless part.is_a?(Asset)
|
40
|
+
|
41
|
+
part.partOf[uid] = [PPARTOF, self]
|
42
|
+
end
|
43
|
+
|
44
|
+
def addRefinement(refinement: args)
|
45
|
+
raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
|
46
|
+
|
47
|
+
refinements[refinement.uid] = [PREFINEMENT, refinement]
|
48
|
+
end
|
49
|
+
|
50
|
+
def load_graph
|
51
|
+
super
|
52
|
+
# TODO: This is bad DRY!! Put the bulk of this method into the base object
|
53
|
+
%i[refinements partOf hasPolicy].each do |connected_object_type|
|
54
|
+
next unless send(connected_object_type)
|
55
|
+
|
56
|
+
send(connected_object_type).each do |_uid, typedconnection|
|
57
|
+
predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
|
58
|
+
object = odrlobject.uid
|
59
|
+
subject = uid
|
60
|
+
repo = repository
|
61
|
+
triplify(subject, predicate, object, repo)
|
62
|
+
odrlobject.load_graph # start the cascade
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def serialize(format:)
|
68
|
+
super
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class AssetCollection < Asset
|
73
|
+
def initialize(type: CASSETCOLLECTION, **args)
|
74
|
+
super(type: type, **args)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/odrl/base.rb
CHANGED
@@ -1,230 +1,230 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "odrl/version"
|
4
|
-
require "linkeddata"
|
5
|
-
|
6
|
-
CPOLICY = ODRLV.Policy.to_s
|
7
|
-
|
8
|
-
PPROFILE = ODRLV.profile.to_s
|
9
|
-
|
10
|
-
CSET = ODRLV.Set.to_s
|
11
|
-
COFFER = ODRLV.Offer.to_s
|
12
|
-
CREQUEST = ODRLV.Request.to_s
|
13
|
-
CAGREEMENT = ODRLV.Agreement.to_s
|
14
|
-
CPRIVACY = ODRLV.Privacy.to_s
|
15
|
-
PASSET = ODRLV.target.to_s
|
16
|
-
CASSET = ODRLV.Asset.to_s
|
17
|
-
CASSETCOLLECTION = ODRLV.Asset.to_s
|
18
|
-
|
19
|
-
CRULE = ODRLV.Rule.to_s
|
20
|
-
CPERMISSION = ODRLV.Permission.to_s
|
21
|
-
PPERMISSION = ODRLV.permission.to_s
|
22
|
-
CPROHIBITION = ODRLV.Prohibition.to_s
|
23
|
-
PPROHIBITION = ODRLV.prohibition.to_s
|
24
|
-
PDUTY = ODRLV.obligation.to_s
|
25
|
-
CDUTY = ODRLV.Duty.to_s
|
26
|
-
|
27
|
-
PRULE = ODRLV.Rule.to_s
|
28
|
-
|
29
|
-
PACTION = ODRLV.action.to_s
|
30
|
-
VUSE = ODRLV.use.to_s # this is goofy ODRL stuff...
|
31
|
-
VTRANSFER = ODRLV.transfer.to_s # this is goofy ODRL stuff...
|
32
|
-
CACTION = ODRLV.Action.to_s
|
33
|
-
|
34
|
-
PREFINEMENT = ODRLV.refinement.to_s
|
35
|
-
|
36
|
-
PASSIGNER = ODRLV.assigner.to_s # now in PARTYFUNCTIONS
|
37
|
-
PASSIGNEE = ODRLV.assignee.to_s
|
38
|
-
CPARTY = ODRLV.Party.to_s
|
39
|
-
CPARTYCOLLECTION = ODRLV.Party.to_s
|
40
|
-
|
41
|
-
PCONSTRAINT = ODRLV.constraint.to_s
|
42
|
-
CCONSTRAINT = ODRLV.Constraint.to_s
|
43
|
-
PLEFT = ODRLV.leftOperand.to_s
|
44
|
-
PRIGHT = ODRLV.rightOperand.to_s
|
45
|
-
POPERATOR = ODRLV.operator.to_s
|
46
|
-
POPERANDREFERENCE = ODRLV.rightOperandReference.to_s
|
47
|
-
PDATATYPE = ODRLV.dataType.to_s
|
48
|
-
PUNIT = ODRLV.unit.to_s
|
49
|
-
PSTATUS = ODRLV.status.to_s
|
50
|
-
|
51
|
-
PPARTOF = ODRLV.partOf.to_s
|
52
|
-
|
53
|
-
PROPERTIES = {
|
54
|
-
title: DCT.title,
|
55
|
-
creator: DCT.creator,
|
56
|
-
description: DCT.description,
|
57
|
-
id: DCT.identifier,
|
58
|
-
type: RDF.type,
|
59
|
-
subject: DCT.subject,
|
60
|
-
uid: ODRLV.uid,
|
61
|
-
label: RDFS.label,
|
62
|
-
issued: DCT.issued
|
63
|
-
}
|
64
|
-
|
65
|
-
OPERATORS = %w[eq gt gteq hasPart isA isAllOf isAnyOf isNoneOf isPartOf lt lteq neq]
|
66
|
-
LEFTOPERANDS = %w[absolutePosition absoluteSize absoluteSpatialPosition absoluteTemporalPosition
|
67
|
-
count dateTime delayPeriod deliveryChannel device elapsedTime event
|
68
|
-
fileFormat industry language media meteredTime payAmount percentage
|
69
|
-
product purpose recipient relativePosition relativeSize relativeSpatialPosition
|
70
|
-
relativeTemporalPosition resolution spatial spatialCoordinates system
|
71
|
-
systemDevice timeInterval unitOfCount version virtualLocation]
|
72
|
-
|
73
|
-
PARTYFUNCTIONS = %w[assignee assigner attributedParty attributingParty compensatedParty
|
74
|
-
compensatingParty consentedParty consentingParty contractedParty contractingParty informedParty
|
75
|
-
informingParty trackedParty trackingParty]
|
76
|
-
|
77
|
-
# INTERESTING>>>> THIS COULD BE USED IF THERE IS A PROFILE...
|
78
|
-
# module RemovableConstants
|
79
|
-
# def def_if_not_defined(const, value)
|
80
|
-
# self.class.const_set(const, value) unless self.class.const_defined?(const)
|
81
|
-
# end
|
82
|
-
|
83
|
-
# def redef_without_warning(const, value)
|
84
|
-
# self.class.send(:remove_const, const) if self.class.const_defined?(const)
|
85
|
-
# self.class.const_set(const, value)
|
86
|
-
# end
|
87
|
-
# end
|
88
|
-
|
89
|
-
module ODRL
|
90
|
-
class Base
|
91
|
-
@@repository = RDF::Repository.new
|
92
|
-
|
93
|
-
# If you add an attribute, you mustr also add it to the constructor,
|
94
|
-
# and to the @attribute list
|
95
|
-
# andn to the .load_graph
|
96
|
-
attr_accessor :title, :creator, :description, :subject, :baseURI, :uid, :id, :type, :label, :issued
|
97
|
-
|
98
|
-
def self.baseURI
|
99
|
-
ENV["ODRL_BASEURI"] || "http://example.org"
|
100
|
-
end
|
101
|
-
|
102
|
-
def self.repository
|
103
|
-
@@repository
|
104
|
-
end
|
105
|
-
|
106
|
-
def repository
|
107
|
-
@@repository
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.clear_repository
|
111
|
-
@@repository.clear!
|
112
|
-
true
|
113
|
-
end
|
114
|
-
|
115
|
-
def initialize(
|
116
|
-
uid:, type:, title: nil,
|
117
|
-
creator: nil,
|
118
|
-
description: nil,
|
119
|
-
issued: nil,
|
120
|
-
subject: nil,
|
121
|
-
baseURI: "http://example.org",
|
122
|
-
id: nil,
|
123
|
-
label: nil,
|
124
|
-
**_
|
125
|
-
)
|
126
|
-
|
127
|
-
@title = title
|
128
|
-
@creator = creator
|
129
|
-
@issued = issued
|
130
|
-
@description = description
|
131
|
-
@subject = subject
|
132
|
-
@baseURI = baseURI || ODRL::Base.baseURI
|
133
|
-
@uid = uid
|
134
|
-
@type = type
|
135
|
-
@label = label || @title
|
136
|
-
@id = @uid
|
137
|
-
|
138
|
-
raise "Every object must have a uid - attempt to create #{@type}" unless @uid
|
139
|
-
raise "Every object must have a type - " unless @type
|
140
|
-
|
141
|
-
$g = RDF::Graph.new
|
142
|
-
$format = if ENV["TRIPLES_FORMAT"]
|
143
|
-
ENV["TRIPLES_FORMAT"].to_sym
|
144
|
-
else
|
145
|
-
:jsonld
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def get_writer(type:)
|
150
|
-
RDF::Writer.for(type)
|
151
|
-
# w.prefix(:foaf, RDF::URI.new("http://xmlns.com/foaf/0.1/"))
|
152
|
-
# w.prefix(:dc, RDF::URI.new("http://purl.org/dc/terms/"))
|
153
|
-
# w.prefix(:rdf, RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
|
154
|
-
# w.prefix(:rdfs, RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#"))
|
155
|
-
# w.prefix(:vcard, RDF::URI.new("http://www.w3.org/2006/vcard/ns#"))
|
156
|
-
# w.prefix(:odrl, RDF::URI.new("http://www.w3.org/ns/odrl/2/"))
|
157
|
-
# w.prefix(:this, RDF::URI.new("http://w3id.org/FAIR_Training_LDP/DAV/home/LDP/DUC-CCE/IPGB#"))
|
158
|
-
# w.prefix(:obo, RDF::URI.new("http://purl.obolibrary.org/obo/"))
|
159
|
-
# w.prefix(:xsd, RDF::URI.new("http://www.w3.org/2001/XMLSchema#"))
|
160
|
-
# w.prefix(:orcid, RDF::URI.new("https://orcid.org/"))
|
161
|
-
# warn "W"
|
162
|
-
# warn w.prefixes.inspect
|
163
|
-
end
|
164
|
-
|
165
|
-
def triplify(s, p, o, repo)
|
166
|
-
s = s.strip if s.instance_of?(String)
|
167
|
-
p = p.strip if p.instance_of?(String)
|
168
|
-
o = o.strip if o.instance_of?(String)
|
169
|
-
|
170
|
-
unless s.respond_to?("uri")
|
171
|
-
|
172
|
-
raise "Subject #{s} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless s.to_s =~ %r{^\w+:/?/?[^\s]+}
|
173
|
-
|
174
|
-
s = RDF::URI.new(s.to_s)
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
unless p.respond_to?("uri")
|
179
|
-
|
180
|
-
raise "Predicate #{p} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless p.to_s =~ %r{^\w+:/?/?[^\s]+}
|
181
|
-
|
182
|
-
p = RDF::URI.new(p.to_s)
|
183
|
-
|
184
|
-
end
|
185
|
-
unless o.respond_to?("uri")
|
186
|
-
o = if o.to_s =~ %r{^\w+:/?/?[^\s]+}
|
187
|
-
RDF::URI.new(o.to_s)
|
188
|
-
elsif o.to_s =~ /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
|
189
|
-
RDF::Literal.new(o.to_s, datatype: RDF::XSD.date)
|
190
|
-
elsif o.to_s =~ /^\d\.\d/
|
191
|
-
RDF::Literal.new(o.to_s, datatype: RDF::XSD.float)
|
192
|
-
elsif o.to_s =~ /^[0-9]+$/
|
193
|
-
RDF::Literal.new(o.to_s, datatype: RDF::XSD.int)
|
194
|
-
else
|
195
|
-
RDF::Literal.new(o.to_s, language: :en)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
triple = RDF::Statement(s, p, o)
|
200
|
-
repo.insert(triple)
|
201
|
-
|
202
|
-
true
|
203
|
-
end
|
204
|
-
|
205
|
-
def self.getuuid
|
206
|
-
Time.now.to_f.to_s.gsub(".", "")[1..14]
|
207
|
-
end
|
208
|
-
|
209
|
-
def load_graph
|
210
|
-
%i[title label issued creator description subject uid id type].each do |method|
|
211
|
-
next unless send(method)
|
212
|
-
next if send(method).empty?
|
213
|
-
|
214
|
-
subject = uid # me!
|
215
|
-
predicate = PROPERTIES[method] # look up the predicate for this property
|
216
|
-
# warn "prediate #{predicate} for method #{method}"
|
217
|
-
object = send(method) # get the value of this property from self
|
218
|
-
# warn "value #{object.to_s}"
|
219
|
-
repo = repository
|
220
|
-
triplify(subject, predicate, object, repo)
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
def serialize(format: $format)
|
225
|
-
format = format.to_sym
|
226
|
-
w = get_writer(type: format)
|
227
|
-
w.dump(repository)
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "odrl/version"
|
4
|
+
require "linkeddata"
|
5
|
+
|
6
|
+
CPOLICY = ODRLV.Policy.to_s
|
7
|
+
|
8
|
+
PPROFILE = ODRLV.profile.to_s
|
9
|
+
|
10
|
+
CSET = ODRLV.Set.to_s
|
11
|
+
COFFER = ODRLV.Offer.to_s
|
12
|
+
CREQUEST = ODRLV.Request.to_s
|
13
|
+
CAGREEMENT = ODRLV.Agreement.to_s
|
14
|
+
CPRIVACY = ODRLV.Privacy.to_s
|
15
|
+
PASSET = ODRLV.target.to_s
|
16
|
+
CASSET = ODRLV.Asset.to_s
|
17
|
+
CASSETCOLLECTION = ODRLV.Asset.to_s
|
18
|
+
|
19
|
+
CRULE = ODRLV.Rule.to_s
|
20
|
+
CPERMISSION = ODRLV.Permission.to_s
|
21
|
+
PPERMISSION = ODRLV.permission.to_s
|
22
|
+
CPROHIBITION = ODRLV.Prohibition.to_s
|
23
|
+
PPROHIBITION = ODRLV.prohibition.to_s
|
24
|
+
PDUTY = ODRLV.obligation.to_s
|
25
|
+
CDUTY = ODRLV.Duty.to_s
|
26
|
+
|
27
|
+
PRULE = ODRLV.Rule.to_s
|
28
|
+
|
29
|
+
PACTION = ODRLV.action.to_s
|
30
|
+
VUSE = ODRLV.use.to_s # this is goofy ODRL stuff...
|
31
|
+
VTRANSFER = ODRLV.transfer.to_s # this is goofy ODRL stuff...
|
32
|
+
CACTION = ODRLV.Action.to_s
|
33
|
+
|
34
|
+
PREFINEMENT = ODRLV.refinement.to_s
|
35
|
+
|
36
|
+
PASSIGNER = ODRLV.assigner.to_s # now in PARTYFUNCTIONS
|
37
|
+
PASSIGNEE = ODRLV.assignee.to_s
|
38
|
+
CPARTY = ODRLV.Party.to_s
|
39
|
+
CPARTYCOLLECTION = ODRLV.Party.to_s
|
40
|
+
|
41
|
+
PCONSTRAINT = ODRLV.constraint.to_s
|
42
|
+
CCONSTRAINT = ODRLV.Constraint.to_s
|
43
|
+
PLEFT = ODRLV.leftOperand.to_s
|
44
|
+
PRIGHT = ODRLV.rightOperand.to_s
|
45
|
+
POPERATOR = ODRLV.operator.to_s
|
46
|
+
POPERANDREFERENCE = ODRLV.rightOperandReference.to_s
|
47
|
+
PDATATYPE = ODRLV.dataType.to_s
|
48
|
+
PUNIT = ODRLV.unit.to_s
|
49
|
+
PSTATUS = ODRLV.status.to_s
|
50
|
+
|
51
|
+
PPARTOF = ODRLV.partOf.to_s
|
52
|
+
|
53
|
+
PROPERTIES = {
|
54
|
+
title: DCT.title,
|
55
|
+
creator: DCT.creator,
|
56
|
+
description: DCT.description,
|
57
|
+
id: DCT.identifier,
|
58
|
+
type: RDF.type,
|
59
|
+
subject: DCT.subject,
|
60
|
+
uid: ODRLV.uid,
|
61
|
+
label: RDFS.label,
|
62
|
+
issued: DCT.issued
|
63
|
+
}
|
64
|
+
|
65
|
+
OPERATORS = %w[eq gt gteq hasPart isA isAllOf isAnyOf isNoneOf isPartOf lt lteq neq]
|
66
|
+
LEFTOPERANDS = %w[absolutePosition absoluteSize absoluteSpatialPosition absoluteTemporalPosition
|
67
|
+
count dateTime delayPeriod deliveryChannel device elapsedTime event
|
68
|
+
fileFormat industry language media meteredTime payAmount percentage
|
69
|
+
product purpose recipient relativePosition relativeSize relativeSpatialPosition
|
70
|
+
relativeTemporalPosition resolution spatial spatialCoordinates system
|
71
|
+
systemDevice timeInterval unitOfCount version virtualLocation]
|
72
|
+
|
73
|
+
PARTYFUNCTIONS = %w[assignee assigner attributedParty attributingParty compensatedParty
|
74
|
+
compensatingParty consentedParty consentingParty contractedParty contractingParty informedParty
|
75
|
+
informingParty trackedParty trackingParty]
|
76
|
+
|
77
|
+
# INTERESTING>>>> THIS COULD BE USED IF THERE IS A PROFILE...
|
78
|
+
# module RemovableConstants
|
79
|
+
# def def_if_not_defined(const, value)
|
80
|
+
# self.class.const_set(const, value) unless self.class.const_defined?(const)
|
81
|
+
# end
|
82
|
+
|
83
|
+
# def redef_without_warning(const, value)
|
84
|
+
# self.class.send(:remove_const, const) if self.class.const_defined?(const)
|
85
|
+
# self.class.const_set(const, value)
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
|
89
|
+
module ODRL
|
90
|
+
class Base
|
91
|
+
@@repository = RDF::Repository.new
|
92
|
+
|
93
|
+
# If you add an attribute, you mustr also add it to the constructor,
|
94
|
+
# and to the @attribute list
|
95
|
+
# andn to the .load_graph
|
96
|
+
attr_accessor :title, :creator, :description, :subject, :baseURI, :uid, :id, :type, :label, :issued
|
97
|
+
|
98
|
+
def self.baseURI
|
99
|
+
ENV["ODRL_BASEURI"] || "http://example.org"
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.repository
|
103
|
+
@@repository
|
104
|
+
end
|
105
|
+
|
106
|
+
def repository
|
107
|
+
@@repository
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.clear_repository
|
111
|
+
@@repository.clear!
|
112
|
+
true
|
113
|
+
end
|
114
|
+
|
115
|
+
def initialize(
|
116
|
+
uid:, type:, title: nil,
|
117
|
+
creator: nil,
|
118
|
+
description: nil,
|
119
|
+
issued: nil,
|
120
|
+
subject: nil,
|
121
|
+
baseURI: "http://example.org",
|
122
|
+
id: nil,
|
123
|
+
label: nil,
|
124
|
+
**_
|
125
|
+
)
|
126
|
+
|
127
|
+
@title = title
|
128
|
+
@creator = creator
|
129
|
+
@issued = issued
|
130
|
+
@description = description
|
131
|
+
@subject = subject
|
132
|
+
@baseURI = baseURI || ODRL::Base.baseURI
|
133
|
+
@uid = uid
|
134
|
+
@type = type
|
135
|
+
@label = label || @title
|
136
|
+
@id = @uid
|
137
|
+
|
138
|
+
raise "Every object must have a uid - attempt to create #{@type}" unless @uid
|
139
|
+
raise "Every object must have a type - " unless @type
|
140
|
+
|
141
|
+
$g = RDF::Graph.new
|
142
|
+
$format = if ENV["TRIPLES_FORMAT"]
|
143
|
+
ENV["TRIPLES_FORMAT"].to_sym
|
144
|
+
else
|
145
|
+
:jsonld
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def get_writer(type:)
|
150
|
+
RDF::Writer.for(type)
|
151
|
+
# w.prefix(:foaf, RDF::URI.new("http://xmlns.com/foaf/0.1/"))
|
152
|
+
# w.prefix(:dc, RDF::URI.new("http://purl.org/dc/terms/"))
|
153
|
+
# w.prefix(:rdf, RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
|
154
|
+
# w.prefix(:rdfs, RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#"))
|
155
|
+
# w.prefix(:vcard, RDF::URI.new("http://www.w3.org/2006/vcard/ns#"))
|
156
|
+
# w.prefix(:odrl, RDF::URI.new("http://www.w3.org/ns/odrl/2/"))
|
157
|
+
# w.prefix(:this, RDF::URI.new("http://w3id.org/FAIR_Training_LDP/DAV/home/LDP/DUC-CCE/IPGB#"))
|
158
|
+
# w.prefix(:obo, RDF::URI.new("http://purl.obolibrary.org/obo/"))
|
159
|
+
# w.prefix(:xsd, RDF::URI.new("http://www.w3.org/2001/XMLSchema#"))
|
160
|
+
# w.prefix(:orcid, RDF::URI.new("https://orcid.org/"))
|
161
|
+
# warn "W"
|
162
|
+
# warn w.prefixes.inspect
|
163
|
+
end
|
164
|
+
|
165
|
+
def triplify(s, p, o, repo)
|
166
|
+
s = s.strip if s.instance_of?(String)
|
167
|
+
p = p.strip if p.instance_of?(String)
|
168
|
+
o = o.strip if o.instance_of?(String)
|
169
|
+
|
170
|
+
unless s.respond_to?("uri")
|
171
|
+
|
172
|
+
raise "Subject #{s} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless s.to_s =~ %r{^\w+:/?/?[^\s]+}
|
173
|
+
|
174
|
+
s = RDF::URI.new(s.to_s)
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
unless p.respond_to?("uri")
|
179
|
+
|
180
|
+
raise "Predicate #{p} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless p.to_s =~ %r{^\w+:/?/?[^\s]+}
|
181
|
+
|
182
|
+
p = RDF::URI.new(p.to_s)
|
183
|
+
|
184
|
+
end
|
185
|
+
unless o.respond_to?("uri")
|
186
|
+
o = if o.to_s =~ %r{^\w+:/?/?[^\s]+}
|
187
|
+
RDF::URI.new(o.to_s)
|
188
|
+
elsif o.to_s =~ /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
|
189
|
+
RDF::Literal.new(o.to_s, datatype: RDF::XSD.date)
|
190
|
+
elsif o.to_s =~ /^\d\.\d/
|
191
|
+
RDF::Literal.new(o.to_s, datatype: RDF::XSD.float)
|
192
|
+
elsif o.to_s =~ /^[0-9]+$/
|
193
|
+
RDF::Literal.new(o.to_s, datatype: RDF::XSD.int)
|
194
|
+
else
|
195
|
+
RDF::Literal.new(o.to_s, language: :en)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
triple = RDF::Statement(s, p, o)
|
200
|
+
repo.insert(triple)
|
201
|
+
|
202
|
+
true
|
203
|
+
end
|
204
|
+
|
205
|
+
def self.getuuid
|
206
|
+
Time.now.to_f.to_s.gsub(".", "")[1..14]
|
207
|
+
end
|
208
|
+
|
209
|
+
def load_graph
|
210
|
+
%i[title label issued creator description subject uid id type].each do |method|
|
211
|
+
next unless send(method)
|
212
|
+
next if send(method).empty?
|
213
|
+
|
214
|
+
subject = uid # me!
|
215
|
+
predicate = PROPERTIES[method] # look up the predicate for this property
|
216
|
+
# warn "prediate #{predicate} for method #{method}"
|
217
|
+
object = send(method) # get the value of this property from self
|
218
|
+
# warn "value #{object.to_s}"
|
219
|
+
repo = repository
|
220
|
+
triplify(subject, predicate, object, repo)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def serialize(format: $format)
|
225
|
+
format = format.to_sym
|
226
|
+
w = get_writer(type: format)
|
227
|
+
w.dump(repository)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|