using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
inte易做图ce ITask
{
int During { get; }
IEnumerable<TaskItem> KeyPaths { get; }
}
class TaskItem : ITask
{
public string Name { get; set; }
public int During { get; set; }
public IEnumerable<TaskItem> KeyPaths { get { return new TaskItem[] { this }; } }
}
class TaskGroup : ITask
{
public List<ITask> Tasks { get; set; }
public int During { get { return Tasks.Max(x => x.During); } }
public IEnumerable<TaskItem> KeyPaths { get { return Tasks.OrderByDescending(x => x.During).First().KeyPaths; } }
}
class TaskString : ITask
{
public List<ITask> Tasks { get; set; }
public int During { get { return Tasks.Sum(x => x.During); } }
public IEnumerable<TaskItem> KeyPaths { get { return Tasks.SelectMany(x => x.KeyPaths); } }
}
static class ITaskHelper
{
public static TaskItem StartWith(string name, int during)
{
return new TaskItem() { Name = name, During = during };
}
public static TaskGroup And(this ITask thistesk, ITask tesk)
{
if (thistesk is TaskGroup)
{
(thistesk as TaskGroup).Tasks.Add(tesk);
return thistesk as TaskGroup;
}
return new TaskGroup() { Tasks = new List<ITask>() { thistesk, tesk } };
}
public static TaskGroup And(this ITask thistesk, string name, int during)
{
return ITaskHelper.And(thistesk, new TaskItem() { Name = name, During = during });
}
public static TaskString Then(this ITask thistesk, ITask tesk)
{
if (thistesk is TaskString)
{
(thistesk as TaskString).Tasks.Add(tesk);
return thistesk as TaskString;
}
return new TaskString() { Tasks = new List<ITask>() { thistesk, tesk } };
}
public static TaskString Then(this ITask thistesk, string name, int during)
{
return ITaskHelper.Then(thistesk, new TaskItem() { Name = name, During = during });
}
}
class Program
{
static void Main(string[] args)
{
var t = ITaskHelper.StartWith("找茶叶", 1).And("洗茶碗", 3).And(ITaskHelper.StartWith("洗茶壶", 2).Then("烧开水", 10)).Then("泡茶", 10).And("招呼客人聊天", 5).Then("上茶", 1);
Console.WriteLine(string.Join("->", t.KeyPaths.Select(x => x.Name)));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
inte易做图ce ITask
{
int During { get; }
IEnumerable<TaskItem> KeyPaths { get; }
}
class TaskItem : ITask
{
public string Name { get; set; }
public int During { get; set; }
public int Order { get; set; }
public IEnumerable<TaskItem> KeyPaths { get { return new TaskItem[] { this }; } }
public override string ToString()
{
return string.Format("{0}({1})", Name, During);
}
}
class TaskGroup : ITask
{
public List<ITask> Tasks { get; set; }
public int During { get { return Tasks.Max(x => x.During); } }
public IEnumerable<TaskItem> KeyPaths { get { return Tasks.OrderByDescending(x => x.During).First().KeyPaths; } }
public override string ToString()
{
return "[" + string.Join(" / ", Tasks.Select(x => x.ToString())) + "]";
}
}
class TaskString : ITask
{
public List<ITask> Tasks { get; set; }
public int During { get { return Tasks.Sum(x => x.During); } }
public IEnumerable<TaskItem> KeyPaths { get { return Tasks.SelectMany(x => x.KeyPaths); } }
public override string ToString()
{
return string.Join(" -> ", Tasks.Select(x => string.Format("{0}", x.ToString())));
}
}
static class ITaskHelper
{
public static TaskItem StartWith(string name, int during)
{
return new TaskItem() { Name = name, During = during };
}
public static TaskGroup And(this ITask thistesk, ITask tesk)
{
if (thistesk is TaskGroup)
{
(thistesk as TaskGroup).Tasks.Add(tesk);
return thistesk as TaskGroup;
}
return new TaskGroup() { Tasks = new List<ITask>() { thistesk, tesk } };
}
public static TaskGroup And(this ITask thistesk, string name, int during)
{
return ITaskHelper.And(thistesk, new TaskItem() { Name = name, During = during });
}
public static TaskString Then(this ITask thistesk, ITask tesk)
{
if (thistesk is TaskString)
{
(thistesk as TaskString).Tasks.Add(tesk);
return thistesk as TaskString;
}
return new TaskString() { Tasks = new List<ITask>() { thistesk, tesk } };
}
public static TaskString Then(this ITask thistesk, string name, int during)
{
return ITaskHelper.Then(thistesk, new TaskItem() { Name = name, During = during });
}
}
private AutoSchedule()
{
Tasks = new List<TaskItem>();
DependList = new Dictionary<TaskItem, List<TaskItem>>();
}
public static AutoSchedule New() { return new AutoSchedule(); }
public AutoSchedule DefineTask(string name, int during)
{
if (Tasks.Any(x => x.Name == name)) throw new ArgumentException(name + " is already in list");
Tasks.Add(new TaskItem() { Name = name, During = during });
return this;
}
bool AIsRequiredB(string a, string b)
{
if (DependList.Keys.Any(x => x.Name == a))
return DependList[DependList.Keys.Single(x => x.Name == a)].Any(x => x.Name == b);
return false;
}
IEnumerable<string> whichRequiresA(string a)
{
if (DependList.Keys.Any(x => x.Name == a))
return DependList[DependList.Keys.Single(x => x.Name == a)].Select(x => x.Name);
return new string[] { };
}