module Data.PseudoBoolean.Parsec
(
opbParser
, parseOPBString
, parseOPBByteString
, parseOPBFile
, wboParser
, parseWBOString
, parseWBOByteString
, parseWBOFile
) where
import Prelude hiding (sum)
import Control.Applicative ((<*))
import Control.Monad
import Data.ByteString.Lazy (ByteString)
import Data.Maybe
import Text.Parsec
import qualified Text.Parsec.ByteString.Lazy as ParsecBS
import Data.PseudoBoolean.Types
import Data.PseudoBoolean.Internal.TextUtil
opbParser :: Stream s m Char => ParsecT s u m Formula
opbParser = formula
wboParser :: Stream s m Char => ParsecT s u m SoftFormula
wboParser = softformula
formula :: Stream s m Char => ParsecT s u m 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 :: Stream s m Char => ParsecT s u m (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 :: Stream s m Char => ParsecT s u m ()
sequence_of_comments = skipMany comment
comment :: Stream s m Char => ParsecT s u m ()
comment = do
_ <- char '*'
_ <- manyTill anyChar eol
return ()
sequence_of_comments_or_constraints :: Stream s m Char => ParsecT s u m [Constraint]
sequence_of_comments_or_constraints = do
xs <- many1 comment_or_constraint
return $ catMaybes xs
comment_or_constraint :: Stream s m Char => ParsecT s u m (Maybe Constraint)
comment_or_constraint =
(comment >> return Nothing) <|> (liftM Just constraint)
objective :: Stream s m Char => ParsecT s u m Sum
objective = do
_ <- string "min:"
zeroOrMoreSpace
obj <- sum
_ <- char ';'
eol
return obj
constraint :: Stream s m Char => ParsecT s u m Constraint
constraint = do
lhs <- sum
op <- relational_operator
zeroOrMoreSpace
rhs <- integer
zeroOrMoreSpace
semi
return (lhs, op, rhs)
sum :: Stream s m Char => ParsecT s u m Sum
sum = many1 weightedterm
weightedterm :: Stream s m Char => ParsecT s u m WeightedTerm
weightedterm = do
w <- integer
oneOrMoreSpace
t <- term
oneOrMoreSpace
return (w,t)
integer :: Stream s m Char => ParsecT s u m Integer
integer = msum
[ unsigned_integer
, char '+' >> unsigned_integer
, char '-' >> liftM negate unsigned_integer
]
unsigned_integer :: Stream s m Char => ParsecT s u m Integer
unsigned_integer = do
ds <- many1 digit
return $! readUnsignedInteger ds
relational_operator :: Stream s m Char => ParsecT s u m Op
relational_operator = (string ">=" >> return Ge) <|> (string "=" >> return Eq)
variablename :: Stream s m Char => ParsecT s u m Var
variablename = do
_ <- char 'x'
i <- unsigned_integer
return $! fromIntegral i
oneOrMoreSpace :: Stream s m Char => ParsecT s u m ()
oneOrMoreSpace = skipMany1 (char ' ')
zeroOrMoreSpace :: Stream s m Char => ParsecT s u m ()
zeroOrMoreSpace = skipMany (char ' ')
eol :: Stream s m Char => ParsecT s u m ()
eol = char '\n' >> return ()
semi :: Stream s m Char => ParsecT s u m ()
semi = char ';' >> eol
term :: Stream s m Char => ParsecT s u m Term
term = oneOrMoreLiterals
oneOrMoreLiterals :: Stream s m Char => ParsecT s u m [Lit]
oneOrMoreLiterals = do
l <- literal
mplus (try $ oneOrMoreSpace >> liftM (l:) (oneOrMoreLiterals)) (return [l])
literal :: Stream s m Char => ParsecT s u m Lit
literal = variablename <|> (char '~' >> liftM negate variablename)
parseOPBString :: SourceName -> String -> Either ParseError Formula
parseOPBString = parse (formula <* eof)
parseOPBByteString :: SourceName -> ByteString -> Either ParseError Formula
parseOPBByteString = parse (formula <* eof)
parseOPBFile :: FilePath -> IO (Either ParseError Formula)
parseOPBFile = ParsecBS.parseFromFile (formula <* eof)
softformula :: Stream s m Char => ParsecT s u m 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 :: Stream s m Char => ParsecT s u m (Maybe Integer)
softheader = do
_ <- string "soft:"
zeroOrMoreSpace
top <- optionMaybe unsigned_integer
zeroOrMoreSpace
semi
return top
wbo_sequence_of_comments_or_constraints :: Stream s m Char => ParsecT s u m [SoftConstraint]
wbo_sequence_of_comments_or_constraints = do
xs <- many1 wbo_comment_or_constraint
return $ catMaybes xs
wbo_comment_or_constraint :: Stream s m Char => ParsecT s u m (Maybe SoftConstraint)
wbo_comment_or_constraint = (comment >> return Nothing) <|> m
where
m = liftM Just $ (constraint >>= \c -> return (Nothing, c)) <|> softconstraint
softconstraint :: Stream s m Char => ParsecT s u m SoftConstraint
softconstraint = do
_ <- char '['
zeroOrMoreSpace
cost <- unsigned_integer
zeroOrMoreSpace
_ <- char ']'
zeroOrMoreSpace
c <- constraint
return (Just cost, c)
parseWBOString :: SourceName -> String -> Either ParseError SoftFormula
parseWBOString = parse (softformula <* eof)
parseWBOByteString :: SourceName -> ByteString -> Either ParseError SoftFormula
parseWBOByteString = parse (softformula <* eof)
parseWBOFile :: FilePath -> IO (Either ParseError SoftFormula)
parseWBOFile = ParsecBS.parseFromFile (softformula <* eof)