Refactor ProductGamesystemGenerator to consider other alternative implementations of product systems
Some checks failed
E2E Testing / test (push) Failing after 1m43s

This commit is contained in:
Sebastian Böckelmann 2024-04-12 14:27:50 +02:00
parent fd4dc7b801
commit 2e66717ae4
2 changed files with 186 additions and 179 deletions

View File

@ -3,21 +3,181 @@ import {ScriptAccountAction} from "../actions/ScriptAccountAction";
import {ScriptAccountCondition} from "../conditions/ScriptAccountCondition"; import {ScriptAccountCondition} from "../conditions/ScriptAccountCondition";
import {State} from "../states/State"; import {State} from "../states/State";
import {ProductGamesystem} from "../ProductGamesystem"; import {ProductGamesystem} from "../ProductGamesystem";
import {ProductGenerationData} from "./ProductGenerationData";
import {ProductGeneratorResult} from "./ProductGeneratorResult";
import {ProductState} from "../states/ProductState";
import {ProductTransition} from "../transitions/ProductTransition";
import {Transition} from "../transitions/Transition";
export abstract class AbstractProductGamesystemGenerator { export abstract class AbstractProductGamesystemGenerator {
productGamesystem: ProductGamesystem
protected integratedSystems: Gamesystem<any, any>[] = []
prepare() { constructor(productGamesystem: ProductGamesystem) {
this.integratedSystems = [] this.productGamesystem = productGamesystem;
} }
abstract generateFromChildsystems(ProductGamesystem: ProductGamesystem): void; generateFromChildsystems(): void {
if(this.productGamesystem.innerGamesystems.length < 2) return;
const leftInitialData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[0])
const rightInitialData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[1])
abstract generateFromBinaryChildsystems(leftSystem: Gamesystem<any, any>, rightSystem: any, left_temp: boolean): void; const initialGenerationResult = this.generateFromBinaryChildsystems(leftInitialData, rightInitialData);
if(this.productGamesystem.innerGamesystems.length > 2) {
for(let i=2; i<this.productGamesystem.innerGamesystems.length; i++) {
const leftData = initialGenerationResult.productGenerationData;
const rightData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[i]);
abstract generateBinaryProductState(leftInnerState: State<any>, rightInnerState: State<any>, left_temp: boolean): State<any>; const generationResult = this.generateFromBinaryChildsystems(leftData, rightData);
this.productGamesystem.states = generationResult.states;
this.productGamesystem.transitions = generationResult.transitions;
}
} else {
this.productGamesystem.states = initialGenerationResult.states;
this.productGamesystem.transitions = initialGenerationResult.transitions
}
}
protected prepareChildsystemForGeneration(childsystem: Gamesystem<any, any>) {
if(childsystem instanceof ProductGamesystem) {
childsystem.productGamesystemGenerator.generateFromChildsystems();
}
return new ProductGenerationData(childsystem.states, childsystem.transitions)
}
protected generateFromBinaryChildsystems(leftSystemData: ProductGenerationData, rightSystemData: ProductGenerationData): ProductGeneratorResult {
const generatedProductStates: ProductState[] = []
const generatedProductTransitions: ProductTransition[] = []
leftSystemData.states.forEach(leftState => {
rightSystemData.states.forEach(rightState => {
for(let i=0; i<leftState.outgoingTransitions.length; i++) {
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
if(startingState != undefined) {
generatedProductStates.push(startingState);
const endingState_right = this.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(endingState_right != undefined) {
const transition_right = this.generateBinaryProductTransition(startingState, endingState_right, rightState.outgoingTransitions[j])
generatedProductTransitions.push(transition_right)
generatedProductStates.push(endingState_right)
}
const endingState_left = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, generatedProductStates);
if(endingState_left != undefined) {
const transition = this.generateBinaryProductTransition(startingState, endingState_left, leftState.outgoingTransitions[i])
generatedProductTransitions.push(transition)
generatedProductStates.push(endingState_left)
}
const endingState_left_right = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(endingState_left_right != undefined) {
const transition = this.generateBinaryProductTransitionMulti(startingState, endingState_left_right, leftState.outgoingTransitions[i], rightState.outgoingTransitions[j]);
if(transition != undefined) {
generatedProductStates.push(endingState_left_right);
generatedProductTransitions.push(transition);
}
}
}
}
if(rightState.outgoingTransitions.length == 0) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
const endingState = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, generatedProductStates);
if(startingState != undefined && endingState != undefined) {
generatedProductStates.push(startingState)
generatedProductStates.push(endingState)
const transition = this.generateBinaryProductTransition(startingState, endingState, leftState.outgoingTransitions[i])
generatedProductTransitions.push(transition)
}
}
}
if(leftState.outgoingTransitions.length == 0) {
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
const endingState = this.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(startingState != undefined && endingState != undefined) {
generatedProductStates.push(startingState)
generatedProductStates.push(endingState)
const transition = this.generateBinaryProductTransition(startingState, endingState, rightState.outgoingTransitions[j]);
generatedProductTransitions.push(transition)
}
}
}
})
})
return new ProductGeneratorResult(generatedProductStates, generatedProductTransitions);
}
protected abstract generateBinaryProductTransition(leftState: State<any>, rightState: State<any>, usedTransition: Transition<any>): ProductTransition;
protected abstract generateBinaryProductTransitionMulti(leftState: State<any>, rightState: State<any>, leftTransition: Transition<any>, rightTransition: Transition<any>): ProductTransition | undefined
protected abstract generateBinaryProductState(leftState: State<any>, rightState: State<any>, generadedStates: ProductState[]): ProductState | undefined
protected findGeneratedProductState(innerStates: State<any>[], productStates: ProductState[]) {
return undefined
}
protected generateCombinedActions(leftActions: ScriptAccountAction[], rightActions: ScriptAccountAction[]): ScriptAccountAction[] {
const combinedActions: ScriptAccountAction[] = []
for(let i=0; i<leftActions.length; i++) {
for(let j=0; j<rightActions.length; j++) {
const combinedAction = leftActions[i].combineActions(rightActions[j])
if(combinedAction == undefined) {
if(!combinedActions.includes(leftActions[i])) {
combinedActions.push(leftActions[i])
}
if(!combinedActions.includes(rightActions[j])) {
combinedActions.push(rightActions[j])
}
} else {
combinedActions.push(combinedAction)
}
}
}
return combinedActions;
}
protected generateCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]): ScriptAccountCondition[] {
const combinedConditions: ScriptAccountCondition[] = [];
for(let i=0; i<leftConditions.length; i++) {
for(let j=0; j<rightConditions.length; j++) {
const combinedCondition = leftConditions[i].combineCondition(rightConditions[j]);
if(combinedCondition == undefined) {
if(!combinedConditions.includes(leftConditions[i])) {
combinedConditions.push(leftConditions[i])
}
if(!combinedConditions.includes(rightConditions[j])) {
combinedConditions.push(rightConditions[j])
}
} else {
combinedConditions.push(combinedCondition)
}
}
}
return combinedConditions;
}
protected contradictCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]): boolean {
for(let i=0; i<leftConditions.length; i++) {
for(let j=0; j<rightConditions.length; j++) {
if(leftConditions[i].isContradicting(rightConditions[j])) {
return true;
}
}
}
return false;
}
} }

