odrl-ruby 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/odrl/base.rb CHANGED
@@ -1,41 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "odrl/version"
4
+ require "linkeddata"
4
5
 
6
+ CPOLICY = ODRLV.Policy.to_s
5
7
 
6
- CPOLICY= ODRLV.Policy.to_s
8
+ PPROFILE = ODRLV.profile.to_s
7
9
 
8
- CSET= ODRLV.Set.to_s
9
- COFFER= ODRLV.Offer.to_s
10
- CREQUEST= ODRLV.Request.to_s
11
- CAGREEMENT= ODRLV.Agreement.to_s
12
- CPRIVACY= ODRLV.Privacy.to_s
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
13
15
  PASSET = ODRLV.target.to_s
14
- CASSET= ODRLV.Asset.to_s
15
- CASSETCOLLECTION= ODRLV.Asset.to_s
16
+ CASSET = ODRLV.Asset.to_s
17
+ CASSETCOLLECTION = ODRLV.Asset.to_s
16
18
 
17
- CRULE= ODRLV.Rule.to_s
18
- CPERMISSION= ODRLV.Permission.to_s
19
+ CRULE = ODRLV.Rule.to_s
20
+ CPERMISSION = ODRLV.Permission.to_s
19
21
  PPERMISSION = ODRLV.permission.to_s
20
- CPROHIBITION= ODRLV.Prohibition.to_s
22
+ CPROHIBITION = ODRLV.Prohibition.to_s
21
23
  PPROHIBITION = ODRLV.prohibition.to_s
22
- PDUTY= ODRLV.obligation.to_s
24
+ PDUTY = ODRLV.obligation.to_s
23
25
  CDUTY = ODRLV.Duty.to_s
24
26
 
25
27
  PRULE = ODRLV.Rule.to_s
26
28
 
27
-
28
29
  PACTION = ODRLV.action.to_s
29
- VUSE = ODRLV.use.to_s # this is goofy ODRL stuff...
30
- VTRANSFER = ODRLV.transfer.to_s # this is goofy ODRL stuff...
31
- CACTION= 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
32
33
 
33
34
  PREFINEMENT = ODRLV.refinement.to_s
34
35
 
35
- PASSIGNER = ODRLV.assigner.to_s
36
- PASSIGNEE = ODRLV.assignee.to_s
37
- CPARTY= ODRLV.Party.to_s
38
- CPARTYCOLLECTION= ODRLV.Party.to_s
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
39
40
 
40
41
  PCONSTRAINT = ODRLV.constraint.to_s
41
42
  CCONSTRAINT = ODRLV.Constraint.to_s
@@ -49,168 +50,181 @@ PSTATUS = ODRLV.status.to_s
49
50
 
50
51
  PPARTOF = ODRLV.partOf.to_s
51
52
 
52
-
53
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,
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
62
63
  }
63
64
 
64
- module ODRL
65
- class Base
66
-
67
- @@repository = RDF::Repository.new()
68
-
69
- attr_accessor :title, :creator, :description, :subject, :baseURI, :uid, :id, :type, :label
70
-
71
- def self.baseURI
72
- return ENV['ODRL_BASEURI'] || "http://example.org"
73
- end
74
-
75
- def self.repository
76
- return @@repository
77
- end
78
- def repository
79
- return @@repository
80
- end
81
-
82
- def self.clear_repository
83
- @@repository.clear!
84
- return true
85
- end
86
-
87
- def initialize(
88
- title: nil,
89
- creator: nil,
90
- description: nil,
91
- subject: nil,
92
- baseURI: "http://example.org",
93
- uid:,
94
- id: nil,
95
- type:,
96
- label: nil,
97
- **_)
98
-
99
- @title = title
100
- @creator = creator
101
- @description = description
102
- @subject = subject
103
- @baseURI = baseURI || ODRL::Base.baseURI
104
- @uid = uid
105
- @type = type
106
- @label = label || @title
107
- @id = @uid
108
-
109
- raise "Every object must have a uid - attempt to create #{@type}" unless @uid
110
- raise "Every object must have a type - " unless @type
111
-
112
-
113
- $g = RDF::Graph.new()
114
- if ENV["TRIPLES_FORMAT"]
115
- $format = ENV["TRIPLES_FORMAT"].to_sym
116
- else
117
- $format = :jsonld
118
- end
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
119
88
 
