Class: ODRL::Profile::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/odrl/profile/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, title:, description:, authors:, version:, license:, prefix: "ex", separator: "#") ⇒ Builder

attr_accessor :logicalConstraints, :conflict_strategies, :rules



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/odrl/profile/builder.rb', line 23

def initialize(uri:, title:, description:, authors:, version:, license:, prefix: "ex", separator: "#")
  @uri = uri
  @title = title
  @description = description
  @authors = authors
  @version = version
  @license = license
  @prefix = prefix
  @separator = separator
  @repository = RDF::Repository.new
  @prefixes = {}
  @policies = []
  @permissions = []
  @prohibitions = []
  @duties = []
  @asset_relations = []
  @party_functional_roles = []
  @actions = []
  @leftOperands = []
  @rightOperands = []
  @operators = []
  @skosMembers = []

  @fullURI = @uri + @separator

  @prefixes.store(@prefix, @fullURI)

  ODRL::Profile::Builder.triplify(@uri, RDF.type, OWL.Ontology, @repository)
  ODRL::Profile::Builder.triplify(@uri, RDF.type, PROFILE.Profile, @repository)
  ODRL::Profile::Builder.triplify(@uri, RDFS.label, @title, @repository)
  ODRL::Profile::Builder.triplify(@uri, OWL.versionInfo, @version, @repository, skip_type_or_language: true)
  ODRL::Profile::Builder.triplify(@uri, DCT.title, @title, @repository)
  ODRL::Profile::Builder.triplify(@uri, DCT.description, @description, @repository)
  ODRL::Profile::Builder.triplify(@uri, DCT.license, @license, @repository)

  @authors.each do |author|
    ODRL::Profile::Builder.triplify(@uri, DCT.creator, author, @repository, skip_type_or_language: true)
  end

  # SKOS

  ODRL::Profile::Builder.triplify(@fullURI, RDF.type, SKOS.Collection, @repository)
  ODRL::Profile::Builder.triplify(@fullURI, SKOS.prefLabel, "Profile Vocabulary", @repository)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def actions
  @actions
end

#asset_relationsObject

Returns the value of attribute asset_relations.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def asset_relations
  @asset_relations
end

#authorsObject

Returns the value of attribute authors.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def authors
  @authors
end

#descriptionObject

Returns the value of attribute description.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def description
  @description
end

#dutiesObject

Returns the value of attribute duties.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def duties
  @duties
end

#fullURIObject

Returns the value of attribute fullURI.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def fullURI
  @fullURI
end

#leftOperandsObject

Returns the value of attribute leftOperands.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def leftOperands
  @leftOperands
end

#licenseObject

Returns the value of attribute license.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def license
  @license
end

#operatorsObject

Returns the value of attribute operators.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def operators
  @operators
end

#party_functional_rolesObject

Returns the value of attribute party_functional_roles.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def party_functional_roles
  @party_functional_roles
end

#permissionsObject

Returns the value of attribute permissions.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def permissions
  @permissions
end

#policiesObject

Returns the value of attribute policies.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def policies
  @policies
end

#prefixObject

Returns the value of attribute prefix.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def prefix
  @prefix
end

#prefixesObject

Returns the value of attribute prefixes.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def prefixes
  @prefixes
end

#prohibitionsObject

Returns the value of attribute prohibitions.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def prohibitions
  @prohibitions
end

#repositoryObject

Returns the value of attribute repository.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def repository
  @repository
end

#rightOperandsObject

Returns the value of attribute rightOperands.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def rightOperands
  @rightOperands
end

#separatorObject

Returns the value of attribute separator.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def separator
  @separator
end

#skosMembersObject

Returns the value of attribute skosMembers.



20
21
22
# File 'lib/odrl/profile/builder.rb', line 20

def skosMembers
  @skosMembers
end

#titleObject

Returns the value of attribute title.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def title
  @title
end

#uriObject

Returns the value of attribute uri.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def uri
  @uri
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/odrl/profile/builder.rb', line 19

def version
  @version
end

Class Method Details

.triplify(s, p, o, repo, skip_type_or_language: false) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/odrl/profile/builder.rb', line 118

