Ejemplo n.º 1
0
        // public IEnumerable<DB_Parent> Parents => DB_Parents
        //.Where(dbElement => dbElement.ElementType.Equals(FlexData.ElementType.DB_Parent))
        //.Cast<DB_ParentElement>();

        //private IQueryable<DB_ParentElement> LoadAllDB_ParentElements() => DB_Elements
        //    .Where(dbElement => dbElement.ElementType.Equals(FlexData.ElementType.DB_ParentElement))
        //    .Cast<DB_ParentElement>();

        //public Task<IQueryable<DB_ParentElement>> LoadAllDB_ParentElementsAsync() => Task.Run(() => LoadAllDB_ParentElements());

        //private IQueryable<ParentElementBuilder> LoadAllParents() => LoadAllDB_ParentElements()
        //    .Select(dbElement => LoadParent(dbElement.ID));

        //public Task<IQueryable<ParentElementBuilder>> LoadAllParentsAsync() => Task.Run(() => LoadAllParents());

        //public Task<ParentElementBuilder> LoadParentAsync(int parentBuilder_ID) => Task.Run(() => LoadParent(parentBuilder_ID));

        //private ParentElementBuilder LoadParent(int parentBuilder_ID)
        //{
        //    ParentElementBuilder parentBuilder;
        //    DB_Parent dbParent = DB_Parents.Single(element => element.ID.Equals(parentBuilder_ID));
        //    parentBuilder = FlexData.Parent.BuilderOfType((FlexData.ParentType)dbParent.ParentType);
        //    parentBuilder.FlexDB_ID = dbParent.ID;
        //    LoadParentLayersOf(parentBuilder);
        //    LoadChildrenOf(parentBuilder);
        //    return parentBuilder;
        //}

        //private void LoadParentLayersOf(ParentElementBuilder parentBuilder)
        //{
        //    switch (parentBuilder)
        //    {
        //        case IndependentClauseBuilder clauseBuilder:
        //            CopyPhraseLayersOf(clauseBuilder, DB_Clauses.Single(clause => clause.ID.Equals(clauseBuilder.FlexDB_ID)));
        //            break;
        //        case SubordinateClauseBuilder clauseBuilder:
        //            CopyPhraseLayersOf(clauseBuilder, DB_Clauses.Single(clause => clause.ID.Equals(clauseBuilder.FlexDB_ID)));
        //            break;
        //        case NounPhraseBuilder nounPhraseBuilder:
        //            CopyPhraseLayersOf(nounPhraseBuilder, DB_NounPhrases.Single(nounPhrase => nounPhrase.ID.Equals(nounPhraseBuilder.FlexDB_ID)));
        //            break;
        //        case VerbPhraseBuilder verbPhraseBuilder:
        //            CopyPhraseLayersOf(verbPhraseBuilder, DB_VerbPhrases.Single(nounPhrase => nounPhrase.ID.Equals(verbPhraseBuilder.FlexDB_ID)));
        //            break;
        //        case AdjectivePhraseBuilder adjectivePhraseBuilder:
        //            CopyPhraseLayersOf(adjectivePhraseBuilder, DB_AdjectivePhrases.Single(nounPhrase => nounPhrase.ID.Equals(adjectivePhraseBuilder.FlexDB_ID)));
        //            break;
        //        case AdverbPhraseBuilder adverbPhraseBuilder:
        //            CopyPhraseLayersOf(adverbPhraseBuilder, DB_AdverbPhrases.Single(nounPhrase => nounPhrase.ID.Equals(adverbPhraseBuilder.FlexDB_ID)));
        //            break;
        //        case PrepositionalPhraseBuilder prepositionalPhraseBuilder:
        //            CopyPhraseLayersOf(prepositionalPhraseBuilder, DB_PrepositionalPhrases.Single(nounPhrase => nounPhrase.ID.Equals(prepositionalPhraseBuilder.FlexDB_ID)));
        //            break;
        //    }
        //}

        //private void CopyPhraseLayersOf(ClauseBuilder clauseBuilder, IPhrase dbPhrase)
        //{
        //    clauseBuilder.DiscourseFunctionSpecified = dbPhrase.DiscourseFunction != null;
        //    if (clauseBuilder.DiscourseFunctionSpecified) clauseBuilder.DiscourseFunction = (discourseFunction)dbPhrase.DiscourseFunction;
        //    clauseBuilder.AppositiveSpecified = dbPhrase.Appositive != null;
        //    if (clauseBuilder.AppositiveSpecified) clauseBuilder.Appositive = (bool)dbPhrase.Appositive;
        //    switch (dbPhrase)
        //    {
        //        case IClause dbClause:
        //            CopyClauseLayerOf(clauseBuilder, dbClause);
        //            break;
        //    }
        //}

        //private void CopyClauseLayerOf(ClauseBuilder clauseBuilder, IClause dbClause)
        //{
        //    clauseBuilder.AggregateAuxiliarySpecified = dbClause.AggregateAuxiliary != null;
        //    if (clauseBuilder.AggregateAuxiliarySpecified) clauseBuilder.AggregateAuxiliary = (bool)dbClause.AggregateAuxiliary;
        //    clauseBuilder.Complementiser = dbClause.Complementizer;
        //    clauseBuilder.FormSpecified = dbClause.Form != null;
        //    if (clauseBuilder.FormSpecified) clauseBuilder.Form = (form)dbClause.Form;
        //    clauseBuilder.InterrogativeTypeSpecified = dbClause.InterrogativeType != null;
        //    if (clauseBuilder.InterrogativeTypeSpecified) clauseBuilder.InterrogativeType = (interrogativeType)dbClause.InterrogativeType;
        //    clauseBuilder.Modal = dbClause.Modal;
        //    clauseBuilder.NegatedSpecified = dbClause.Negated != null;
        //    if (clauseBuilder.NegatedSpecified) clauseBuilder.Negated = (bool)dbClause.Negated;
        //    clauseBuilder.PassiveSpecified = dbClause.Passive != null;
        //    if (clauseBuilder.PassiveSpecified) clauseBuilder.Passive = (bool)dbClause.Passive;
        //    clauseBuilder.PerfectSpecified = dbClause.Perfect != null;
        //    if (clauseBuilder.PerfectSpecified) clauseBuilder.Perfect = (bool)dbClause.Perfect;
        //    clauseBuilder.PersonSpecified = dbClause.Person != null;
        //    if (clauseBuilder.PersonSpecified) clauseBuilder.Person = (person)dbClause.Person;
        //    clauseBuilder.ProgressiveSpecified = dbClause.Progressive != null;
        //    if (clauseBuilder.ProgressiveSpecified) clauseBuilder.Progressive = (bool)dbClause.Progressive;
        //    clauseBuilder.SuppressGenitiveInGerundSpecified = dbClause.SuppressGenitiveInGerund != null;
        //    if (clauseBuilder.SuppressGenitiveInGerundSpecified) clauseBuilder.SuppressGenitiveInGerund = (bool)dbClause.SuppressGenitiveInGerund;
        //    clauseBuilder.SuppressedComplementiserSpecified = dbClause.SuppressedComplementizer != null;
        //    if (clauseBuilder.SuppressedComplementiserSpecified) clauseBuilder.SuppressedComplementiser = (bool)dbClause.SuppressedComplementizer;
        //    clauseBuilder.TenseSpecified = dbClause.Tense != null;
        //    if (clauseBuilder.TenseSpecified) clauseBuilder.Tense = (tense)dbClause.Tense;
        //}

        //private void CopyPhraseLayersOf(PhraseBuilder phraseBuilder, IPhrase dbPhrase)
        //{
        //    phraseBuilder.DiscourseFunctionSpecified = dbPhrase.DiscourseFunction != null;
        //    if (phraseBuilder.DiscourseFunctionSpecified) phraseBuilder.DiscourseFunction = (discourseFunction)dbPhrase.DiscourseFunction;
        //    phraseBuilder.AppositiveSpecified = dbPhrase.Appositive != null;
        //    if (phraseBuilder.AppositiveSpecified) phraseBuilder.Appositive = (bool)dbPhrase.Appositive;
        //    switch (dbPhrase)
        //    {
        //        case INounPhrase dbNounPhrase:
        //            CopyNounPhraseLayerOf((NounPhraseBuilder)phraseBuilder, dbNounPhrase);
        //            break;
        //        case IVerbPhrase dbVerbPhrase:
        //            CopyVerbPhraseLayerOf((VerbPhraseBuilder)phraseBuilder, dbVerbPhrase);
        //            break;
        //        case IAdjectivePhrase dbAdjectivePhrase:
        //            CopyAdjectivePhraseLayerOf((AdjectivePhraseBuilder)phraseBuilder, dbAdjectivePhrase);
        //            break;
        //        case IAdverbPhrase dbAdverbPhrase:
        //            CopyAdverbPhraseLayerOf((AdverbPhraseBuilder)phraseBuilder, dbAdverbPhrase);
        //            break;
        //    }
        //}

        //private void CopyNounPhraseLayerOf(NounPhraseBuilder nounPhraseBuilder, INounPhrase dbNounPhrase)
        //{
        //    nounPhraseBuilder.AdjectiveOrderingSpecified = dbNounPhrase.AdjectiveOrdering != null;
        //    if (nounPhraseBuilder.AdjectiveOrderingSpecified) nounPhraseBuilder.AdjectiveOrdering = (bool)dbNounPhrase.AdjectiveOrdering;
        //    nounPhraseBuilder.ElidedSpecified = dbNounPhrase.Elided != null;
        //    if (nounPhraseBuilder.ElidedSpecified) nounPhraseBuilder.Elided = (bool)dbNounPhrase.Elided;
        //    nounPhraseBuilder.NumberSpecified = dbNounPhrase.Number != null;
        //    if (nounPhraseBuilder.NumberSpecified) nounPhraseBuilder.Number = (numberAgreement)dbNounPhrase.Number;
        //    nounPhraseBuilder.GenderSpecified = dbNounPhrase.Gender != null;
        //    if (nounPhraseBuilder.GenderSpecified) nounPhraseBuilder.Gender = (gender)dbNounPhrase.Gender;
        //    nounPhraseBuilder.PersonSpecified = dbNounPhrase.Person != null;
        //    if (nounPhraseBuilder.PersonSpecified) nounPhraseBuilder.Person = (person)dbNounPhrase.Person;
        //    nounPhraseBuilder.PossessiveSpecified = dbNounPhrase.Possessive != null;
        //    if (nounPhraseBuilder.PossessiveSpecified) nounPhraseBuilder.Possessive = (bool)dbNounPhrase.Possessive;
        //    nounPhraseBuilder.PronominalSpecified = dbNounPhrase.Pronominal != null;
        //    if (nounPhraseBuilder.PronominalSpecified) nounPhraseBuilder.Pronominal = (bool)dbNounPhrase.Pronominal;
        //}

        //private void CopyVerbPhraseLayerOf(VerbPhraseBuilder verbPhraseBuilder, IVerbPhrase dbVerbPhrase)
        //{
        //    verbPhraseBuilder.AggregateAuxiliarySpecified = dbVerbPhrase.AggregateAuxiliary != null;
        //    if (verbPhraseBuilder.AggregateAuxiliarySpecified) verbPhraseBuilder.AggregateAuxiliary = (bool)dbVerbPhrase.AggregateAuxiliary;
        //    verbPhraseBuilder.FormSpecified = dbVerbPhrase.Form != null;
        //    if (verbPhraseBuilder.FormSpecified) verbPhraseBuilder.Form = (form)dbVerbPhrase.Form;
        //    verbPhraseBuilder.Modal = dbVerbPhrase.Modal;
        //    verbPhraseBuilder.NegatedSpecified = dbVerbPhrase.Negated != null;
        //    if (verbPhraseBuilder.NegatedSpecified) verbPhraseBuilder.Negated = (bool)dbVerbPhrase.Negated;
        //    verbPhraseBuilder.PassiveSpecified = dbVerbPhrase.Passive != null;
        //    if (verbPhraseBuilder.PassiveSpecified) verbPhraseBuilder.Passive = (bool)dbVerbPhrase.Passive;
        //    verbPhraseBuilder.PerfectSpecified = dbVerbPhrase.Perfect != null;
        //    if (verbPhraseBuilder.PerfectSpecified) verbPhraseBuilder.Perfect = (bool)dbVerbPhrase.Perfect;
        //    verbPhraseBuilder.PersonSpecified = dbVerbPhrase.Person != null;
        //    if (verbPhraseBuilder.PersonSpecified) verbPhraseBuilder.Person = (person)dbVerbPhrase.Person;
        //    verbPhraseBuilder.ProgressiveSpecified = dbVerbPhrase.Progressive != null;
        //    if (verbPhraseBuilder.ProgressiveSpecified) verbPhraseBuilder.Progressive = (bool)dbVerbPhrase.Progressive;
        //    verbPhraseBuilder.SuppressGenitiveInGerundSpecified = dbVerbPhrase.SuppressGenitiveInGerund != null;
        //    if (verbPhraseBuilder.SuppressGenitiveInGerundSpecified) verbPhraseBuilder.SuppressGenitiveInGerund = (bool)dbVerbPhrase.SuppressGenitiveInGerund;
        //    verbPhraseBuilder.SuppressedComplementiserSpecified = dbVerbPhrase.SuppressedComplementizer != null;
        //    if (verbPhraseBuilder.SuppressedComplementiserSpecified) verbPhraseBuilder.SuppressedComplementiser = (bool)dbVerbPhrase.SuppressedComplementizer;
        //    verbPhraseBuilder.TenseSpecified = dbVerbPhrase.Tense != null;
        //    if (verbPhraseBuilder.TenseSpecified) verbPhraseBuilder.Tense = (tense)dbVerbPhrase.Tense;
        //}

        //private void CopyAdjectivePhraseLayerOf(AdjectivePhraseBuilder adjectivePhraseBuilder, IAdjectivePhrase dbAdjectivePhrase)
        //{
        //    adjectivePhraseBuilder.ComparativeSpecified = dbAdjectivePhrase.Comparative != null;
        //    if (adjectivePhraseBuilder.ComparativeSpecified) adjectivePhraseBuilder.Comparative = (bool)dbAdjectivePhrase.Comparative;
        //    adjectivePhraseBuilder.SuperlativeSpecified = dbAdjectivePhrase.Superlative != null;
        //    if (adjectivePhraseBuilder.SuperlativeSpecified) adjectivePhraseBuilder.Superlative = (bool)dbAdjectivePhrase.Superlative;
        //}

        //private void CopyAdverbPhraseLayerOf(AdverbPhraseBuilder adverbPhraseBuilder, IAdverbPhrase dbAdverbPhrase)
        //{
        //    adverbPhraseBuilder.ComparativeSpecified = dbAdverbPhrase.Comparative != null;
        //    if (adverbPhraseBuilder.ComparativeSpecified) adverbPhraseBuilder.Comparative = (bool)dbAdverbPhrase.Comparative;
        //    adverbPhraseBuilder.SuperlativeSpecified = dbAdverbPhrase.Superlative != null;
        //    if (adverbPhraseBuilder.SuperlativeSpecified) adverbPhraseBuilder.Superlative = (bool)dbAdverbPhrase.Superlative;
        //}

        //private void LoadChildrenOf(ParentElementBuilder parent)
        //{
        //    foreach (DB_ParentChildRelation eachRelation in DB_ParentChildRelations.Where(relation => relation.Parent.Equals(parent.FlexDB_ID)))
        //    {
        //        ElementBuilder child = Load(eachRelation.Child);
        //        parent.AddChildWithRole(child, (ParentElementBuilder.ChildRole)eachRelation.Role);
        //    }
        //    foreach (DB_ChildOrdering eachDB_Ordering in DB_ChildOrderings.Where(ordering => ordering.Parent.Equals(parent.FlexDB_ID)))
        //    {
        //        parent.ChildOrderings.Add(new ParentElementBuilder.ChildOrdering
        //        {
        //            Before = parent.Children.Single(child => child.FlexDB_ID.Equals(eachDB_Ordering.Child_Before)),
        //            After = parent.Children.Single(child => child.FlexDB_ID.Equals(eachDB_Ordering.Child_After))
        //        });
        //    }
        //}

        private void SaveParent(ParentElementBuilder parentBuilder)
        {
            RealizationResult realization = parentBuilder.AsRealizableTree().Realize();

            if (realization.Outcome == RealizationOutcome.Success)
            {
                DB_Element dbSavedElement;
                bool       isNewDB_Element = parentBuilder.FlexDB_ID == 0;
                // First we need to make sure we have a ParentElementBuilder with a valid FlexDB_ID.
                // We'll need that FlexDB_ID so DB_ParentChildRelations can refer to it when we save them in the database.
                if (isNewDB_Element)        // The parent builder does not already exist in the database.  We need to insert it so it has an ID.
                {
                    dbSavedElement            = new DB_Element(FlexData.ElementType.DB_Parent);
                    dbSavedElement.FormsCount = parentBuilder.CountForms();
                    DB_Elements.InsertOnSubmit(dbSavedElement);
                    SubmitChanges();
                    // Now we have the ID available from the database.  Assign the ID to the in-memory object, so we'll remember it's not new if we update it
                    parentBuilder.FlexDB_ID = dbSavedElement.ID;
                }
                else    // The parent builder already exists in the database
                {
                    dbSavedElement            = DB_Elements.Single(dbElement => dbElement.ID.Equals(parentBuilder.FlexDB_ID));
                    dbSavedElement.FormsCount = parentBuilder.CountForms();
                }
                UpdateParentLayersFor(parentBuilder, realization);
                DeleteChildOrderingsFor(parentBuilder);
                DeleteChildRelationsFor(parentBuilder);
                CreateChildRelationsFor(parentBuilder);
                CreateChildOrderingsFor(parentBuilder);
                SubmitChanges();
                OnParentChanged(parentBuilder.FlexDB_ID);
            }
        }
