--------------------------------------------------------------------------------
--  See end of this file for licence information.
--------------------------------------------------------------------------------
-- |
--  Module      :  Rules
--  Copyright   :  (c) 2003, Graham Klyne, 2009 Vasili I Galchin, 2011, 2012, 2022 Douglas Burke
--  License     :  GPL V2
--
--  Maintainer  :  Douglas Burke
--  Stability   :  experimental
--  Portability :  H98
--
--  This module collects references and provides access to all of the
--  rulesets, variable binding modifiers and variable binding filters
--  built in to Swish.
--
--------------------------------------------------------------------------------

module Swish.RDF.BuiltIn.Rules
    ( findRDFOpenVarBindingModifier
    , rdfRulesetMap
    , allRulesets, allDatatypeRulesets
    )
where

import Swish.Datatype (typeRules, typeMkModifiers)
import Swish.Namespace (ScopedName)
import Swish.Ruleset (getRulesetNamespace)
import Swish.VarBinding (openVbmName, nullVarBindingModify, makeVarFilterModify, varFilterEQ, varFilterNE)

import Swish.RDF.BuiltIn.Datatypes (allDatatypes)
import Swish.RDF.Ruleset (RDFRuleset, RDFRulesetMap)
import Swish.RDF.ProofContext (rulesetRDF, rulesetRDFS, rulesetRDFD)

import Swish.RDF.VarBinding
    ( RDFOpenVarBindingModify
    , rdfVarBindingUriRef, rdfVarBindingBlank
    , rdfVarBindingLiteral
    , rdfVarBindingUntypedLiteral, rdfVarBindingTypedLiteral
    , rdfVarBindingXMLLiteral, rdfVarBindingDatatyped
    , rdfVarBindingMemberProp
    )

import qualified Data.Map as M

------------------------------------------------------------
--  Declare variable binding filters list
------------------------------------------------------------

