Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/Projects/Project/Steps/Start.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Start

condition:
deadline: addMonths(CreateDate, 2)

actions:
- roles: [Student]
type: Submit
Expand Down
13 changes: 10 additions & 3 deletions UvA.Workflow.Api/WorkflowInstances/Dtos/WorkflowInstanceDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ RoleAction[] Permissions

public record FieldDto();

public record StepDto(string Id, BilingualString Title, string? Event, DateTime? DateCompleted, StepDto[]? Children)
public record StepDto(
string Id,
BilingualString Title,
string? Event,
DateTime? DateCompleted,
DateTime? Deadline,
StepDto[]? Children)
{
public static StepDto Create(Step step, WorkflowInstance instance)
public static StepDto Create(Step step, WorkflowInstance instance, ModelService modelService)
=> new(
step.Name,
step.DisplayTitle,
step.EndEvent,
step.GetEndDate(instance),
step.Children.Length != 0 ? step.Children.Select(s => Create(s, instance)).ToArray() : null
step.GetDeadline(instance, modelService),
step.Children.Length != 0 ? step.Children.Select(s => Create(s, instance, modelService)).ToArray() : null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<WorkflowInstanceDto> Create(WorkflowInstance instance, Cancell
instance.ParentId,
actions.Select(ActionDto.Create).ToArray(),
[],
entityType.Steps.Select(s => StepDto.Create(s, instance)).ToArray(),
entityType.Steps.Select(s => StepDto.Create(s, instance, modelService)).ToArray(),
submissions
.Select(s => submissionDtoFactory.Create(instance, s.Form, s.Event, s.QuestionStatus))
.ToArray(),
Expand Down
2 changes: 1 addition & 1 deletion UvA.Workflow.Tests/Builders/EventBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EventBuilder AsCompleted(string completionDateString)
/// <returns>The updated <see cref="EventBuilder"/> instance with the completion date set.</returns>
public EventBuilder AsCompleted(int daysAgo = 1)
{
date = DateTime.UtcNow.AddDays(daysAgo);
date = DateTime.Now.AddDays(daysAgo);
return this;
}

Expand Down
52 changes: 52 additions & 0 deletions UvA.Workflow.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,56 @@ public void TestCondition()

Assert.Single(exp.Properties, p => p is ComplexLookup);
}

[Fact]
public void TestDate_DaysAfter()
{
var exp = ExpressionParser.Parse("addDays(now, 5)");
var context = new ObjectContext(new Dictionary<Lookup, object?>());

var res = exp.Execute(context);

Assert.IsType<DateTime>(res);
var date = (DateTime)res;
Assert.Equal(DateTime.Now.AddDays(5).Date, date.Date);
}

[Fact]
public void TestDate_WeeksAfter()
{
var exp = ExpressionParser.Parse("addWeeks(now, 5)");
var context = new ObjectContext(new Dictionary<Lookup, object?>());

var res = exp.Execute(context);

Assert.IsType<DateTime>(res);
var date = (DateTime)res;
Assert.Equal(DateTime.Now.AddDays(5 * 7).Date, date.Date);
}

[Fact]
public void TestDate_MonthsAfter()
{
var exp = ExpressionParser.Parse("addMonths(now, 5)");
var context = new ObjectContext(new Dictionary<Lookup, object?>());

var res = exp.Execute(context);

Assert.IsType<DateTime>(res);
var date = (DateTime)res;
Assert.Equal(DateTime.Now.AddMonths(5).Date, date.Date);
}

[Fact]
public void TestDate_CombinedAfter()
{
var exp = ExpressionParser.Parse("addDays(addMonths(now, 5), 3)");
var context = new ObjectContext(new Dictionary<Lookup, object?>());

var res = exp.Execute(context);

Assert.IsType<DateTime>(res);
var date = (DateTime)res;
Assert.Equal(DateTime.Now.AddMonths(5).AddDays(3).Date, date.Date);
}
}
2 changes: 2 additions & 0 deletions UvA.Workflow/Expressions/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public record Expression
private static readonly Dictionary<string, Function> Functions = new()
{
["addDays"] = new Function<DateTime?, int, DateTime?>((d, i) => d?.AddDays(i)),
["addMonths"] = new Function<DateTime?, int, DateTime?>((d, i) => d?.AddMonths(i)),
["addWeeks"] = new Function<DateTime?, int, DateTime?>((d, i) => d?.AddDays(7 * i)),
["if"] = new Function<bool, object?, object?, object?>((b, t1, t2) => b ? t1 : t2),
["contains"] = new Function<IEnumerable<object>, object, bool>((a, o) => a?.Contains(o) == true),
["and"] = new Function<bool, bool, bool>((a, b) => a && b),
Expand Down
4 changes: 3 additions & 1 deletion UvA.Workflow/WorkflowInstances/WorkflowInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class WorkflowInstance
public string EntityType { get; set; } = null!;
public string? CurrentStep { get; set; }

public DateTime CreatedOn { get; set; }

// public List<LogEntry> LogEntries { get; set; } = [];
public Dictionary<string, BsonValue> Properties { get; set; } = null!;
public Dictionary<string, InstanceEvent> Events { get; set; } = null!;
Expand Down Expand Up @@ -75,7 +77,7 @@ public void RecordEvent(string eventId, DateTime? date = null)
Events[eventId] = new InstanceEvent
{
Id = eventId,
Date = date ?? DateTime.UtcNow
Date = date ?? DateTime.Now
};
}

Expand Down
1 change: 1 addition & 0 deletions UvA.Workflow/WorkflowInstances/WorkflowInstanceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async Task<WorkflowInstance> Create(
{
EntityType = entityType,
ParentId = parentId,
CreatedOn = DateTime.Now,
Properties = initialProperties ?? new Dictionary<string, BsonValue>(),
Events = new Dictionary<string, InstanceEvent>()
};
Expand Down
7 changes: 6 additions & 1 deletion UvA.Workflow/WorkflowModel/Conditions/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class Condition
/// </summary>
public Date? Date { get; set; }

/// <summary>
/// Check if a deadline has passed
/// </summary>
public Date? Deadline { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this replace Condition.Date entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess; though there is a small difference, currently the deadline condition is returned to the UI and the date is not


/// <summary>
/// Use a named reusable condition
/// </summary>
Expand All @@ -44,7 +49,7 @@ public class Condition

[JsonIgnore] [YamlIgnore] public Condition? NamedCondition { get; set; }

public ConditionPart Part => Value ?? Logical ?? Date ?? Event ?? NamedCondition?.Part!;
public ConditionPart Part => Value ?? Logical ?? Date ?? Deadline ?? Event ?? NamedCondition?.Part!;

public IEnumerable<Lookup> Properties => Part?.Properties ?? [];

Expand Down
2 changes: 2 additions & 0 deletions UvA.Workflow/WorkflowModel/ObjectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static ObjectContext Create(WorkflowInstance instance, ModelService model
);
dict.Add("Id", instance.Id);
dict.Add("CurrentStep", instance.CurrentStep);
dict.Add("CreateDate", instance.CreatedOn);

foreach (var ev in instance.Events.Values.Where(e => e.Date != null))
dict.Add(ev.Id + "Event", ev.Date);
return new ObjectContext(dict);
Expand Down
12 changes: 12 additions & 0 deletions UvA.Workflow/WorkflowModel/Step.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using UvA.Workflow.Expressions;
using Date = UvA.Workflow.Entities.Domain.Conditions.Date;

namespace UvA.Workflow.Entities.Domain;

public enum StepHierarchyMode
Expand Down Expand Up @@ -77,6 +80,15 @@ public class Step
return null;
}

public DateTime? GetDeadline(WorkflowInstance instance, ModelService modelService)
{
if (Condition?.Deadline == null)
return null;
var exp = ExpressionParser.Parse(Condition.Deadline.Source);
var context = ObjectContext.Create(instance, modelService);
return exp.Execute(context) as DateTime?;
}

public bool HasEnded(ObjectContext context)
{
if (Ends != null)
Expand Down