<commit_before>﻿using NMF.Collections.Generic;
using NMF.Expressions;
using NMF.Expressions.Linq;
using NMF.Models.Collections;
using NMF.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace NMF.Models
{
    [DebuggerDisplayAttribute("Model {ModelUri}")]
    public class Model : ModelElement
    {
        static Model()
        {
            PromoteSingleRootElement = true;
        }

        public static bool PromoteSingleRootElement { get; set; }

        public Model()
        {
            RootElements = new ObservableCompositionOrderedSet<IModelElement>(this);
        }

        public Uri ModelUri
        {
            get;
            set;
        }

        public IOrderedSetExpression<IModelElement> RootElements { get; private set; }

        public override IEnumerableExpression<IModelElement> Children
        {
            get
            {
                return RootElements.Concat(Extensions);
            }
        }

        protected override IModelElement GetModelElementForReference(string reference, int index)
        {
            if (reference == "#" && index < RootElements.Count)
            {
                return RootElements[index];
            }
            else
            {
                int num;
                if (int.TryParse(reference, out num) && num > 0 && num < RootElements.Count)
                {
                    return RootElements[num];
                }
                return null;
            }
        }

        public override IEnumerableExpression<IModelElement> ReferencedElements
        {
            get
            {
                return base.ReferencedElements.Concat<IModelElement>(RootElements);
            }
        }

        protected override string GetRelativePathForNonIdentifiedChild(IModelElement child)
        {
            if (RootElements.Count == 1 && PromoteSingleRootElement)
            {
                return null;
            }
            else
            {
                var index = RootElements.IndexOf(child);
                if (index != -1)
                {
                    return "/" + index.ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        protected internal override Uri CreateUriWithFragment(string fragment, bool absolute)
        {
            if (fragment != null)
            {
                var leftPad = fragment == string.Empty ?
                    "/" : string.Empty;
                if (ModelUri == null || !absolute || !ModelUri.IsAbsoluteUri)
                {
                    return new Uri("/" + leftPad + fragment, UriKind.Relative);
                }
                else
                {
                    return new Uri(ModelUri, "#/" + leftPad + fragment);
                }
            }
            else
            {
                if (absolute)
                {
                    return ModelUri;
                }
                else
                {
                    return null;
                }
            }
        }

        public override IModelElement Resolve(string path)
        {
            if (string.IsNullOrEmpty(path)) return this;
            if (path.StartsWith("//")) path = path.Substring(2);
            if (path.StartsWith("#//")) path = path.Substring(3);
            if (RootElements.Count == 1 && PromoteSingleRootElement)
            {
                var root = RootElements[0] as ModelElement;
                if (root != null)
                {
                    var resolved = root.Resolve(path);
                    if (resolved != null) return resolved;
                }
            }
            var baseResolve = base.Resolve(path);
            if (baseResolve != null || !PromoteSingleRootElement || RootElements.Count != 1) return baseResolve;
            var rootCasted = RootElements[0] as ModelElement;
            if (rootCasted != null)
            {
                return rootCasted.Resolve(path);
            }
            else
            {
                return null;
            }
        }

        protected override string GetRelativePathForChild(IModelElement child)
        {
            if (PromoteSingleRootElement && RootElements.Count == 1 && child == RootElements[0])
            {
                return string.Empty;
            }
            return base.GetRelativePathForChild(child);
        }

        public override Meta.IClass GetClass()
        {
            return NMF.Models.Repository.MetaRepository.Instance.ResolveClass("http://nmf.codeplex.com/nmeta/#//NMeta/Model/");
        }
    }
}
<commit_msg>Fix small bug in PromoteSingleRootElement setting<commit_after>﻿using NMF.Collections.Generic;
using NMF.Expressions;
using NMF.Expressions.Linq;
using NMF.Models.Collections;
using NMF.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace NMF.Models
{
    [DebuggerDisplayAttribute("Model {ModelUri}")]
    public class Model : ModelElement
    {
        static Model()
        {
            PromoteSingleRootElement = true;
        }

        public static bool PromoteSingleRootElement { get; set; }

        public Model()
        {
            RootElements = new ObservableCompositionOrderedSet<IModelElement>(this);
        }

        public Uri ModelUri
        {
            get;
            set;
        }

        public IOrderedSetExpression<IModelElement> RootElements { get; private set; }

        public override IEnumerableExpression<IModelElement> Children
        {
            get
            {
                return RootElements.Concat(Extensions);
            }
        }

        protected override IModelElement GetModelElementForReference(string reference, int index)
        {
            if (reference == "#" && index < RootElements.Count)
            {
                return RootElements[index];
            }
            else
            {
                int num;
                if (int.TryParse(reference, out num) && num > 0 && num < RootElements.Count)
                {
                    return RootElements[num];
                }
                return null;
            }
        }

        public override IEnumerableExpression<IModelElement> ReferencedElements
        {
            get
            {
                return base.ReferencedElements.Concat<IModelElement>(RootElements);
            }
        }

        protected override string GetRelativePathForNonIdentifiedChild(IModelElement child)
        {
            if (RootElements.Count == 1 && PromoteSingleRootElement)
            {
                return null;
            }
            else
            {
                var index = RootElements.IndexOf(child);
                if (index != -1)
                {
                    return "/" + index.ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        protected internal override Uri CreateUriWithFragment(string fragment, bool absolute)
        {
            if (fragment != null)
            {
                var leftPad = fragment == string.Empty ?
                    "/" : string.Empty;
                if (ModelUri == null || !absolute || !ModelUri.IsAbsoluteUri)
                {
                    return new Uri("/" + leftPad + fragment, UriKind.Relative);
                }
                else
                {
                    return new Uri(ModelUri, "#/" + leftPad + fragment);
                }
            }
            else
            {
                if (absolute)
                {
                    return ModelUri;
                }
                else
                {
                    return null;
                }
            }
        }

        public override IModelElement Resolve(string path)
        {
            if (string.IsNullOrEmpty(path)) return this;
            if (path.StartsWith("//")) path = path.Substring(2);
            if (path.StartsWith("#//")) path = path.Substring(3);
            if (RootElements.Count == 1 && PromoteSingleRootElement)
            {
                var root = RootElements[0] as ModelElement;
                if (root != null)
                {
                    var resolved = root.Resolve(path);
                    if (resolved != null) return resolved;
                }
            }
            var baseResolve = base.Resolve(path);
            if (baseResolve != null || PromoteSingleRootElement || RootElements.Count != 1) return baseResolve;
            var rootCasted = RootElements[0] as ModelElement;
            if (rootCasted != null)
            {
                return rootCasted.Resolve(path);
            }
            else
            {
                return null;
            }
        }

        protected override string GetRelativePathForChild(IModelElement child)
        {
            if (PromoteSingleRootElement && RootElements.Count == 1 && child == RootElements[0])
            {
                return string.Empty;
            }
            return base.GetRelativePathForChild(child);
        }

        public override Meta.IClass GetClass()
        {
            return NMF.Models.Repository.MetaRepository.Instance.ResolveClass("http://nmf.codeplex.com/nmeta/#//NMeta/Model/");
        }
    }
}

{'fraction_non_alphanumeric': 0.08372827804107424, 'fraction_numerical': 0.002317008952080042, 'mean_word_length': 1.2706838833094214, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '5486438', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2208, 'n_words': 489}