120
- end
121
-
122
- def get_writer(type:)
123
- w = RDF::Writer.for(type)
124
- # w.prefix(:foaf, RDF::URI.new("http://xmlns.com/foaf/0.1/"))
125
- # w.prefix(:dc, RDF::URI.new("http://purl.org/dc/terms/"))
126
- # w.prefix(:rdf, RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
127
- # w.prefix(:rdfs, RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#"))
128
- # w.prefix(:vcard, RDF::URI.new("http://www.w3.org/2006/vcard/ns#"))
129
- # w.prefix(:odrl, RDF::URI.new("http://www.w3.org/ns/odrl/2/"))
130
- # w.prefix(:this, RDF::URI.new("http://w3id.org/FAIR_Training_LDP/DAV/home/LDP/DUC-CCE/IPGB#"))
131
- # w.prefix(:obo, RDF::URI.new("http://purl.obolibrary.org/obo/"))
132
- # w.prefix(:xsd, RDF::URI.new("http://www.w3.org/2001/XMLSchema#"))
133
- # w.prefix(:orcid, RDF::URI.new("https://orcid.org/"))
134
- # warn "W"
135
- # warn w.prefixes.inspect
136
-
137
- return w
138
- end
139
-
140
- def triplify(s, p, o, repo)
141
-
142
- if s.class == String
143
- s = s.strip
144
- end
145
- if p.class == String
146
- p = p.strip
147
- end
148
- if o.class == String
149
- o = o.strip
150
- end
151
-
152
- unless s.respond_to?('uri')
153
-
154
- if s.to_s =~ /^\w+:\/?\/?[^\s]+/
155
- s = RDF::URI.new(s.to_s)
156
- else
157
- raise "Subject #{s.to_s} must be a URI-compatible thingy #{s}, #{p}, #{o}"
158
- end
159
- end
160
-
161
- unless p.respond_to?('uri')
162
-
163
- if p.to_s =~ /^\w+:\/?\/?[^\s]+/
164
- p = RDF::URI.new(p.to_s)
165
- else
166
- raise "Predicate #{p.to_s} must be a URI-compatible thingy #{s}, #{p}, #{o}"
167
- end
168
- end
169
- unless o.respond_to?('uri')
170
- if o.to_s =~ /^\w+:\/?\/?[^\s]+/
171
- o = RDF::URI.new(o.to_s)
172
- elsif o.to_s =~ /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
173
- o = RDF::Literal.new(o.to_s, :datatype => RDF::XSD.date)
174
- elsif o.to_s =~ /^\d\.\d/
175
- o = RDF::Literal.new(o.to_s, :datatype => RDF::XSD.float)
176
- elsif o.to_s =~ /^[0-9]+$/
177
- o = RDF::Literal.new(o.to_s, :datatype => RDF::XSD.int)
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
178
144
  else
179
- o = RDF::Literal.new(o.to_s, :language => :en)
180
- end
181
- end
182
-
183
- triple = RDF::Statement(s, p, o)
184
- repo.insert(triple)
185
-
186
- return true
187
- end
188
-
189
- def self.getuuid
190
- return Time.now.to_f.to_s.gsub("\.", "")[1..14]
191
- end
192
-
193
- def load_graph
194
- [:title, :label, :creator, :description, :subject, :uid, :id, :type].each do |method|
195
- next unless self.send(method)
196
- next if self.send(method).empty?
197
- subject = self.uid
198
- predicate = PROPERTIES[method]
199
- # warn "prediate #{predicate} for method #{method}"
200
- object = self.send(method)
201
- repo = self.repository
202
- triplify(subject, predicate, object, repo)
145
+ :jsonld
203
146
  end
