Alloy Modeling Language and Concurrent Database Query Adventures

This article describes a small example of how the use of the Alloy modeling language can help in software development.













About the quality of software and tools



Typeable , . :







  1. Haskell
  2. -
  3. , QA

    ( Octopod QA)
  4. pre-production


, . , .







. , , 30 . CI, 30 . , 30 , CI .







, : QA- , - . , , !







, , ? , , !







Alloy



Alloy. Alloy – , , .







, Alloy . Alloy , . , .







. :







newAuthCode
  :: (MonadWhatever m)
  => DB.Client
  -> DB.SessionId
  -> m DB.AuthorizationCode
newAuthCode clid sid = do
  let codeData = mkAuthCodeFor clid sid
  void $ DB.deleteAllCodes clid sid
  void $ DB.insertAuthCode codeData
  return code
      
      





HTTP- , , . , . Β« Β» (uniqueness constraint violation).







?









, , Alloy. , . newAuthCode



Alloy. , , , .







, , .







, , . , . Alloy, :







open util/time  //    Time

sig Operation       //    ...
  { delete : Time   // ...   -  
  , insert : Time   // ...    - 
  }
  { lt[delete,insert]  //    
    lt[first,delete]   //      
                       //     
  }
  run {some Operation} for 4 //    
                             //  <= 4 
      
      





. , Operation



, .







, alloy . 'execute' 'show', :













Alloy , 'next'.







, ( 'next’ 'Table'):







β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
β”‚this/Operationβ”‚deleteβ”‚insertβ”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚Operation⁰    β”‚TimeΒΉ β”‚TimeΒ³ β”‚ ← Operation⁰    TimeΒΉ 
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€      TimeΒ³
β”‚OperationΒΉ    β”‚TimeΒ² β”‚TimeΒ³ β”‚ ← OperationΒΉ    TimeΒ² 
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜       TimeΒ³
                         ↑
                        !
      
      





, , , Alloy , !







, : , , . , , postgresql , .







!







!



, .







code <- run $ do
  handleJust constraintViolation
    (launchPG $ selectCodeForSession clid scope sid
    (launchPG . pgWithTransaction $ newAuthCode clid scope sid)
      
      





, , , , . , select



, .







?



Alloy , :







open util/time //  Time

sig Token {} //    Token

one sig DBState //    
 {userToken : Token lone -> Time}
    //          
    // (..       )

sig Operation {
   delete : Time
 , insert : Time
 , select : Time //      select
}
{
  lt[first,delete]   //       
                     //   

  lt[delete,insert]  //    delete

  lte[insert,select] // select      insert'

  no userToken.(insert.prev) //    (.. 
  => insert = select         //     ),
                             //       
                             //   (..   
                             // 'INSERT RETURNING').
                             //     
                             // ,  select   
}
      
      





. DBState



, , select, , . , , , .







, . , :







fact Trace {                           //  Trace   
 all t : Time - first | {              //   ,  :

   some delete.t => no userToken.t       //   ,  

   some insert.t => some userToken.t     //   ,   

   no delete.t and no insert.t           //     ,  ,
    => userToken.t = userToken.(t.prev)  //   
  }
}
      
      





, .







, , . Alloy . , , , select.







Alloy .







assert selectIsGood {         // ,    
 all s : Operation.select |   // ,   select,
  some userToken.s            //    
}

check selectIsGood for 6 // ,  selectIsGood  
      
      





, :







β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚DBState β”‚userToken   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€
β”‚DBState⁰│TokenΒ²β”‚TimeΒ³β”‚
β”‚        β”‚      β”œβ”€β”€β”€β”€β”€β”€  ← TokenΒ²      TimeΒ³  Time⁡
β”‚        β”‚      β”‚Time⁡│
β”‚        β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚        β”‚TokenΒ³β”‚TimeΒ²β”‚  ← TokenΒ³     TimeΒ².
β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
                   ↑
                     
                  Time², Time³  Time⁡
                 ,   
                 Time⁴  !

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
β”‚Operation     β”‚deleteβ”‚insertβ”‚selectβ”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚Operation⁰    β”‚ TIME⁴│ Time⁡│ Time⁡│
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚OperationΒΉ    β”‚ TimeΒΉβ”‚ TimeΒ³β”‚ TIME⁴│   ←     Time⁴ 
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€     select    OperationΒΉ!
β”‚OperationΒ²    β”‚ TimeΒΉβ”‚ TimeΒ²β”‚ TimeΒ²β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
                  ↑       ↑      ↑
                  , 
                  
      
      





. , . -, . , , , . , , select .







, , , , , !







.









, Alloy. , , . , .







, , , , , . , Alloy, «».







, . ? , , , , , .







Alloy?



, , , Alloy:












P.S. , .








All Articles