virtual_assembly-semantizer 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d4643867375d8d1c6d4828a81e1c322d24bb0b230b711d1d959d1593b9901b2
4
- data.tar.gz: 63aaeb5ddfef5149ca23656a759a270856d3984de0933f9e49b354bbd61b907a
3
+ metadata.gz: fe29003dc39de713a7bc46b5874cbae84b7b67bf37174cf77359a4a07e26ec20
4
+ data.tar.gz: a4c3cf7410d3440cbd164482eba1ec14749056639b19adc49e752dcdbab85f33
5
5
  SHA512:
6
- metadata.gz: 2387084e7f1bc560ac7a44bddebf213cfda8b9b00dee8d75e8026d67f40b1172f1a047ed1931def2cc83d61f42ed774970f49d3debcc774ca7cb8db51d9a0f86
7
- data.tar.gz: 44b7c5011c9d4b1789d9cb59b853cc9e64774eb0adee78e5e654e8212658f76e649ee7498c804f20d4280209aa63bc53a14e7da4c8ed717cdac2953879c5a0f4
6
+ metadata.gz: 6cb10e4e1edb5c51ee937d6daf6d2687e2b6ad4130cc1eaf9a492d4f785885ce59a52354012978f64ff6b1327d0df88a0159ed3da76e5373ba42f12e2f1d3bc8
7
+ data.tar.gz: 92671cd75919f29ccd2ea99fe00611ce41598ff48e076872ebe19db7190220fb1f4f5b5c72bf592e966e5631edd94ad572f4ddf7c21216c74e2ab97d75d35166
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright © 2023 Maxime Lecoq, <maxime@lecoqlibre.fr>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy of this software
@@ -23,153 +25,142 @@ require 'virtual_assembly/semantizer/semantic_property'
23
25
  # A semanticObject holds semantic properties (SemanticProperty)
24
26
  # that refers to linked data concepts.
25
27
  #
26
- # For example, a Person object including this module could register
28
+ # For example, a Person object including this module could register
27
29
  # in its initializer method a semantic property for its name like:
28
30
  # Person.registerSemanticProperty("http://xmlns.com/foaf/0.1/name") {self.name}
29
- module VirtualAssembly::Semantizer::SemanticObject
30
-
31
- # The semantic ID implements the concept of linked data ID.
32
- #
33
- # This ID is an uri pointing to the location of the object
34
- # on the web like "https://mywebsite/myobject" for instance.
35
- #
36
- # If a SemanticObject doesn't define its ID, it
37
- # will be considered as a blank node.
38
- #
39
- # This should be a String or nil.
40
- attr_accessor :semanticId
41
-
42
- # The semantic type implements the concept of linked data type
43
- # (also called class).
44
- #
45
- # This type is an uri pointing to the location of the linked
46
- # data concept on the web like "http://xmlns.com/foaf/0.1/Person"
47
- # for instance.
48
- #
49
- # This should be a String or nil.
50
- attr_accessor :semanticType
51
-
52
- # This Array stores the semantic properties of the object.
53
- # To append a SemanticProperty, use the dedicated
54
- # registerSemanticProperty method. You should pass the value
55
- # of the property as a block (callback) like so:
56
- # registerSemanticProperty("http://xmlns.com/foaf/0.1/name") {self.name}.
57
- attr_reader :semanticProperties
58
-
59
- # If the semanticId is nil, the object will be treated as a blank node.
60
- def initialize(semanticId = nil, semanticType = nil)
61
- @semanticProperties = Array.new
62
-
63
- # This Hash allows us to find a property using its name.
64
- #
65
- # Hash<String, Integer>
66
- #
67
- # The key store the name of a property (String).
68
- # The value store the index of the property in the
69
- # semanticProperties array (Integer).
70
- @semanticPropertiesNameIndex = Hash.new
31
+ module VirtualAssembly
32
+ module Semantizer
33
+ module SemanticObject
34
+ # The semantic ID implements the concept of linked data ID.
35
+ #
36
+ # This ID is an uri pointing to the location of the object
37
+ # on the web like "https://mywebsite/myobject" for instance.
38
+ #
39
+ # If a SemanticObject doesn't define its ID, it
40
+ # will be considered as a blank node.
41
+ #
42
+ # This should be a String or nil.
43
+ attr_accessor :semanticId
44
+
45
+ # The semantic type implements the concept of linked data type
46
+ # (also called class).
47
+ #
48
+ # This type is an uri pointing to the location of the linked
49
+ # data concept on the web like "http://xmlns.com/foaf/0.1/Person"
50
+ # for instance.
51
+ #
52
+ # This should be a String or nil.
53
+ attr_accessor :semanticType
54
+
55
+ # If the semanticId is nil, the object will be treated as a blank node.
56
+ def initialize(semanticId = nil, semanticType = nil)
57
+ @semanticPropertiesMap = {}
71
58
 
