我正在尝试建模一个非常简单的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)
答案 0 :(得分:1)
如果我理解正确,我的目标是创造一个“跟随观察者”的观察,当一个男人开始看着一个女人时,一个“跟随观察者”开始,当男人停止看着那个女人时结束。 “跟随观察”应该包括男女最近位置的元组。
这里的想法是使用CombineLatest
,它将采用两个可观察量,并且当它们中的任何一个产生一个值时,组合器将被评估两个最近的可观察值,它产生一个值。结合可观察。但是,CombineLatest
仅在两个可观察对象都已完成时才完成。在这种情况下,我们希望在两个源中的任何一个完成时完成observable。为此,我们定义了以下扩展方法(我不相信这样的方法已经存在,但可能有一个更简单的解决方案):
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
尽早完成该观察。为了得到“跟随观察者”,我们选择男人正在看女人的条纹。然后我们将其映射到一个位置元组。
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));
答案 1 :(得分:0)
我不确定我理解为什么你将男性和女性的位置流建模为IObservable<IObservable<T>>
而不仅仅是IObservable<T>
,但这可能有用:
public static IObservable<Tuple<double, double>> GetLocationsObservable(IObservable<IObservable<Man>> menObservable,
IObservable<IObservable<Woman>> womenObservable)
{
return Observable.CombineLatest(
menObservable.Switch(),
womenObservable.Switch(),
(man, woman) => new {man, woman})
.Where(manAndWoman => manAndWoman.man.LookingAt == manAndWoman.woman.Id)
.Select(manAndWoman => Tuple.Create(manAndWoman.man.Location, manAndWoman.woman.Location));
}
当按下开关时,开关基本上“切换”到新的observable,这会使流变平。 where和select非常简单。
我有一种潜在的怀疑,我误解了有关要求的事情,但我想我会提交我的答案,以防它有所帮助。