Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SKOS Concept parsing to allow use of intermediate SKOS Concept #10

Merged
merged 22 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/org/datafoodconsortium/connector/codegen/queries.mtl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@
[query public isPrimitive(t: Type): Boolean = if (t.name = 'String' or t.name = 'Boolean' or t.name = 'Real' or t.name = 'Integer') then true else false endif /]
[query public isPrimitive(anElement: TypedElement): Boolean = anElement.type.isPrimitive() /]

[comment This specific to the ruby codegen /]
[query public isSKOSConceptClass(c: Class): Boolean = (c.name = 'SKOSConcept') /]
[query public isSKOSConceptClass(c: Classifier): Boolean = (c.name = 'SKOSConcept') /]

[comment query public getAttributeForStereotypedOperation(c: Class, s: String, o: String): String
= invoke('org.datafoodconsortium.connector.codegen.common.Common', 'getAttributeForStereotypedOperation(org.eclipse.uml2.uml.Class, java.lang.String, java.lang.String)', Sequence{c, s, o})
/]
Expand Down Expand Up @@ -190,4 +194,4 @@

[comment query public getUnimplementedOperations(c: Classifier): Sequence(Operation)
= invoke('org.datafoodconsortium.connector.codegen.common.Common', 'getUnimplementedOperations(org.eclipse.uml2.uml.Classifier)', Sequence{c})
/]
/]
3 changes: 2 additions & 1 deletion src/org/datafoodconsortium/connector/codegen/ruby/class.mtl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
class DataFoodConsortium::Connector::[aClass.name.toUpperFirst()/][generateGeneralization(aClass)/]

[if (aClass.isSemantic() and aClass.generalization->isEmpty())]include VirtualAssembly::Semantizer::SemanticObject[/if]

[if (isSKOSConceptClass(aClass))]include DataFoodConsortium::Connector::SKOSHelper[/if]

[for (property: Property | aClass.ownedAttribute) separator('\n')]
# @return ['['/][property.type.name /][']'/]
attr_accessor :[property.name /]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[comment encoding = UTF-8 /]
[module classifier('http://www.eclipse.org/uml2/5.0.0/UML')]

[import org::datafoodconsortium::connector::codegen::queries /]
[import org::datafoodconsortium::connector::codegen::ruby::common /]

[template public generateImports(classifier: Classifier)]
[for (ei: ElementImport | classifier.elementImport->sortedBy(i: ElementImport | i.importedElement.name)) separator('\n')][if (ei.importedElement.oclIsTypeOf(Class))][generateImport(ei)/][/if][/for]
[if (classifier.oclIsTypeOf(Class))]require "virtual_assembly/semantizer"[/if]
[if (isSKOSConceptClass(classifier))]require 'datafoodconsortium/connector/skos_helper'[/if]
[/template]

[template public generateImport(ei: ElementImport)]require "datafoodconsortium/connector/[generateFileName(ei.importedElement.name) /]"[/template]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module DataFoodConsortium::Connector::SKOSHelper
def addAttribute(name, value)
self.instance_variable_set("@#{name}", value)
self.define_singleton_method(name) do
instance_variable_get("@#{name}")
end
end

def hasAttribute(name)
self.methods.include?(:"#{name}")
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# frozen_string_literal: true

# MIT License
#
#
# Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,124 +22,139 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'datafoodconsortium/connector/skos_helper'
require 'datafoodconsortium/connector/skos_concept'
require 'datafoodconsortium/connector/skos_parser_element'

class DataFoodConsortium::Connector::SKOSInstance
include DataFoodConsortium::Connector::SKOSHelper

def addAttribute(name, value)
self.instance_variable_set("@#{name}", value)
self.define_singleton_method(name) do
return instance_variable_get("@#{name}")
end
end

def hasAttribute(name)
return self.methods.include?(:"#{name}")
end

# Return a list of singelton methods, ie the list of Concept available
def topConcepts
self.methods(false).sort
end
end

class DataFoodConsortium::Connector::SKOSParser
CONCEPT_SCHEMES = ["Facet", "productTypes"].freeze

def initialize
@results = DataFoodConsortium::Connector::SKOSInstance.new
@skosConcepts = {}
@rootElements = []
@broaders = {}
# Flag used to tell the parser to use SkosConcept object when parsing data from Concept Scheme
# defined in CONCEPT_SCHEMES
@useSkosConcept = false
end

def parse(data)
init

data.each do |element|
current = DataFoodConsortium::Connector::SKOSParserElement.new(element)

setSkosConceptFlag(current)

