---------------------------------------------------------------------------------- See end of this file for licence information.---------------------------------------------------------------------------------- |-- Module : Monad-- Copyright : (c) 2003, Graham Klyne, 2009 Vasili I Galchin,-- 2011, 2012 Douglas Burke-- License : GPL V2---- Maintainer : Douglas Burke-- Stability : experimental-- Portability : H98---- Composed state and IO monad for Swish----------------------------------------------------------------------------------moduleSwish.Monad(SwishStateIO,SwishState(..),SwishStatus(..),SwishFormat(..),NamedGraphMap-- * Create and modify the Swish state,emptyState,setFormat,setBase,setGraph,modGraphs,findGraph,findFormula,modRules,findRule,modRulesets,findRuleset,findOpenVarModify,findDatatype,setInfo,resetInfo,setError,resetError,setStatus-- * Error handling,swishError,reportLine)whereimportSwish.Namespace(ScopedName,getScopeNamespace)importSwish.QName(QName)importSwish.Ruleset(getMaybeContextAxiom,getMaybeContextRule)importSwish.Rule(Formula(..))importSwish.RDF.Datatype(RDFDatatype)importSwish.RDF.Graph(RDFGraph,emptyRDFGraph)importSwish.RDF.Ruleset(RDFFormula,RDFRule,RDFRuleMap,RDFRuleset,RDFRulesetMap)importSwish.RDF.VarBinding(RDFOpenVarBindingModify)importSwish.RDF.BuiltIn(findRDFOpenVarBindingModifier,findRDFDatatype,rdfRulesetMap)importControl.Monad.Trans(MonadTrans(..))importControl.Monad.State(StateT(..),modify)importData.List(nub)importSystem.IO(hPutStrLn,stderr)importqualifiedData.MapasM{-|
The supported input and output formats.
-}dataSwishFormat=Turtle-- ^ Turtle format|N3-- ^ N3 format|NT-- ^ NTriples formatderivingEqinstanceShowSwishFormatwhereshowN3="N3"showNT="Ntriples"showTurtle="Turtle"-- show RDF = "RDF/XML"-- | The State for a Swish \"program\".dataSwishState=SwishState{format::SwishFormat-- ^ format to use for I/O,base::MaybeQName-- ^ base to use rather than file name,graph::RDFGraph-- ^ current graph,graphs::NamedGraphMap-- ^ script processor named graphs,rules::RDFRuleMap-- ^ script processor named rules,rulesets::RDFRulesetMap-- ^ script processor rulesets,infomsg::MaybeString-- ^ information message, or Nothing,errormsg::MaybeString-- ^ error message, or Nothing,exitcode::SwishStatus-- ^ current status}-- | Status of the processor--dataSwishStatus=SwishSuccess-- ^ successful run|SwishGraphCompareError-- ^ graphs do not compare|SwishDataInputError-- ^ input data problem (ie format/syntax)|SwishDataAccessError-- ^ data access error|SwishArgumentError-- ^ command-line argument error|SwishExecutionError-- ^ error executing a Swish scriptderiving(Eq,Enum)instanceShowSwishStatuswhereshowSwishSuccess="Success."showSwishGraphCompareError="The graphs do not compare as equal."showSwishDataInputError="There was a format or syntax error in the input data."showSwishDataAccessError="There was a problem accessing data."showSwishArgumentError="Argument error: use -h or -? for help."showSwishExecutionError="There was a problem executing a Swish script."-- | The state monad used in executing Swish programs.typeSwishStateIOa=StateTSwishStateIOa-- | The default state for Swish: no loaded graphs or rules, and format-- set to 'N3'.emptyState::SwishStateemptyState=SwishState{format=N3,base=Nothing,graph=emptyRDFGraph,graphs=M.empty,rules=M.empty,rulesets=rdfRulesetMap,infomsg=Nothing,errormsg=Nothing,exitcode=SwishSuccess}-- | Change the format.setFormat::SwishFormat->SwishState->SwishStatesetFormatfmstate=state{format=fm}-- | Change (or remove) the base URI.setBase::MaybeQName->SwishState->SwishStatesetBasebsstate=state{base=bs}-- | Change the current graph.setGraph::RDFGraph->SwishState->SwishStatesetGraphgrstate=state{graph=gr}-- | Modify the named graphs.modGraphs::(NamedGraphMap->NamedGraphMap)->SwishState->SwishStatemodGraphsgrmodstate=state{graphs=grmod(graphsstate)}-- | Find a named graph.findGraph::ScopedName->SwishState->Maybe[RDFGraph]findGraphnamstate=M.lookupnam(graphsstate)-- | Find a formula. The search is first made in the named graphs-- and then, if not found, the rulesets.findFormula::ScopedName->SwishState->MaybeRDFFormulafindFormulanamstate=casefindGraphnamstateofNothing->getMaybeContextAxiomnam(nub$M.elems$rulesetsstate)Just[]->Just$FormulanamemptyRDFGraphJustgrs->Just$Formulanam(headgrs)-- | Modify the named rules.modRules::(RDFRuleMap->RDFRuleMap)->SwishState->SwishStatemodRulesrlmodstate=state{rules=rlmod(rulesstate)}-- | Find a named rule.findRule::ScopedName->SwishState->MaybeRDFRulefindRulenamstate=caseM.lookupnam(rulesstate)ofNothing->getMaybeContextRulenam$nub$M.elems$rulesetsstatejustlr->justlr-- | Modify the rule sets.modRulesets::(RDFRulesetMap->RDFRulesetMap)->SwishState->SwishStatemodRulesetsrsmodstate=state{rulesets=rsmod(rulesetsstate)}-- | Find a rule set.findRuleset::ScopedName->SwishState->MaybeRDFRulesetfindRulesetnamstate=M.lookup(getScopeNamespacenam)(rulesetsstate)-- | Find a modify rule.findOpenVarModify::ScopedName->SwishState->MaybeRDFOpenVarBindingModifyfindOpenVarModifynam_=findRDFOpenVarBindingModifiernam-- | Find a data type declaration.findDatatype::ScopedName->SwishState->MaybeRDFDatatypefindDatatypenam_=findRDFDatatypenam-- | Set the information message.setInfo::String->SwishState->SwishStatesetInfomsgstate=state{infomsg=Justmsg}-- | Clear the information message.resetInfo::SwishState->SwishStateresetInfostate=state{infomsg=Nothing}-- | Set the error message.setError::String->SwishState->SwishStatesetErrormsgstate=state{errormsg=Justmsg}-- | Clear the error message.resetError::SwishState->SwishStateresetErrorstate=state{errormsg=Nothing}-- | Set the status.setStatus::SwishStatus->SwishState->SwishStatesetStatusecstate=state{exitcode=ec}{-
setVerbose :: Bool -> SwishState -> SwishState
setVerbose f state = state { banner = f }
-}{-
-- | The graphs dictionary contains named graphs and/or lists
-- of graphs that are created and used by script statements.
data NamedGraph = NamedGraph
{ ngName :: ScopedName
, ngGraph :: [RDFGraph]
}
-}-- | A LookupMap for the graphs dictionary.typeNamedGraphMap=M.MapScopedName[RDFGraph]-- | Report error and set exit status codeswishError::String->SwishStatus->SwishStateIO()swishErrormsgsts=domapM_reportLine[msg,showsts++"\n"]modify$setStatussts-- | Output the text to the standard error stream (a new line is-- added to the output).reportLine::String->SwishStateIO()reportLine=lift.hPutStrLnstderr------------------------------------------------------------------------------------ Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,-- 2011, 2012 Douglas Burke -- All rights reserved.---- This file is part of Swish.---- Swish is free software; you can redistribute it and/or modify-- it under the terms of the GNU General Public License as published by-- the Free Software Foundation; either version 2 of the License, or-- (at your option) any later version.---- Swish is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU General Public License for more details.---- You should have received a copy of the GNU General Public License-- along with Swish; if not, write to:-- The Free Software Foundation, Inc.,-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA----------------------------------------------------------------------------------