# EYE Components for GPS -- Jos De Roo

PREFIX math: <http://www.w3.org/2000/10/swap/math#>
PREFIX list: <http://www.w3.org/2000/10/swap/list#>
PREFIX log: <http://www.w3.org/2000/10/swap/log#>
PREFIX gps: <https://eyereasoner.github.io/eye/reasoning/gps/gps-schema#>

# find paths in the state space from initial state to goal state within limits
CONSTRUCT {
    ?SCOPE gps:findpath (?Goal ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort)).
} WHERE {
    ?SCOPE gps:findpaths (() ?Goal () 0.0 0.0 1.0 1.0 ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort 1)).
} ;

CONSTRUCT {
    ?SCOPE gps:findpath (?Goal ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort ?MaxStagecount)).
} WHERE {
    ?SCOPE gps:findpaths (() ?Goal () 0.0 0.0 1.0 1.0 ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort ?MaxStagecount)).
} ;

CONSTRUCT {
    ?SCOPE gps:findpaths (?Maps ?Goal ?Path ?Duration ?Cost ?Belief ?Comfort ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort ?MaxStagecount)).
} WHERE {
    ?Goal log:call true.
    true log:callWithCut true.
} ;

CONSTRUCT {
    ?SCOPE gps:findpaths (?Maps_s ?Goal ?Path_s ?Duration_s ?Cost_s ?Belief_s ?Comfort_s ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort ?MaxStagecount)).
} WHERE {
    {?Map gps:description (?From ?Transition ?To ?Action ?Duration_n ?Cost_n ?Belief_n ?Comfort_n)} <= ?Where.
    ?From log:call true.
    ?Where log:call true.
    ?Map gps:description (?From ?Transition ?To ?Action ?Duration_n ?Cost_n ?Belief_n ?Comfort_n).
    (?Maps_s (?Map)) list:append ?Maps_t.
    ?Maps_t gps:stagecount ?Stagecount.
    ?Stagecount math:notGreaterThan ?MaxStagecount.
    (?Duration_s ?Duration_n) math:sum ?Duration_t.
    ?Duration_t math:notGreaterThan ?MaxDuration.
    (?Cost_s ?Cost_n) math:sum ?Cost_t.
    ?Cost_t math:notGreaterThan ?MaxCost.
    (?Belief_s ?Belief_n) math:product ?Belief_t.
    ?Belief_t math:notLessThan ?MinBelief.
    (?Comfort_s ?Comfort_n) math:product ?Comfort_t.
    ?Comfort_t math:notLessThan ?MinComfort.
    (?Path_s (?Action)) list:append ?Path_t.
    ?From log:becomes ?To.
    {?SCOPE gps:findpaths (?Maps_t ?Goal ?Path_t ?Duration_t ?Cost_t ?Belief_t ?Comfort_t ?Path ?Duration ?Cost ?Belief ?Comfort (?MaxDuration ?MaxCost ?MinBelief ?MinComfort ?MaxStagecount))} log:callWithCleanup {?To log:becomes ?From}.
} ;

# counting the number of stages (a stage is a sequence of steps in the same map)
CONSTRUCT {
    () gps:stagecount 1.
} WHERE {
} ;

CONSTRUCT {
    ?A gps:stagecount ?B.
} WHERE {
    ?A list:firstRest (?C ?D).
    ?D list:firstRest (?E ?F).
    ?C log:notEqualTo ?E.
    true log:callWithCut true.
    ?D gps:stagecount ?G.
    (?G 1) math:sum ?B.
} ;

CONSTRUCT {
    ?A gps:stagecount ?B.
} WHERE {
    ?A list:firstRest (?C ?D).
    ?D gps:stagecount ?B.
}
