public void Root_PassesThroughPreviousState_OnUnknownAction() { var initialState = new AppState(); var newState = Reducers.Root(initialState, "Unknown action"); Assert.Equal(initialState, newState); }
public void Root_DeletesItem_OnDeleteItem() { var itemId = AppState.SampleState.Index[1]; var newState = Reducers.Root(AppState.SampleState, new Actions.DeleteItem(itemId)); Assert.DoesNotContain(itemId, (IDictionary <Guid, Item>)newState.Items); }
private Model() { SelectedPoints.Changed += () => RaisePropertyChanged("SelectedPoints"); Reducers.Add(new ReducerRecord( "Iterated Inverse Elevation", "inverse-elevation", new ReducerFactory <IteratedInverseElevationReducer>(), null )); Reducers.Add(new ReducerRecord( "Iterated Optimal", "optimal", new ReducerFactory <IteratedOptimalReducer>(), null )); Reducers.Add(new ReducerRecord( "Hermite Interpolation", "hermite", new ReducerFactory <HermiteReducer>(), new ConstrainedReducerView() )); Reducers.Add(new ReducerRecord( "Least Squares", "least-squares", new ReducerFactory <LeastSquaresReducer>(), new ConstrainedReducerView() )); }
public void Root_TogglesItemValue_OnToggleItem() { var item = new Item(); var state = AppState.FromItems(new Item[] { item }); var newState = Reducers.Root(state, new Actions.ToggleItem(item.ID, true)); Assert.True(newState.Items[item.ID].ToggleValue); }
public void Load(string filename) { // We assume model is clear. XmlDocument doc = new XmlDocument(); doc.Load(filename); BaseCurve.Points.Clear(); XmlNode dictNode = doc.SelectSingleNode("plist/dict"); XmlNode baseCurveNode = dictNode.SelectSingleNode("base-curve"); BaseCurve.Color = Color.FromArgb(int.Parse( baseCurveNode.Attributes["color"].Value, NumberStyles.HexNumber )); XmlNode startPointNode = dictNode.SelectSingleNode("startPoint"); Vector2D startPoint = new Vector2D( double.Parse( startPointNode.Attributes["x"].Value, CultureInfo.InvariantCulture ), double.Parse( startPointNode.Attributes["y"].Value, CultureInfo.InvariantCulture ) ); foreach (XmlNode pointNode in dictNode.SelectNodes("array/string")) { Vector2D vec = PointFromString(pointNode.InnerText); BaseCurve.Points.Add(vec + startPoint); } SelectedCurve = null; ReducedCurves.Clear(); foreach (XmlNode reducedCurveNode in dictNode.SelectNodes("reduced-curve")) { ReducerRecord record = Reducers.Find( r => r.XmlName == reducedCurveNode.Attributes["method"] .Value ); Reducer reducer = record.Factory.Produce(); reducer.ReadCustomAttributes(reducedCurveNode); ReducedBezierCurve curve = new ReducedBezierCurve( BaseCurve, reducer); curve.Degree = int.Parse( reducedCurveNode.Attributes["degree"].Value); curve.Color = Color.FromArgb(int.Parse( reducedCurveNode.Attributes["color"].Value, NumberStyles.HexNumber )); ReducedCurves.Add(curve); } FileName = filename; }
public object Clone() { var dic = new Reducers(); foreach (var pair in this) { dic.Add(pair.Key, pair.Value); } return(dic); }
public void TestApplyAndFilterAggregations() { /** * 127.0.0.1:6379> FT.CREATE test_index SCHEMA name TEXT SORTABLE subj1 NUMERIC SORTABLE subj2 NUMERIC SORTABLE * OK * 127.0.0.1:6379> FT.ADD test_index data1 1.0 FIELDS name abc subj1 20 subj2 70 * OK * 127.0.0.1:6379> FT.ADD test_index data2 1.0 FIELDS name def subj1 60 subj2 40 * OK * 127.0.0.1:6379> FT.ADD test_index data3 1.0 FIELDS name ghi subj1 50 subj2 80 * OK * 127.0.0.1:6379> FT.ADD test_index data1 1.0 FIELDS name abc subj1 30 subj2 20 * OK * 127.0.0.1:6379> FT.ADD test_index data2 1.0 FIELDS name def subj1 65 subj2 45 * OK * 127.0.0.1:6379> FT.ADD test_index data3 1.0 FIELDS name ghi subj1 70 subj2 70 * OK */ Client cl = GetClient(); Schema sc = new Schema(); sc.AddSortableTextField("name", 1.0); sc.AddSortableNumericField("subj1"); sc.AddSortableNumericField("subj2"); cl.CreateIndex(sc, new ConfiguredIndexOptions()); cl.AddDocument(new Document("data1").Set("name", "abc").Set("subj1", 20).Set("subj2", 70)); cl.AddDocument(new Document("data2").Set("name", "def").Set("subj1", 60).Set("subj2", 40)); cl.AddDocument(new Document("data3").Set("name", "ghi").Set("subj1", 50).Set("subj2", 80)); cl.AddDocument(new Document("data4").Set("name", "abc").Set("subj1", 30).Set("subj2", 20)); cl.AddDocument(new Document("data5").Set("name", "def").Set("subj1", 65).Set("subj2", 45)); cl.AddDocument(new Document("data6").Set("name", "ghi").Set("subj1", 70).Set("subj2", 70)); AggregationBuilder r = new AggregationBuilder().Apply("(@subj1+@subj2)/2", "attemptavg") .GroupBy("@name", Reducers.Avg("@attemptavg").As("avgscore")) .Filter("@avgscore>=50") .SortBy(10, SortedField.Ascending("@name")); // actual search AggregationResult res = cl.Aggregate(r); Row?r1 = res.GetRow(0); Assert.NotNull(r1); Assert.Equal("def", r1.Value.GetString("name")); Assert.Equal(52.5, r1.Value.GetDouble("avgscore")); Row?r2 = res.GetRow(1); Assert.NotNull(r2); Assert.Equal("ghi", r2.Value.GetString("name")); Assert.Equal(67.5, r2.Value.GetDouble("avgscore")); }
public void Save(string filename) { using (XmlTextWriter tw = new XmlTextWriter(filename, Encoding.ASCII)) { tw.Formatting = Formatting.Indented; tw.Indentation = 4; tw.WriteStartDocument(true); tw.WriteStartElement("bezier-toy"); tw.WriteStartElement("base-curve"); tw.WriteAttributeString( "color", BaseCurve.Color.ToArgb().ToString("X8") ); foreach (Vector2D point in BaseCurve.Points) { tw.WriteStartElement("point"); tw.WriteAttributeString( "x", point.X.ToString(CultureInfo.InvariantCulture) ); tw.WriteAttributeString( "y", point.Y.ToString(CultureInfo.InvariantCulture) ); tw.WriteEndElement(); } tw.WriteEndElement(); foreach (ReducedBezierCurve curve in ReducedCurves) { tw.WriteStartElement("reduced-curve"); tw.WriteAttributeString( "method", Reducers.Find(r => r.Factory.CanProduce(curve.Reducer)) .XmlName ); tw.WriteAttributeString("degree", curve.Degree.ToString()); tw.WriteAttributeString( "color", curve.Color.ToArgb().ToString("X8") ); curve.Reducer.WriteCustomAttributes(tw); tw.WriteEndElement(); } tw.WriteEndElement(); tw.WriteEndDocument(); } }
public void TestAggregations() { /** * 127.0.0.1:6379> FT.CREATE test_index SCHEMA name TEXT SORTABLE count NUMERIC SORTABLE * OK * 127.0.0.1:6379> FT.ADD test_index data1 1.0 FIELDS name abc count 10 * OK * 127.0.0.1:6379> FT.ADD test_index data2 1.0 FIELDS name def count 5 * OK * 127.0.0.1:6379> FT.ADD test_index data3 1.0 FIELDS name def count 25 */ Client cl = GetClient(); Schema sc = new Schema(); sc.AddSortableTextField("name", 1.0); sc.AddSortableNumericField("count"); cl.CreateIndex(sc, new ConfiguredIndexOptions()); cl.AddDocument(new Document("data1").Set("name", "abc").Set("count", 10)); cl.AddDocument(new Document("data2").Set("name", "def").Set("count", 5)); cl.AddDocument(new Document("data3").Set("name", "def").Set("count", 25)); AggregationBuilder r = new AggregationBuilder() .GroupBy("@name", Reducers.Sum("@count").As("sum")) .SortBy(10, SortedField.Descending("@sum")); // actual search AggregationResult res = cl.Aggregate(r); Row?r1 = res.GetRow(0); Assert.NotNull(r1); Assert.Equal("def", r1.Value.GetString("name")); Assert.Equal(30, r1.Value.GetInt64("sum")); Assert.Equal(30.0, r1.Value.GetDouble("sum")); Assert.Equal(0L, r1.Value.GetInt64("nosuchcol")); Assert.Equal(0.0, r1.Value.GetDouble("nosuchcol")); Assert.Null(r1.Value.GetString("nosuchcol")); Row?r2 = res.GetRow(1); Assert.NotNull(r2); Assert.Equal("abc", r2.Value.GetString("name")); Assert.Equal(10L, r2.Value.GetInt64("sum")); }
public override IDataMutation?TryDipatch(IStateAction action, Action <IReducerResult> sendResult, Action onCompled) { var reducers = Reducers.Where(r => r.ShouldReduceStateForAction(action)).ToList(); if (reducers.Count == 0) { return(null); } return(MutatingEngine.CreateMutate(action.ActionName, action.Query, async data => { try { var isFail = false; foreach (var reducer in reducers) { var mayResult = await reducer.Reduce(data, action); var tempData = Collapse(from result in mayResult from _ in MayUse(() => sendResult(result)) where result.IsOk select result.Data); if (tempData.IsNothing()) { isFail = true; } data = tempData.Or(data); } return isFail ? Maybe <MutatingContext <TData> > .Nothing : data; } finally { onCompled(); } })); }
public void Root_SetsValueToZero_OnResetAction() { var newState = Reducers.Root(new AppState(8), new Actions.ResetCounter()); Assert.Equal(0, newState.CounterValue); }
public void Root_IncrementsValue_OnIncrementAction() { var newState = Reducers.Root(new AppState(), new Actions.IncrementCounter(5)); Assert.Equal(5, newState.CounterValue); }
public async Task TestCursor() { /* * 127.0.0.1:6379> FT.CREATE test_index SCHEMA name TEXT SORTABLE count NUMERIC SORTABLE * OK * 127.0.0.1:6379> FT.ADD test_index data1 1.0 FIELDS name abc count 10 * OK * 127.0.0.1:6379> FT.ADD test_index data2 1.0 FIELDS name def count 5 * OK * 127.0.0.1:6379> FT.ADD test_index data3 1.0 FIELDS name def count 25 */ Client cl = GetClient(); Schema sc = new Schema(); sc.AddSortableTextField("name", 1.0); sc.AddSortableNumericField("count"); cl.CreateIndex(sc, new ConfiguredIndexOptions()); cl.AddDocument(new Document("data1").Set("name", "abc").Set("count", 10)); cl.AddDocument(new Document("data2").Set("name", "def").Set("count", 5)); cl.AddDocument(new Document("data3").Set("name", "def").Set("count", 25)); AggregationBuilder r = new AggregationBuilder() .GroupBy("@name", Reducers.Sum("@count").As("sum")) .SortBy(10, SortedField.Descending("@sum")) .Cursor(1, 3000); // actual search AggregationResult res = cl.Aggregate(r); Row?row = res.GetRow(0); Assert.NotNull(row); Assert.Equal("def", row.Value.GetString("name")); Assert.Equal(30, row.Value.GetInt64("sum")); Assert.Equal(30.0, row.Value.GetDouble("sum")); Assert.Equal(0L, row.Value.GetInt64("nosuchcol")); Assert.Equal(0.0, row.Value.GetDouble("nosuchcol")); Assert.Null(row.Value.GetString("nosuchcol")); res = cl.CursorRead(res.CursorId, 1); Row?row2 = res.GetRow(0); Assert.NotNull(row2); Assert.Equal("abc", row2.Value.GetString("name")); Assert.Equal(10, row2.Value.GetInt64("sum")); Assert.True(cl.CursorDelete(res.CursorId)); try { cl.CursorRead(res.CursorId, 1); Assert.True(false); } catch (RedisException) { } _ = new AggregationBuilder() .GroupBy("@name", Reducers.Sum("@count").As("sum")) .SortBy(10, SortedField.Descending("@sum")) .Cursor(1, 1000); await Task.Delay(1000).ForAwait(); try { cl.CursorRead(res.CursorId, 1); Assert.True(false); } catch (RedisException) { } }
public bool RegisterReducers <TAction>(params Reducer <TState, TAction>[] reducers) { return(Reducers <TAction> .RegisterReducers(this, reducers)); }
public void Root_PassesThroughPreviousState_OnUnknownAction() { var newState = Reducers.Root(AppState.Empty, "Unknown action"); Assert.Equal(AppState.Empty, newState); }
public void Root_EmptiesItemList_OnResetItems() { var newState = Reducers.Root(AppState.SampleState, new Actions.ResetItems()); Assert.Empty(newState.Items); }
public void Save(string filename) { if (Model.Instance.SaveBztPng) { SaveBztPngFunc(filename); } using (XmlTextWriter tw = new XmlTextWriter(filename, Encoding.UTF8)) { tw.Formatting = Formatting.Indented; tw.Indentation = 4; tw.WriteStartDocument(true); tw.WriteDocType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null); tw.WriteStartElement("plist"); tw.WriteAttributeString( "version", "1.0"); tw.WriteStartElement("dict"); tw.WriteStartElement("base-curve"); tw.WriteAttributeString( "color", BaseCurve.Color.ToArgb().ToString("X8") ); tw.WriteEndElement(); Vector2D startPoint = BaseCurve.Points.First(); tw.WriteStartElement("startPoint"); tw.WriteAttributeString( "x", startPoint.X.ToString(CultureInfo.InvariantCulture) ); tw.WriteAttributeString( "y", startPoint.Y.ToString(CultureInfo.InvariantCulture) ); tw.WriteEndElement(); tw.WriteElementString("key", "points"); //tw.WriteStartElement("array"); tw.WriteStartElement("array"); foreach (Vector2D point in BaseCurve.Points) { Vector2D temp = point - startPoint; tw.WriteElementString("string", "{" + temp.X.ToString(CultureInfo.InvariantCulture) + "," + (-temp.Y).ToString(CultureInfo.InvariantCulture) + "}"); } tw.WriteEndElement(); foreach (ReducedBezierCurve curve in ReducedCurves) { tw.WriteStartElement("reduced-curve"); tw.WriteAttributeString( "method", Reducers.Find(r => r.Factory.CanProduce(curve.Reducer)) .XmlName ); tw.WriteAttributeString("degree", curve.Degree.ToString()); tw.WriteAttributeString( "color", curve.Color.ToArgb().ToString("X8") ); curve.Reducer.WriteCustomAttributes(tw); tw.WriteEndElement(); } tw.WriteEndElement(); tw.WriteEndDocument(); } }
public void Root_AddsItem_OnAddItem() { var newState = Reducers.Root(AppState.Empty, new Actions.AddItem()); Assert.Single(newState.Items); }