Ejemplo n.º 2
0
        //private IQueryable<WordElementBuilder> LoadAllWords() => DB_Elements
        //    .Where(dbElement => dbElement.ElementType.Equals(FlexData.ElementType.DB_Word))
        //    .Select(dbBuilder => LoadWord(dbBuilder.ID));

        //private WordElementBuilder LoadWord(int wordBuilder_ID)
        //{
        //    WordElementBuilder wordBuilder;
        //    DB_Word dbWord = DB_Words.Single(element => element.ID.Equals(wordBuilder_ID));
        //    wordBuilder = FlexData.Word.BuilderOfType((FlexData.WordType)dbWord.WordType);
        //    wordBuilder.FlexDB_ID = wordBuilder_ID;
        //    LoadWordLayersOf(wordBuilder);
        //    Task.Run(() =>
        //    {
        //        WeightedWord defaultWeightedWord = DB_WeightedWords
        //            .Where(dbWeightedWord => dbWeightedWord.WordElement.Equals(wordBuilder_ID) && dbWord.DefaultWeightedWord.Equals(dbWeightedWord.ID))
        //            .Select(dbWeightedWord => new WeightedWord(dbWeightedWord.Text, dbWeightedWord.Weight))
        //            .Single();
        //        IEnumerable<WeightedWord> alternates = DB_WeightedWords
        //            .Where(dbWeightedWord => dbWeightedWord.WordElement.Equals(wordBuilder_ID) && !dbWord.DefaultWeightedWord.Equals(dbWeightedWord.ID))
        //            .Select(dbWeightedWord => new WeightedWord(dbWeightedWord.Text, dbWeightedWord.Weight));
        //        if (alternates.Count().Equals(0))
        //            wordBuilder.WordSource = new SingleWordSource(defaultWeightedWord.Text);
        //        else
        //            wordBuilder.WordSource = new WordSelector(defaultWeightedWord, alternates);
        //    });
        //    return wordBuilder;
        //}

        //private void LoadWordLayersOf(WordElementBuilder wordBuilder)
        //{
        //    LayerWord wordLayer = LayerWords.Single(wordLayer => wordLayer.ID.Equals(wordBuilder.FlexDB_ID));
        //    wordBuilder.ExpletiveSubjectSpecified = wordLayer.ExpletiveSubject != null;
        //    if (wordBuilder.ExpletiveSubjectSpecified) wordBuilder.ExpletiveSubject = (bool)wordLayer.ExpletiveSubject;
        //    wordBuilder.ProperSpecified = wordLayer.Proper != null;
        //    if (wordBuilder.ProperSpecified) wordBuilder.Proper = (bool)wordLayer.Proper;
        //    wordBuilder.InflectionSpecified = wordLayer.Inflection != null;
        //    if (wordBuilder.InflectionSpecified) wordBuilder.Inflection = (inflection)wordLayer.Inflection;
        //    wordBuilder.CannedSpecified = wordLayer.Canned != null;
        //    if (wordBuilder.CannedSpecified) wordBuilder.Canned = (bool)wordLayer.Canned;
        //    switch (wordBuilder)
        //    {
        //        case PronounBuilder pronounBuilder:
        //            LoadPronounLayerOf(pronounBuilder);
        //            break;
        //    }
        //}

        //private void LoadPronounLayerOf(PronounBuilder pronounBuilder)
        //{
        //    LayerPronoun pronounLayer = LayerPronouns.Single(pronounLayer => pronounLayer.ID.Equals(pronounBuilder.FlexDB_ID));
        //    pronounBuilder.CaseSpecified = pronounLayer.PronounCase != null;
        //    if (pronounBuilder.CaseSpecified) pronounBuilder.Case = (PronounCase)pronounLayer.PronounCase;
        //    pronounBuilder.PersonSpecified = pronounLayer.Person != null;
        //    if (pronounBuilder.PersonSpecified) pronounBuilder.Person = (person)pronounLayer.Person;
        //    pronounBuilder.NumberSpecified = pronounLayer.Number != null;
        //    if (pronounBuilder.NumberSpecified) pronounBuilder.Number = (numberAgreement)pronounLayer.Number;
        //    pronounBuilder.GenderSpecified = pronounLayer.Gender != null;
        //    if (pronounBuilder.GenderSpecified) pronounBuilder.Gender = (gender)pronounLayer.Gender;
        //}

        private void SaveWord(WordElementBuilder wordBuilder)
        {
            DB_Element dbSavedElement;
            bool       isNewDB_Element = wordBuilder.FlexDB_ID == 0;

            // If the word doesn't support variations, and we've already saved a matching DB_Word in the database, don't save another one
            if (!wordBuilder.SupportsVariations)
            {
                DB_Word existingDB_WordThatMatches = DB_Words
                                                     .Where(dbWord => dbWord.WordType.Equals(FlexData.Word.TypeOf(wordBuilder)))
                                                     .FirstOrDefault(wordElement => wordElement.DefaultForm.Equals(wordBuilder.WordSource.DefaultWord));
                if (existingDB_WordThatMatches != null)
                {
                    wordBuilder.FlexDB_ID = existingDB_WordThatMatches.ID;
                    return;
                }
            }
            // Make sure we have a WordElementBuilder with a valid FlexDB_ID.
            // We'll need that FlexDB_ID so DB_WeightedWords can refer to it when we save them in the database.
            if (isNewDB_Element)        // The word builder does not already exist in the database.  We need to insert it so it has an ID.
            {
                dbSavedElement            = new DB_Element(FlexData.ElementType.DB_Word);
                dbSavedElement.FormsCount = wordBuilder.CountForms();
                DB_Elements.InsertOnSubmit(dbSavedElement);
                SubmitChanges();
                // Now we have the ID available from the database.  Assign the ID to the in-memory object, so we'll remember it's not new if we update it
                wordBuilder.FlexDB_ID = dbSavedElement.ID;
            }
            else  // The word builder already exists in the database
            {
                dbSavedElement            = DB_Elements.Single(dbElement => dbElement.ID.Equals(wordBuilder.FlexDB_ID));
                dbSavedElement.FormsCount = wordBuilder.CountForms();
            }
            UpdateWordLayersFor(wordBuilder);
            SubmitChanges();
            OnWordChanged(wordBuilder.FlexDB_ID);
        }