gpt4 book ai didi

c# - 加入 Rx 流

转载 作者:IT王子 更新时间:2023-10-29 04:22:20 25 4
gpt4 key购买 nike

我正在尝试对(对我来说)不重要的 Rx 查询建模:

  • 房间里有男人和女人。
  • 他们进出房间,在房间里时,他们有时会改变位置。
  • 每个男人在给定时间可以看一个(或零个)女人。
  • 每个人都有以下属性:

    class Man
    {
    public const int LookingAtNobody = 0;
    public int Id { get; set; }
    public double Location { get; set; }
    public int LookingAt { get; set; }
    }
  • 每个女人都有以下属性:

    class Woman
    {
    public int Id { get; set; }
    public double Location { get; set; }
    }
  • 为了代表男人,我们有 IObservable<IObservable<Man>> ,并代表我们拥有的女性IObservable<IObservable<Woman>> .

您将如何使用 Rx 生成从男性到他们正在看的女性的向量:IObservable<IObservable<Tuple<double,double>>>

为了提供帮助,这里有一些针对一些简单情况的单元测试:

public class Tests : ReactiveTest
{
[Test]
public void Puzzle1()
{
var scheduler = new TestScheduler();

var m1 = scheduler.CreateHotObservable(
OnNext(100, new Man { Id = 1, Location = 1.0, LookingAt = Man.LookingAtNobody }),
OnNext(200, new Man { Id = 1, Location = 2.0, LookingAt = 10 }),
OnCompleted<Man>(300));

var w1 = scheduler.CreateHotObservable(
OnNext(150, new Woman { Id = 10, Location = 10.0 }),
OnNext(250, new Woman { Id = 10, Location = 20.0 }),
OnCompleted<Woman>(350));

var men = scheduler.CreateHotObservable(OnNext(50, m1));
var women = scheduler.CreateHotObservable(OnNext(50, w1));

var results = runQuery(scheduler, women, men);

var innerResults = (from msg in results
where msg.Value.HasValue
select msg.Value.Value).ToArray();
var expectedVector1 = new[]
{
OnNext(200, Tuple.Create(2.0, 10.0)),
OnNext(250, Tuple.Create(2.0, 20.0)),
OnCompleted<Tuple<double,double>>(300),
};
ReactiveAssert.AreElementsEqual(expectedVector1, innerResults[0]);
}
[Test]
public void Puzzle2()
{
var scheduler = new TestScheduler();

var m1 = scheduler.CreateHotObservable(
OnNext(100, new Man { Id = 1, Location = 1.0, LookingAt = Man.LookingAtNobody }),
OnNext(200, new Man { Id = 1, Location = 2.0, LookingAt = 10 }),
OnCompleted<Man>(400));

var w1 = scheduler.CreateHotObservable(
OnNext(150, new Woman { Id = 10, Location = 10.0 }),
OnNext(250, new Woman { Id = 10, Location = 20.0 }),
OnCompleted<Woman>(350));

var men = scheduler.CreateHotObservable(OnNext(50, m1));
var women = scheduler.CreateHotObservable(OnNext(50, w1));

var results = runQuery(scheduler, women, men);

var innerResults = (from msg in results
where msg.Value.HasValue
select msg.Value.Value).ToArray();
var expectedVector1 = new[]
{
OnNext(200, Tuple.Create(2.0, 10.0)),
OnNext(250, Tuple.Create(2.0, 20.0)),
OnCompleted<Tuple<double,double>>(350),
};
ReactiveAssert.AreElementsEqual(expectedVector1, innerResults[0]);
}
[Test]
public void Puzzle3()
{
var scheduler = new TestScheduler();

var m1 = scheduler.CreateHotObservable(
OnNext(100, new Man { Id = 1, Location = 1.0, LookingAt = Man.LookingAtNobody }),
OnNext(200, new Man { Id = 1, Location = 2.0, LookingAt = 10 }),
OnNext(300, new Man { Id = 1, Location = 2.0, LookingAt = Man.LookingAtNobody }),
OnCompleted<Man>(400));

var w1 = scheduler.CreateHotObservable(
OnNext(150, new Woman { Id = 10, Location = 10.0 }),
OnNext(250, new Woman { Id = 10, Location = 20.0 }),
OnCompleted<Woman>(350));

var men = scheduler.CreateHotObservable(OnNext(50, m1));
var women = scheduler.CreateHotObservable(OnNext(50, w1));

var results = runQuery(scheduler, women, men);

var innerResults = (from msg in results
where msg.Value.HasValue
select msg.Value.Value).ToArray();
var expectedVector1 = new[]
{
OnNext(200, Tuple.Create(2.0, 10.0)),
OnNext(250, Tuple.Create(2.0, 20.0)),
OnCompleted<Tuple<double,double>>(300),
};
ReactiveAssert.AreElementsEqual(expectedVector1, innerResults[0]);
}
[Test]
public void Puzzle4()
{
var scheduler = new TestScheduler();

var m1 = scheduler.CreateHotObservable(
OnNext(100, new Man { Id = 1, Location = 1.0, LookingAt = Man.LookingAtNobody }),
OnNext(200, new Man { Id = 1, Location = 2.0, LookingAt = 10 }),
OnNext(300, new Man { Id = 1, Location = 3.0, LookingAt = 20 }),
OnNext(400, new Man { Id = 1, Location = 4.0, LookingAt = 20 }),
OnCompleted<Man>(500));

var w1 = scheduler.CreateHotObservable(
OnNext(150, new Woman { Id = 10, Location = 10.0 }),
OnNext(250, new Woman { Id = 10, Location = 20.0 }),
OnCompleted<Woman>(350));
var w2 = scheduler.CreateHotObservable(
OnNext(155, new Woman { Id = 20, Location = 100.0 }),
OnNext(255, new Woman { Id = 20, Location = 200.0 }),
OnNext(355, new Woman { Id = 20, Location = 300.0 }),
OnCompleted<Woman>(455));

var men = scheduler.CreateHotObservable(OnNext(50, m1));
var women = scheduler.CreateHotObservable(OnNext(50, w1), OnNext(50, w2));

var results = runQuery(scheduler, women, men);

var innerResults = (from msg in results
where msg.Value.HasValue
select msg.Value.Value).ToArray();
var expectedVector1 = new[]
{
OnNext(200, Tuple.Create(2.0, 10.0)),
OnNext(250, Tuple.Create(2.0, 20.0)),
OnCompleted<Tuple<double,double>>(300),
};
var expectedVector2 = new[]
{
OnNext(300, Tuple.Create(3.0, 200.0)),
OnNext(355, Tuple.Create(3.0, 300.0)),
OnNext(400, Tuple.Create(4.0, 300.0)),
OnCompleted<Tuple<double,double>>(455),
};
ReactiveAssert.AreElementsEqual(expectedVector1, innerResults[0]);
ReactiveAssert.AreElementsEqual(expectedVector2, innerResults[1]);
}

private static IEnumerable<Recorded<Notification<IList<Recorded<Notification<Tuple<double, double>>>>>>> runQuery(TestScheduler scheduler, IObservable<IObservable<Woman>> women, IObservable<IObservable<Man>> men)
{
// assuming nested sequences are hot
var vectors =
from manDuration in men
join womanDuration in women on manDuration equals womanDuration
select from man in manDuration
join woman in womanDuration on manDuration equals womanDuration
where man.LookingAt == woman.Id
select Tuple.Create(man.Location, woman.Location);

var query = vectors.Select(vectorDuration =>
{
var vectorResults = scheduler.CreateObserver<Tuple<double, double>>();
vectorDuration.Subscribe(vectorResults);
return vectorResults.Messages;
});

var results = scheduler.Start(() => query, 0, 0, 1000).Messages;
return results;
}
}

