<commit_before>package de.darwinspl.configuration.generator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import de.darwinspl.feature.analyses.DwSatisfiabilityChecker;
import eu.hyvar.feature.HyFeature;
import eu.hyvar.feature.HyFeatureModel;
import eu.hyvar.feature.configuration.HyConfiguration;
import eu.hyvar.feature.configuration.HyConfigurationElement;
import eu.hyvar.feature.configuration.HyConfigurationFactory;
import eu.hyvar.feature.configuration.HyFeatureSelected;
import eu.hyvar.feature.configuration.HyFeatureSelection;
import eu.hyvar.feature.configuration.util.HyConfigurationCompleter;
import eu.hyvar.feature.configuration.util.HyConfigurationUtil;
import eu.hyvar.feature.configuration.util.HyConfigurationWellFormednessException;
import eu.hyvar.feature.constraint.HyConstraintModel;
import eu.hyvar.feature.util.HyFeatureEvolutionUtil;

public class DarwinConfigurationGenerator {

	// TODO incorporate versions
	// TODO incorporate attributes
	// TODO incorporate constraints
	
	private Random rand;
	private HyConfigurationFactory factory;
	
	public HyConfiguration generateConfiguration(HyFeatureModel featureModel, HyConstraintModel constraintModel, Date date) {
		rand = new Random();
		
		factory = HyConfigurationFactory.eINSTANCE;
		
		HyConfiguration configuration = factory.createHyConfiguration();
		configuration.setFeatureModel(featureModel);
		
		
		
		HyFeature rootFeature = HyFeatureEvolutionUtil.getRootFeature(featureModel, date);
		HyConfigurationUtil.selectFeature(rootFeature, configuration, date);
		
		List<HyFeature> possibleFeatures = new ArrayList<HyFeature>(HyFeatureEvolutionUtil.getFeatures(featureModel, date).size());
		possibleFeatures.addAll(HyFeatureEvolutionUtil.getFeatures(featureModel, date));
		possibleFeatures.remove(rootFeature);
		
		try {
			configuration = HyConfigurationCompleter.completeConfiguration(configuration, date);
		} catch (HyConfigurationWellFormednessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		removeFeaturesOfPossibleSelections(possibleFeatures, configuration);
		
		DwSatisfiabilityChecker satChecker = new DwSatisfiabilityChecker();
		
		
		// TODO dead end possible?
		while(possibleFeatures.size()>0) {
			HyFeatureSelection selection = null;
			
			// select random feature
			int index = rand.nextInt(possibleFeatures.size());		
			HyFeature feature = possibleFeatures.get(index);
			
			// randomly decide about selection / deselection
			int selected = rand.nextInt(2);
			
			boolean deselected = false;
			
			if(selected == 0) {
				// not selected
				deselected = true;
				selection = HyConfigurationUtil.deselectFeature(feature, configuration, date);
			} else {			
				// selected
				selection = HyConfigurationUtil.selectFeature(feature, configuration, date);
			}
			
			boolean wasSat = true;
			
			if(!satChecker.isSatisfiable(configuration, constraintModel, date)) {
				wasSat = false;
				configuration.getElements().remove(selection);
				
				if(deselected) {
					wasSat = true;
					deselected = false;
					
					selection = factory.createHyFeatureSelected();
					
					selection.setSelectedFeature(feature);
					
					configuration.getElements().add(selection);
				} else {
					deselected = true;
					
					selection = factory.createHyFeatureDeselected();
					
					selection.setSelectedFeature(feature);
					
					configuration.getElements().add(selection);
				}
			}
			
			if(wasSat && selection instanceof HyFeatureSelected) {
				try {
					configuration = HyConfigurationCompleter.completeConfiguration(configuration, date);
				} catch (HyConfigurationWellFormednessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			// remove all newly selected / deselected features from set of possible configurations
			removeFeaturesOfPossibleSelections(possibleFeatures, configuration);
//			possibleFeatures.remove(index);
		}
		
		
		return configuration;
	}
	
	protected void removeFeaturesOfPossibleSelections(List<HyFeature> possibleFeature, HyConfiguration configuration) {
		for(HyConfigurationElement configEle: configuration.getElements()) {
			if(configEle instanceof HyFeatureSelection) {
				possibleFeature.remove(((HyFeatureSelection)configEle).getSelectedFeature());
			}
		}
	}
}<commit_msg>tested random configurations<commit_after>package de.darwinspl.configuration.generator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import de.darwinspl.feature.analyses.DwSatisfiabilityChecker;
import eu.hyvar.feature.HyFeature;
import eu.hyvar.feature.HyFeatureModel;
import eu.hyvar.feature.configuration.HyConfiguration;
import eu.hyvar.feature.configuration.HyConfigurationElement;
import eu.hyvar.feature.configuration.HyConfigurationFactory;
import eu.hyvar.feature.configuration.HyFeatureSelected;
import eu.hyvar.feature.configuration.HyFeatureSelection;
import eu.hyvar.feature.configuration.util.HyConfigurationCompleter;
import eu.hyvar.feature.configuration.util.HyConfigurationUtil;
import eu.hyvar.feature.configuration.util.HyConfigurationWellFormednessException;
import eu.hyvar.feature.constraint.HyConstraintModel;
import eu.hyvar.feature.util.HyFeatureEvolutionUtil;

public class DarwinConfigurationGenerator {

	// TODO incorporate versions
	// TODO incorporate attributes
	// TODO incorporate constraints
	
	private Random rand;
	private HyConfigurationFactory factory;
	
	public HyConfiguration generateConfiguration(HyFeatureModel featureModel, HyConstraintModel constraintModel, Date date) {
		rand = new Random();
		
		factory = HyConfigurationFactory.eINSTANCE;
		
		HyConfiguration configuration = factory.createHyConfiguration();
		configuration.setFeatureModel(featureModel);
		
		
		
		HyFeature rootFeature = HyFeatureEvolutionUtil.getRootFeature(featureModel, date);
		HyConfigurationUtil.selectFeature(rootFeature, configuration, date);
		
		List<HyFeature> possibleFeatures = new ArrayList<HyFeature>(HyFeatureEvolutionUtil.getFeatures(featureModel, date).size());
		possibleFeatures.addAll(HyFeatureEvolutionUtil.getFeatures(featureModel, date));
		possibleFeatures.remove(rootFeature);
		
//		try {
//			configuration = HyConfigurationCompleter.completeConfiguration(configuration, date);
//		} catch (HyConfigurationWellFormednessException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		possibleFeatures.remove(rootFeature);
//		removeFeaturesOfPossibleSelections(possibleFeatures, configuration);
		
		DwSatisfiabilityChecker satChecker = new DwSatisfiabilityChecker();
		
		
		// TODO dead end possible?
		while(possibleFeatures.size()>0) {
			HyFeatureSelection selection = null;
			
			// select random feature
			int index = rand.nextInt(possibleFeatures.size());		
			HyFeature feature = possibleFeatures.get(index);
			
			// randomly decide about selection / deselection
			int selected = rand.nextInt(2);
			
			boolean deselected = false;
			
			if(selected == 0) {
				// not selected
				deselected = true;
				selection = HyConfigurationUtil.deselectFeature(feature, configuration, date);
			} else {			
				// selected
				selection = HyConfigurationUtil.selectFeature(feature, configuration, date);
			}
			
//			boolean wasSat = true;
			
			if(!satChecker.isSatisfiable(configuration, constraintModel, date)) {
//				wasSat = false;
				configuration.getElements().remove(selection);
				
				if(deselected) {
//					wasSat = true;
					deselected = false;
					
					selection = factory.createHyFeatureSelected();
					
					selection.setSelectedFeature(feature);
					
					configuration.getElements().add(selection);
				} else {
					deselected = true;
					
					selection = factory.createHyFeatureDeselected();
					
					selection.setSelectedFeature(feature);
					
					configuration.getElements().add(selection);
				}
			}
			
//			if(wasSat && selection instanceof HyFeatureSelected) {
//				try {
//					configuration = HyConfigurationCompleter.completeConfiguration(configuration, date);
//				} catch (HyConfigurationWellFormednessException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
			
			// remove all newly selected / deselected features from set of possible configurations
//			removeFeaturesOfPossibleSelections(possibleFeatures, configuration);
			possibleFeatures.remove(index);
		}
		
		
		return configuration;
	}
	
	protected void removeFeaturesOfPossibleSelections(List<HyFeature> possibleFeature, HyConfiguration configuration) {
		for(HyConfigurationElement configEle: configuration.getElements()) {
			if(configEle instanceof HyFeatureSelection) {
				possibleFeature.remove(((HyFeatureSelection)configEle).getSelectedFeature());
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.09112492933860938, 'fraction_numerical': 0.0011305822498586771, 'mean_word_length': 4.546081504702195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '4160390', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2264, 'n_words': 439}