odrl-ruby 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/odrl/action.rb CHANGED
@@ -1,114 +1,106 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ODRL
4
-
5
- # ODRL::Action
6
- # Describes an action like "use"
7
- #
8
- # @author Mark D Wilkinson
9
- # @attr [URI] (optional) uid the URI of the Action node
10
- # @attr [[ODRL::Refinement]] (optional) ODRL Refinement objects
11
- # @attr [URI] predicate (optional) the predicate you wish to use with this action
12
- # @attr [string] value (required) a string like "use"
13
- # @attr [string] vallabel (optional) a string like "use"
14
- class Action < Base
15
-
16
- attr_accessor :uid, :refinements, :predicate, :type, :value, :vallabel
17
-
18
-
19
- # constructor
20
- # @param [Hash] opts the options to create a message with.
21
- # @option opts [String] :value the string value of rthe action, like "use"
22
- # @option opts [String] :vallabel the string for the label, like "use"
23
- #
24
- def initialize(value:, vallabel: "", type: CACTION, **args)
25
- @value = value
26
- @vallabel = vallabel || @value
27
- raise "Actions must haves a value such as 'use' - I'm dead!" unless @value
28
- @value = "http://www.w3.org/ns/odrl/2/#{@value}" unless @value =~ /http:\/\// # if it is already a URI, then let it go
29
-
30
- @uid = @value
31
- # unless @uid
32
- # self.uid = Base.baseURI + "#action_" + Base.getuuid
33
- # end
34
- super(uid: @uid, type: type, **args)
35
-
36
-
37
- @refinements = Hash.new
38
-
39
- args[:refinements] = [args[:refinements]] unless args[:refinements].is_a? Array
40
- if !(args[:refinements].first.nil?)
41
- args[:refinements].each do |c|
42
- self.addRefinement(refinement: c)
43
- end
44
- end
45
-
46
- self.predicate = PACTION unless self.predicate
47
-
4
+ # ODRL::Action
5
+ # Describes an action like "use"
6
+ #
7
+ # @author Mark D Wilkinson
8
+ # @attr [URI] (optional) uid the URI of the Action node
9
+ # @attr [[ODRL::Refinement]] (optional) ODRL Refinement objects
10
+ # @attr [URI] predicate (optional) the predicate you wish to use with this action
11
+ # @attr [string] value (required) a string like "use"
12
+ # @attr [string] vallabel (optional) a string like "use"
13
+ class Action < Base
14
+ attr_accessor :uid, :refinements, :predicate, :type, :value, :vallabel
15
+
16
+ # constructor
17
+ # @param [Hash] opts the options to create a message with.
18
+ # @option opts [String] :value the string value of rthe action, like "use"
19
+ # @option opts [String] :vallabel the string for the label, like "use"
20
+ #
21
+ def initialize(value:, vallabel: "", type: CACTION, **args)
22
+ @value = value
23
+ @vallabel = vallabel || @value
24
+ raise "Actions must haves a value such as 'use' - I'm dead!" unless @value
25
+
26
+ # if it is already a URI, then let it go
27
+ @value = "http://www.w3.org/ns/odrl/2/#{@value}" unless @value =~ %r{http://}
28
+
29
+ @uid = @value
30
+ # unless @uid
31
+ # self.uid = Base.baseURI + "#action_" + Base.getuuid
32
+ # end
33
+ super(uid: @uid, type: type, **args)
34
+
35
+ @refinements = {}
36
+
37
+ args[:refinements] = [args[:refinements]] unless args[:refinements].is_a? Array
38
+ unless args[:refinements].first.nil?
39
+ args[:refinements].each do |c|
40
+ addRefinement(refinement: c)
48
41
  end
42
+ end
49
43
 
50
- # Adds an ODRL Refinement
51
- #
52
- # @param refinement [ODRL::Refinement] the refinement to the action
53
- #
54
- def addRefinement(refinement: args)
55
- unless refinement.is_a?(Constraint)
56
- raise "Refinement is not an ODRL Constraint"
57
- else
58
- self.refinements[refinement.uid] = [PREFINEMENT, refinement]
59
- end
60
- end
44
+ self.predicate = PACTION unless predicate
45
+ end
61
46
 
47
+ # Adds an ODRL Refinement
48
+ #
49
+ # @param refinement [ODRL::Refinement] the refinement to the action
50
+ #
51
+ def addRefinement(refinement: args)
52
+ raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
62
53
 
63
- # Causes the triples of this object to be formed in the in-memory store
64
- # This includes any "cascading" objects for which this is the subject of the triple
65
- #
66
- def load_graph
67
- super
68
- # TODO This is bad DRY!! Put the bulk of this method into the base object
69
- [:refinements].each do |connected_object_type|
70
- next unless self.send(connected_object_type)
71
- self.send(connected_object_type).each do |uid, typedconnection|
72
- predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
73
- object = odrlobject.uid
74
- subject = self.uid
75
- repo = self.repository
76
- triplify(subject, predicate, object, repo)
77
- odrlobject.load_graph # start the cascade
78
- end
79
- end
80
- subject = self.uid
81
- object = self.vallabel
82
- predicate = SCHEMA.name
83
- repo = self.repository
84
- triplify(subject, predicate, object, repo)
85
- object = self.vallabel
86
- predicate = RDFS.label
87
- repo = self.repository
88
- triplify(subject, predicate, object, repo)
89
- end
54
+ refinements[refinement.uid] = [PREFINEMENT, refinement]
55
+ end
90
56
 
91
- # Returns the serialized RDF for this object and cascading related objects
92
- #
93
- # @param format [Symbol] a valid RDF::Writer format (e.g. :turtle)
94
- #
95
- def serialize(format:)
96
- super
57
+ # Causes the triples of this object to be formed in the in-memory store
58
+ # This includes any "cascading" objects for which this is the subject of the triple
59
+ #
60
+ def load_graph
61
+ super
62
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
63
+ [:refinements].each do |connected_object_type|
64
+ next unless send(connected_object_type)
65
+
66
+ send(connected_object_type).each do |_uid, typedconnection|
67
+ predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
68
+ object = odrlobject.uid
69
+ subject = uid
70
+ repo = repository
71
+ triplify(subject, predicate, object, repo)
72
+ odrlobject.load_graph # start the cascade
97
73
  end
98
-
74
+ end
75
+ subject = uid
76
+ object = vallabel
77
+ predicate = SCHEMA.name
78
+ repo = repository
79
+ triplify(subject, predicate, object, repo)
80
+ object = vallabel
81
+ predicate = RDFS.label
82
+ repo = repository
83
+ triplify(subject, predicate, object, repo)
99
84
  end
100
85
 
101
-
102
-
103
- class Use < Action
104
- def initialize(type: CACTION, **args)
105
- super(type: :type, **args)
106
- end
86
+ # Returns the serialized RDF for this object and cascading related objects
87
+ #
88
+ # @param format [Symbol] a valid RDF::Writer format (e.g. :turtle)
89
+ #
90
+ def serialize(format:)
91
+ super
107
92
  end
108
- class Transfer < Action
109
- def initialize(type: CACTION, **args)
110
- super(type: :type, **args)
111
- end
93
+ end
94
+
95
+ class Use < Action
96
+ def initialize(type: CACTION, **args)
97
+ super(type: :type, **args)
112
98
  end
99
+ end
113
100
 
101
+ class Transfer < Action
102
+ def initialize(type: CACTION, **args)
103
+ super(type: :type, **args)
104
+ end
105
+ end
114
106
  end
data/lib/odrl/asset.rb CHANGED
@@ -1,90 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
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
4
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)
5
40
 
