From 392c6096df3a56d8d23278624f670661b6c7f734 Mon Sep 17 00:00:00 2001 From: Joana Bergsiek Date: Thu, 4 Jan 2024 02:57:34 +0100 Subject: [PATCH] Ignore un-nested permutations which are not possible --- packages/Sandblocks-Core/SBBlock.class.st | 12 +++++++ .../Sandblocks-Smalltalk/SBVariant.class.st | 23 ++++++++++++++ .../Sandblocks-Utils/SBPermutation.class.st | 31 ++++++++++++------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/packages/Sandblocks-Core/SBBlock.class.st b/packages/Sandblocks-Core/SBBlock.class.st index 091b5929..faac9c3e 100644 --- a/packages/Sandblocks-Core/SBBlock.class.st +++ b/packages/Sandblocks-Core/SBBlock.class.st @@ -2229,6 +2229,18 @@ SBBlock >> parentHasDynamicNumberOfChildren [ ^ self parentSandblock fixedNumberOfChildren not ] +{ #category : #accessing } +SBBlock >> parentVariant [ + + ^ self parentSandblock + ifNil: [nil] + ifNotNil: [:theParent | + theParent isVariant + ifTrue: [theParent] + ifFalse: [theParent parentVariant]] + +] + { #category : #actions } SBBlock >> pasteAfter [ diff --git a/packages/Sandblocks-Smalltalk/SBVariant.class.st b/packages/Sandblocks-Smalltalk/SBVariant.class.st index 2f90e72c..386c972e 100644 --- a/packages/Sandblocks-Smalltalk/SBVariant.class.st +++ b/packages/Sandblocks-Smalltalk/SBVariant.class.st @@ -155,6 +155,29 @@ SBVariant >> alternativesEqual: otherAlternatives [ areSame] ] +{ #category : #converting } +SBVariant >> asNestedPaths [ + + | allPaths | + allPaths := OrderedCollection new. + self asNestedPaths: allPaths currentPath: (SBPermutation new referencedVariants: OrderedCollection new). + ^ allPaths +] + +{ #category : #converting } +SBVariant >> asNestedPaths: allPaths currentPath: aPermutation [ + + "Private helper function" + self namedBlocks withIndexCollect: [:aNamedBlock :i | | nestedVariants currentPath | + nestedVariants := aNamedBlock block childSandblocks select: #isVariant. + currentPath := aPermutation copyWith: (self id -> i). + currentPath referencedVariants: (aPermutation referencedVariants copyWith: self). + nestedVariants + ifEmpty: [allPaths add: currentPath] + ifNotEmpty: [:children | children do: [:child | + child asNestedPaths: allPaths currentPath: currentPath]]] +] + { #category : #converting } SBVariant >> asProxy [ diff --git a/packages/Sandblocks-Utils/SBPermutation.class.st b/packages/Sandblocks-Utils/SBPermutation.class.st index 89a6b07c..bc74624f 100644 --- a/packages/Sandblocks-Utils/SBPermutation.class.st +++ b/packages/Sandblocks-Utils/SBPermutation.class.st @@ -14,23 +14,32 @@ Class { { #category : #utils } SBPermutation class >> allPermutationsOf: aCollectionOfVariants [ - | permutations | + | permutations topLevelVariants nestedPermutations | aCollectionOfVariants ifEmpty:[^{SBNilPermutation new referencedVariants: {}}]. - permutations := (1 to: aCollectionOfVariants first alternativesCount) collect: #asArray. + topLevelVariants := aCollectionOfVariants select: [:aVariant | aVariant parentVariant isNil]. + nestedPermutations := topLevelVariants collect: #asNestedPaths. + permutations := nestedPermutations first. - (2 to: aCollectionOfVariants size) do: [:i | | alternatives | - alternatives := (aCollectionOfVariants at: i) alternativesCount. - permutations := permutations gather: [:aCollectionOfIndexes | - (1 to: alternatives) collect: [:aTabIndex | - {aCollectionOfIndexes. aTabIndex} flatten]]]. + (2 to: topLevelVariants size) do: [:i | | nestedPermutation | + nestedPermutation := (nestedPermutations at: i). + permutations := permutations gather: [:aPermutation | + nestedPermutation collect: [:aNestedPermutation | self newCombinedOf: aPermutation and: aNestedPermutation]]]. - ^ permutations collect: [:aCollectionOfIndexes | - (self withAll: (aCollectionOfIndexes withIndexCollect: [:anAlternativeIndex :aVariantIndex | - (aCollectionOfVariants at: aVariantIndex) id -> anAlternativeIndex])) - referencedVariants: aCollectionOfVariants] + ^ permutations ] +{ #category : #utils } +SBPermutation class >> newCombinedOf: onePermutation and: anotherPermutation [ + + | result | + result := self new referencedVariants: (onePermutation referencedVariants, anotherPermutation referencedVariants). + result addAll: onePermutation. + result addAll: anotherPermutation. + ^ result + +] + { #category : #actions } SBPermutation >> apply [