# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 module RTM::Sugar::Topic module Counterparts # Returns all Roles belonging to Associations in which this Topic # is a player and for which this Topic is not the player. # # A filter-hash may be used to filter for the the type # of the Role this Topic playes # (:rtype), for the Association type (:atype) and/or # for the type of the returned Roles (:otype). The # identifier may be a topic reference. # # The result may be empty. # # :call-seq: # counterparts -> Array of Roles # counterparts({:rtype => identifier}) -> Array of Roles # counterparts({:atype => identifier}) -> Array of Roles # counterparts({:otype => identifier}) -> Array of Roles # counterparts({:rtype => identifier, :atype => identifier}) -> Array of Roles # counterparts({:rtype => identifier, :atype => identifier, :otype => identifier}) -> Array of Roles # def counterparts(filter={}) roles.select{|r| filter[:rtype] ? r.type == self.topic_map.get(filter[:rtype]) : true}. select{|r| filter[:atype] ? r.parent.type == self.topic_map.get(filter[:atype]) : true}. inject([]){|all,r| all+=r.counterparts(filter)} end # Returns all Topics that are players of Roles belonging to Associations # in which this Topic is another player. The resulting Array does # not contain duplicates. # # A filter-hash may be used to filter for the the type # of the Role this Topic playes # (:rtype), for the Association type (:atype) and/or # for the type of the Roles the returned Topics play (:otype). The # identifier may be a topic reference. # # The result may be empty. # # :call-seq: # counterplayers -> Array of Topics # counterplayers(:rtype => identifier) -> Array of Topics # counterplayers(:atype => identifier) -> Array of Topics # counterplayers(:otype => identifier) -> Array of Topics # counterplayers(:rtype => identifier1, :atype => identifier2) -> Array of Topics # counterplayers(:rtype => identifier1, :atype => identifier2, :otype => identifier3) -> Array of Topics # def counterplayers(filter={}) topic_map.cached self, :counterplayers, filter do counterparts(filter).map{|r| r.player}.uniq end end # Returns all players (Topics) which share a counterplayer with this Topic (player), # If type is given, it may constrain the type of the Role this Topic should play # (and therefore the type of the Roles the peers play) # or the type of the Association this Topic plays a Role in # (and therefore the type of the Associations the peers play an Role in). # # Type may be a topic reference. # # The result may be empty. # # :call-seq: # peers -> Array of Topics # peers(type) -> Array of Topics # def peers(type=:any) _peers = [] if type == :any self.roles.each do |r| _peers = _peers + r.peers(:arity => :loose, :atype => :loose, :otype => :loose, :rtype => :loose) end else type = topic_map.get(type) if type if self.roles.map{|r| r.type}.include?(type) #-> rtype self.roles(type).each do |r| _peers = _peers + r.peers(:arity => :loose, :atype => :loose, :otype => :loose, :rtype => :strict) end elsif self.roles.map{|r| r.parent.type}.include?(type) #->atype self.roles.select{|r| r.parent.type == type}.each do |r| _peers = _peers + r.peers(:arity => :loose, :atype => :strict, :otype => :loose, :rtype => :strict) end elsif self.roles.map{|r| r.counterparts}.flatten.map{|r| r.type}.include?(type) #-> otype # nothing can happen so far else return [] end else return [] end end return _peers.map{|r| r.player} end # Returns all Associations in which this Topic # is a player. # # A filter-hash may be used to filter # for the type of the Role this Topic plays (:rtype), # for the Association type (:atype), # for the type of another role (:otype) as well as # for the player of this other role (:oplayer). # Each value of this filter-hash may be a topic reference. # # The result may be empty. # # :call-seq: # associations_played -> Array of Associations # associations_played({:rtype => identifier}) -> Array of Associations # associations_played({:atype => identifier}) -> Array of Associations # associations_played({:otype => identifier}) -> Array of Associations # associations_played({:otype => identifier, :oplayer => identifier}) -> Array of Associations # associations_played({:rtype => identifier, :atype => identifier}) -> Array of Associations # associations_played({:rtype => identifier, :atype => identifier, :otype => identifier}) -> Array of Associations # associations_played({:rtype => identifier, :atype => identifier, :otype => identifier, :oplayer => identifier}) -> Array of Associations # def associations_played(filter = {}) topic_map.cached self, :associations_played, filter do raise("associations_played(filter): filter must be a Hash") unless filter.is_a?(Hash) default_hash = {:rtype => :any, :atype => :any, :otype => :any, :oplayer => :any} filter = default_hash.merge(filter) # get all roles played roles = self.roles_played(filter[:rtype], filter[:atype]) filter.delete(:rtype) filter.delete(:atype) #filter for the other roles types and players if there are other roles roles = roles.reject do |role| if role.parent.roles.size == 1 #association size if (filter[:otype] != :any) || filter[:player] != :any end else role.counterparts(filter).empty? end end # return parent associations roles.map{|role| role.parent}.uniq end end end end