module Data.PseudoBoolean.Attoparsec
(
opbParser
, parseOPBByteString
, parseOPBFile
, wboParser
, parseWBOByteString
, parseWBOFile
) where
import Prelude hiding (sum)
import Control.Applicative ((<|>), (<*))
import Control.Monad
import Data.Attoparsec.ByteString.Char8 hiding (isDigit)
import qualified Data.Attoparsec.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as BSLazy
import Data.Char
import Data.Maybe
import Data.PseudoBoolean.Types
import Data.PseudoBoolean.Internal.TextUtil
opbParser :: Parser Formula
opbParser = formula
wboParser :: Parser SoftFormula
wboParser = softformula
formula :: Parser Formula
formula = do
h <- optionMaybe hint
sequence_of_comments
obj <- optionMaybe objective
cs <- sequence_of_comments_or_constraints
return $
Formula
{ pbObjectiveFunction = obj
, pbConstraints = cs
, pbNumVars = fromMaybe (pbComputeNumVars obj cs) (fmap fst h)
, pbNumConstraints = fromMaybe (length cs) (fmap snd h)
}
hint :: Parser (Int,Int)
hint = try $ do
_ <- char '*'
zeroOrMoreSpace
_ <- string "#variable="
zeroOrMoreSpace
nv <- unsigned_integer
oneOrMoreSpace
_ <- string "#constraint="
zeroOrMoreSpace
nc <- unsigned_integer
_ <- manyTill anyChar eol
return (fromIntegral nv, fromIntegral nc)
sequence_of_comments :: Parser ()
sequence_of_comments = skipMany comment
comment :: Parser ()
comment = do
_ <- char '*'
_ <- manyTill anyChar eol
skipMany space
return ()
sequence_of_comments_or_constraints :: Parser [Constraint]
sequence_of_comments_or_constraints = do
xs <- many' comment_or_constraint
return $ catMaybes xs
comment_or_constraint :: Parser (Maybe Constraint)
comment_or_constraint =
(comment >> return Nothing) <|> (liftM Just constraint)
objective :: Parser Sum
objective = do
_ <- string "min:"
zeroOrMoreSpace
obj <- sum
semi
return obj
constraint :: Parser Constraint
constraint = do
lhs <- sum
op <- relational_operator
zeroOrMoreSpace
rhs <- integer
zeroOrMoreSpace
semi
return (lhs, op, rhs)
sum :: Parser Sum
sum = many1 weightedterm
weightedterm :: Parser WeightedTerm
weightedterm = do
w <- integer
oneOrMoreSpace
t <- term
oneOrMoreSpace
return (w,t)
integer :: Parser Integer
integer = msum
[ unsigned_integer
, char '+' >> unsigned_integer
, char '-' >> liftM negate unsigned_integer
]
unsigned_integer :: Parser Integer
unsigned_integer = do
ds <- takeWhile1 isDigit
return $! readUnsignedInteger $ BS.unpack ds
relational_operator :: Parser Op
relational_operator = (string ">=" >> return Ge) <|> (string "=" >> return Eq)
variablename :: Parser Var
variablename = do
_ <- char 'x'
i <- unsigned_integer
return $! fromIntegral i
oneOrMoreSpace :: Parser ()
oneOrMoreSpace = skipMany1 (char ' ')
zeroOrMoreSpace :: Parser ()
zeroOrMoreSpace = skipSpace
eol :: Parser ()
eol = char '\n' >> return ()
semi :: Parser ()
semi = char ';' >> skipSpace
term :: Parser Term
term = oneOrMoreLiterals
oneOrMoreLiterals :: Parser [Lit]
oneOrMoreLiterals = do
l <- literal
mplus (try $ oneOrMoreSpace >> liftM (l:) (oneOrMoreLiterals)) (return [l])
literal :: Parser Lit
literal = variablename <|> (char '~' >> liftM negate variablename)
parseOPBByteString :: BSLazy.ByteString -> Either String Formula
parseOPBByteString s = L.eitherResult $ L.parse (formula <* endOfInput) s
parseOPBFile :: FilePath -> IO (Either String Formula)
parseOPBFile fname = do
s <- BSLazy.readFile fname
return $ parseOPBByteString s
softformula :: Parser SoftFormula
softformula = do
h <- optionMaybe hint
sequence_of_comments
top <- softheader
cs <- wbo_sequence_of_comments_or_constraints
return $
SoftFormula
{ wboTopCost = top
, wboConstraints = cs
, wboNumVars = fromMaybe (wboComputeNumVars cs) (fmap fst h)
, wboNumConstraints = fromMaybe (length cs) (fmap snd h)
}
softheader :: Parser (Maybe Integer)
softheader = do
_ <- string "soft:"
zeroOrMoreSpace
top <- optionMaybe unsigned_integer
zeroOrMoreSpace
semi
return top
wbo_sequence_of_comments_or_constraints :: Parser [SoftConstraint]
wbo_sequence_of_comments_or_constraints = do
xs <- many' wbo_comment_or_constraint
return $ catMaybes xs
wbo_comment_or_constraint :: Parser (Maybe SoftConstraint)
wbo_comment_or_constraint = (comment >> return Nothing) <|> m
where
m = liftM Just $ (constraint >>= \c -> return (Nothing, c)) <|> softconstraint
softconstraint :: Parser SoftConstraint
softconstraint = do
_ <- char '['
zeroOrMoreSpace
cost <- unsigned_integer
zeroOrMoreSpace
_ <- char ']'
zeroOrMoreSpace
c <- constraint
return (Just cost, c)
parseWBOByteString :: BSLazy.ByteString -> Either String SoftFormula
parseWBOByteString s = L.eitherResult $ L.parse (softformula <* endOfInput) s
parseWBOFile :: FilePath -> IO (Either String SoftFormula)
parseWBOFile fname = do
s <- BSLazy.readFile fname
return $ parseWBOByteString s
optionMaybe :: Parser a -> Parser (Maybe a)
optionMaybe p = option Nothing (liftM Just p)