View File

@ -8,124 +8,12 @@ import {ScriptAccountCondition} from "../conditions/ScriptAccountCondition";
import {ProductTransition} from "../transitions/ProductTransition"; import {ProductTransition} from "../transitions/ProductTransition";
import {Transition} from "../transitions/Transition"; import {Transition} from "../transitions/Transition";
import {ScriptAccountAction} from "../actions/ScriptAccountAction"; import {ScriptAccountAction} from "../actions/ScriptAccountAction";
import {AbstractProductGamesystemGenerator} from "./AbstractProductGamesystemGenerator";
export class ProductGamesystemGenerator { export class ProductGamesystemGenerator extends AbstractProductGamesystemGenerator{
productGamesystem: ProductGamesystem
constructor(productGamesystem: ProductGamesystem) { protected generateBinaryProductTransition(startingState: ProductState, endingState: ProductState, usedTransition: Transition<any>) {
this.productGamesystem = productGamesystem;
}
generateFromChildsystems(): void {
if(this.productGamesystem.innerGamesystems.length < 2) return;
const leftInitialData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[0])
const rightInitialData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[1])
const initialGenerationResult = this.generateFromBinaryChildsystems(leftInitialData, rightInitialData);
if(this.productGamesystem.innerGamesystems.length > 2) {
for(let i=2; i<this.productGamesystem.innerGamesystems.length; i++) {
const leftData = initialGenerationResult.productGenerationData;
const rightData = this.prepareChildsystemForGeneration(this.productGamesystem.innerGamesystems[i]);
const generationResult = this.generateFromBinaryChildsystems(leftData, rightData);
this.productGamesystem.states = generationResult.states;
this.productGamesystem.transitions = generationResult.transitions;
}
} else {
this.productGamesystem.states = initialGenerationResult.states;
this.productGamesystem.transitions = initialGenerationResult.transitions
}
}
private prepareChildsystemForGeneration(childsystem: Gamesystem<any, any>) {
if(childsystem instanceof ProductGamesystem) {
childsystem.productGamesystemGenerator.generateFromChildsystems();
}
return new ProductGenerationData(childsystem.states, childsystem.transitions)
}
private generateFromBinaryChildsystems(leftSystemData: ProductGenerationData, rightSystemData: ProductGenerationData): ProductGeneratorResult {
const generatedProductStates: ProductState[] = []
const generatedProductTransitions: ProductTransition[] = []
leftSystemData.states.forEach(leftState => {
rightSystemData.states.forEach(rightState => {
for(let i=0; i<leftState.outgoingTransitions.length; i++) {
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
if(startingState != undefined) {
generatedProductStates.push(startingState);
const endingState_right = this.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(endingState_right != undefined) {
const transition_right = this.generateBinaryProductTransition(startingState, endingState_right, rightState.outgoingTransitions[j])
generatedProductTransitions.push(transition_right)
generatedProductStates.push(endingState_right)
}
const endingState_left = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, generatedProductStates);
if(endingState_left != undefined) {
const transition = this.generateBinaryProductTransition(startingState, endingState_left, leftState.outgoingTransitions[i])
generatedProductTransitions.push(transition)
generatedProductStates.push(endingState_left)
}
const endingState_left_right = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(endingState_left_right != undefined) {
const leftConditions = leftState.outgoingTransitions[i].scriptAccountConditions;
const rightConditions = rightState.outgoingTransitions[j].scriptAccountConditions;
if(!this.contradictCombinedConditions(leftConditions, rightConditions)) {
const transition = new ProductTransition(startingState, endingState_left_right)
transition.scriptAccountActions = this.generateCombinedActions(leftState.outgoingTransitions[i].scriptAccountActions, rightState.outgoingTransitions[j].scriptAccountActions);
transition.scriptAccountConditions = this.generateCombinedConditions(leftState.outgoingTransitions[i].scriptAccountConditions, rightState.outgoingTransitions[j].scriptAccountConditions);
generatedProductTransitions.push(transition)
generatedProductStates.push(endingState_left_right)
}
}
}
}
if(rightState.outgoingTransitions.length == 0) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
const endingState = this.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, generatedProductStates);
if(startingState != undefined && endingState != undefined) {
generatedProductStates.push(startingState)
generatedProductStates.push(endingState)
const transition = this.generateBinaryProductTransition(startingState, endingState, leftState.outgoingTransitions[i])
generatedProductTransitions.push(transition)
}
}
}
if(leftState.outgoingTransitions.length == 0) {
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
const startingState = this.generateBinaryProductState(leftState, rightState, generatedProductStates);
const endingState = this.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, generatedProductStates);
if(startingState != undefined && endingState != undefined) {
generatedProductStates.push(startingState)
generatedProductStates.push(endingState)
const transition = this.generateBinaryProductTransition(startingState, endingState, rightState.outgoingTransitions[j]);
generatedProductTransitions.push(transition)
}
}
}
})
})
return new ProductGeneratorResult(generatedProductStates, generatedProductTransitions);
}
private generateBinaryProductTransition(startingState: ProductState, endingState: ProductState, usedTransition: Transition<any>) {
const transition = new ProductTransition(startingState, endingState); const transition = new ProductTransition(startingState, endingState);
transition.scriptAccountActions = [... usedTransition.scriptAccountActions]; transition.scriptAccountActions = [... usedTransition.scriptAccountActions];
transition.scriptAccountConditions = [... usedTransition.scriptAccountConditions]; transition.scriptAccountConditions = [... usedTransition.scriptAccountConditions];
@ -133,7 +21,22 @@ export class ProductGamesystemGenerator {
return transition; return transition;
} }
private generateBinaryProductState(leftState: State<any>, rightState: State<any>, generatedStates: ProductState[]): ProductState | undefined { protected generateBinaryProductTransitionMulti(startingState: ProductState, endingState: ProductState, leftTransition: Transition<any>, rightTransition: Transition<any>) {
const leftConditions = leftTransition.scriptAccountConditions;
const rightConditions = rightTransition.scriptAccountConditions;
if(!this.contradictCombinedConditions(leftConditions, rightConditions)) {
const transition = new ProductTransition(startingState, endingState)
transition.scriptAccountActions = this.generateCombinedActions(leftTransition.scriptAccountActions, rightTransition.scriptAccountActions);
transition.scriptAccountConditions = this.generateCombinedConditions(leftTransition.scriptAccountConditions, rightTransition.scriptAccountConditions);
return transition;
}
return undefined;
}
protected generateBinaryProductState(leftState: State<any>, rightState: State<any>, generatedStates: ProductState[]): ProductState | undefined {
const combinedStateConditions: ScriptAccountCondition[] = leftState.conditions.concat(rightState.conditions); const combinedStateConditions: ScriptAccountCondition[] = leftState.conditions.concat(rightState.conditions);
for(let i=0; i<combinedStateConditions.length; i++) { for(let i=0; i<combinedStateConditions.length; i++) {
for(let j=0; j<combinedStateConditions.length; j++) { for(let j=0; j<combinedStateConditions.length; j++) {
@ -155,63 +58,7 @@ export class ProductGamesystemGenerator {
return binaryProductState; return binaryProductState;
} }
private findGeneratedProductState(innerStates: State<any>[], productStates: ProductState[]) {
return undefined
}
private generateCombinedActions(leftActions: ScriptAccountAction[], rightActions: ScriptAccountAction[]): ScriptAccountAction[] {
const combinedActions: ScriptAccountAction[] = []
for(let i=0; i<leftActions.length; i++) {
for(let j=0; j<rightActions.length; j++) {
const combinedAction = leftActions[i].combineActions(rightActions[j])
if(combinedAction == undefined) {
if(!combinedActions.includes(leftActions[i])) {
combinedActions.push(leftActions[i])
}
if(!combinedActions.includes(rightActions[j])) {
combinedActions.push(rightActions[j])
}
} else {
combinedActions.push(combinedAction)
}
}
}
return combinedActions;
}
private generateCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]): ScriptAccountCondition[] {
const combinedConditions: ScriptAccountCondition[] = [];
for(let i=0; i<leftConditions.length; i++) {
for(let j=0; j<rightConditions.length; j++) {
const combinedCondition = leftConditions[i].combineCondition(rightConditions[j]);
if(combinedCondition == undefined) {
if(!combinedConditions.includes(leftConditions[i])) {
combinedConditions.push(leftConditions[i])
}
if(!combinedConditions.includes(rightConditions[j])) {
combinedConditions.push(rightConditions[j])
}
} else {
combinedConditions.push(combinedCondition)
}
}
}
return combinedConditions;
}
private contradictCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]): boolean {
for(let i=0; i<leftConditions.length; i++) {
for(let j=0; j<rightConditions.length; j++) {
if(leftConditions[i].isContradicting(rightConditions[j])) {
return true;
}
}
}
return false;
}