/// <summary>Generate an editable tree from <paramref name="text"/>, then try to realize that tree.</summary> public void ParseText(string text) { IElementTreeNode editableTree = FlexibleRealizerFactory.EditableTreeFrom(text); SetModel(editableTree); SelectNode(editableTree); }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case NounBuilder nb: AddChildWithRole(nb, ChildRole.Head); break; case AdjectiveBuilder ab: AddChildWithRole(ab, ChildRole.Head); break; case AdjectivePhraseBuilder apb: AddChildWithRole(apb, ChildRole.Modifier); break; case NominalModifierBuilder nmb: AddChildWithRole(nmb, ChildRole.Modifier); break; default: AddUnassignedChild(child); break; } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case AdverbBuilder ab: AddHead(ab); break; case AdverbPhraseBuilder apb: AddHead(apb); break; case ConjunctionBuilder cb: SetCoordinator(cb); break; // Adverb phrase can't have a specifier -- this is a temporary holding place that needs to be fixed during application of dependencies case DeterminerBuilder db: AddUnassignedChild(db); break; default: AddUnassignedChild(child); break; } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case NounPhraseBuilder npb: AddSubject(npb); break; case VerbPhraseBuilder vpb: SetPredicate(vpb); break; case CoordinatedPhraseBuilder cpb: AssignRoleFor(cpb); break; case AdverbPhraseBuilder apb: AddUnassignedChild(apb); break; case PrepositionBuilder pb: AddUnassignedChild(pb); break; default: AddUnassignedChild(child); break; } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case AdjectiveBuilder ab: AddHead(ab); break; case AdverbBuilder ab: AddModifier(ab); break; case AdjectivePhraseBuilder apb: AddHead(apb); break; case AdverbPhraseBuilder apb: AddModifier(apb); break; case PrepositionalPhraseBuilder ppb: AddComplement(ppb); break; case ConjunctionBuilder cb: SetCoordinator(cb); break; default: AddUnassignedChild(child); break; } }
/// <summary>Replace <paramref name="existingChild"/> with <paramref name="newChild"/>, using the same role and preserving the ordering of children.</summary> public void ReplaceChild(IElementTreeNode existingChild, IElementTreeNode newChild) { if (newChild != null) { ChildRole existingRole = RoleFor(existingChild); ChildrenAndRoles.Remove(existingChild); existingChild.Parent = null; AddChildWithRole(newChild, existingRole); UpdateChildOrderingReferences(existingChild, newChild); } else { RemoveChild(existingChild); } // Update the ChildOrdering references from oldReference to newReference void UpdateChildOrderingReferences(IElementTreeNode oldReference, IElementTreeNode newReference) { foreach (ChildOrdering eachChildOrdering in ChildOrderings) { if (eachChildOrdering.Before == existingChild) { eachChildOrdering.Before = newChild; } if (eachChildOrdering.After == existingChild) { eachChildOrdering.After = newChild; } } } }
/// <summary>The user wants to browse WordNet synsets.</summary> private void BrowseWordNetMenuItem_Click(object sender, RoutedEventArgs e) { WordNetBrowserWindow wordNetBrowser = new WordNetBrowserWindow(); wordNetBrowser.SynsetDragStarted += TreeEditor.OnSynsetDragStarted; wordNetBrowser.SynsetDragCancelled += TreeEditor.OnSynsetDragCancelled; wordNetBrowser.SynsetDropCompleted += TreeEditor.OnSynsetDropCompleted; wordNetBrowser.AddDroppedWordConverter(e => { IElementTreeNode droppedNode = null; // We could get a dropped IElementTree node in one of two forms: // 1. It's in the IDataObject as an IElementTreeNode, ready to use; or // 2. It's in the IDataObject as a Task<ElementBuilder> that we can run to get the IElementTreeNode if (e.Data.GetDataPresent(typeof(IElementTreeNode))) { droppedNode = (IElementTreeNode)e.Data.GetData(typeof(IElementTreeNode)); } else if (e.Data.GetDataPresent(typeof(Task))) { Task <IElementTreeNode> getNodeTask = (Task <IElementTreeNode>)e.Data.GetData(typeof(Task)); droppedNode = getNodeTask.Result; } return((droppedNode is WordElementBuilder wordBuilder) ? new WordSpecification(wordBuilder.WordSource.DefaultWord, wordBuilder switch { NounBuilder nounBuilder => WordNetData.PartOfSpeech.Noun, VerbBuilder verbBuilder => WordNetData.PartOfSpeech.Verb, AdjectiveBuilder adjectiveBuilder => WordNetData.PartOfSpeech.Adjective, AdverbBuilder adverbBuilder => WordNetData.PartOfSpeech.Adverb, _ => WordNetData.PartOfSpeech.Unspecified }) : null); });
/// <summary>Load the ElementBuilder with the specified <paramref name="elementID"/> from the Flex database, and return it.</summary> //private ElementBuilder Load(int elementID) //{ // DB_Element element = DB_Elements.Single(dbElement => dbElement.ID.Equals(elementID)); // ElementBuilder result; // switch ((FlexData.ElementType)element.ElementType) // { // case FlexData.ElementType.DB_Word: // result = LoadWord(elementID); // break; // case FlexData.ElementType.DB_Parent: // result = LoadParent(elementID); // break; // default: // throw new InvalidOperationException(""); // } // return result; //} /// <summary>Async version of <see cref="Load(int)"/>.</summary> //public Task<ElementBuilder> LoadAsync(int elementID) => Task.Run(() => Load(elementID)); /// <summary>Async version of <see cref="Save(IElementTreeNode)"/>.</summary> public Task SaveAsync(IElementTreeNode element) => Task.Run(() => { using (TransactionScope transaction = new TransactionScope()) { Save(element); transaction.Complete(); } OnSaveCompleted(element); });
internal void Save() { int elementID = Element switch { IElementTreeNode node => node.FlexDB_ID, DB_Element dbElement => dbElement.ID, _ => throw new InvalidOperationException("Unknown Element type in SynsetToElementBindingViewModel") }; //FlexData.Context.SaveSynsetToElementBinding(Synset.ID, elementID, Weight); } }
/// <summary>Set <paramref name="specifier"/> as the ONLY specifier for this noun phrase</summary> private void SetSpecifier(IElementTreeNode specifier) { if (Specifiers.Count() == 0) { AddChildWithRole(specifier, ChildRole.Specifier); } else { throw new InvalidOperationException("Can't add multiple specifiers to a noun phrase"); } }
/// <summary>Do a comparison to determine the relative order of <paramref name="aDescendant"/> and <paramref name="anotherDescendant"/></summary> /// <remarks>This method is invoked by the CompareTo method that implements IComparable for PartOfSpeechBuilder. Because this method is invoked /// on the most recent common ancestor of <paramref name="aDescendantPartOfSpeech"/> and <paramref name="anotherDescendantPartOfSpeech"/>, finding /// the ordering of those descendant parts of speech is equivalent to finding the ordering of the children of this common ancestor which are /// respective ancestors of those parts of speech.</remarks> public int CompareDescendants(IElementTreeNode aDescendant, IElementTreeNode anotherDescendant) { IElementTreeNode childAncestorOfADescendant = Children.Where(child => aDescendant.IsInSubtreeOf(child)).Single(); IElementTreeNode childAncestorOfAnotherDescendant = Children.Where(child => anotherDescendant.IsInSubtreeOf(child)).Single(); IElementTreeNode childToLookFor; // See if we can find a chain of ChildOrdering that connects aChild to anotherChild, in Before -> After order childToLookFor = childAncestorOfADescendant; do { ChildOrdering link = ChildOrderings.Where(ordering => ordering.Before == childToLookFor).FirstOrDefault(); if (link == null) // We're at the end of the chain and it didn't lead to what we're looking for { break; // Out of the do loop } else { if (link.After == childAncestorOfAnotherDescendant) { return(-1); // We found the chain we were looking for } else { childToLookFor = link.After; // Keep following the chain } } } while (true); // We failed to find a chain connecting aChild to anotherChild in Before -> After order. // Now we'll see if such a chain exists in After -> Before order childToLookFor = childAncestorOfADescendant; do { ChildOrdering link = ChildOrderings.Where(ordering => ordering.After == childToLookFor).FirstOrDefault(); if (link == null) // We're at the end of the chain and it didn't lead to what we're looking for { break; // Out of the do loop } else { if (link.Before == childAncestorOfAnotherDescendant) { return(1); // We found the chain we were looking for } else { childToLookFor = link.Before; // Keep following the chain } } } while (true); // We couldn't find a chain between these two IElementTreeNodes in either direction. // As far as this parent is concerned, they are equal. return(0); }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case ParticleBuilder pb: AddChildWithRole(pb, ChildRole.Head); break; default: AddUnassignedChild(child); break; } }
/// <summary>Add <paramref name="node"/> to the tree in which this exists.</summary> public override IParent Add(IElementTreeNode node) { IParent modifiedParent; switch (node) { case AdverbBuilder advb: modifiedParent = this.AsAdverbPhrase(); modifiedParent.AddChild(advb); return(modifiedParent); default: return(null); } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case WhDeterminerBuilder wdb: SetWhWord(wdb); break; case WhPronounBuilder wpb: SetWhWord(wpb); break; default: base.AssignRoleFor(child); break; } }
/// <summary>Save <paramref name="element"/> to the Flex database.</summary> private void Save(IElementTreeNode element) { switch (element) { case WordElementBuilder wb: SaveWord(wb); break; case ParentElementBuilder pb: SaveParent(pb); break; default: break; } }
private void BuildChildrenOf(ParentElementBuilder parentBuilder, IEnumerable <UnifiedNode> nodeResults, IEnumerable <GetWeightedWordsForTreeResult> weightedWordResults, List <GetChildOrderingsForTreeResult> childOrderingResults) { foreach (UnifiedNode eachChildResult in nodeResults.Where(nodeResult => nodeResult.ParentID.Equals(parentBuilder.FlexDB_ID))) { IElementTreeNode child = BuildTreeNode((int)eachChildResult.ID, nodeResults, weightedWordResults, childOrderingResults); parentBuilder.AddChildWithRole(child, (ParentElementBuilder.ChildRole)eachChildResult.Role); } foreach (GetChildOrderingsForTreeResult eachChildOrderingResult in childOrderingResults.Where(childOrderingResult => childOrderingResult.Parent.Equals(parentBuilder.FlexDB_ID))) { parentBuilder.ChildOrderings.Add(new ParentElementBuilder.ChildOrdering { Before = parentBuilder.Children.Single(child => child.FlexDB_ID.Equals(eachChildOrderingResult.Child_Before)), After = parentBuilder.Children.Single(child => child.FlexDB_ID.Equals(eachChildOrderingResult.Child_After)) }); } }
/// <summary>Delete the selected node from the edited tree.</summary> /// <remarks>If the selected node is the Stem of the tree, clear the workspace. Otherwise prune the selected node from the tree structure.</remarks> public void DeleteSelection() { if (SelectedNode != null) { if (SelectedNode == SelectedNode.Stem) { ClearModel(); } else { IElementTreeNode newSelection = (IElementTreeNode)SelectedNode.Stem; SelectedNode.Remove(); newSelection.Root.OnTreeStructureChanged(); SelectNode(newSelection); } } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case WhAdverbBuilder wab: SetWhWord(wab); break; case AdverbBuilder ab: SetWhWord(ab); break; default: AddUnassignedChild(child); break; } }
/// <summary>Assign <paramref name="elementBuilderTree"/> as the model for this editor.</summary> private void SetModel(IElementTreeNode elementBuilderTree) { ClearModel(); ModelRoot = elementBuilderTree.Root; ElementBuilderGraph graph = ElementBuilderGraph.Of(elementBuilderTree); ElementGraphArea.LogicCore = new ElementBuilderLogicCore(graph); ElementGraphArea.GenerateGraph(true, true); ElementDescription.DataContext = this; Properties.DataContext = this; XmlLabel.DataContext = this; // I think the animation looks cool when we put a new tree in the GraphArea, but we don't want to trigger that animation every time the selected vertex changes. ZoomCtrl.IsAnimationEnabled = true; ZoomCtrl.ZoomToFill(); ZoomCtrl.IsAnimationEnabled = false; ModelRoot.TreeStructureChanged += Model_TreeStructureChanged; }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case PronounBuilder pb: SetWhWord(pb); break; case AdjectiveBuilder adjb: AddHead(adjb); break; case AdverbBuilder advb: AddModifier(advb); break; default: AddUnassignedChild(child); break; } }
/// <summary>Try to transform <paramref name="editableTree"/> into realizable form and if successful, try to realize it.</summary> /// <remarks>Raise an event indicating whether the process succeeded or not</remarks> private RealizationResult TryToRealize(IElementTreeNode editableTree) { RealizationResult result = editableTree.Realize(); switch (result.Outcome) { case RealizationOutcome.Success: XmlSpec = result.XML; OnTextRealized(result.Text); break; case RealizationOutcome.FailedToTransform: case RealizationOutcome.FailedToBuildSpec: XmlSpec = null; OnRealizationFailed(editableTree); break; default: break; } return(result); }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case NounPhraseBuilder npb: // Might be a WhNounPhraseBuilder AddSubject(npb); break; case VerbPhraseBuilder vpb: SetPredicate(vpb); break; case CoordinatedPhraseBuilder cpb: AssignRoleFor(cpb); break; case IndependentClauseBuilder icb: Assimilate(icb); break; case ConjunctionBuilder cb: SetComplementizer(cb); break; case AdverbPhraseBuilder apb: // Might be a WhAdverbPhraseBuilder AddUnassignedChild(apb); break; case PrepositionBuilder pb: AddUnassignedChild(pb); break; case AdjectivePhraseBuilder whadjpb: // Might be a WhAdjectivePhraseBuilder AddUnassignedChild(whadjpb); break; default: throw new InvalidOperationException("Subordinate clause can't find a role for this element"); } }
/// <summary>Make a lightweight copy of the children in <paramref name="anotherParent"/>, and add the copied children to this ParentElementBuilder.</summary> /// <returns>this</returns> internal ParentElementBuilder LightweightCopyChildrenFrom(ParentElementBuilder anotherParent) { // Create a copy of each child and put the copies in a Dictionary so they can be looked up using the original child as a key Dictionary <IElementTreeNode, IElementTreeNode> copiedChildren = new Dictionary <IElementTreeNode, IElementTreeNode>(); foreach (KeyValuePair <IElementTreeNode, ChildRole> eachChildAndRole in anotherParent.ChildrenAndRoles) { IElementTreeNode copiedChild = eachChildAndRole.Key.CopyLightweight(); AddChildWithRole(copiedChild, eachChildAndRole.Value); copiedChildren.Add(eachChildAndRole.Key, copiedChild); } // Create copies of the ChildOrderings that refer to the copied children foreach (ChildOrdering eachOrdering in anotherParent.ChildOrderings) { ChildOrderings.Add(new ChildOrdering { Before = copiedChildren[eachOrdering.Before], After = copiedChildren[eachOrdering.After] }); } return(this); }
/// <summary>Remove any ChildOrderings that refer to <paramref name="child"/>.</summary> private protected void RemoveChildOrderingsThatReferTo(IElementTreeNode child) { ChildOrdering orderingWithTheRemovedChildAsBefore = ChildOrderings.FirstOrDefault(ordering => ordering.Before == child); ChildOrdering orderingWithTheRemovedChildAsAfter = ChildOrderings.FirstOrDefault(ordering => ordering.After == child); if (orderingWithTheRemovedChildAsBefore != null) { if (orderingWithTheRemovedChildAsAfter != null) // The removed child is between two other children { orderingWithTheRemovedChildAsBefore.Before = orderingWithTheRemovedChildAsAfter.Before; ChildOrderings.Remove(orderingWithTheRemovedChildAsAfter); } else { ChildOrderings.Remove(orderingWithTheRemovedChildAsBefore); // The removed child has no other children before it } } else { ChildOrderings.Remove(orderingWithTheRemovedChildAsAfter); // The removed child has no other children after it } }
private protected override void AssignRoleFor(IElementTreeNode child) { switch (child) { case PrepositionBuilder pb: AddHead(pb); break; case PrepositionalPhraseBuilder ppb: AddHead(ppb); break; case NounBuilder nb: AddComplement(nb); break; case NounPhraseBuilder npb: AddComplement(npb); break; case VerbPhraseBuilder vpb: AddComplement(vpb); break; case CoordinatedPhraseBuilder cpb: AssignRoleFor(cpb); break; case ConjunctionBuilder cb: SetCoordinator(cb); break; default: AddUnassignedChild(child); break; } }
/// <summary>Configure the ChildOrderings of this ParentElementBuilder so the parts of speech in <paramref name="childToOrder"/> remain in the same order /// as they are found in the <paramref name="partsOfSpeech"/> Dictionary.</summary> void IParent.SetChildOrdering(IElementTreeNode childToOrder, Dictionary <PartOfSpeechBuilder, int> partsOfSpeech) { IEnumerable <PartOfSpeechBuilder> partsOfSpeechInChildToOrder = childToOrder.GetElementsOfTypeInSubtree <PartOfSpeechBuilder>(); IEnumerable <IGrouping <IElementTreeNode, PartOfSpeechBuilder> > partsOfSpeechGroupedByChild = GetElementsOfTypeInSubtree <PartOfSpeechBuilder>().GroupBy(pos => ChildContaining(pos)); IElementTreeNode lastChildBefore = partsOfSpeechGroupedByChild .Where(group => group.All(pos => partsOfSpeech[pos] < partsOfSpeechInChildToOrder.Min(pos => partsOfSpeech[pos]))) .OrderBy(group => group.Max(pos => partsOfSpeech[pos])) .Select(group => group.Key) .LastOrDefault(); IElementTreeNode firstChildAfter = partsOfSpeechGroupedByChild .Where(group => group.All(pos => partsOfSpeech[pos] > partsOfSpeechInChildToOrder.Max(pos => partsOfSpeech[pos]))) .OrderBy(group => group.Min(pos => partsOfSpeech[pos])) .Select(group => group.Key) .FirstOrDefault(); if (lastChildBefore != null) { if (firstChildAfter != null) { } else { SetChildOrdering(childToOrder, lastChildBefore, NodeRelation.Last); } } else { if (firstChildAfter != null) { SetChildOrdering(childToOrder, firstChildAfter, NodeRelation.First); } else { } } }
/// <summary>Add <paramref name="node"/> to the tree in which this exists</summary> public override IParent Add(IElementTreeNode node) { IParent modifiedParent; switch (node) { case AdverbBuilder advb: modifiedParent = this.AsAdjectivePhrase(); modifiedParent.AddChild(advb); return(modifiedParent); case AdverbPhraseBuilder apb: modifiedParent = this.AsAdjectivePhrase(); modifiedParent.AddChild(apb); return(modifiedParent); case PrepositionalPhraseBuilder ppb: modifiedParent = this.AsAdjectivePhrase(); modifiedParent.AddChild(ppb); return(modifiedParent); default: return(null); } }
/// <summary>Return the ChildRole assigned for <paramref name="child"/>.</summary> /// <exception cref="KeyNotFoundException"></exception> public ChildRole RoleFor(IElementTreeNode child) => ChildrenAndRoles[child];
/// <summary>Return true if this ParentElementBuilder can add <paramref name="potentialChild"/> as a child.</summary> public bool CanAddChild(IElementTreeNode potentialChild) => CanAddChildOfType(potentialChild.GetType());
/// <summary>Sever the parent-child link between this and <paramref name="childToRemove"/>.</summary> public void RemoveChild(IElementTreeNode childToRemove) { ChildrenAndRoles.Remove(childToRemove); RemoveChildOrderingsThatReferTo(childToRemove); childToRemove.Parent = null; }