Skip to content

Commit

Permalink
Simplify storage of semantic properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Jun 5, 2023
1 parent e5aa147 commit 2d77b93
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions lib/virtual_assembly/semantizer/semantic_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,26 @@ module SemanticObject
# This should be a String or nil.
attr_accessor :semanticType

# This Array stores the semantic properties of the object.
# To append a SemanticProperty, use the dedicated
# registerSemanticProperty method. You should pass the value
# of the property as a block (callback) like so:
# registerSemanticProperty("http://xmlns.com/foaf/0.1/name") {self.name}.
attr_reader :semanticProperties

# If the semanticId is nil, the object will be treated as a blank node.
def initialize(semanticId = nil, semanticType = nil)
@semanticProperties = []

# This Hash allows us to find a property using its name.
#
# Hash<String, Integer>
#
# The key store the name of a property (String).
# The value store the index of the property in the
# semanticProperties array (Integer).
@semanticPropertiesNameIndex = {}
@semanticPropertiesMap = {}

# Ensure to call the setter methods
self.semanticId = semanticId
self.semanticType = semanticType
end

# This Array stores the semantic properties of the object.
# To append a SemanticProperty, use the dedicated
# registerSemanticProperty method. You should pass the value
# of the property as a block (callback) like so:
# registerSemanticProperty("http://xmlns.com/foaf/0.1/name") {self.name}.
def semanticProperties
@semanticPropertiesMap.values
end

def hasSemanticProperty?(name)
@semanticPropertiesNameIndex.include?(name)
@semanticPropertiesMap.key?(name)
end

def isBlankNode?
Expand All @@ -94,10 +87,7 @@ def semanticPropertyValue(name)
# Given its name, returns the corresponding SemanticProperty
# stored by this object or nil if the property does not exist.
def semanticProperty(name)
index = @semanticPropertiesNameIndex.fetch(name)
@semanticProperties.at(index)
rescue StandardError
nil
@semanticPropertiesMap[name]
end

# Use this method to append a semantic property to this object.
Expand Down Expand Up @@ -161,17 +151,15 @@ def serialize(serializer)
def createOrUpdateSemanticProperty(name, valueGetter)
# Update
if hasSemanticProperty?(name)
semanticProperty = semanticProperty(name)
semanticProperty.valueGetter = valueGetter unless semanticProperty.nil?
property = semanticProperty(name)
property&.valueGetter = valueGetter

# Create
else
semanticProperty = VirtualAssembly::Semantizer::SemanticProperty.new(name, &valueGetter)
@semanticProperties.push(semanticProperty)
index = @semanticProperties.count - 1
@semanticPropertiesNameIndex.store(name, index)
property = VirtualAssembly::Semantizer::SemanticProperty.new(name, &valueGetter)
@semanticPropertiesMap[name] = property
end
semanticProperty
property
end
end
end
Expand Down

0 comments on commit 2d77b93

Please sign in to comment.