72
59
  # Ensure to call the setter methods
73
60
  self.semanticId = semanticId
74
61
  self.semanticType = semanticType
75
- end
76
-
77
- def hasSemanticProperty?(name)
78
- return @semanticPropertiesNameIndex.include?(name)
79
- end
80
-
81
- def isBlankNode?
82
- return @semanticId == nil || @semanticId == ""
83
- end
84
-
85
- # Given the name of the property, it returns the value
86
- # associated to a property of this object.
87
- def semanticPropertyValue(name)
88
- index = @semanticPropertiesNameIndex.fetch(name, nil)
89
- return index != nil ? @semanticProperties[index].value : nil;
90
- end
91
-
92
- # Use this method to append a semantic property to this object.
93
- # The value of the property should be passed as a block so its
94
- # value would be up to date when we will access it.
95
- def registerSemanticProperty(name, &valueGetter)
62
+ end
63
+
64
+ # This Array stores the semantic properties of the object.
65
+ # To append a SemanticProperty, use the dedicated
66
+ # registerSemanticProperty method. You should pass the value
67
+ # of the property as a block (callback) like so:
68
+ # registerSemanticProperty("http://xmlns.com/foaf/0.1/name") {self.name}.
69
+ def semanticProperties
70
+ @semanticPropertiesMap.values
71
+ end
72
+
73
+ def hasSemanticProperty?(name)
74
+ @semanticPropertiesMap.key?(name)
75
+ end
76
+
77
+ def isBlankNode?
78
+ @semanticId.nil? || @semanticId == ''
79
+ end
80
+
81
+ # Given the name of the property, it returns the value
82
+ # associated to a property of this object.
83
+ def semanticPropertyValue(name)
84
+ semanticProperty(name)&.value
85
+ end
86
+
87
+ # Given its name, returns the corresponding SemanticProperty
88
+ # stored by this object or nil if the property does not exist.
89
+ def semanticProperty(name)
90
+ @semanticPropertiesMap[name]
91
+ end
92
+
93
+ # Use this method to append a semantic property to this object.
94
+ # The value of the property should be passed as a block so its
95
+ # value would be up to date when we will access it.
96
+ def registerSemanticProperty(name, &valueGetter)
96
97
  createOrUpdateSemanticProperty(name, valueGetter)
97
- end
98
-
99
- # Sets the semantic id of the object and registers the
100
- # corresponding semantic property.
101
- #
102
- # The semantic ID implements the concept of linked data ID.
103
- #
104
- # This ID is an uri pointing to the location of the object
105
- # on the web like "https://mywebsite/myobject" for instance.
106
- #
107
- # If a SemanticObject doesn't define its ID, it
108
- # will be considered as a blank node.
109
- #
110
- # This should be a String or nil.
111
- def semanticId=(uri)
98
+ end
99
+
100
+ # Sets the semantic id of the object and registers the
101
+ # corresponding semantic property.
102
+ #
103
+ # The semantic ID implements the concept of linked data ID.
104
+ #
105
+ # This ID is an uri pointing to the location of the object
106
+ # on the web like "https://mywebsite/myobject" for instance.
107
+ #
108
+ # If a SemanticObject doesn't define its ID, it
109
+ # will be considered as a blank node.
110
+ #
111
+ # This should be a String or nil.
112
+ def semanticId=(uri)
112
113
  @semanticId = uri
