Class: ODRL::Rule

Inherits:
Base
  • Object
show all
Defined in:
lib/odrl/rule.rb

Direct Known Subclasses

Duty, Permission, Prohibition

Instance Attribute Summary collapse

Attributes inherited from Base

#baseURI, #creator, #description, #id, #issued, #label, #subject, #title

Instance Method Summary collapse

Methods inherited from Base

baseURI, clear_repository, #get_writer, getuuid, repository, #repository, #triplify

Constructor Details

#initialize(uid: nil, constraints: nil, assets: nil, predicate: nil, action: nil, assigner: nil, assignee: nil, type: CRULE, **args) ⇒ Rule

Returns a new instance of Rule.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/odrl/rule.rb', line 11

def initialize(
  uid: nil,
  constraints: nil,
  assets: nil,
  predicate: nil,
  action: nil,
  assigner: nil,
  assignee: nil,
  type: CRULE,
  **args
)
  @uid = uid

  @uid ||= Base.baseURI + "#rule_" + Base.getuuid
  super(uid: @uid, type: type, **args)

  @constraints = {}
  @assets = {}
  @assigner = {}
  @assignee = {}
  @action = {}

  assets = [assets] unless assets.is_a? Array
  unless assets.first.nil?
    assets.each do |c|
      addAsset(asset: c)
    end
  end
  constraints = [constraints] unless constraints.is_a? Array
  return if constraints.first.nil?

  constraints.each do |c|
    addConstraint(constraint: c)
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def action
  @action
end

#assetsObject

Returns the value of attribute assets.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def assets
  @assets
end

#assigneeObject

Returns the value of attribute assignee.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def assignee
  @assignee
end

#assignerObject

Returns the value of attribute assigner.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def assigner
  @assigner
end

#constraintsObject

Returns the value of attribute constraints.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def constraints
  @constraints
end

#predicateObject

Returns the value of attribute predicate.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def predicate
  @predicate
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def type
  @type
end

#uidObject

Returns the value of attribute uid.



9
10
11
# File 'lib/odrl/rule.rb', line 9

def uid
  @uid
end

Instance Method Details

#addAction(action:) ⇒ Object



60
61
62
63
64
# File 'lib/odrl/rule.rb', line 60

def addAction(action:)
  raise "Action is not an ODRL Action" unless action.is_a?(Action)

  self.action[action.uid] = [PACTION, action]
end

#addAsset(asset:) ⇒ Object



47
48
49
50
51
52
# File 'lib/odrl/rule.rb', line 47

def addAsset(asset:)
  raise "Asset is not an ODRL Asset" unless asset.is_a?(Asset)

  uid = asset.uid
  assets[uid] = [PASSET, asset]
end

#addAssignee(party:) ⇒ Object



72
73
74
75
76
# File 'lib/odrl/rule.rb', line 72

def addAssignee(party:)
  raise "Asigner is not an ODRL Party" unless party.is_a?(Party)

  assignee[party.uid] = [PASSIGNEE, party]
end

#addAssigner(party:) ⇒ Object



66
67
68
69
70
# File 'lib/odrl/rule.rb', line 66

def addAssigner(party:)
  raise "Assigner is not an ODRL Party" unless party.is_a?(Party)

  assigner[party.uid] = [PASSIGNER, party]
end

#addConstraint(constraint:) ⇒ Object



54
55
56
57
58
# File 'lib/odrl/rule.rb', line 54

def addConstraint(constraint:)
  raise "Constraint is not an ODRL Constraint" unless constraint.is_a?(Constraint)

  constraints[constraint.uid] = [PCONSTRAINT, constraint]
end

#load_graphObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/odrl/rule.rb', line 78

def load_graph
  super
  %i[constraints assets action assigner assignee].each do |connected_object_type|
    next unless send(connected_object_type)

    send(connected_object_type).each do |_uid, typedconnection|
      predicate, odrlobject = typedconnection # e.g. "action", ActionObject
      object = odrlobject.uid
      subject = uid
      repo = repository
      triplify(subject, predicate, object, repo)
      odrlobject.load_graph  # start the cascade
    end
  end
end

#serialize(format:) ⇒ Object



94
95
96
# File 'lib/odrl/rule.rb', line 94

def serialize(format:)
  super
end