<commit_before>package de.darwinspl.reconfigurator.client.hyvarrec;

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

import org.eclipse.emf.ecore.EObject;

import de.darwinspl.anomalies.explanations.AnomalyConstraintExplanation;
import de.darwinspl.anomaly.DwAnomaly;
import de.darwinspl.anomaly.DwDeadFeatureAnomaly;
import de.darwinspl.anomaly.DwFalseOptionalFeatureAnomaly;
import de.darwinspl.anomaly.DwVoidFeatureModelAnomaly;
import de.darwinspl.anomaly.explanation.DwAnomalyExplanation;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperation;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationAttribute;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationAttributeRename;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraint;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraintCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraintDelete;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationContext;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnum;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnumLiteral;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnumLiteralCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeature;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureGroup;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureRename;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureType;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureVersion;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationGroupType;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormula;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormulaCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormulaDelete;
import de.darwinspl.feature.evolution.editoroperation.EditorOperationExplanation;
import de.darwinspl.feature.evolution.editoroperation.EditoroperationFactory;
import eu.hyvar.context.HyContextModel;
import eu.hyvar.context.HyContextualInformation;
import eu.hyvar.context.contextValidity.HyValidityFormula;
import eu.hyvar.context.contextValidity.HyValidityModel;
import eu.hyvar.dataValues.HyEnum;
import eu.hyvar.dataValues.HyEnumLiteral;
import eu.hyvar.evolution.HyName;
import eu.hyvar.evolution.util.HyEvolutionUtil;
import eu.hyvar.feature.HyFeature;
import eu.hyvar.feature.HyFeatureAttribute;
import eu.hyvar.feature.HyFeatureChild;
import eu.hyvar.feature.HyFeatureModel;
import eu.hyvar.feature.HyFeatureType;
import eu.hyvar.feature.HyGroup;
import eu.hyvar.feature.HyGroupComposition;
import eu.hyvar.feature.HyGroupType;
import eu.hyvar.feature.HyVersion;
import eu.hyvar.feature.constraint.HyConstraint;
import eu.hyvar.feature.constraint.HyConstraintModel;
import eu.hyvar.feature.expression.HyAbstractFeatureReferenceExpression;
import eu.hyvar.feature.expression.HyBinaryExpression;
import eu.hyvar.feature.expression.HyExpression;
import eu.hyvar.feature.expression.HyUnaryExpression;
import eu.hyvar.feature.util.HyFeatureEvolutionUtil;

public class DwEditorOperationAnalyzer {
	
	private HyContextModel contextModel;
	private HyValidityModel contextValidityModel;
	private HyFeatureModel featureModel;
	private HyConstraintModel constraintModel;
	private Date date;
	
	List<DwEditorOperation> operationList;

	List<HyFeature> deadFeatureList = new ArrayList<HyFeature>();
	List<HyFeature> falseOptionalList = new ArrayList<HyFeature>();
	
	private DwAnalysesClient client;
	
	public DwEditorOperationAnalyzer(DwAnalysesClient client) {
		this.client = client;
	}
	
