Module: RTM::Sugar::Topic::Counterparts

Defined in:
rtm/lib/rtm/sugar/topic/counterparts.rb,
rtm/spec/rtm/sugar/topic/counterparts_spec.rb

Instance Method Summary

Instance Method Details

- (Object) associations_played(filter = {})

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


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'rtm/lib/rtm/sugar/topic/counterparts.rb', line 123

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

- (Object) counterparts(filter = {})

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


26
27
28
29
30
# File 'rtm/lib/rtm/sugar/topic/counterparts.rb', line 26

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

- (Object) counterplayers(filter = {})

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


52
53
54
55
56
# File 'rtm/lib/rtm/sugar/topic/counterparts.rb', line 52

def counterplayers(filter={})
  topic_map.cached self, :counterplayers, filter do
    counterparts(filter).map{|r| r.player}.uniq
  end
end

- (Object) peers(type = :any)

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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'rtm/lib/rtm/sugar/topic/counterparts.rb', line 72

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