6
- # ODRL::Action
7
- # Describes an action like "use"
8
- #
9
- # @author Mark D Wilkinson
10
- class Asset < Base
11
- attr_accessor :uid, :hasPolicy, :refinements, :partOf
12
-
13
- def initialize(type: CASSET, hasPolicy: nil, refinements: nil, partOf: nil, **args)
14
- @uid = uid
15
- unless @uid
16
- self.uid = Base.baseURI + "#asset_" + Base.getuuid
17
- end
18
- super(type: type, uid: @uid, **args)
19
-
20
- @partOf = partOf
21
- @hasPolicy = hasPolicy
22
-
23
- if @hasPolicy and !(@hasPolicy.is_a? Policy) # if it exists and is the wrong type
24
- raise "The policy of an Asset must be of type ODRL::Policy. The provided value will be discarded"
25
- @hasPolicy = nil
26
- end
27
- if @partOf and !(@partOf.is_a? AssetCollection) # if it exists and is the wrong type
28
- raise "The parent collection of an Asset must be of type ODRL::AssetCollection. The provided value will be discarded"
29
- @partOf = nil
30
- end
31
-
32
- @refinements = Hash.new
33
- refinements = [refinements] unless refinements.is_a? Array
34
- if !(refinements.first.nil?)
35
- refinements.each do |c|
36
- self.addRefinement(refinement: c)
37
- end
38
- end
39
- end
40
-
41
-
42
- def addPart(part: args)
43
- unless self.is_a?(AssetCollection)
44
- raise "Asset cannot be added as part of something that is not an asset collection"
45
- end
46
- unless part.is_a?(Asset)
47
- raise "Only Assets can be added as part of asset collections"
48
- end
49
- part.partOf[self.uid] = [PPARTOF, self]
50
- end
51
-
52
- def addRefinement(refinement: args)
53
- unless refinement.is_a?(Constraint)
54
- raise "Refinement is not an ODRL Constraint"
55
- else
56
- self.refinements[refinement.uid] = [PREFINEMENT, refinement]
57
- end
58
- end
59
-
60
- def load_graph
61
- super
62
- # TODO This is bad DRY!! Put the bulk of this method into the base object
63
- [:refinements, :partOf, :hasPolicy].each do |connected_object_type|
64
- next unless self.send(connected_object_type)
65
- self.send(connected_object_type).each do |uid, typedconnection|
66
- predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
67
- object = odrlobject.uid
68
- subject = self.uid
69
- repo = self.repository
70
- triplify(subject, predicate, object, repo)
71
- odrlobject.load_graph # start the cascade
72
- end
73
- end
74
- end
41
+ part.partOf[uid] = [PPARTOF, self]
42
+ end
75
43
 