204
- end
205
-
206
- def serialize(format: $format)
207
- format = format.to_sym
208
- w = get_writer(type: format)
209
- return w.dump(self.repository)
210
- end
211
-
212
- private
213
-
214
-
215
- 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
216
230
  end
@@ -1,104 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ODRL
4
+ class Constraint < Base
5
+ attr_accessor :uid, :rightOperand, :leftOperand, :operator, :rightOperandReference, :dataType, :unit, :status
4
6
 
5
- class Constraint < Base
6
-
7
- attr_accessor :uid, :rightOperand, :leftOperand, :operator, :rightOperandReference, :dataType, :unit, :status
8
- def initialize(
9
- uid: nil,
10
- rightOperand:,
11
- operator:,
12
- leftOperand:,
13
- rightOPerandReference: nil,
14
- dataType: nil,
15
- unit: nil,
16
- status: nil,
17
- type: CCONSTRAINT,
18
- **args)
7
+ def initialize(
8
+ rightOperand:, operator:, leftOperand:, uid: nil,
9
+ rightOPerandReference: nil,
10
+ dataType: nil,
11
+ unit: nil,
12
+ status: nil,
13
+ type: CCONSTRAINT,
14
+ **args
15
+ )
19
16
 
20
- @uid = uid
21
- unless @uid
22
- @uid = Base.baseURI + "#constraint_" + Base.getuuid
23
- end
24
- super(uid: @uid, type: type, **args)
17
+ @uid = uid
18
+ @uid ||= Base.baseURI + "#constraint_" + Base.getuuid
19
+ super(uid: @uid, type: type, **args)
25
20
 
26
- @rightOperand = rightOperand
27
- raise "Constraints must haves a Right operand such as 'event' - I'm dead!" unless @rightOperand
28
- @rightOperand = "http://www.w3.org/ns/odrl/2/#{@rightOperand}" unless @rightOperand =~ /https?:\/\// # if it is already a URI, then let it go
21
+ @rightOperand = rightOperand
22
+ raise "Constraints must haves a Right operand such as 'event' - I'm dead!" unless @rightOperand
29
23
 
30
- @leftOperand = leftOperand
31
- raise "Constraints must haves a Left Operand such as 'http://some.event.org/on-now' - I'm dead!" unless @leftOperand
32
- @leftOperand = "http://www.w3.org/ns/odrl/2/#{@leftOperand}" unless @leftOperand =~ /https?:\/\// # if it is already a URI, then let it go
24
+ # if it is already a URI, then let it go
25
+ # @rightOperand = "http://www.w3.org/ns/odrl/2/#{@rightOperand}" unless @rightOperand =~ %r{https?://}
33
26
 
34
- @operator = operator
35
- raise "Constraints must haves an operator such as 'eq' - I'm dead!" unless @operator
36
- @operator = "http://www.w3.org/ns/odrl/2/#{@operator}" unless @operator =~ /https?:\/\// # if it is already a URI, then let it go
27
+ @leftOperand = leftOperand
28
+ unless LEFTOPERANDS.include?(@leftOperand)
29
+ warn "The selected leftOperand is not part of the default set... just sayin'"
30
+ end
31
+ unless @leftOperand
32
+ raise "Constraints must haves a Left Operand such as 'http://some.event.org/on-now' - I'm dead!"
33
+ end
37
34
 
