virtual_assembly-semantizer 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe29003dc39de713a7bc46b5874cbae84b7b67bf37174cf77359a4a07e26ec20
|
4
|
+
data.tar.gz: a4c3cf7410d3440cbd164482eba1ec14749056639b19adc49e752dcdbab85f33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
# 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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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(
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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(
|
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
|
-
|
157
|
-
|
158
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
64
|
+
end
|
57
65
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
66
|
+
# Fetch and returns the value associated to this property.
|
67
|
+
def value
|
68
|
+
@valueGetter.call
|
69
|
+
end
|
62
70
|
|
63
|
-
|
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
|
+
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-
|
11
|
+
date: 2023-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-ld
|