def add_related(self, *objects):
master = None <EOL>slaves = set([]) <EOL>solitaire = set([]) <EOL>for new in objects:<EOL><INDENT>if isinstance(new, self.cls):<EOL><INDENT>if master is None:<EOL><INDENT>master = new<EOL><DEDENT>else:<EOL><INDENT>slaves.add(new)<EOL><DEDENT>for item in new.items:<EOL><INDENT>existing = self.lookup.get(item)<EOL>if existing is not None:<EOL><INDENT>slaves.add(existing)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>cluster = self.lookup.get(new)<EOL>if cluster is None:<EOL><INDENT>solitaire.add(new)<EOL><DEDENT>elif master is None:<EOL><INDENT>master = cluster<EOL><DEDENT>elif master != cluster:<EOL><INDENT>slaves.add(cluster)<EOL><DEDENT><DEDENT><DEDENT>if master is None:<EOL><INDENT>master = self.cls([])<EOL><DEDENT>for slave in slaves:<EOL><INDENT>master.update(slave)<EOL><DEDENT>for item in solitaire:<EOL><INDENT>master.add_item(item)<EOL><DEDENT>for item in master.items:<EOL><INDENT>self.lookup[item] = master<EOL><DEDENT>
Add related items

           The arguments can be individual items or cluster objects containing
           several items.

           When two groups of related items share one or more common members,
           they will be merged into one cluster.