if current.isConcept? || current.isCollection?
if !@skosConcepts.has_key?(current.id)
concept = createSKOSConcept(current)
@skosConcepts[current.id] = concept
end

def initialize()
@results = DataFoodConsortium::Connector::SKOSInstance.new
@skosConcepts = {}
@rootElements = []
@broaders = {}
end

def parse(data)
init()

data.each do |element|
current = DataFoodConsortium::Connector::SKOSParserElement.new(element)

if (current.isConcept?() || current.isCollection?())
if (!@skosConcepts.has_key?(current.id))
concept = createSKOSConcept(current)
@skosConcepts[current.id] = concept
end

if (current.hasBroader())
current.broader.each do |broaderId|
if (!@broaders.has_key?(broaderId))
@broaders[broaderId] = []
end

@broaders[broaderId].push(current.id)
end

# No broader, save the concept to the root
else
@rootElements.push(current.id)
end
if current.hasBroader
current.broader.each do |broaderId|
if !@broaders.has_key?(broaderId)
@broaders[broaderId] = []
end
end

@rootElements.each do |rootElementId|
setResults(@results, rootElementId)
end

return @results
@broaders[broaderId].push(current.id)
end
# No broader, save the concept to the root
else
@rootElements.push(current.id)
end
end
end

protected

def createSKOSConcept(element)
skosConcept = DataFoodConsortium::Connector::SKOSConcept.new(element.id)
skosConcept.semanticType = element.type
return skosConcept
@rootElements.each do |rootElementId|
setResults(@results, rootElementId)
end

def getValueWithoutPrefix(property)
name = 'undefined'

if (!property.include?('http') && property.include?(':'))
name = property.split(':')[1]
elsif (property.include?('#'))
name = property.split('#')[1]
end
@results
end

name = name.gsub('-', '_');
protected

# workaround to fix concepts starting with a number
# see https://github.com/datafoodconsortium/connector-ruby/issues/3
# see https://github.com/datafoodconsortium/ontology/issues/66
if (name.match?("^[0-9]"))
name = "_" + name
end
def createSKOSConcept(element)
skosConcept = DataFoodConsortium::Connector::SKOSConcept.new(
element.id, broaders: element.broader, narrowers: element.narrower
)
skosConcept.semanticType = element.type

skosConcept
end

return name.upcase
def getValueWithoutPrefix(property)
name = 'undefined'

if !property.include?('http') && property.include?(':')
name = property.split(':')[1]
elsif property.include?('#')
name = property.split('#')[1]
end

private

def init()
@results = DataFoodConsortium::Connector::SKOSInstance.new
@skosConcepts = {}
@rootElements = []
@broaders = {}
name = name.gsub('-', '_')

# workaround to fix concepts starting with a number
# see https://github.com/datafoodconsortium/connector-ruby/issues/3
# see https://github.com/datafoodconsortium/ontology/issues/66
name = "_" + name if name.match?("^[0-9]")

name.upcase
end

private

def init
@results = DataFoodConsortium::Connector::SKOSInstance.new
@skosConcepts = {}
@rootElements = []
@broaders = {}
@useSkosConcept = false
end

def setResults(parent, id)
name = getValueWithoutPrefix(id)

if !parent.hasAttribute(name)
if @useSkosConcept && !@skosConcepts[id].nil?
parent.addAttribute(name, @skosConcepts[id])
else
parent.addAttribute(name, DataFoodConsortium::Connector::SKOSInstance.new)
end
end

def setResults(parent, id)
name = getValueWithoutPrefix(id)
# Leaf concepts, stop the process
if !@broaders.has_key?(id)
parent.instance_variable_set("@#{name}", @skosConcepts[id])
return
end

if (!parent.hasAttribute(name))
parent.addAttribute(name, DataFoodConsortium::Connector::SKOSInstance.new)
end

# Leaf concepts, stop the process
if (!@broaders.has_key?(id))
parent.instance_variable_set("@#{name}", @skosConcepts[id])
return
end
@broaders[id].each do |narrower|
parentSkosInstance = parent.instance_variable_get("@#{name}")

@broaders[id].each do |narrower|
childName = getValueWithoutPrefix(narrower)
parentSkosInstance = parent.instance_variable_get("@#{name}")
setResults(parentSkosInstance, narrower) # recursive call
end
setResults(parentSkosInstance, narrower) # recursive call
end
end

end
def setSkosConceptFlag(current)
@useSkosConcept = true if current.isConceptScheme? && matchingConceptSchemes(current)
end

def matchingConceptSchemes(current)
regex = /#{CONCEPT_SCHEMES.join("|")}/

current.id =~ regex
end
end
Loading