38
- @rightOperandReference = rightOperandReference
39
- @dataType = dataType
40
- @unit = unit
41
- @status = status
42
- end
35
+ # if it is already a URI, then let it go
36
+ @leftOperand = "http://www.w3.org/ns/odrl/2/#{@leftOperand}" unless @leftOperand =~ %r{https?://}
43
37
 
44
- def load_graph
45
- super
46
- # TODO This is bad DRY!! Put the bulk of this method into the base object
47
- if self.rightOperand
48
- predicate = PRIGHT
49
- object = self.rightOperand
50
- subject = self.uid
51
- repo = self.repository
52
- triplify(subject, predicate, object, repo)
53
- end
54
- if self.leftOperand
55
- predicate = PLEFT
56
- object = self.leftOperand
57
- subject = self.uid
58
- repo = self.repository
59
- triplify(subject, predicate, object, repo)
60
- end
61
- if self.operator
62
- predicate = POPERATOR
63
- object = self.operator
64
- subject = self.uid
65
- repo = self.repository
66
- triplify(subject, predicate, object, repo)
67
- end
68
- if self.rightOperandReference
69
- predicate = POPERANDREFERENCE
70
- object = self.rightOperandReference
71
- subject = self.uid
72
- repo = self.repository
73
- triplify(subject, predicate, object, repo)
74
- end
75
- if self.dataType
76
- predicate = PDATATYPE
77
- object = self.dataType
78
- subject = self.uid
79
- repo = self.repository
80
- triplify(subject, predicate, object, repo)
81
- end
82
- if self.unit
83
- predicate = PUNIT
84
- object = self.unit
85
- subject = self.uid
86
- repo = self.repository
87
- triplify(subject, predicate, object, repo)
88
- end
89
- if self.status
90
- predicate = PSTATUS
91
- object = self.status
92
- subject = self.uid
93
- repo = self.repository
94
- triplify(subject, predicate, object, repo)
95
- end
96
- end
38
+ @operator = operator
39
+ raise "Constraints must haves an operator such as 'eq' - I'm dead!" unless @operator
97
40
 
98
- def serialize(format:)
99
- super
100
- end
41
+ warn "The selected operator is not part of the default set... just sayin'" unless OPERATORS.include?(@operator)
101
42
 
43
+ # if it is already a URI, then let it go
44
+ @operator = "http://www.w3.org/ns/odrl/2/#{@operator}" unless @operator =~ %r{https?://}
45
+
46
+ @rightOperandReference = rightOperandReference
47
+ @dataType = dataType
48
+ @unit = unit
49
+ @status = status
102
50
  end
103
51
 
52
+ def load_graph
53
+ super
54
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
55
+ if rightOperand
56
+ predicate = PRIGHT
57
+ object = rightOperand
58
+ subject = uid
59
+ repo = repository
60
+ triplify(subject, predicate, object, repo)
61
+ end
62
+ if leftOperand
63
+ predicate = PLEFT
64
+ object = leftOperand
65
+ subject = uid
66
+ repo = repository
67
+ triplify(subject, predicate, object, repo)
68
+ end
69
+ if operator
70
+ predicate = POPERATOR
71
+ object = operator
72
+ subject = uid
73
+ repo = repository
74
+ triplify(subject, predicate, object, repo)
75
+ end
76
+ if rightOperandReference
77
+ predicate = POPERANDREFERENCE
78
+ object = rightOperandReference
79
+ subject = uid
80
+ repo = repository
81
+ triplify(subject, predicate, object, repo)
82
+ end
83
+ if dataType
84
+ predicate = PDATATYPE
85
+ object = dataType
86
+ subject = uid
87
+ repo = repository
88
+ triplify(subject, predicate, object, repo)
89
+ end
90
+ if unit
91
+ predicate = PUNIT
92
+ object = unit
93
+ subject = uid
94
+ repo = repository
95
+ triplify(subject, predicate, object, repo)
96
+ end
97
+ return unless status
98
+
99
+ predicate = PSTATUS
100
+ object = status
101
+ subject = uid
102
+ repo = repository
103
+ triplify(subject, predicate, object, repo)
104
+ end
105
+
106
+ def serialize(format:)
107
+ super
108
+ end
109
+ end
104
110
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ODRL
4
4
  module ODRL
5
- VERSION = "0.1.4"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end