def as_trap_collection( self, *, intention_type: IntentionType = IntentionType.GARBAGE, ) -> TrapCollection: traps = [] for trap in self.traps: cubeables = [] if not trap: raise self.InvalidDistribution('Empty trap') for constrained_node in trap: cubeable = constrained_node.node if isinstance(cubeable, AllNode) and len( cubeable.children) == 1: cubeables.extend(cubeable.children) else: cubeables.append(cubeable) traps.append( Trap( AllNode(cubeables), intention_type=intention_type, )) return TrapCollection(traps)
def _span_tree( self, option: t.Union[Printing, PrintingNode], item: t.Any, is_top_level: bool, _type: str, release: t.Optional[CubeRelease] = None, ) -> None: constrained_node = (release.constrained_nodes.node_for_node( AllNode((option, )) if isinstance(option, Printing) else option) if release else None) if isinstance(option, Printing): _item = PrintingTreeItem(option, _type, constrained_node) else: _item = NodeTreeItem( option, _type, constrained_node, ) _option_type = 'any' if isinstance(option, AnyNode) else 'all' for child in option.children: self._span_tree(child, _item, False, _option_type) if is_top_level: item.addTopLevelItem(_item) else: item.addChild(_item)
def trap_collection_to_trap_distribution( traps: TrapCollection, nodes: NodeCollection, ) -> t.Tuple[TrapDistribution, t.List[ConstrainedNode], t.List[t.Tuple[ PrintingNode, int]]]: constraint_map: t.Dict[PrintingNode, t.List[ConstrainedNode]] = defaultdict(list) for distribution_node in nodes: constraint_map[distribution_node.node].append(distribution_node) distribution: t.List[t.List[DistributionNode]] = [ [] for _ in range(len(traps)) ] removed: t.List[t.Tuple[PrintingNode, int]] = [] for index, trap in enumerate(traps): for child in trap.node.children: printing_node = child if isinstance( child, PrintingNode) else AllNode((child, )) try: distribution[index].append( DistributionNode(constraint_map[printing_node].pop())) except (KeyError, IndexError): removed.append((printing_node, index)) added = list(itertools.chain(*(nodes for nodes in constraint_map.values()))) return TrapDistribution(traps=distribution), added, removed
def as_trap_collection(self) -> FrozenMultiset[Trap]: traps = [] for trap in self.traps: cubeables = [] if not trap: raise Exception('Empty trap') for constrained_node in trap: cubeable = constrained_node.node if isinstance(cubeable, AllNode) and len(cubeable.children) == 1: cubeables.extend(cubeable.children) else: cubeables.append(cubeable) traps.append(Trap(AllNode(cubeables))) return FrozenMultiset(traps)
def trap_collection_to_trap_distribution( cls, traps: t.Collection[Trap], constrained_nodes: t.Iterable[ConstrainedNode], ) -> t.Tuple[TrapDistribution, t.List[ConstrainedNode], t.List[t.Tuple[PrintingNode, int]]]: constraint_map = {} #type: t.Dict[PrintingNode, t.List[ConstrainedNode]] for constrained_node in constrained_nodes: try: constraint_map[constrained_node.node].append(constrained_node) except KeyError: constraint_map[constrained_node.node] = [constrained_node] distribution = [[] for _ in range(len(traps))] #type: t.List[t.List[ConstrainedNode]] removed = [] #type: t.List[t.Tuple[PrintingNode, int]] for index, trap in enumerate(traps): for child in trap.node.children: printing_node = child if isinstance(child, PrintingNode) else AllNode((child,)) try: distribution[index].append(constraint_map[printing_node].pop()) except (KeyError, IndexError): removed.append((printing_node, index)) added = list( itertools.chain( *( nodes for nodes in constraint_map.values() ) ) ) return TrapDistribution(traps=distribution), added, removed
def test(): db = Loader.load() strategy = JsonId(db) cube = CubeLoader(db).load() constrained_nodes = NodeCollection( ConstrainedNodeFetcher(db).fetch_garbage()) groups = GroupMap(_GROUP_WEIGHTS) # s = '{"cube_delta": {}, "nodes_delta": {"nodes": []}}' # patch = strategy.deserialize(CubePatch, s) patch = CubePatch( CubeDeltaOperation({ db.cardboards['Brainstorm'].from_expansion('ICE'): -1, db.cardboards['Brainstorm'].from_expansion('EMA'): 1, # Trap( # AllNode( # ( # db.cardboards['Brainstorm'].from_expansion('ICE'), # db.cardboards['Web'].from_expansion('LEA'), # ) # ), # intention_type=IntentionType.SYNERGY, # ): 2 }), NodesDeltaOperation({ # ConstrainedNode( # node = AllNode( # ( # db.cardboards['Web'].from_expansion('LEA'), # ) # ), # groups = ['ok', 'lmao'], # value = 2, # ): 1, ConstrainedNode( node=AllNode(( db.cardboards['Brainstorm'].from_expansion('ICE'), db.cardboards['Web'].from_expansion('LEA'), )), groups=['lolHAHA'], value=1, ): 1, })) print(patch) meta_cube = MetaCube(cube, constrained_nodes, groups) verbose_patch = patch.as_verbose(meta_cube) print(verbose_patch) updater = CubeUpdater(meta_cube, patch) print(updater) report = UpdateReport(updater) for notification in report.notifications: print(notification.title + '\n' + notification.content + '\n\n')