	public void constructEditorOperations() {
		operationList = new ArrayList<DwEditorOperation>();
		DwEditorOperation obj = null;
		if (getFeatureModel() != null) {
			
			for(HyFeature feature : getFeatureModel().getFeatures()) {
				// FEATURE CREATE
				obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureCreate();
				obj.setEvoStep(feature.getValidSince());
				((DwEditorOperationFeature) obj).setFeature(feature);
				operationList.add(obj);
				
				// FEATURE DELETE
				if (feature.getValidUntil() != null) {
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureDelete();
					obj.setEvoStep(feature.getValidUntil());
					((DwEditorOperationFeature) obj).setFeature(feature);
					operationList.add(obj);
				}
				
				// FEATURE NAME
				for (HyName name : feature.getNames()) {
					if (name.getValidSince() != null && !name.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change
						HyName predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getNames(), new Date(name.getValidSince().getTime() -1L));
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureRename();
						obj.setEvoStep(name.getValidSince());
						((DwEditorOperationFeature) obj).setFeature(feature);
						((DwEditorOperationFeatureRename) obj).setOldName(predecessor);
						((DwEditorOperationFeatureRename) obj).setNewName(name);
						operationList.add(obj);
					}
				}
				
				// FEATURE TYPE
				for (HyFeatureType type : feature.getTypes()) {
					if (type.getValidSince() != null && !type.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change
						HyFeatureType predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getTypes(), new Date(type.getValidSince().getTime() -1L));
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureType();
						obj.setEvoStep(type.getValidSince());
						((DwEditorOperationFeature) obj).setFeature(feature);
						((DwEditorOperationFeatureType) obj).setOldType(predecessor);
						((DwEditorOperationFeatureType) obj).setNewType(type);
						operationList.add(obj);
					}
				}
				
				for (HyVersion version : feature.getVersions()) {
					// FEATURE CREATE
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureVersionCreate();
					obj.setEvoStep(feature.getValidSince());
					((DwEditorOperationFeatureVersion) obj).setFeature(feature);
					((DwEditorOperationFeatureVersion) obj).setVersion(version);
					operationList.add(obj);

					// FEATURE CREATE
					if (version.getValidUntil() != null) {
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureVersionDelete();
						obj.setEvoStep(feature.getValidSince());
						((DwEditorOperationFeatureVersion) obj).setFeature(feature);
						((DwEditorOperationFeatureVersion) obj).setVersion(version);
						operationList.add(obj);
					}
				}
				
				// FEATURE GROUP
				for (HyGroupComposition group : feature.getGroupMembership()) {
					if (group.getValidSince() != null && !group.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change
						HyGroupComposition predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getGroupMembership(), new Date(group.getValidSince().getTime() -1L));
						if (!group.getCompositionOf().equals(predecessor.getCompositionOf())) {
							// TODO: only recognize group changes or group composition changes too?
							obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureGroup();
							obj.setEvoStep(group.getValidSince());
							((DwEditorOperationFeature) obj).setFeature(feature);
							((DwEditorOperationFeatureGroup) obj).setOldGroup(predecessor);
							((DwEditorOperationFeatureGroup) obj).setNewGroup(group);
							operationList.add(obj);
						}
					}
				}
				
				
				for (HyFeatureAttribute attribute : feature.getAttributes()) {
					
					// ATTRIBUTE CREATE
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeCreate();
					obj.setEvoStep(attribute.getValidSince());
					((DwEditorOperationAttribute) obj).setAttribute(attribute);
					operationList.add(obj);

					// ATTRIBUTE DELETE
					if (attribute.getValidUntil() != null) {
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeDelete();
						obj.setEvoStep(attribute.getValidSince());
						((DwEditorOperationAttribute) obj).setAttribute(attribute);
						operationList.add(obj);
					}

					// ATTRIBUTE RENAME
					for (HyName name : attribute.getNames()) {
						if (name.getValidSince() != null && !name.getValidSince().equals(attribute.getValidSince())) { // don't interpret default as change
							HyName predecessor = HyEvolutionUtil.getValidTemporalElement(attribute.getNames(), new Date(name.getValidSince().getTime() -1L));
							obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeRename();
							obj.setEvoStep(name.getValidSince());
							((DwEditorOperationAttribute) obj).setAttribute(attribute);
							((DwEditorOperationAttributeRename) obj).setOldName(predecessor);
							((DwEditorOperationAttributeRename) obj).setNewName(name);
							operationList.add(obj);
						}
					}
				}
			}
			
			for (HyGroup group : getFeatureModel().getGroups()) {		
				for (HyGroupType type : group.getTypes()) {
										
					// GROUP TYPE
					if (type.getValidSince() != null && !type.getValidSince().equals(group.getValidSince())) { // don't interpret default as change
						HyGroupType predecessor = HyEvolutionUtil.getValidTemporalElement(group.getTypes(), new Date(type.getValidSince().getTime() -1L));
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationGroupType();
						obj.setEvoStep(type.getValidSince());
						((DwEditorOperationGroupType) obj).setGroup(group);
						((DwEditorOperationGroupType) obj).setOldType(predecessor);
						((DwEditorOperationGroupType) obj).setNewType(type);
						operationList.add(obj);
					}
				}
			}
		}
		
		List<HyEnum> enumList = new ArrayList<HyEnum>();
		if (getContextModel() != null) {
			enumList.addAll(getContextModel().getEnums());
		}
		if (getFeatureModel() != null) {
			enumList.addAll(getFeatureModel().getEnums());
		}
		for (HyEnum hyEnum : enumList) {
			
			// ENUM CREATE
			obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationEnumCreate();
			obj.setEvoStep(hyEnum.getValidSince());
			((DwEditorOperationEnum) obj).setEnum(hyEnum);
			operationList.add(obj);
			<commit_msg>added superseded() usage when trying to get the predecessor<commit_after>package de.darwinspl.reconfigurator.client.hyvarrec;

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

import org.eclipse.emf.ecore.EObject;

import de.darwinspl.anomalies.explanations.AnomalyConstraintExplanation;
import de.darwinspl.anomaly.DwAnomaly;
import de.darwinspl.anomaly.DwDeadFeatureAnomaly;
import de.darwinspl.anomaly.DwFalseOptionalFeatureAnomaly;
import de.darwinspl.anomaly.DwVoidFeatureModelAnomaly;
import de.darwinspl.anomaly.explanation.DwAnomalyExplanation;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperation;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationAttribute;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationAttributeRename;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraint;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraintCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationConstraintDelete;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationContext;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnum;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnumLiteral;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationEnumLiteralCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeature;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureGroup;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureRename;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureType;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationFeatureVersion;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationGroupType;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormula;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormulaCreate;
import de.darwinspl.feature.evolution.editoroperation.DwEditorOperationValidityFormulaDelete;
import de.darwinspl.feature.evolution.editoroperation.EditorOperationExplanation;
import de.darwinspl.feature.evolution.editoroperation.EditoroperationFactory;
import eu.hyvar.context.HyContextModel;
import eu.hyvar.context.HyContextualInformation;
import eu.hyvar.context.contextValidity.HyValidityFormula;
import eu.hyvar.context.contextValidity.HyValidityModel;
import eu.hyvar.dataValues.HyEnum;
import eu.hyvar.dataValues.HyEnumLiteral;
import eu.hyvar.evolution.HyName;
import eu.hyvar.evolution.util.HyEvolutionUtil;
import eu.hyvar.feature.HyFeature;
import eu.hyvar.feature.HyFeatureAttribute;
import eu.hyvar.feature.HyFeatureChild;
import eu.hyvar.feature.HyFeatureModel;
import eu.hyvar.feature.HyFeatureType;
import eu.hyvar.feature.HyGroup;
import eu.hyvar.feature.HyGroupComposition;
import eu.hyvar.feature.HyGroupType;
import eu.hyvar.feature.HyVersion;
import eu.hyvar.feature.constraint.HyConstraint;
import eu.hyvar.feature.constraint.HyConstraintModel;
import eu.hyvar.feature.expression.HyAbstractFeatureReferenceExpression;
import eu.hyvar.feature.expression.HyBinaryExpression;
import eu.hyvar.feature.expression.HyExpression;
import eu.hyvar.feature.expression.HyUnaryExpression;
import eu.hyvar.feature.util.HyFeatureEvolutionUtil;

public class DwEditorOperationAnalyzer {
	
	private HyContextModel contextModel;
	private HyValidityModel contextValidityModel;
	private HyFeatureModel featureModel;
	private HyConstraintModel constraintModel;
	private Date date;
	
	List<DwEditorOperation> operationList;

	List<HyFeature> deadFeatureList = new ArrayList<HyFeature>();
	List<HyFeature> falseOptionalList = new ArrayList<HyFeature>();
	
	private DwAnalysesClient client;
	
	public DwEditorOperationAnalyzer(DwAnalysesClient client) {
		this.client = client;
	}
	
	public void constructEditorOperations() {
		operationList = new ArrayList<DwEditorOperation>();
		DwEditorOperation obj = null;
		if (getFeatureModel() != null) {
			
			for(HyFeature feature : getFeatureModel().getFeatures()) {
				// FEATURE CREATE
				obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureCreate();
				obj.setEvoStep(feature.getValidSince());
				((DwEditorOperationFeature) obj).setFeature(feature);
				operationList.add(obj);
				
				// FEATURE DELETE
				if (feature.getValidUntil() != null) {
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureDelete();
					obj.setEvoStep(feature.getValidUntil());
					((DwEditorOperationFeature) obj).setFeature(feature);
					operationList.add(obj);
				}
				
				// FEATURE NAME
				for (HyName name : feature.getNames()) {
					if (name.getValidSince() != null && !name.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change						
						HyName predecessor = (HyName) name.getSupersededElement();
						if (predecessor == null) {
							predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getNames(), new Date(name.getValidSince().getTime() -1L));
						}
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureRename();
						obj.setEvoStep(name.getValidSince());
						((DwEditorOperationFeature) obj).setFeature(feature);
						((DwEditorOperationFeatureRename) obj).setOldName(predecessor);
						((DwEditorOperationFeatureRename) obj).setNewName(name);
						operationList.add(obj);
					}
				}
				
				// FEATURE TYPE
				for (HyFeatureType type : feature.getTypes()) {
					if (type.getValidSince() != null && !type.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change
						HyFeatureType predecessor = (HyFeatureType) type.getSupersededElement();
						if (predecessor == null) {
							predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getTypes(), new Date(type.getValidSince().getTime() -1L));
						}
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureType();
						obj.setEvoStep(type.getValidSince());
						((DwEditorOperationFeature) obj).setFeature(feature);
						((DwEditorOperationFeatureType) obj).setOldType(predecessor);
						((DwEditorOperationFeatureType) obj).setNewType(type);
						operationList.add(obj);
					}
				}
				
				for (HyVersion version : feature.getVersions()) {
					// FEATURE CREATE
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureVersionCreate();
					obj.setEvoStep(feature.getValidSince());
					((DwEditorOperationFeatureVersion) obj).setFeature(feature);
					((DwEditorOperationFeatureVersion) obj).setVersion(version);
					operationList.add(obj);

					// FEATURE CREATE
					if (version.getValidUntil() != null) {
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureVersionDelete();
						obj.setEvoStep(feature.getValidSince());
						((DwEditorOperationFeatureVersion) obj).setFeature(feature);
						((DwEditorOperationFeatureVersion) obj).setVersion(version);
						operationList.add(obj);
					}
				}
				
				// FEATURE GROUP
				for (HyGroupComposition group : feature.getGroupMembership()) {
					if (group.getValidSince() != null && !group.getValidSince().equals(feature.getValidSince())) { // don't interpret default as change
						HyGroupComposition predecessor = (HyGroupComposition) group.getSupersededElement();
						if (predecessor == null) {
							predecessor = HyEvolutionUtil.getValidTemporalElement(feature.getGroupMembership(), new Date(group.getValidSince().getTime() -1L));
						}
						if (!group.getCompositionOf().equals(predecessor.getCompositionOf())) {
							// TODO: only recognize group changes or group composition changes too?
							obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationFeatureGroup();
							obj.setEvoStep(group.getValidSince());
							((DwEditorOperationFeature) obj).setFeature(feature);
							((DwEditorOperationFeatureGroup) obj).setOldGroup(predecessor);
							((DwEditorOperationFeatureGroup) obj).setNewGroup(group);
							operationList.add(obj);
						}
					}
				}
				
				
				for (HyFeatureAttribute attribute : feature.getAttributes()) {
					
					// ATTRIBUTE CREATE
					obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeCreate();
					obj.setEvoStep(attribute.getValidSince());
					((DwEditorOperationAttribute) obj).setAttribute(attribute);
					operationList.add(obj);

					// ATTRIBUTE DELETE
					if (attribute.getValidUntil() != null) {
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeDelete();
						obj.setEvoStep(attribute.getValidSince());
						((DwEditorOperationAttribute) obj).setAttribute(attribute);
						operationList.add(obj);
					}

					// ATTRIBUTE RENAME
					for (HyName name : attribute.getNames()) {
						if (name.getValidSince() != null && !name.getValidSince().equals(attribute.getValidSince())) { // don't interpret default as change
							HyName predecessor = (HyName) name.getSupersededElement();
							if (predecessor == null) {
								predecessor = HyEvolutionUtil.getValidTemporalElement(attribute.getNames(), new Date(name.getValidSince().getTime() -1L));
							}
							obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationAttributeRename();
							obj.setEvoStep(name.getValidSince());
							((DwEditorOperationAttribute) obj).setAttribute(attribute);
							((DwEditorOperationAttributeRename) obj).setOldName(predecessor);
							((DwEditorOperationAttributeRename) obj).setNewName(name);
							operationList.add(obj);
						}
					}
				}
			}
			
			for (HyGroup group : getFeatureModel().getGroups()) {		
				for (HyGroupType type : group.getTypes()) {
										
					// GROUP TYPE
					if (type.getValidSince() != null && !type.getValidSince().equals(group.getValidSince())) { // don't interpret default as change
						HyGroupType predecessor = (HyGroupType) type.getSupersededElement();
						if (predecessor == null) {
							predecessor = HyEvolutionUtil.getValidTemporalElement(group.getTypes(), new Date(type.getValidSince().getTime() -1L));
						}
						obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationGroupType();
						obj.setEvoStep(type.getValidSince());
						((DwEditorOperationGroupType) obj).setGroup(group);
						((DwEditorOperationGroupType) obj).setOldType(predecessor);
						((DwEditorOperationGroupType) obj).setNewType(type);
						operationList.add(obj);
					}
				}
			}
		}
		
		List<HyEnum> enumList = new ArrayList<HyEnum>();
		if (getContextModel() != null) {
			enumList.addAll(getContextModel().getEnums());
		}
		if (getFeatureModel() != null) {
			enumList.addAll(getFeatureModel().getEnums());
		}
		for (HyEnum hyEnum : enumList) {
			
			// ENUM CREATE
			obj = EditoroperationFactory.eINSTANCE.createDwEditorOperationEnumCreate();
			obj.setEvoStep(hyEnum.getValidSince());
			((DwEditorOperationEnum) obj).setEnum(hyEnum);
			operationList.add(obj);
			
{'fraction_non_alphanumeric': 0.10824742268041238, 'fraction_numerical': 0.00046438190768087673, 'mean_word_length': 5.128343767785998, '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': '2377227', 'n_tokens_mistral': 7513, 'n_tokens_neox': 6186, 'n_words': 896}