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 " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
iD String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" (" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
rels' String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
where rels' :: String
rels' = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ ((Int, RelationBits) -> String)
-> [(Int, RelationBits)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int, RelationBits) -> String
forall {a}. Show a => (a, RelationBits) -> String
showRel ([(Int, RelationBits)] -> [String])
-> [(Int, RelationBits)] -> [String]
forall a b. (a -> b) -> a -> b
$ Map Int RelationBits -> [(Int, RelationBits)]
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 RelationBits -> RelationBits -> Bool
forall a. Eq a => a -> a -> Bool
== RelationBits
allRelationBits = String
"??? " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
n
| Bool
otherwise = (Relation -> Char) -> [Relation] -> String
forall a b. (a -> b) -> [a] -> [b]
map Relation -> Char
relationToChar (RelationBits -> [Relation]
fromBits RelationBits
r) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
n
fromID :: IntervalID -> Allen Interval
fromID :: Int -> Allen Interval
fromID Int
n = (Map Int Interval -> Interval) -> Allen Interval
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Map Int Interval -> Int -> Interval
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
(Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool) -> Eq Relation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Relation -> Relation -> Bool
== :: Relation -> Relation -> Bool
$c/= :: Relation -> Relation -> Bool
/= :: Relation -> Relation -> Bool
Eq, Int -> Relation -> ShowS
[Relation] -> ShowS
Relation -> String
(Int -> Relation -> ShowS)
-> (Relation -> String) -> ([Relation] -> ShowS) -> Show Relation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Relation -> ShowS
showsPrec :: Int -> Relation -> ShowS
$cshow :: Relation -> String
show :: Relation -> String
$cshowList :: [Relation] -> ShowS
showList :: [Relation] -> ShowS
Show, Int -> Relation
Relation -> Int
Relation -> [Relation]
Relation -> Relation
Relation -> Relation -> [Relation]
Relation -> Relation -> Relation -> [Relation]
(Relation -> Relation)
-> (Relation -> Relation)
-> (Int -> Relation)
-> (Relation -> Int)
-> (Relation -> [Relation])
-> (Relation -> Relation -> [Relation])
-> (Relation -> Relation -> [Relation])
-> (Relation -> Relation -> Relation -> [Relation])
-> Enum 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
$csucc :: Relation -> Relation
succ :: Relation -> Relation
$cpred :: Relation -> Relation
pred :: Relation -> Relation
$ctoEnum :: Int -> Relation
toEnum :: Int -> Relation
$cfromEnum :: Relation -> Int
fromEnum :: Relation -> Int
$cenumFrom :: Relation -> [Relation]
enumFrom :: Relation -> [Relation]
$cenumFromThen :: Relation -> Relation -> [Relation]
enumFromThen :: Relation -> Relation -> [Relation]
$cenumFromTo :: Relation -> Relation -> [Relation]
enumFromTo :: Relation -> Relation -> [Relation]
$cenumFromThenTo :: Relation -> Relation -> Relation -> [Relation]
enumFromThenTo :: Relation -> Relation -> Relation -> [Relation]
Enum, Relation
Relation -> Relation -> Bounded Relation
forall a. a -> a -> Bounded a
$cminBound :: Relation
minBound :: Relation
$cmaxBound :: Relation
maxBound :: 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 = [Relation
forall a. Bounded a => a
minBound..]
allRelationBits :: RelationBits
allRelationBits :: RelationBits
allRelationBits = [RelationBits] -> RelationBits
relationUnion ([RelationBits] -> RelationBits) -> [RelationBits] -> RelationBits
forall a b. (a -> b) -> a -> b
$ (Relation -> RelationBits) -> [Relation] -> [RelationBits]
forall a b. (a -> b) -> [a] -> [b]
map Relation -> RelationBits
toBits [Relation]
allRelations
toBits :: Relation -> RelationBits
toBits :: Relation -> RelationBits
toBits = Int -> RelationBits
forall a. Bits a => Int -> a
bit (Int -> RelationBits)
-> (Relation -> Int) -> Relation -> RelationBits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Relation -> Int
forall a. Enum a => a -> Int
fromEnum
fromBits :: RelationBits -> [Relation]
fromBits :: RelationBits -> [Relation]
fromBits RelationBits
bits = [Relation
x | Relation
x <- [Relation]
allRelations, RelationBits
bits RelationBits -> RelationBits -> RelationBits
forall a. Bits a => a -> a -> a
.&. Relation -> RelationBits
toBits Relation
x RelationBits -> RelationBits -> Bool
forall a. Eq a => a -> a -> Bool
/= RelationBits
0]
relationUnion :: [RelationBits] -> RelationBits
relationUnion :: [RelationBits] -> RelationBits
relationUnion = (RelationBits -> RelationBits -> RelationBits)
-> RelationBits -> [RelationBits] -> RelationBits
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' RelationBits -> RelationBits -> RelationBits
forall a. Bits a => a -> a -> a
(.|.) RelationBits
0
relationIntersection :: [RelationBits] -> RelationBits
relationIntersection :: [RelationBits] -> RelationBits
relationIntersection = (RelationBits -> RelationBits -> RelationBits)
-> RelationBits -> [RelationBits] -> RelationBits
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' RelationBits -> RelationBits -> RelationBits
forall a. Bits a => a -> a -> a
(.&.) RelationBits
0