113
- registerSemanticProperty("@id") {self.semanticId}
114
- end
115
-
116
- # Sets the semantic type of the object and registers the
117
- # corresponding semantic property.
118
- #
119
- # The semantic type implements the concept of linked data type
120
- # (also called class).
121
- #
122
- # This type is an uri pointing to the location of the linked
123
- # data concept on the web like "http://xmlns.com/foaf/0.1/Person"
124
- # for instance.
125
- #
126
- # This should be a String or nil.
127
- def semanticType=(type)
114
+ property = registerSemanticProperty('@id') { semanticId }
115
+ property.valueSetter = ->(value) { @semanticId = value }
116
+ end
117
+
118
+ # Sets the semantic type of the object and registers the
119
+ # corresponding semantic property.
120
+ #
121
+ # The semantic type implements the concept of linked data type
122
+ # (also called class).
123
+ #
124
+ # This type is an uri pointing to the location of the linked
125
+ # data concept on the web like "http://xmlns.com/foaf/0.1/Person"
126
+ # for instance.
127
+ #
128
+ # This should be a String or nil.
129
+ def semanticType=(type)
128
130
  @semanticType = type
129
- registerSemanticProperty("@type") {self.semanticType}
130
- end
131
-
132
- # Serialize all the semantic properties of this object
133
- # to an output format.
134
- #
135
- # You could use the HashSerializer to export as a Hash.
136
- # This Hash should be then exported to JSON for instance.
137
- def serialize(serializer)
138
- return serializer.process(self)
139
- end
140
-
141
- protected
142
-
143
- # If the semantic property already exist in this object, this
144
- # method will simply update the valueGetter of the property.
145
- #
146
- # If this object does not holds the property, the new property
147
- # will be added into the semanticProperties Array of this object.
148
- def createOrUpdateSemanticProperty(name, valueGetter)
149
- # Update
150
- if (hasSemanticProperty?(name))
151
- semanticProperty = findSemanticProperty(name)
152
- if (semanticProperty != nil)
153
- semanticProperty.valueGetter = valueGetter
154
- end
155
-
156
- # Create
157
- else
158
- @semanticProperties.push(VirtualAssembly::Semantizer::SemanticProperty.new(name, &valueGetter))
159
- index = @semanticProperties.count - 1
160
- @semanticPropertiesNameIndex.store(name, index);
161
- end
162
- end
163
-
164
- # Given its name, returns the corresponding SemanticProperty
165
- # stored by this object or nil if the property does not exist.
166
- def findSemanticProperty(name)
167
- begin
168
- index = @semanticPropertiesNameIndex.fetch(name)
169
- return @semanticProperties.at(index)
170
- rescue
171
- return nil
172
- end
131
+ property = registerSemanticProperty('@type') { semanticType }
132
+ property.valueSetter = ->(value) { @semanticType = value }
133
+ end
134
+
135
+ # Serialize all the semantic properties of this object
136
+ # to an output format.
137
+ #
138
+ # You could use the HashSerializer to export as a Hash.
139
+ # This Hash should be then exported to JSON for instance.
140
+ def serialize(serializer)
141
+ serializer.process(self)
142
+ end
143
+
144
+ protected
145
+
146
+ # If the semantic property already exist in this object, this
147
+ # method will simply update the valueGetter of the property.
148
+ #
149
+ # If this object does not holds the property, the new property
150
+ # will be added into the semanticProperties Array of this object.
151
+ def createOrUpdateSemanticProperty(name, valueGetter)
152
+ # Update
153
+ if hasSemanticProperty?(name)
154
+ property = semanticProperty(name)
155
+ property&.valueGetter = valueGetter
156
+
157
+ # Create
158
+ else
159
+ property = VirtualAssembly::Semantizer::SemanticProperty.new(name, &valueGetter)
160
+ @semanticPropertiesMap[name] = property
173
161
  end