76
- def serialize(format:)
77
- super
78
- end
44
+ def addRefinement(refinement: args)
45
+ raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
79
46
 
47
+ refinements[refinement.uid] = [PREFINEMENT, refinement]
80
48
  end
81
49
 
82
- class AssetCollection < Asset
83
-
84
- def initialize(type: CASSETCOLLECTION, **args)
85
- super(type: type, **args)
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
86
63
  end
64
+ end
87
65
  end
88
66
 
67
+ def serialize(format:)
68
+ super
69
+ end
70
+ end
89
71
 
72
+ class AssetCollection < Asset
73
+ def initialize(type: CASSETCOLLECTION, **args)
74
+ super(type: type, **args)
75
+ end
76
+ end
90
77
  end
data/lib/odrl/base.rb CHANGED
@@ -59,6 +59,7 @@ PROPERTIES = {
59
59
  subject: DCT.subject,
60
60
  uid: ODRLV.uid,
61
61
  label: RDFS.label,
62
+ issued: DCT.issued,
62
63
  }
63
64
 
64
65
  module ODRL
@@ -66,7 +67,10 @@ module ODRL
66
67
 
67
68
  @@repository = RDF::Repository.new()
68
69
 
69
- attr_accessor :title, :creator, :description, :subject, :baseURI, :uid, :id, :type, :label
70
+ # If you add an attribute, you mustr also add it to the constructor,
71
+ # and to the @attribute list
72
+ # andn to the .load_graph
73
+ attr_accessor :title, :creator, :description, :subject, :baseURI, :uid, :id, :type, :label, :issued
70
74
 
71
75
  def self.baseURI
72
76
  return ENV['ODRL_BASEURI'] || "http://example.org"
