First of all, your domain is super fun to model! You’ll see below why!

Unfortunately, I think until we have subqueries or aggregations in TypeQL, we cannot actually implement what you’re looking for, mostly because of the “6A+” and “4A+” pieces. I’ll illustrate the bits we can do first.

the classification of grades is easy (and can be modeled in multiple interesting ways):

```
when {
$s isa student;
$course (attendee: $s, class: $c) isa course-attendance, has exam-result $m;
$m > 90; $m < 100;
$A "A" isa grade;
} then {
(attendee: $s, course: $course, grade: $A) isa course-outcome;
}
```

so here we’re checking the `exam-result`

outcome attached to a `course`

relation. We also look up some pre-inserted `grade`

instance (you need to pre-create `A`

, `B`

…), and create a course outcome relation that points at the course and the grade attribute! (so we’re using an attribute on a relation in the `when`

, and creating a nested relation that also has an attribute role player

You’d create a couple variants of these to create your grading rules.

The step we’re currently unable to serve is the count of how many "A"s a student got. eg. we can’t do this:

[making up some syntax]

```
set a-count $as as match $s isa student; (attendee: $x, grade: $g) isa course-outcome; $g "A" isa grade; count $g;
set b-count $as as match $s isa student; (attendee: $x, grade: $g) isa course-outcome; $g "B" isa grade; count $g;
...
```

in a rule or in a match query at this time.

The closest you can get is to hard-code the rules as follows:

```
when {
$s isa student; $g "A" isa grade;
$outcome-1 (attendee: $s, grade: $g) isa course-outcome;
$outcome-2 (attendee: $s, grade: $g) isa course-outcome;
$outcome-3 (attendee: $s, grade: $g) isa course-outcome;
$outcome-4 (attendee: $s, grade: $g) isa course-outcome;
$outcome-5 (attendee: $s, grade: $g) isa course-outcome;
$outcome-6 (attendee: $s, grade: $g) isa course-outcome;
not { $outcome-1 is $outcome-2;}; not { $outcome-1 is $outcome-3;}; not { $outcome-1 is $outcome-4;};
not { $outcome-1 is $outcome-5;}; not { $outcome-1 is $outcome-6;};
not { $outcome-2 is $outcome-3;}; not { $outcome-2 is $outcome-4;}; not { $outcome-2 is $outcome-5;};
not { $outcome-2 is $outcome-6;};
not { $outcome-3 is $outcome-4;}; not { $outcome-3 is $outcome-5;}; not { $outcome-3 is $outcome-6;};
not { $outcome-4 is $outcome-5;}; not { $outcome-4 is $outcome-6;};
not { $outcome-5 is $outcome-6;};
[ some other constraint for "D"]
} then {
$s has classification "outstanding";
}
```

then you can chain the other rules:

```
when {
$s isa student; $g "A" isa grade;
not { $s has classification "outstanding"; };
$outcome-1 (attendee: $s, grade: $g) isa course-outcome;
$outcome-2 (attendee: $s, grade: $g) isa course-outcome;
$outcome-3 (attendee: $s, grade: $g) isa course-outcome;
$outcome-4 (attendee: $s, grade: $g) isa course-outcome;
not { $outcome-1 is $outcome-2;}; not { $outcome-1 is $outcome-3;}; not { $outcome-1 is $outcome-4;};
not { $outcome-2 is $outcome-3;}; not { $outcome-2 is $outcome-4;};
not { $outcome-3 is $outcome-4;};
[ some other constraint for "D"]
} then {
$s has classification "good";
}
```

etc.

then you can chain the other rules:

```
when {
$s isa student; $g "A" isa grade;
not { $s has classificaton "outstanding"; };
not { $s has classification "good"; };
$outcome-1 (attendee: $s, grade: $g) isa course-outcome;
$outcome-2 (attendee: $s, grade: $g) isa course-outcome;
not { $outcome-1 is $outcome-2;}; not { $outcome-1 is $outcome-3;};
not { $outcome-2 is $outcome-3;};
[ some other constraint for "D"]
} then {
$s has classification "pass";
}
```

this is mildly horrific, but may work… although I expect it to hit the reasoning engine quite hard if you have a decent amount of data. Super interesting case though!