174
-
175
- end
162
+ property
163
+ end
164
+ end
165
+ end
166
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright © 2023 Maxime Lecoq, <maxime@lecoqlibre.fr>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy of this software
@@ -15,15 +17,15 @@
15
17
  # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16
18
  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
19
 
18
- # The SemanticPropety class is designed to turn properties of
20
+ # The SemanticPropety class is designed to turn properties of
19
21
  # objects into linked data.
20
22
  #
21
23
  # A SemanticProperty has a name and a corresponding value that
22
24
  # can be fetched later (so its value would be up to date).
23
25
  #
24
- # This class is intented to be used through the SemanticObject
26
+ # This class is intented to be used through the SemanticObject
25
27
  # class.
26
- #
28
+ #
27
29
  # For instance, we can tell that the name of a Person object refers
28
30
  # to the linked data concept "name" from the FOAF project. The name
29
31
  # of the property would be the uri of the FOAF:name property while the
@@ -31,33 +33,45 @@
31
33
  #
32
34
  # You should use a block to pass the value like so:
33
35
  # SemanticProperty.new("http://xmlns.com/foaf/0.1/name") {self.name}
34
- class VirtualAssembly::Semantizer::SemanticProperty
35
-
36
- # The name of the property. It generally points to an uri
37
- # like "http://xmlns.com/foaf/0.1/name" or it is used to
38
- # define a reserved linked data property like "@id".
39
- #
40
- # This should be a String.
41
- attr_accessor :name
42
-
43
- # The function to call when the value is requested.
44
- #
45
- # This should be a Proc passed as a Block.
46
- attr_accessor :valueGetter
47
-
48
- # @param name The name of the property, like
49
- # "http://xmlns.com/foaf/0.1/name" or "@id" for instance.
50
- #
51
- # @param valueGetter A Proc used to retrieve the value of the
52
- # property when requested.
53
- def initialize(name, &valueGetter)
36
+ module VirtualAssembly
37
+ module Semantizer
38
+ class SemanticProperty
39
+ # The name of the property. It generally points to an uri
40
+ # like "http://xmlns.com/foaf/0.1/name" or it is used to
41
+ # define a reserved linked data property like "@id".
42
+ #
43
+ # This should be a String.
44
+ attr_accessor :name
45
+
46
+ # The function to call when the value is requested.
47
+ #
48
+ # This should be a Proc passed as a Block.
49
+ attr_accessor :valueGetter
50
+
51
+ # The function to call to store a new value.
52
+ #
53
+ # This should be a Proc
54
+ attr_accessor :valueSetter
55
+
56
+ # @param name The name of the property, like
57
+ # "http://xmlns.com/foaf/0.1/name" or "@id" for instance.
58
+ #
59
+ # @param valueGetter A Proc used to retrieve the value of the
60
+ # property when requested.
61
+ def initialize(name, &valueGetter)
54
62
  @name = name
55
63
  @valueGetter = valueGetter
56
- end
64
+ end
57
65
 
58
- # Fetch and returns the value associated to this property.
59
- def value
60
- return @valueGetter.call
61
- end
66
+ # Fetch and returns the value associated to this property.
67
+ def value
68
+ @valueGetter.call
69
+ end
62
70
 
63
- end
71
+ # Stores a new value for this property.
72
+ def value=(new_value)
73
+ @valueSetter.call(new_value)
74
+ end
75
+ end
76
+ end
77
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtual_assembly-semantizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Lecoq
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-07 00:00:00.000000000 Z
11
+ date: 2023-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-ld