module Data.Allen.Interval ( interval
, intervalCount
, fromID
, assume
, assumeSet
, assumeBits
, setRelation
, getConstraints
) where
import Control.Monad
import Control.Monad.State
import Data.Allen.Types
import Data.Allen.Relation
import Data.Bits
import qualified Data.Map.Strict as Map
interval :: Allen IntervalID
interval :: Allen IntervalID
interval = do
IntervalGraph
intervals <- forall s (m :: * -> *). MonadState s m => m s
get
let iD :: IntervalID
iD = forall k a. Map k a -> IntervalID
Map.size IntervalGraph
intervals
iRelations :: Map IntervalID RelationBits
iRelations = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(IntervalID
x, RelationBits
allRelationBits) | IntervalID
x <- [IntervalID
0 .. IntervalID
iD forall a. Num a => a -> a -> a
- IntervalID
1]]
intervals' :: IntervalGraph
intervals' = forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (\Interval
x -> Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
x RelationBits
allRelationBits IntervalID
iD) IntervalGraph
intervals
i :: Interval
i = IntervalID -> Map IntervalID RelationBits -> Interval
Interval IntervalID
iD Map IntervalID RelationBits
iRelations
forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
iD Interval
i IntervalGraph
intervals'
forall (m :: * -> *) a. Monad m => a -> m a
return IntervalID
iD
intervalCount :: Allen Int
intervalCount :: Allen IntervalID
intervalCount = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets forall k a. Map k a -> IntervalID
Map.size
setRelation :: Interval -> RelationBits -> IntervalID -> Interval
setRelation :: Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
i1 RelationBits
r IntervalID
i2 = Interval
i1 { intervalRelations :: Map IntervalID RelationBits
intervalRelations = Map IntervalID RelationBits
relations }
where relations :: Map IntervalID RelationBits
relations = forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
i2 RelationBits
r forall a b. (a -> b) -> a -> b
$ Interval -> Map IntervalID RelationBits
intervalRelations Interval
i1
assume :: IntervalID -> Relation -> IntervalID -> Allen ()
assume :: IntervalID
-> Relation -> IntervalID -> StateT IntervalGraph Identity ()
assume IntervalID
id1 Relation
r = IntervalID
-> RelationBits -> IntervalID -> StateT IntervalGraph Identity ()
assumeBits IntervalID
id1 (Relation -> RelationBits
toBits Relation
r)
assumeSet :: IntervalID -> [Relation] -> IntervalID -> Allen ()
assumeSet :: IntervalID
-> [Relation] -> IntervalID -> StateT IntervalGraph Identity ()
assumeSet IntervalID
id1 = IntervalID
-> RelationBits -> IntervalID -> StateT IntervalGraph Identity ()
assumeBits IntervalID
id1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [RelationBits] -> RelationBits
relationUnion forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Relation -> RelationBits
toBits
assumeBits :: IntervalID -> RelationBits -> IntervalID -> Allen ()
assumeBits :: IntervalID
-> RelationBits -> IntervalID -> StateT IntervalGraph Identity ()
assumeBits IntervalID
id1 RelationBits
r IntervalID
id2 = do
Interval
i1 <- IntervalID -> Allen Interval
fromID IntervalID
id1
Interval
i2 <- IntervalID -> Allen Interval
fromID IntervalID
id2
let i1' :: Interval
i1' = Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
i1 RelationBits
r IntervalID
id2
i2' :: Interval
i2' = Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
i2 (RelationBits -> RelationBits
converse RelationBits
r) IntervalID
id1
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
id1 Interval
i1' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
id2 Interval
i2'
(IntervalID, IntervalID) -> StateT IntervalGraph Identity ()
propogate (IntervalID
id1, IntervalID
id2)
propogate :: (IntervalID, IntervalID) -> Allen ()
propogate :: (IntervalID, IntervalID) -> StateT IntervalGraph Identity ()
propogate (IntervalID, IntervalID)
r = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate' [(IntervalID, IntervalID)
r]
propogate' :: StateT [(IntervalID, IntervalID)] Allen ()
propogate' :: StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate' = do
[(IntervalID, IntervalID)]
toDo <- forall s (m :: * -> *). MonadState s m => m s
get
case [(IntervalID, IntervalID)]
toDo of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
((IntervalID
i, IntervalID
j):[(IntervalID, IntervalID)]
_) -> do
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a. [a] -> [a]
tail
(IntervalID, IntervalID)
-> StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate'' (IntervalID
i, IntervalID
j)
(IntervalID, IntervalID)
-> StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate'' (IntervalID
j, IntervalID
i)
StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate'
propogate'' :: (IntervalID, IntervalID) -> StateT [(IntervalID, IntervalID)] Allen ()
propogate'' :: (IntervalID, IntervalID)
-> StateT
[(IntervalID, IntervalID)] (StateT IntervalGraph Identity) ()
propogate'' (IntervalID
i, IntervalID
j) = do
IntervalID
count <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Allen IntervalID
intervalCount
let range :: [IntervalID]
range = [IntervalID
k | IntervalID
k <- [IntervalID
0 .. IntervalID
count forall a. Num a => a -> a -> a
- IntervalID
1], IntervalID
k forall a. Eq a => a -> a -> Bool
/= IntervalID
i, IntervalID
k forall a. Eq a => a -> a -> Bool
/= IntervalID
j]
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [IntervalID]
range forall a b. (a -> b) -> a -> b
$ \IntervalID
k -> do
RelationBits
constraints <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ RelationBits -> RelationBits -> RelationBits
compose forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
k IntervalID
i forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
i IntervalID
j
RelationBits
nkj <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
k IntervalID
j
let rkj :: RelationBits
rkj = RelationBits
nkj forall a. Bits a => a -> a -> a
.&. RelationBits
constraints
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RelationBits
rkj forall a. Bits a => a -> a -> a
.|. RelationBits
nkj forall a. Eq a => a -> a -> Bool
== RelationBits
nkj Bool -> Bool -> Bool
&& RelationBits
rkj forall a. Ord a => a -> a -> Bool
< RelationBits
nkj) forall a b. (a -> b) -> a -> b
$ do
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((IntervalID
k, IntervalID
j)forall a. a -> [a] -> [a]
:)
Interval
intervalK <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ IntervalID -> Allen Interval
fromID IntervalID
k
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
k (Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
intervalK RelationBits
rkj IntervalID
j)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [IntervalID]
range forall a b. (a -> b) -> a -> b
$ \IntervalID
k -> do
RelationBits
constraints <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ RelationBits -> RelationBits -> RelationBits
compose forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
i IntervalID
j forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
j IntervalID
k
RelationBits
nik <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
i IntervalID
k
let rik :: RelationBits
rik = RelationBits
nik forall a. Bits a => a -> a -> a
.&. RelationBits
constraints
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RelationBits
rik forall a. Bits a => a -> a -> a
.|. RelationBits
nik forall a. Eq a => a -> a -> Bool
== RelationBits
nik Bool -> Bool -> Bool
&& RelationBits
rik forall a. Ord a => a -> a -> Bool
< RelationBits
nik) forall a b. (a -> b) -> a -> b
$ do
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((IntervalID
i, IntervalID
k)forall a. a -> [a] -> [a]
:)
Interval
intervalI <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ IntervalID -> Allen Interval
fromID IntervalID
i
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert IntervalID
i (Interval -> RelationBits -> IntervalID -> Interval
setRelation Interval
intervalI RelationBits
rik IntervalID
k)
getConstraints :: IntervalID -> IntervalID -> Allen RelationBits
getConstraints :: IntervalID
-> IntervalID -> StateT IntervalGraph Identity RelationBits
getConstraints IntervalID
id1 IntervalID
id2 = forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault RelationBits
0 IntervalID
id2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval -> Map IntervalID RelationBits
intervalRelations forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IntervalID -> Allen Interval
fromID IntervalID
id1