-- |List of rdfOpenVarBindingModify values for predefined filters
--
rdfVarBindingFilters :: [RDFOpenVarBindingModify]
rdfVarBindingFilters :: [RDFOpenVarBindingModify]
rdfVarBindingFilters =
    [ forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingUriRef
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingBlank
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingLiteral
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingUntypedLiteral
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingTypedLiteral
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingXMLLiteral
    , forall {t} {a} {b}.
(t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 RDFLabel -> RDFVarBindingFilter
rdfVarBindingMemberProp
    , forall {t} {a} {b}.
(t -> t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter2 RDFLabel -> RDFLabel -> RDFVarBindingFilter
rdfVarBindingDatatyped
    -- , filterN nullVarBindingModify
    , forall {t} {a} {b}.
(t -> t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter2 forall b a. Eq b => a -> a -> VarBindingFilter a b
varFilterEQ
    , forall {t} {a} {b}.
(t -> t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter2 forall b a. Eq b => a -> a -> VarBindingFilter a b
varFilterNE
    ]
    where
      -- Swish.RDF.VarBinding.openVbmName seems to require that the label
      -- list not be evaluated which means that we can not replace these
      -- statements by ones like
      --
      --    filter1 f (lb:_) = makeVarFilterModift $ f lb
      --
      filter1 :: (t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter1 t -> VarBindingFilter a b
f [t]
lbs = forall a b. VarBindingFilter a b -> VarBindingModify a b
makeVarFilterModify forall a b. (a -> b) -> a -> b
$ t -> VarBindingFilter a b
f (forall a. [a] -> a
head [t]
lbs)
      filter2 :: (t -> t -> VarBindingFilter a b) -> [t] -> VarBindingModify a b
filter2 t -> t -> VarBindingFilter a b
f [t]
lbs = forall a b. VarBindingFilter a b -> VarBindingModify a b
makeVarFilterModify forall a b. (a -> b) -> a -> b
$ t -> t -> VarBindingFilter a b
f (forall a. [a] -> a
head [t]
lbs) ([t]
lbs forall a. [a] -> Int -> a
!! Int
1)
      -- filterN f lbs = makeVarFilterModify $ f ...

------------------------------------------------------------
--  Declare variable binding modifiers map
------------------------------------------------------------

rdfVarBindingModifiers :: [RDFOpenVarBindingModify]
rdfVarBindingModifiers :: [RDFOpenVarBindingModify]
rdfVarBindingModifiers =
    [ forall a b. OpenVarBindingModify a b
nullVarBindingModify
    ]

------------------------------------------------------------
--  Find a named built-in OpenVarBindingModifier
------------------------------------------------------------

allOpenVarBindingModify :: [RDFOpenVarBindingModify]
allOpenVarBindingModify :: [RDFOpenVarBindingModify]
allOpenVarBindingModify =
    [RDFOpenVarBindingModify]
rdfVarBindingFilters    forall a. [a] -> [a] -> [a]
++
    [RDFOpenVarBindingModify]
rdfVarBindingModifiers  forall a. [a] -> [a] -> [a]
++
    [RDFOpenVarBindingModify]
dtsVarBindingModifiers

dtsVarBindingModifiers :: [RDFOpenVarBindingModify]
-- dtsVarBindingModifiers = concatMap dtVarBindingModifiers allDatatypes
dtsVarBindingModifiers :: [RDFOpenVarBindingModify]
dtsVarBindingModifiers = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall ex lb vn. Datatype ex lb vn -> [OpenVarBindingModify lb vn]
typeMkModifiers [RDFDatatype]
allDatatypes

{-
dtVarBindingModifiers dtval =
    map (makeRdfDtOpenVarBindingModify dtval) (tvalMod dtval)
-}

-- | Find the named open variable binding modifier.
findRDFOpenVarBindingModifier :: ScopedName -> Maybe RDFOpenVarBindingModify
findRDFOpenVarBindingModifier :: ScopedName -> Maybe RDFOpenVarBindingModify
findRDFOpenVarBindingModifier ScopedName
nam =
    forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ScopedName
nam forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\RDFOpenVarBindingModify
ovbm -> (forall lb vn. OpenVarBindingModify lb vn -> ScopedName
openVbmName RDFOpenVarBindingModify
ovbm, RDFOpenVarBindingModify
ovbm))
                                [RDFOpenVarBindingModify]
allOpenVarBindingModify

------------------------------------------------------------
--  Lookup map for built-in rulesets
------------------------------------------------------------

-- | A 'LookupMap' of 'allRulesets'.
rdfRulesetMap :: RDFRulesetMap
rdfRulesetMap :: RDFRulesetMap
rdfRulesetMap = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\Ruleset RDFGraph
r -> (forall ex. Ruleset ex -> Namespace
getRulesetNamespace Ruleset RDFGraph
r, Ruleset RDFGraph
r)) [Ruleset RDFGraph]
allRulesets

-- | All the rule sets known to Swish.
allRulesets :: [RDFRuleset]
allRulesets :: [Ruleset RDFGraph]
allRulesets =
    [ Ruleset RDFGraph
rulesetRDF
    , Ruleset RDFGraph
rulesetRDFS
    , Ruleset RDFGraph
rulesetRDFD
    ]
    forall a. [a] -> [a] -> [a]
++ [Ruleset RDFGraph]
allDatatypeRulesets

-- | The data type rule sets known to Swish.
allDatatypeRulesets :: [RDFRuleset]
allDatatypeRulesets :: [Ruleset RDFGraph]
allDatatypeRulesets = forall a b. (a -> b) -> [a] -> [b]
map forall ex lb vn. Datatype ex lb vn -> Ruleset ex
typeRules [RDFDatatype]
allDatatypes

--------------------------------------------------------------------------------
--
--  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,
--    2011, 2012, 2022 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
--
--------------------------------------------------------------------------------