<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Threading;
using NMF.Serialization;
using NMF.Models.Repository;
using NMF.Expressions;
using NMF.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Web;
using System.Collections;

namespace NMF.Models
{
    /// <summary>
    /// Defines the base class for a model element implementation
    /// </summary>
    public abstract class ModelElement : IModelElement, INotifyPropertyChanged
    {
        private IModelElement parent;
        private ObservableList<ModelElementExtension> extensions;

        /// <summary>
        /// Gets the model that contains the current model element
        /// </summary>
        public Model Model
        {
            get
            {
                IModelElement current = this;
                while (current != null)
                {
                    var model = current as Model;
                    if (model != null)
                    {
                        return model;
                    }
                    else
                    {
                        current = current.Parent;
                    }
                }
                return null;
            }
        }


        /// <summary>
        /// Sets the parent for the current model element to the given element
        /// </summary>
        /// <param name="newParent">The new parent for the given element</param>
        private void SetParent(IModelElement newParent)
        {
            if (newParent != parent)
            {
                var oldParent = parent;
                parent = newParent;
                if (newParent != null)
                {
                    var newModel = newParent.Model;
                    var oldModel = oldParent != null ? oldParent.Model : null;
                    if (oldParent != null)
                    {
                        oldParent.Deleted -= CascadeDelete;
                        if (EnforceModels && oldModel != null && oldModel == oldParent.Parent)
                        {
                            oldModel.RootElements.Add(newParent);
                        }
                    }
                    newParent.Deleted += CascadeDelete;
                    var newParentME = newParent as ModelElement;
                    if (newParentME != null)
                    {
                        newParentME.OnChildCreated(this);
                    }
                    if (newModel != oldModel)
                    {
                        PropagateNewModel(newModel, oldModel, this);
                    }
                }
                else
                {
                    if (oldParent != null)
                    {
                        var oldModel = oldParent.Model;
                        oldParent.Deleted -= CascadeDelete;<commit_msg>Fixed cyclic dependency when an element's old model is the element's new model.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Threading;
using NMF.Serialization;
using NMF.Models.Repository;
using NMF.Expressions;
using NMF.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Web;
using System.Collections;

namespace NMF.Models
{
    /// <summary>
    /// Defines the base class for a model element implementation
    /// </summary>
    public abstract class ModelElement : IModelElement, INotifyPropertyChanged
    {
        private IModelElement parent;
        private ObservableList<ModelElementExtension> extensions;

        /// <summary>
        /// Gets the model that contains the current model element
        /// </summary>
        public Model Model
        {
            get
            {
                IModelElement current = this;
                while (current != null)
                {
                    var model = current as Model;
                    if (model != null)
                    {
                        return model;
                    }
                    else
                    {
                        current = current.Parent;
                    }
                }
                return null;
            }
        }


        /// <summary>
        /// Sets the parent for the current model element to the given element
        /// </summary>
        /// <param name="newParent">The new parent for the given element</param>
        private void SetParent(IModelElement newParent)
        {
            if (newParent != parent)
            {
                var oldParent = parent;
                parent = newParent;
                if (newParent != null)
                {
                    var newModel = newParent.Model;
                    var oldModel = oldParent != null ? oldParent.Model : null;
                    if (oldParent != null)
                    {
                        oldParent.Deleted -= CascadeDelete;
                        if (EnforceModels && oldModel != null && oldModel == oldParent.Parent && oldModel != newModel)
                        {
                            oldModel.RootElements.Add(newParent);
                        }
                    }
                    newParent.Deleted += CascadeDelete;
                    var newParentME = newParent as ModelElement;
                    if (newParentME != null)
                    {
                        newParentME.OnChildCreated(this);
                    }
                    if (newModel != oldModel)
                    {
                        PropagateNewModel(newModel, oldModel, this);
                    }
                }
                else
                {
                    if (oldParent != null)
                    {
                        var oldModel = oldParent.Model;
                        oldParent.Deleted -= CascadeDelete;
{'fraction_non_alphanumeric': 0.07441634241245136, 'fraction_numerical': 0.0, 'mean_word_length': 1.1814002828854313, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3924269', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1315, 'n_words': 367}