(注意:这个问题交叉发布到 Rx 论坛:http://social.msdn.microsoft.com/Forums/en-US/rx/thread/e73ae4e2-68c3-459a-a5b6-ea957b205abe)

最佳答案

如果我对您的理解正确,目标是创建一个“跟随可观察对象”的可观察对象,其中“跟随可观察对象”从男人开始看女人时开始,到男人停止看女人时结束。 “follow observable”应该包含男人和女人最近位置的元组。

这里的想法是使用CombineLatest,它将接受两个可观察值,当它们中的任何一个产生一个值时,组合器将针对两个可观察值的最新值进行评估,从而产生一个组合可观察值中的值。但是,CombineLatest 仅在两个可观察对象都已完成时才完成。在这种情况下,我们希望在两个来源中的任何一个完成时完成可观察对象。为此,我们定义了以下扩展方法(我不认为这样的方法已经存在,但可能有更简单的解决方案):

public static IObservable<TSource>
UntilCompleted<TSource, TWhile>(this IObservable<TSource> source,
IObservable<TWhile> lifetime)
{
return Observable.Create<TSource>(observer =>
{
var subscription = source.Subscribe(observer);
var limiter = lifetime.Subscribe(next => { }, () =>
{
subscription.Dispose();
observer.OnCompleted();
});
return new CompositeDisposable(subscription, limiter);
});
}

此方法类似于TakeUntil,但它不是一直持续到lifetime 产生一个值,而是一直持续到lifetime 完成。我们还可以定义一个简单的扩展方法,它采用满足谓词的第一个连胜:

public static IObservable<TSource>
Streak<TSource>(this IObservable<TSource> source,
Func<TSource, bool> predicate)
{
return source.SkipWhile(x => !predicate(x)).TakeWhile(predicate);
}

现在对于最终查询,我们使用 CombineLatest 将所有男性和所有女性组合在一起,并使用 UntilCompleted 尽早完成该可观察对象。为了获得“follow observables”,我们选择男人注视女人的那一连串。然后我们简单地将其映射到一个位置元组。

var vectors =
from manDuration in men
from womanDuration in women
select manDuration
.CombineLatest(womanDuration, (m, w) => new { Man = m, Woman = w })
.UntilCompleted(womanDuration)
.UntilCompleted(manDuration)
.Streak(pair => pair.Man.LookingAt == pair.Woman.Id)
.Select(pair => Tuple.Create(pair.Man.Location, pair.Woman.Location));

这通过了您的所有测试,但它无法处理男人看女人 10 一会儿,然后看 20 一会儿,然后再看 10 一会儿的场景;仅使用第一个 连胜。要观察所有条纹,我们可以使用以下扩展方法,它返回条纹的可观察值:

public static IObservable<IObservable<TSource>>
Streaks<TSource>(this IObservable<TSource> source,
Func<TSource, bool> predicate)
{
return Observable.Create<IObservable<TSource>>(observer =>
{
ReplaySubject<TSource> subject = null;
bool previous = false;
return source.Subscribe(x =>
{
bool current = predicate(x);
if (!previous && current)
{
subject = new ReplaySubject<TSource>();
observer.OnNext(subject);
}
if (previous && !current) subject.OnCompleted();
if (current) subject.OnNext(x);
previous = current;
}, () =>
{
if (subject != null) subject.OnCompleted();
observer.OnCompleted();
});
});
}

通过只订阅一次源流,并使用 ReplaySubject,此方法适用于热和冷可观察对象。现在对于最终查询,我们按如下方式选择所有条纹:

var vectors =
from manDuration in men
from womanDuration in women
from streak in manDuration
.CombineLatest(womanDuration, (m, w) => new { Man = m, Woman = w })
.UntilCompleted(womanDuration)
.UntilCompleted(manDuration)
.Streaks(pair => pair.Man.LookingAt == pair.Woman.Id)
select streak.Select(pair =>
Tuple.Create(pair.Man.Location, pair.Woman.Location));

关于c# - 加入 Rx 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9758083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com