Intersecting collections and counting lists

Hi so in my application I have some Employees

	'Employees' : collection ['BSN'] {
		'BSN' : text
		'Name' : text
	}

Some Ministry storing their criminal records

	'Ministry of Justice Records': collection ['Employee'] {
		'Employee' : text -> ^ .'Employees'[]
		'Criminal history' : stategroup (
			'None' { }
			'Crime' { }
		)
	}

And an application that has a list of R&D Employees that worked on the application

	'My Applications': collection ['ID'] {
		can-read: user is ( > 'Creator') || user .'Type'?'Admin'
		can-update: user . 'Type'?'Unknown'
		'ID' : text
		'Creator' : text -> ^ . 'Users'[]
		'Date' : number 'date'
		'type': stategroup (
			'Lump Sum' { }
			'Investment' { }
		)
		'R&D Employees': collection ['Employee'] {
			'Employee' : text -> ^ ^ .'Employees'[]
		}
		'Number of R&D hours': number 'hours'
		'Additional R&D hours': number 'hours'
		'Application valid': text = switch .'Number of R&D hours' compare ( 500 ) (
			| >=  => "Yes"
			| <  => "No: Number of R&D should be at least 500h"
		)
		'Approval': stategroup (
			'Approved' { }
			'Not approved' { }
		)

	}

I would like to create a command that lets me check that less than 30% of the R&D employees have a positive criminal record. Here is some pseudo code that shows more or less what I am trying to do:

def check_application (application): 
	employees = application.get_R&D_employees()
	return check_criminal_record (employees) && check_tax_profile (employees) && check_skill (employees)

// Should return the yes if less than 30% of employees have Crime in their criminal record
def check_criminal_record(employees)

	// Get list of booleans (crime or none) that indicates if employees have a crime
	RecordsOfEmployees = intersection (employees, MinistryOfJustice).crime

	//Count how many employees have a crime
	NumberOfCrimes = count(RecordsOfEmployees, Crime)

	return NumberOfCrimes/Length(employees) < 0.3
	

// I would like to do similar things for the rest of the checks (check_tax_profile and check_skill)
			

I am a bit confused on where to start with I should try to obtain the intersection of R&D Employees and the Ministry but should I use a switch statement within a walk on the Ministry ? Furthermore I’m really confused how I’m going to do the count part of the code…

You mention that you want a command, but from your description I think you want to derive certain values. For the intersection, you need a reference-set. For the counting part, you need count :slight_smile: . This should help:

root {
	'Employees' : collection ['BSN'] {
		'BSN' : text
		'Name' : text
		'Records': reference-set -> downstream ^ .'Ministry of Justice Records'* = inverse >'Employee'
		'Criminal Record': stategroup = switch <'Records'* .'Criminal history'?'Crime' (
			| nodes => 'Yes' ( )
			| none => 'No' ( )
		) (
			'Yes' { }
			'No' { }
		)
	}
	'Ministry of Justice Records': collection ['Employee'] {
		'Employee' : text -> ^ .'Employees'[] -<'Records'
		'Criminal history' : stategroup (
			'None' { }
			'Crime' { }
		)
	}
	'Criminals Threshold': number 'percentage' = 30
	'My Applications': collection ['ID'] {
		// ...
		'ID' : text
		// ...
		'R&D Employees': collection ['Employee'] {
			'Employee' : text -> ^ ^ .'Employees'[]
			'Criminal Record': stategroup = switch >'Employee'.'Criminal Record' (
				|'Yes' => 'Yes' ( )
				|'No' => 'No' ( )
			)  (
				'Yes' { }
				'No' { }
			)
		}
		'Number of R&D Employees': number 'count' = count .'R&D Employees'*
		'Number of Criminals': number 'count' = count .'R&D Employees'* .'Criminal Record'?'Yes'
		'Percentage Criminals': number 'percentage' = switch .'Number of R&D Employees' compare ( 0 ) (
			| > as $'positive count' => from 'factor' division (
				.'Number of Criminals' as 'count', /*refers to division rule = 'count' / 'count'*/
				$'positive count'
			)
			| <= => 0
		)
		'Too many criminals': stategroup = switch .'Percentage Criminals' compare ( ^ .'Criminals Threshold' ) (
			| >= => 'Yes' ( )
			| <  => 'No' ( )
		) (
			'Yes' { }
			'No' { }
		)
		// ...
	}
}

numerical-types
	'count'
	'percentage'
		= 'factor' * 1 * 10 ^ 2
	'factor'
		= 'count' / 'count'