def self.triplify(s, p, o, repo, skip_type_or_language: false)
  s = s.strip if s.instance_of?(String)
  p = p.strip if p.instance_of?(String)
  o = o.strip if o.instance_of?(String)

  unless s.respond_to?("uri")

    raise "Subject #{s} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless s.to_s =~ %r{^\w+:/?/?[^\s]+}

    s = RDF::URI.new(s.to_s)

  end

  unless p.respond_to?("uri")

    raise "Predicate #{p} must be a URI-compatible thingy #{s}, #{p}, #{o}" unless p.to_s =~ %r{^\w+:/?/?[^\s]+}

    p = RDF::URI.new(p.to_s)

  end
  unless o.respond_to?("uri")
    o = if o.to_s =~ %r{^\w+:/?/?[^\s]+}
          RDF::URI.new(o.to_s)
        elsif o.to_s =~ /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
          RDF::Literal.new(o.to_s, datatype: (RDF::XSD.date if skip_type_or_language == false))
        elsif o.to_s =~ /^\d\.\d/
          RDF::Literal.new(o.to_s, datatype: (RDF::XSD.float if skip_type_or_language == false))
        elsif o.to_s =~ /^[0-9]+$/
          RDF::Literal.new(o.to_s, datatype: (RDF::XSD.int if skip_type_or_language == false))
        else
          RDF::Literal.new(o.to_s, language: (:en if skip_type_or_language == false))
        end
  end

  triple = RDF::Statement(s, p, o)
  repo.insert(triple)

  true
end

Instance Method Details

#buildObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/odrl/profile/builder.rb', line 67

def build
  repo = repository # just shorter :-)


  [policies, permissions, prohibitions, duties, asset_relations, party_functional_roles, actions, leftOperands, rightOperands, operators].flatten.each do |elem|
    ODRL::Profile::Builder.triplify(elem.uri, RDFS.isDefinedBy, @fullURI, repo)
    elem.parent_property and ODRL::Profile::Builder.triplify(elem.uri, RDFS.subPropertyOf, elem.parent_property, repo)
    elem.parent_class and ODRL::Profile::Builder.triplify(elem.uri, RDFS.subClassOf, elem.parent_class, repo)

    elem.build(repo: repo)

    @skosMembers << elem.uri
  end
end

#build_skos(uri, members, label, repo) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/odrl/profile/builder.rb', line 81

def build_skos(uri, members, label, repo)
  ODRL::Profile::Builder.triplify(uri, RDF.type, SKOS.Collection, repo)
  ODRL::Profile::Builder.triplify(uri, SKOS.prefLabel, label, repo)

  members.each do |member|
    ODRL::Profile::Builder.triplify(uri, SKOS.member, member.uri, repo)
  end
end

#get_writer(type: :turtle) ⇒ Object



114
115
116
# File 'lib/odrl/profile/builder.rb', line 114

def get_writer(type: :turtle)
  RDF::Writer.for(type)
end

#serialize(format: :turtle) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/odrl/profile/builder.rb', line 90

def serialize(format: :turtle)
  format = format.to_sym

  ## All profile SKOS members

  @skosMembers.each do |member|
    ODRL::Profile::Builder.triplify(@fullURI, SKOS.member, member, @repository)
  end

  ## Specific profile SKOS members

  @policies.length > 0 and build_skos(@fullURI + "policies", @policies, "Policies", @repository)
  @permissions.length > 0 and build_skos(@fullURI + "permissions", @permissions, "Permissions", @repository)
  @prohibitions.length > 0 and build_skos(@fullURI + "prohibitions", @prohibitions, "Prohibitions", @repository)
  @duties.length > 0 and build_skos(@fullURI + "duties", @duties, "Duties", @repository)
  @actions.length > 0 and build_skos(@fullURI + "actions", @actions, "Actions for Rules", @repository)
  @asset_relations.length > 0 and build_skos(@fullURI + "asset_relations", @asset_relations, "Asset Relations", @repository)
  @party_functional_roles.length > 0 and build_skos(@fullURI + "partyFunctions", @party_functional_roles, "Party Functions", @repository)
  @leftOperands.length > 0 and build_skos(@fullURI + "constraintLeftOperand", @leftOperands, "Left Operands", @repository)
  @rightOperands.length > 0 and build_skos(@fullURI + "constraintRightOperand", @rightOperands, "Right Operands", @repository)
  @operators.length > 0 and build_skos(@fullURI + "operators", @operators, "Operators", @repository)

  w = get_writer(type: format)
  w.dump(repository, nil, prefixes: @prefixes)
end