module Data.Allen.Types ( Interval(..)
, Allen
, IntervalID
, IntervalGraph
, Relation(..)
, RelationBits
, allRelations
, allRelationBits
, toBits
, fromBits
, relationUnion
, relationIntersection
, relationToChar
, fromID
) where
import Control.Monad.State
import Data.Bits
import Data.List (intercalate, foldl')
import Data.Word (Word16)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
type IntervalID = Int
type IntervalGraph = Map IntervalID Interval
data Interval = Interval { Interval -> Int
intervalID :: Int
, Interval -> Map Int RelationBits
intervalRelations :: Map IntervalID RelationBits
}
instance Show Interval where
show :: Interval -> String
show (Interval Int
iD Map Int RelationBits
rels) = String
"Interval " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
iD forall a. Semigroup a => a -> a -> a
<> String
" (" forall a. Semigroup a => a -> a -> a
<> String
rels' forall a. Semigroup a => a -> a -> a
<> String
")"
where rels' :: String
rels' = forall a. [a] -> [[a]] -> [a]
intercalate String
", " forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a}. Show a => (a, RelationBits) -> String
showRel forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [(k, a)]
Map.toList Map Int RelationBits
rels
showRel :: (a, RelationBits) -> String
showRel (a
n, RelationBits
r) | RelationBits
r forall a. Eq a => a -> a -> Bool
== RelationBits
allRelationBits = String
"??? " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show a
n
| Bool
otherwise = forall a b. (a -> b) -> [a] -> [b]
map Relation -> Char
relationToChar (RelationBits -> [Relation]
fromBits RelationBits
r) forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show a
n
fromID :: IntervalID -> Allen Interval
fromID :: Int -> Allen Interval
fromID Int
n = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (forall k a. Ord k => Map k a -> k -> a
Map.! Int
n)
type Allen = State IntervalGraph
data Relation = Precedes
| Meets
| Overlaps
| FinishedBy
| Contains
| Starts
| Equals
| StartedBy
| During
| Finishes
| OverlappedBy
| MetBy
| PrecededBy
deriving (Relation -> Relation -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Relation -> Relation -> Bool
$c/= :: Relation -> Relation -> Bool
== :: Relation -> Relation -> Bool
$c== :: Relation -> Relation -> Bool
Eq, Int -> Relation -> ShowS
[Relation] -> ShowS
Relation -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Relation] -> ShowS
$cshowList :: [Relation] -> ShowS
show :: Relation -> String
$cshow :: Relation -> String
showsPrec :: Int -> Relation -> ShowS
$cshowsPrec :: Int -> Relation -> ShowS
Show, Int -> Relation
Relation -> Int
Relation -> [Relation]
Relation -> Relation
Relation -> Relation -> [Relation]
Relation -> Relation -> Relation -> [Relation]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Relation -> Relation -> Relation -> [Relation]
$cenumFromThenTo :: Relation -> Relation -> Relation -> [Relation]
enumFromTo :: Relation -> Relation -> [Relation]
$cenumFromTo :: Relation -> Relation -> [Relation]
enumFromThen :: Relation -> Relation -> [Relation]
$cenumFromThen :: Relation -> Relation -> [Relation]
enumFrom :: Relation -> [Relation]
$cenumFrom :: Relation -> [Relation]
fromEnum :: Relation -> Int
$cfromEnum :: Relation -> Int
toEnum :: Int -> Relation
$ctoEnum :: Int -> Relation
pred :: Relation -> Relation
$cpred :: Relation -> Relation
succ :: Relation -> Relation
$csucc :: Relation -> Relation
Enum, Relation
forall a. a -> a -> Bounded a
maxBound :: Relation
$cmaxBound :: Relation
minBound :: Relation
$cminBound :: Relation
Bounded)
relationToChar :: Relation -> Char
relationToChar :: Relation -> Char
relationToChar Relation
r = case Relation
r of
Relation
Precedes -> Char
'p'
Relation
Meets -> Char
'm'
Relation
Overlaps -> Char
'o'
Relation
FinishedBy -> Char
'F'
Relation
Contains -> Char
'D'
Relation
Starts -> Char
's'
Relation
Equals -> Char
'e'
Relation
StartedBy -> Char
'S'
Relation
During -> Char
'd'
Relation
Finishes -> Char
'f'
Relation
OverlappedBy -> Char
'O'
Relation
MetBy -> Char
'M'
Relation
PrecededBy -> Char
'P'
type RelationBits = Word16
allRelations :: [Relation]
allRelations :: [Relation]
allRelations = [forall a. Bounded a => a
minBound..]
allRelationBits :: RelationBits
allRelationBits :: RelationBits
allRelationBits = [RelationBits] -> RelationBits
relationUnion forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Relation -> RelationBits
toBits [Relation]
allRelations
toBits :: Relation -> RelationBits
toBits :: Relation -> RelationBits
toBits = forall a. Bits a => Int -> a
bit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
fromBits :: RelationBits -> [Relation]
fromBits :: RelationBits -> [Relation]
fromBits RelationBits
bits = [Relation
x | Relation
x <- [Relation]
allRelations, RelationBits
bits forall a. Bits a => a -> a -> a
.&. Relation -> RelationBits
toBits Relation
x forall a. Eq a => a -> a -> Bool
/= RelationBits
0]
relationUnion :: [RelationBits] -> RelationBits
relationUnion :: [RelationBits] -> RelationBits
relationUnion = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Bits a => a -> a -> a
(.|.) RelationBits
0
relationIntersection :: [RelationBits] -> RelationBits
relationIntersection :: [RelationBits] -> RelationBits
relationIntersection = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Bits a => a -> a -> a
(.&.) RelationBits
0