Skip to content

Commit

Permalink
add usage method to the agent models (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni authored Nov 3, 2023
1 parent e9ff8c1 commit b49ad10
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ GEM

PLATFORMS
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
activesupport (~> 4)
Expand Down Expand Up @@ -248,4 +249,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.4.12
2.3.26
27 changes: 26 additions & 1 deletion lib/ontologies_linked_data/models/agents/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,38 @@ class Agent < LinkedData::Models::Base
attribute :identifiers, namespace: :adms, property: :identifier, enforce: %i[Identifier list unique_identifiers]
attribute :affiliations, enforce: %i[Agent list is_organization], namespace: :org, property: :memberOf
attribute :creator, type: :user, enforce: [:existence]

embed :identifiers, :affiliations
embed_values affiliations: LinkedData::Models::Agent.goo_attrs_to_load + [identifiers: LinkedData::Models::AgentIdentifier.goo_attrs_to_load]
serialize_methods :usages

write_access :creator
access_control_load :creator

def usages
id = self.id
q = Goo.sparql_query_client.select(:id, :property, :status).distinct
.from(LinkedData::Models::OntologySubmission.uri_type)
.where(
[:id,
RDF::URI.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
LinkedData::Models::OntologySubmission.uri_type
],
[:id,
LinkedData::Models::OntologySubmission.attribute_uri(:submissionStatus),
:status
]
)

q = q.union([[:id, :property, id]])
q.filter("?status = <#{RDF::URI.new(LinkedData::Models::SubmissionStatus.id_prefix + 'RDF')}> || ?status = <#{RDF::URI.new(LinkedData::Models::SubmissionStatus.id_prefix + 'UPLOADED')}>")
data = q.each_solution.map { |x| [x[:id], x[:property], x[:status]] }
data = data.group_by(&:shift)
data.transform_values do |values|
r = values.select { |value| value.last['RDF'] }
r = values.select { |value| value.last['UPLOADED'] } if r.empty?
r.map(&:first)
end
end

def unique_identifiers(inst, attr)
inst.bring(attr) if inst.bring?(attr)
Expand Down
79 changes: 65 additions & 14 deletions test/models/test_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
class TestAgent < LinkedData::TestCase

def self.before_suite
backend_4s_delete
self.new("before_suite").teardown
@@user1 = LinkedData::Models::User.new(:username => "user11111", :email => "some1111@email.org" )
@@user1 = LinkedData::Models::User.new(:username => "user11111", :email => "some1111@email.org")
@@user1.passwordHash = "some random pass hash"
@@user1.save
end
Expand All @@ -15,32 +16,28 @@ def self.after_suite
@@user1.delete
end



def test_agent_no_valid

@agents =[
LinkedData::Models::Agent.new(name:"name 0", email:"test_0@test.com", agentType: 'organization',creator: @@user1 ),
LinkedData::Models::Agent.new(name:"name 1", email:"test_1@test.com", agentType: 'person', creator: @@user1 ),
LinkedData::Models::Agent.new(name:"name 2", email:"test_2@test.com", agentType: 'person', creator: @@user1 )
@agents = [
LinkedData::Models::Agent.new(name: "name 0", email: "test_0@test.com", agentType: 'organization', creator: @@user1),
LinkedData::Models::Agent.new(name: "name 1", email: "test_1@test.com", agentType: 'person', creator: @@user1),
LinkedData::Models::Agent.new(name: "name 2", email: "test_2@test.com", agentType: 'person', creator: @@user1)
]
@identifiers = [
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ROR', creator: @@user1),
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ORCID', creator: @@user1),
]

@identifiers.each {|i| i.save}
@identifiers.each { |i| i.save }

affiliations = @agents[0..2].map{ |a| a.save }
affiliations = @agents[0..2].map { |a| a.save }
agent = @agents.last
agent.affiliations = affiliations


refute agent.valid?
refute_nil agent.errors[:affiliations][:is_organization]

affiliations.each{|x| x.delete}

affiliations.each { |x| x.delete }

agents = @agents[0..2].map do |a|
a.identifiers = @identifiers
Expand All @@ -54,8 +51,7 @@ def test_agent_no_valid
refute second_agent.valid?
refute_nil second_agent.errors[:identifiers][:unique_identifiers]


@identifiers.each{|i| i.delete}
@identifiers.each { |i| i.delete }
end

def test_identifier_find
Expand All @@ -69,6 +65,7 @@ def test_identifier_find

id.delete
end

def test_identifier_no_valid
refute LinkedData::Models::AgentIdentifier.new(notation: 'https://ror.org/000h6jb29', schemaAgency: 'ROR', creator: @@user1).valid?
id = LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29"', schemaAgency: 'ROR', creator: @@user1)
Expand All @@ -82,4 +79,58 @@ def test_identifier_no_valid
id.delete
end

def test_agent_usages
count, acronyms, ontologies = create_ontologies_and_submissions(ont_count: 3, submission_count: 1,
process_submission: false)

o1 = ontologies[0]
o2 = ontologies[1]
o3 = ontologies[2]
sub1 = o1.latest_submission(status: :any)
sub2 = o2.latest_submission(status: :any)
sub3 = o3.latest_submission(status: :any)
refute_nil sub1
refute_nil sub2
refute_nil sub3

agents = [LinkedData::Models::Agent.new(name: "name 0", email: "test_0@test.com", agentType: 'organization', creator: @@user1).save,
LinkedData::Models::Agent.new(name: "name 1", email: "test_1@test.com", agentType: 'organization', creator: @@user1).save,
LinkedData::Models::Agent.new(name: "name 2", email: "test_2@test.com", agentType: 'person', creator: @@user1).save]

sub1.hasCreator = [agents.last]
sub1.publisher = agents[0..1]
sub1.fundedBy = [agents[0]]
sub1.bring_remaining
assert sub1.valid?
sub1.save

sub2.hasCreator = [agents.last]
sub2.endorsedBy = [agents[0]]
sub2.fundedBy = agents[0..1]
sub2.bring_remaining
assert sub2.valid?
sub2.save

usages = agents[0].usages

assert_equal 2, usages.size

refute_nil usages[sub1.id]
assert_equal usages[sub1.id].map(&:to_s).sort, ["http://purl.org/dc/terms/publisher", "http://xmlns.com/foaf/0.1/fundedBy"].sort
refute_nil usages[sub2.id].map(&:to_s).sort, ["http://omv.ontoware.org/2005/05/ontology#endorsedBy", "http://xmlns.com/foaf/0.1/fundedBy"].sort

sub3.copyrightHolder = agents[0]
sub3.bring_remaining
sub3.save

usages = agents[0].usages
assert_equal 3, usages.size

refute_nil usages[sub1.id]
assert_equal usages[sub1.id].map(&:to_s).sort, ["http://purl.org/dc/terms/publisher", "http://xmlns.com/foaf/0.1/fundedBy"].sort
assert_equal usages[sub2.id].map(&:to_s).sort, ["http://omv.ontoware.org/2005/05/ontology#endorsedBy", "http://xmlns.com/foaf/0.1/fundedBy"].sort
assert_equal usages[sub3.id].map(&:to_s), ["http://schema.org/copyrightHolder"]

agents.each{|x| x.delete}
end
end

0 comments on commit b49ad10

Please sign in to comment.