# Deriving actuator commands
#
# Sensor readings, targets, and disturbances describe a small control
# problem. N3 arithmetic rules derive commands that anticipate a
# disturbance and correct the remaining error. A query returns the commands.

@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix math: <http://www.w3.org/2000/10/swap/math#> .
@prefix : <https://example.org/control-system#> .

# Facts
# input
:input1 :measurement1 (6 11) .
:input2 :measurement2 true .
:input3 :measurement3 56967 .

# disturbance
:disturbance1 :measurement3 35766 .
:disturbance2 :measurement1 (45 39) .

# state
:state1 :observation1 80 .
:state2 :observation2 false .
:state3 :observation3 22 .

# output
:output2 :measurement4 24 .
:output2 :target2 29 .

# control logic
{
    :input1 :measurement10 ?M1 .
    :input2 :measurement2 true .
    :disturbance1 :measurement3 ?D1 .
    (?M1 19.6) math:product ?C1 .
    (10 ?C2) math:exponentiation ?D1 .
    (?C1 ?C2) math:difference ?C .
} => {
    :actuator1 :control1 ?C .
} .

{
    :input3 :measurement3 ?M3 .
    :state3 :observation3 ?P3 .
    :output2 :measurement4 ?M4 .
    :output2 :target2 ?T2 .
    (?T2 ?M4) math:difference ?E .
    (?P3 ?M4) math:difference ?D .
    (5.8 ?E) math:product ?C1 .
    (7.3 ?E) math:quotient ?N .
    (?N ?D) math:product ?C2 .
    (?C1 ?C2) math:sum ?C .
} => {
    :actuator2 :control1 ?C .
} .

{
    ?I :measurement10 ?M .
} <= {
    ?I :measurement1 (?M1 ?M2) .
    ?M1 math:lessThan ?M2 .
    (?M2 ?M1) math:difference ?M3 .
    (?M3 0.5) math:exponentiation ?M .
} .

{
    ?I :measurement10 ?M1 .
} <= {
    ?I :measurement1 (?M1 ?M2) .
    ?M1 math:notLessThan ?M2 .
} .

# Query answer
{
    ?Actuator :control1 ?Command .
} log:query {
    ?Actuator :control1 ?Command .
} .