@@ -87,7 +91,8 @@ module ODRL
87
91
  def initialize(
88
92
  title: nil,
89
93
  creator: nil,
90
- description: nil,
94
+ description: nil,
95
+ issued: nil,
91
96
  subject: nil,
92
97
  baseURI: "http://example.org",
93
98
  uid:,
@@ -98,6 +103,7 @@ module ODRL
98
103
 
99
104
  @title = title
100
105
  @creator = creator
106
+ @issued = issued
101
107
  @description = description
102
108
  @subject = subject
103
109
  @baseURI = baseURI || ODRL::Base.baseURI
@@ -191,14 +197,15 @@ module ODRL
191
197
  end
192
198
 
193
199
  def load_graph
194
- [:title, :label, :creator, :description, :subject, :uid, :id, :type].each do |method|
200
+ [:title, :label, :issued, :creator, :description, :subject, :uid, :id, :type].each do |method|
195
201
  next unless self.send(method)
196
202
  next if self.send(method).empty?
197
- subject = self.uid
198
- predicate = PROPERTIES[method]
203
+ subject = self.uid # me!
204
+ predicate = PROPERTIES[method] # look up the predicate for this property
199
205
  # warn "prediate #{predicate} for method #{method}"
200
- object = self.send(method)
201
- repo = self.repository
206
+ object = self.send(method) # get the value of this property from self
207
+ # warn "value #{object.to_s}"
208
+ repo = self.repository
202
209
  triplify(subject, predicate, object, repo)
203
210
  end
204
211
  end
@@ -1,104 +1,105 @@
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 @leftOperand
29
+ raise "Constraints must haves a Left Operand such as 'http://some.event.org/on-now' - I'm dead!"
30
+ end
37
31
 
38
- @rightOperandReference = rightOperandReference
39
- @dataType = dataType
40
- @unit = unit
41
- @status = status
42
- end
32
+ # if it is already a URI, then let it go
33
+ @leftOperand = "http://www.w3.org/ns/odrl/2/#{@leftOperand}" unless @leftOperand =~ %r{https?://}
43
34
 
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
35
+ @operator = operator
36
+ raise "Constraints must haves an operator such as 'eq' - I'm dead!" unless @operator
97
37
 
98
- def serialize(format:)
99
- super
100
- end
38
+ # if it is already a URI, then let it go
39
+ @operator = "http://www.w3.org/ns/odrl/2/#{@operator}" unless @operator =~ %r{https?://}
101
40
 
41
+ @rightOperandReference = rightOperandReference
42
+ @dataType = dataType
43
+ @unit = unit
44
+ @status = status
102
45
  end
103
46
 
47
+ def load_graph
48
+ super
49
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
50
+ if rightOperand
51
+ predicate = PRIGHT
52
+ object = rightOperand
53
+ subject = uid
54
+ repo = repository
55
+ triplify(subject, predicate, object, repo)
56
+ end
57
+ if leftOperand
58
+ predicate = PLEFT
59
+ object = leftOperand
60
+ subject = uid
61
+ repo = repository
62
+ triplify(subject, predicate, object, repo)
63
+ end
64
+ if operator
65
+ predicate = POPERATOR
66
+ object = operator
67
+ subject = uid
68
+ repo = repository
69
+ triplify(subject, predicate, object, repo)
70
+ end
71
+ if rightOperandReference
72
+ predicate = POPERANDREFERENCE
73
+ object = rightOperandReference
74
+ subject = uid
75
+ repo = repository
76
+ triplify(subject, predicate, object, repo)
77
+ end
78
+ if dataType
79
+ predicate = PDATATYPE
80
+ object = dataType
81
+ subject = uid
82
+ repo = repository
83
+ triplify(subject, predicate, object, repo)
84
+ end
85
+ if unit
86
+ predicate = PUNIT
87
+ object = unit
88
+ subject = uid
89
+ repo = repository
90
+ triplify(subject, predicate, object, repo)
91
+ end
92
+ return unless status
93
+
94
+ predicate = PSTATUS
95
+ object = status
96
+ subject = uid
97
+ repo = repository
98
+ triplify(subject, predicate, object, repo)
99
+ end
100
+
101
+ def serialize(format:)
102
+ super
103
+ end
104
+ end
104
105
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ODRL
4
4
  module ODRL
5
- VERSION = "0.1.4"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end