<commit_before>package org.foam.ucm.util

import org.foam.annotation.Annotation
import com.google.common.base.Preconditions
import org.foam.flowannotation.Include
import java.util.Collections
import java.util.HashSet
import java.util.Set
import org.foam.ucm.Scenario
import org.foam.ucm.ScenarioHolder
import org.foam.ucm.Step
import org.foam.ucm.UseCase

class MyTest {
	
}
class UcmUtils {
	
	def static Iterable<Annotation> getStepAnnotations(UseCase useCase) {
		val holders = useCase.branches.values
		val extScenarioLists = holders.map[it.extensions]
		val varScenarioLists = holders.map[it.variations]
		val allScenarios = (extScenarioLists + varScenarioLists).flatten + Collections.singletonList(useCase.mainScenario)
		allScenarios.map[getStepAnnotations(it)].flatten
	}
	
	def static Iterable<Annotation> getStepAnnotations(Scenario scenario) {
		scenario.steps.map[it.annotations].flatten
	}
	
	def static Iterable<Step> allSteps(UseCase useCase) {
		allScenarios(useCase).map[it.steps].flatten
	}
	
	def static Iterable<Scenario> allScenarios(UseCase useCase) {
		return Collections.singletonList(useCase.mainScenario) 
			+ useCase.branches.values.map[it.branches.map[value as Scenario]].flatten
	}
	
	def static Set<UseCase> getPreceededTransitively(UseCase useCase) {
		Preconditions.checkNotNull(useCase)
		
		val result = new HashSet<UseCase>
		getPreceededRecursively(useCase, result)
		result
	}
	
	def private static void getPreceededRecursively(UseCase useCase, Set<UseCase> result) {
		for (preceededUseCase : useCase.preceeded) {
			if (!result.contains(preceededUseCase)) {
				result += preceededUseCase
				getPreceededRecursively(preceededUseCase, result)
			}
		}
	}
	
	def static Set<UseCase> getIncluded(UseCase useCase) {
		allSteps(useCase).map[
			annotations.filter(Include).map[inlinedUseCase]
		].flatten.toSet
	}
	
	def static Set<UseCase> getIncludedTransitively(UseCase useCase) {
		Preconditions.checkNotNull(useCase)
		
		val result = <UseCase> newHashSet
		getIncludedUseCasesRecursively(useCase, result)
		result
	}
	
	def private static void getIncludedUseCasesRecursively(UseCase useCase, Set<UseCase> result) {
		val includedUseCases = getIncluded(useCase)
		
		for (includedUseCase  : includedUseCases) {
			if (!result.contains(includedUseCase)) {
				result += includedUseCase				
				getIncludedUseCasesRecursively(includedUseCase, result)
			}
		}
	}
	
	def static UseCase getUseCase(Step step) {
		Preconditions.checkNotNull(step)
		val scenario = step.eContainer as Scenario
		getUseCase(scenario)
	}
	
	def static UseCase getUseCase(Scenario scenario) {
		Preconditions.checkNotNull(scenario)
		val scenarioParent = scenario.eContainer()
		return switch scenarioParent {
			ScenarioHolder: {
				val entry = scenarioParent.eContainer
				entry.eContainer as UseCase
			}
			default:
				scenarioParent as UseCase
		}
	}
	
	def static Scenario getScenario(Step step) {
		Preconditions.checkNotNull(step)
		step.eContainer as Scenario
	}
}<commit_msg>cleaned xtend code<commit_after>package org.foam.ucm.util

import com.google.common.base.Preconditions
import java.util.Set
import org.foam.flowannotation.Include
import org.foam.ucm.Scenario
import org.foam.ucm.ScenarioHolder
import org.foam.ucm.Step
import org.foam.ucm.UseCase

class UcmUtils {
	
	def static getStepAnnotations(UseCase useCase) {
		val holders = useCase.branches.values
		val extScenarioLists = holders.map[extensions]
		val varScenarioLists = holders.map[variations]
		val allScenarios = #[useCase.mainScenario] + (extScenarioLists + varScenarioLists).flatten
		allScenarios.map[getStepAnnotations].flatten
	}
	
	def static getStepAnnotations(Scenario scenario) {
		scenario.steps.map[annotations].flatten
	}
	
	def static allSteps(UseCase useCase) {
		useCase.allScenarios.map[steps].flatten
	}
	
	def static allScenarios(UseCase useCase) {
		#[useCase.mainScenario]
		+ useCase.branches.values.map[branches.map[value as Scenario]].flatten
	}
	
	def static Set<UseCase> getPreceededTransitively(UseCase useCase) {
		Preconditions.checkNotNull(useCase)
		
		val result = <UseCase> newHashSet
		getPreceededRecursively(useCase, result)
		result
	}
	
	def private static void getPreceededRecursively(UseCase useCase, Set<UseCase> result) {
		for (preceededUseCase : useCase.preceeded) {
			if (!result.contains(preceededUseCase)) {
				result += preceededUseCase
				getPreceededRecursively(preceededUseCase, result)
			}
		}
	}
	
	def static getIncluded(UseCase useCase) {
		useCase.allSteps.map[
			annotations.filter(Include).map[inlinedUseCase]
		].flatten.toSet
	}
	
	def static getIncludedTransitively(UseCase useCase) {
		Preconditions.checkNotNull(useCase)
		
		val result = <UseCase> newHashSet
		getIncludedUseCasesRecursively(useCase, result)
		result
	}
	
	def private static void getIncludedUseCasesRecursively(UseCase useCase, Set<UseCase> result) {
		for (includedUseCase  : useCase.getIncluded) {
			if (!result.contains(includedUseCase)) {
				result += includedUseCase				
				getIncludedUseCasesRecursively(includedUseCase, result)
			}
		}
	}
	
	def static UseCase getUseCase(Step step) {
		Preconditions.checkNotNull(step)
		(step.eContainer as Scenario).getUseCase
	}
	
	def static UseCase getUseCase(Scenario scenario) {
		Preconditions.checkNotNull(scenario)
		val scenarioParent = scenario.eContainer
		return switch scenarioParent {
			ScenarioHolder: {
				val entry = scenarioParent.eContainer
				entry.eContainer as UseCase
			}
			default:
				scenarioParent as UseCase
		}
	}
	
	def static Scenario getScenario(Step step) {
		Preconditions.checkNotNull(step)
		step.eContainer as Scenario
	}
}
{'fraction_non_alphanumeric': 0.08641756684965468, 'fraction_numerical': 0.0, 'mean_word_length': 4.9017763845350055, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2011284', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1825, 'n_words': 347}