private readonly Dictionary<NameInstance, MutationCollection> mutations; //only contains atomic keys that change into composite ones I think public CompositeNameInstance GetRoot() { if (mutations.Count != 0) { root = (CompositeNameInstance)GetMutatedState(root, mutations); mutations.Clear(); root.SetParent(null);//sets all parent references } return root; }
//SPEC: A character is an atomic name instance. A box composition is a composite name instance with one element per box. //SPEC: A box contains one name binding per element in that box, which may be a character or box composition. //SPEC: A box is thus a composite name instance, unless it contains a single character or is placeholder, in which case those are directly represented in the containing Box Composition. /// <summary> Gets all NameInstances in the specified box that hasn't bound at all yet. </summary> public NameInstanceBinding(BoxViewModel rootViewModel) { Contract.Requires(rootViewModel != null); this.rootViewModel = rootViewModel; this.mutations = new Dictionary<NameInstance, MutationCollection>(ReferenceEqualityComparer); this.root = new CompositeNameInstance(new List<NameInstance>(), RootPosition.Instance); this.rootViewModel.Elements.CollectionChanged += onBoxElementsCollectionChanged; if (this.rootViewModel.Elements.Count != 0) onBoxElementsCollectionChanged(rootViewModel.Elements, NotifyBoxElementsChangedEventArgs.Create(this.rootViewModel.Elements, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.rootViewModel.Elements))); }
/// <summary> Tries to find an expression of the form ?∈set, and returns the involved name instance and set. </summary> /// <param name="expression"> The expression that may be of the form ?∈set. </param> /// <param name="toBeDefined"> If the specified expression was of said form, this is the left hand operand. </param> /// <param name="toBeDefinedIn"> If the specified expression was of said form, this is the right hand operand's set. </param> public static bool TryIdentifyDefinition(CompositeNameInstance expression, out NameInstance toBeDefined, out Set toBeDefinedIn) { Contract.Requires(expression != null); if (expression.Elements.Count == 3 && expression.Elements[0].Name == null //first expression element is still unknown && expression.Elements[1].Name?.Notion == DefaultNotions.InDefining && expression.Elements[2].Name?.Notion is Set) { toBeDefined = expression.Elements[0]; toBeDefinedIn = (Set)expression.Elements[2].Name.Notion; return true; } else { toBeDefined = null; toBeDefinedIn = null; return false; } }
/// <summary> Creates a composite expression containing the specified characters as element name instance. </summary> /// <param name="position"> The resulting position of the expression to create. </param> public static CompositeNameInstance LinearNameInstanceFrom(IPosition position, params char[] expressionElements) { var elements = expressionElements.Select((c, i) => new AtomicNameInstance(c, new LinearPosition(i))); CompositeNameInstance result = new CompositeNameInstance(elements.ToList<NameInstance>(), position); if (position == RootPosition.Instance) result = result.Finalize2(); return result; }
public void AToThePower2Test() { var treeLeafA = new AtomicNameInstance('a', new AbsoluteScriptPosition()); var treeLeafA2 = new AtomicNameInstance('2', new AbsoluteScriptPosition(Script.Super/********************/.ToSingleton())); var root = new CompositeNameInstance(new NameInstance[] { treeLeafA, treeLeafA2 }.ToList(), new LinearPosition(0)); treeLeafA.SetParent(root); treeLeafA2.SetParent(root); var inferrer = new NameInstanceNameInferrer(new[] { Notations.aSup2 }.ToReadOnlyList()); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(result[0].Count == 1); Contract.Assert((result[0][0].Binding.Name as Name).Notation == Notations.aSup2); Contract.Assert(result[0][0].Binding.Index == (LeafIndex)0); Contract.Assert(result[0][0].Occupation.SequenceEqual(new[] { true, true })); }
public void ASub2FailTest() { //tests that a_2 is not matched by a^2 var treeLeafA = new AtomicNameInstance('a', new AbsoluteScriptPosition()); var treeLeafA2 = new AtomicNameInstance('2', new AbsoluteScriptPosition(Script.Sub/**********************/.ToSingleton())); var root = new CompositeNameInstance(new NameInstance[] { treeLeafA, treeLeafA2 }.ToList(), new LinearPosition(0)); treeLeafA.SetParent(root); treeLeafA2.SetParent(root); var inferrer = new NameInstanceNameInferrer(new[] { Notations.aSup2 }.ToReadOnlyList()); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 0); }
public void AToThePowerBSeparateTest() { var treeLeafA = new AtomicNameInstance('a', new AbsoluteScriptPosition()); var treeLeafA2 = new AtomicNameInstance('b', new AbsoluteScriptPosition(Script.Super.ToSingleton())); var root = new CompositeNameInstance(new NameInstance[] { treeLeafA, treeLeafA2 }.ToList(), new LinearPosition(0)); treeLeafA.SetParent(root); treeLeafA2.SetParent(root); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); }
public void asososTest() { var a = new AtomicNameInstance('a', new LinearPosition(0)); var s = new AtomicNameInstance('s', new LinearPosition(1)); var o = new AtomicNameInstance('o', new LinearPosition(2)); var s_ = new AtomicNameInstance('s', new LinearPosition(3)); var o_ = new AtomicNameInstance('o', new LinearPosition(4)); var s__ = new AtomicNameInstance('s', new LinearPosition(5)); var root = new CompositeNameInstance(new NameInstance[] { a, s, o, s_, o_, s__ }.ToList(), new LinearPosition(0)); root.SetParent(null); var inferrer = new NameInstanceNameInferrer(new[] { Notations.sos, Notations.aso }.ToReadOnlyList()); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(result[0].Select(match => (match.Binding.Name as Name).Notation).ContainsSameElements(Notations.aso.Notation, Notations.sos.Notation)); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.aso)].Binding.Index.Index == 0); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.sos)].Binding.Index.Index == 3); }
private void TwoLeafTest(CompositeNameInstance root) { var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); var names = result[0].Select(match => match.Binding.Name).ToList(); var leafIndices = result[0].Select(match => match.Binding.Index).ToList(); var expectedNames = new[] { Notations.a, Notations.b }; var expectedLeafIndices = new[] { (LeafIndex)0, (LeafIndex)1 }; Contract.Assert(Enumerable.SequenceEqual(names, expectedNames, GloballyScopedMethods.ReferenceEqualityComparer)); Contract.Assert(Enumerable.SequenceEqual(leafIndices, expectedLeafIndices)); }
public void SinCosTest() { var s = new AtomicNameInstance('s', new LinearPosition(0)); var i = new AtomicNameInstance('i', new LinearPosition(1)); var n = new AtomicNameInstance('n', new LinearPosition(2)); var sinRoot = new CompositeNameInstance(new NameInstance[] { s, i, n }.ToList(), new LinearPosition(0)); var c = new AtomicNameInstance('c', new LinearPosition(0)); var o = new AtomicNameInstance('o', new LinearPosition(1)); var s_ = new AtomicNameInstance('s', new LinearPosition(2)); var cosRoot = new CompositeNameInstance(new NameInstance[] { c, o, s_ }.ToList(), new LinearPosition(0)); var root = new CompositeNameInstance(new NameInstance[] { sinRoot, cosRoot }.ToList(), new LinearPosition(0)); root.SetParent(null); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); var test = result[0].Select(match => match.Binding).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(Enumerable.SequenceEqual(result[0].Select(match => ((Name)match.Binding.Name).Notation), new[] { Notations.sin.Notation, Notations.cos.Notation })); Contract.Assert(result[0][0].Binding.Index.Index == 0); Contract.Assert(result[0][1].Binding.Index.Index == 3); }
private void SinPlusCosTest(CompositeNameInstance root) { var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(result[0].Count == 2); Assert(result, new[] { new Expectation(Notations.sin, 0), new Expectation(Notations.cos, 3) }); }
public void SinPlusCosTest() { var s = new AtomicNameInstance('s', new LinearPosition(0)); var i = new AtomicNameInstance('i', new LinearPosition(1)); var n = new AtomicNameInstance('n', new LinearPosition(2)); var c = new AtomicNameInstance('c', new LinearPosition(3)); var o = new AtomicNameInstance('o', new LinearPosition(4)); var s_ = new AtomicNameInstance('s', new LinearPosition(5)); var root = new CompositeNameInstance(new NameInstance[] { s, i, n, c, o, s_ }.ToList(), new LinearPosition(0)); root.SetParent(null); SinPlusCosTest(root); root = new NameInstanceBinding(new BoxViewModel(new BoxCtor(new[] { new TextCtor("sincos") }.ToReadOnlyList(), getFontSize: getFontSize))).GetRoot(); SinPlusCosTest(root); var boxRoot = new BoxViewModel(); boxRoot.Elements.Insert(0, new TextCtor("ncos")); boxRoot.Elements.Insert(0, new TextCtor("si")); root = new NameInstanceBinding(boxRoot).GetRoot(); SinPlusCosTest(root); }
public void SinTest() { var s = new AtomicNameInstance('s', new LinearPosition(0)); var i = new AtomicNameInstance('i', new LinearPosition(1)); var n = new AtomicNameInstance('n', new LinearPosition(2)); var test = Notations.sos; var root = new CompositeNameInstance(new NameInstance[] { s, i, n }.ToList(), new LinearPosition(0)); root.SetParent(null); SinTest(root); var binding = new NameInstanceBinding(new BoxViewModel(new BoxCtor(new[] { new TextCtor("sin") }.ToReadOnlyList(), getFontSize: getFontSize))); root = binding.GetRoot(); SinTest(root); var boxRoot = new BoxViewModel(); boxRoot.Elements.Insert(0, new TextCtor("si")); boxRoot.Elements.Insert(2, new TextCtor("n")); root = new NameInstanceBinding(boxRoot).GetRoot(); SinTest(root); }
private void LeafTwoIdenticalNamesTest(CompositeNameInstance root) { var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 4); var expectation1 = new[] { new Expectation(Notations.d1, 0), new Expectation(Notations.d1, 1) }; var expectation2 = new[] { new Expectation(Notations.d1, 0), new Expectation(Notations.d2, 1) }; var expectation3 = new[] { new Expectation(Notations.d2, 0), new Expectation(Notations.d1, 1) }; var expectation4 = new[] { new Expectation(Notations.d2, 0), new Expectation(Notations.d2, 1) }; Assert(result, expectation1, expectation2, expectation3, expectation4); }
public void LeafTwoIdenticalNamesTest() { var treeLeafA = new AtomicNameInstance('d', new LinearPosition(0)); var treeLeafA2 = new AtomicNameInstance('d', new LinearPosition(1)); var root = new CompositeNameInstance(new NameInstance[] { treeLeafA, treeLeafA2 }.ToList(), new LinearPosition(0)); root.SetParent(null); LeafTwoIdenticalNamesTest(root); root = new NameInstanceBinding(new BoxViewModel(new BoxCtor(new[] { new TextCtor("d"), new TextCtor("d") }.ToReadOnlyList(), getFontSize: getFontSize))).GetRoot(); LeafTwoIdenticalNamesTest(root); var boxRoot = new BoxViewModel(); boxRoot.Elements.Insert(0, new TextCtor("d")); boxRoot.Elements.Insert(0, new TextCtor("d")); root = new NameInstanceBinding(boxRoot).GetRoot(); LeafTwoIdenticalNamesTest(root); }
/// <summary> Gets the name instances of the specified box or box composition position. </summary> private NameInstance GetNameInstance(CompositeNameInstance root, Stack<int> position) { if (position.Count == 0) return root; CompositeNameInstance node = root; while (position.Count != 1) { CompositeNameInstance nestedNode = (CompositeNameInstance)node.Elements[position.Pop()]; //do deep clone if temp is a Name and replace it in Elements //check that no element of temp has any mutations planned node = nestedNode; } return node.Elements[position.Pop()]; }
public void SinCosWithArgsTest() { var s = new AtomicNameInstance('s', new LinearPosition(0)); var i = new AtomicNameInstance('i', new LinearPosition(1)); var n = new AtomicNameInstance('n', new LinearPosition(2)); var sin = new CompositeNameInstance(new NameInstance[] { s, i, n }.ToList(), new LinearPosition(0)); var sinarg = new AtomicNameInstance('a', new LinearPosition(0)); var sinRoot = new CompositeNameInstance(new NameInstance[] { sin, sinarg }.ToList(), new LinearPosition(0)); var c = new AtomicNameInstance('c', new LinearPosition(0)); var o = new AtomicNameInstance('o', new LinearPosition(1)); var s_ = new AtomicNameInstance('s', new LinearPosition(2)); var cos = new CompositeNameInstance(new NameInstance[] { c, o, s_ }.ToList(), new LinearPosition(0)); var cosarg = new AtomicNameInstance('b', new LinearPosition(0)); var cosRoot = new CompositeNameInstance(new NameInstance[] { cos, cosarg }.ToList(), new LinearPosition(0)); var root = new CompositeNameInstance(new NameInstance[] { sinRoot, cosRoot }.ToList(), new LinearPosition(0)); root.SetParent(null); var result = inferrer.Match(root).ToList(); var test = result[0].IndexOf(match => match.Binding.Name == Notations.sin); Contract.Assert(result[0].Select(match => match.Binding.Name.Notation).ContainsSameElements(Notations.sin.Notation, Notations.a.Notation, Notations.cos.Notation, Notations.b.Notation)); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.sin)].Binding.Index.Index == 0); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.a)].Binding.Index.Index == 3); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.cos)].Binding.Index.Index == 4); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.b)].Binding.Index.Index == 7); }
public void SinCosWithArgsWithout4thTest() { var s = new AtomicNameInstance('s', new LinearPosition(0)); var i = new AtomicNameInstance('i', new LinearPosition(1)); var n = new AtomicNameInstance('n', new LinearPosition(2)); var sin = new CompositeNameInstance(new NameInstance[] { s, i, n }.ToList(), new LinearPosition(0)); var sinarg = new AtomicNameInstance('a', new LinearPosition(0)); var sinRoot = new CompositeNameInstance(new NameInstance[] { sin, sinarg }.ToList(), new LinearPosition(0)); var c = new AtomicNameInstance('c', new LinearPosition(0)); var o = new AtomicNameInstance('o', new LinearPosition(1)); var s_ = new AtomicNameInstance('s', new LinearPosition(2)); var cos = new CompositeNameInstance(new NameInstance[] { c, o, s_ }.ToList(), new LinearPosition(0)); var cosarg = new AtomicNameInstance('b', new LinearPosition(0)); var cosRoot = new CompositeNameInstance(new NameInstance[] { cos, cosarg }.ToList(), new LinearPosition(0)); var root = new CompositeNameInstance(new NameInstance[] { sinRoot, cosRoot }.ToList(), new LinearPosition(0)); root.SetParent(null); var result = inferrer.Match(root, new BitArray(new[] { false, false, false, true/*of the leaf 'a'*/, false, false, false, false })).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(result[0].Select(match => (match.Binding.Name as Name).Notation).ContainsSameElements(Notations.sin.Notation/*, Notations.a*/, Notations.cos.Notation, Notations.b.Notation)); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.sin)].Binding.Index.Index == 0); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.cos)].Binding.Index.Index == 4); Contract.Assert(result[0][result[0].IndexOf(match => (match.Binding.Name as Name).Notation == Notations.b)].Binding.Index.Index == 7); }
public void TwoIdenticalLeafTest() { var treeLeafA = new AtomicNameInstance('a', new LinearPosition(0)); var treeLeafA2 = new AtomicNameInstance('a', new LinearPosition(1)); var root = new CompositeNameInstance(new NameInstance[] { treeLeafA, treeLeafA2 }.ToList(), new LinearPosition(0)); root.SetParent(null); var result = inferrer.Match(root).ToList(); Contract.Assert(result.Count == 1); Contract.Assert(result[0].Count == 2); Contract.Assert(Contract.ForAll(result, f => f.Sum(match => match.Occupation.Cast<bool>().Count(_ => _)) == result[0].Count));//counts the bits. There must be one bit per leaf Assert(result, new[] { new Expectation(Notations.a, 0), new Expectation(Notations.a, 1) }); }