View
A view in HiLang represents a summary/subset of one or more entities where the projection is defined by the source entity. The most useful views are Node
and Edge
that enable any model to be viewed as nodes.
Views can be created in two ways
entity Cousins.Person= Node ()
where every member of the view has a corresponding member within the entityentity Cousins.Person= Edge (From = this, To = Mother, Name = Name, TypeName = "Mother")
where expressions
The Plan demonstrates the definition of view.
entity Plan.Project : Versioned
= Node (SKey = SKey, Name = Name, TypeName = "Plan"),
BI.Item (WBS = WBS,
Name = Name,
ActualCost = ActualCost,
PlanCost = PlanCost,
ImpliedCost = ImpliedCost,
Plan = Duration,
Actual = Duration,
TypeName = "Plan")
entity Plan.Tasks.Task : Versioned
= Node (SKey = SKey, Name = Name, TypeName = "Plan-Task"),
Edge (From = Project, To = this, TypeName = "Plan-Task", Name = Name),
Plan.Tasks.SubTask (Task = this, Parent = Parent),
BI.Item (WBS = BIWBS,
Name = Name,
ActualCost = ActualCost,
PlanCost = PlanCost,
ImpliedCost = ImpliedCost,
Plan = Duration,
Actual = Actual.Duration,
TypeName = Parent = null ? "Task" : "SubTask")
segment Plan.Tasks.TaskResource = Node (SKey = SKey, Name = Resource.Name, TypeName = "Plan-Task-Resource"),
Edge (From = owner, To = Resource, Name = Resource.Name, TypeName = "Plan-Task-Resource")
entity Plan.Resource : Versioned
= Node (SKey = SKey, Name = Name, TypeName = "Plan-Resource")
view BI.Item = Node (SKey = SKey, Name = Name, TypeName = "BI-Cost-" + TypeName)
(
WBS : String,
TypeName : String
)
{
Name : String,
ActualCost : Decimal,
PlanCost : Decimal,
ImpliedCost : Decimal,
Plan : Plan.Duration,
Actual : Plan.Duration
}
[
PlanStart = Plan.Start,
PlanEnd = Plan.End,
PlanTime = Plan.Time,
ActualStart = Actual.Start,
ActualEnd = Actual.End,
ActualTime = Actual.Time
];
is equvilent to the SQL definition
CREATE OR REPLACE VIEW BI_ITEM
( WBS,
TypeName,
Name,
ActualCost,
PlanCost,
ImpliedCost,
Plan,
PlanStart,
PlanEnd,
PlanTime,
ActualStart,
ActualEnd,
ActualTime
) AS
SELECT WBS,
'Plan'
Name,
ActualCost,
PlanCost,
ImpliedCost,
Duration_Start,
Duration_End,
Duration_Time,
Duration_Start,
Duration_End,
Duration_Time,
FROM Plan_Project
UNION ALL
SELECT WBS,
'Task'
Name,
ActualCost,
PlanCost,
ImpliedCost,
Duration_Start,
Duration_End,
Duration_Time,
Actual.Start,
Actual.End,
Actual.Time,
Plan_Tasks_Task.Duration,
Actual.Duration,
FROM Plan_Tasks_Task LEFT OUTER JOIN
Actual ON Plan_Tasks_Task.Id = Actual.owner
WHERE Parent_Id IS NULL
UNION ALL
SELECT WBS,
'Sub-Task'
Name,
ActualCost,
PlanCost,
ImpliedCost,
Duration_Start,
Duration_End,
Duration_Time,
Actual.Start,
Actual.End,
Actual.Time,
Plan_Tasks_Task.Duration,
Actual.Duration,
FROM Plan_Tasks_Task LEFT OUTER JOIN
Actual ON Plan_Tasks_Task.Id = Actual.owner
WHERE Parent_Id IS NOT NULL
;
CREATE OR REPLACE VIEW Node (SKey, Name, TypeName) AS
SELECT SKey, Name, 'Plan' FROM Plan_Project
UNION ALL
SELECT SKey, Name, 'Plan-Task' FROM Plan_Tasks_Task
UNION ALL
SELECT SKey, Name, 'Plan-Resource' FROM Plan_Resource
UNION ALL
SELECT SKey, Name, 'Plan-Task-Resource' FROM Plan_Tasks_TaskResource
UNION ALL
SELECT SKey, Name, 'BI-Item-' || BI_ITEM.TypeName FROM BI_ITEM
;
CREATE OR REPLACE VIEW Edge ("From", "To", TypeName, Name) AS
SELECT Project.SKey, Plan_Tasks_Task.SKey, 'Plan-Task', Name
FROM Plan_Tasks_Task INNER JOIN
Plan_Project AS Project ON Plan_Tasks_Task.Project_Id = Project.Id
WHERE Parent_Id IS NULL
UNION ALL
SELECT Parent.SKey, Plan_Tasks_Task.SKey, 'Plan-Task-Part', Name
FROM Plan_Tasks_Task INNER JOIN
Plan_Tasks_Task as Parent ON Plan_Tasks_Task.Parent_Id = Id
UNION ALL
SELECT Plan_Tasks_Task.SKey, Resource.SKey, 'Plan-Task-Resource', Name
FROM Plan_Tasks_Task INNER JOIN
Plan_Resource AS Resource ON Plan_Tasks_Task.Resource_Id = Resource.Id
;
HiLang syntax is much clearer, because the mapping of an entity / segment / aspect to a view is part of the definition of the element and not the view.
Views are evaluated in parallel in the HiLang generated code and use any key references to select elements. Node
and Edge
use the derived property SKey, which is a text encoding of the elements key which is decoded to only query elements that are of the key type. This is especially useful when navigating from Edge
to a Node
which queries Node
using the key