Commits

Doug Burke  committed ebb219a

Removed Namespace constructor

  • Participants
  • Parent commits 88ffcd4

Comments (0)

Files changed (21)

 
   - The constructors for ScopedName and QName have been removed in
     order to allow a simple experimental optimisation (partly added in
-    0.4.0.0).
+    0.4.0.0). A similar change has been added for Namespace (but no
+    optimisation).
 
 0.4.0.0:
 

File Swish/RDF/ClassRestrictionRule.hs

 import Swish.RDF.VarBinding (VarBinding(..))
 import Swish.RDF.Vocabulary (namespaceRDFD)
 
-import Swish.Utils.Namespace (Namespace(..),ScopedName, namespaceToBuilder)
+import Swish.Utils.Namespace (Namespace, ScopedName, namespaceToBuilder)
 import Swish.Utils.PartOrderedCollection
     ( minima, maxima
     , partCompareEq, partComparePair

File Swish/RDF/N3Parser.hs

     , mapFind, mapFindMaybe, mapReplaceOrAdd, mapAdd, mapReplace )
 
 import Swish.Utils.Namespace
-    ( Namespace(..)
+    ( Namespace, makeNamespace
     , ScopedName
     , getScopeNamespace
     , getScopedNameURI
 setPrefix :: Maybe T.Text -> URI -> N3State -> N3State
 setPrefix pre uri st =  st { prefixUris=p' }
     where
-        p' = mapReplaceOrAdd (Namespace pre uri) (prefixUris st)
+        p' = mapReplaceOrAdd (makeNamespace pre uri) (prefixUris st)
 
 -- | Set name for special syntax element
 setSName :: String -> ScopedName -> N3State -> N3State
                     -> L.Text       -- ^ input to be parsed
                     -> Either String a
 parseAnyfromText parser mbase input =
-  let pmap   = LookupMap [Namespace Nothing hashURI]
+  let pmap   = LookupMap [makeNamespace Nothing hashURI]
       muri   = fmap (makeQNameScopedName Nothing) mbase
       smap   = LookupMap $ specialTable muri
       pstate = N3State
 getDefaultPrefix = do
   s <- stGet
   case getPrefixURI s Nothing of
-    Just uri -> return $ Namespace Nothing uri
+    Just uri -> return $ makeNamespace Nothing uri
     _ -> fail "No default prefix defined; how unexpected!"
 
 addBase :: URI -> N3Parser ()
 findPrefix pre = do
   st <- stGet
   case mapFindMaybe (Just pre) (prefixUris st) of
-    Just uri -> return $ Namespace (Just pre) uri
+    Just uri -> return $ makeNamespace (Just pre) uri
     Nothing  -> failBad $ "Prefix '" ++ T.unpack pre ++ ":' not bound."
   
 localQName :: T.Text -> N3Parser ScopedName

File Swish/RDF/RDFDatatypeXsdInteger.hs

 
 import Swish.RDF.Ruleset (makeRuleset)
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, namespaceToBuilder, makeNSScopedName)
+import Swish.Utils.Namespace (Namespace, ScopedName, namespaceToBuilder, makeNSScopedName)
 
 import Swish.RDF.Vocabulary
     ( namespaceRDF

File Swish/RDF/RDFDatatypeXsdString.hs

     )
 
 import Swish.RDF.Ruleset (makeRuleset)
-import Swish.Utils.Namespace (Namespace(..), ScopedName, namespaceToBuilder, makeNSScopedName)
+import Swish.Utils.Namespace (Namespace, ScopedName, namespaceToBuilder, makeNSScopedName)
 
 import Swish.RDF.Vocabulary
     ( namespaceRDF

File Swish/RDF/RDFGraph.hs

     where
 
 import Swish.Utils.Namespace
-    ( Namespace(..)
+    ( Namespace, makeNamespace, getNamespaceTuple
     , getScopedNameURI
     , ScopedName
     , getScopeLocal, getScopeNamespace
 import Data.Char (ord, isDigit)
 import Data.List (intersect, union, findIndices, foldl')
 import Data.Ord (comparing)
+-- import Data.Tuple (swap) not in 6.12.3
 import Data.String (IsString(..))
 import Data.Time (UTCTime, Day, ParseTime, parseTime, formatTime)
 import System.Locale (defaultTimeLocale)  
 import Text.Printf
 
+swap :: (a,b) -> (b,a)
+swap (a,b) = (b,a)
+
 -- | RDF graph node values
 --
 --  cf. <http://www.w3.org/TR/rdf-concepts/#section-Graph-syntax>
 data RevNamespace = RevNamespace Namespace
 
 instance LookupEntryClass RevNamespace URI (Maybe T.Text) where
-    keyVal   (RevNamespace (Namespace pre uri)) = (uri,pre)
-    newEntry (uri,pre) = RevNamespace (Namespace pre uri)
+    keyVal   (RevNamespace ns) = swap $ getNamespaceTuple ns
+    newEntry (uri,pre) = RevNamespace (makeNamespace pre uri)
 
 type RevNamespaceMap = LookupMap RevNamespace
 

File Swish/RDF/RDFParser.hs

     )
 
 import Swish.Utils.LookupMap (LookupMap(..))
-import Swish.Utils.Namespace (Namespace(..), ScopedName)
+import Swish.Utils.Namespace (Namespace, makeNamespace, ScopedName)
 
 import qualified Data.Text      as T
 import qualified Data.Text.Lazy as L
                 , namespaceRDFD     -- datatypes
                 , namespaceOWL
                 , namespaceLOG
-                , Namespace Nothing $ fromJust (parseURIReference "#") -- is this correct?
+                , makeNamespace Nothing $ fromJust (parseURIReference "#") -- is this correct?
                 ]
 
 {-|

File Swish/RDF/RDFProofContext.hs

 
 import Swish.RDF.Ruleset (makeRuleset)
 import Swish.RDF.Datatype (typeMkCanonicalForm)
-import Swish.Utils.Namespace (Namespace(..), makeNSScopedName)
+import Swish.Utils.Namespace (Namespace, makeNSScopedName)
 
 import Swish.RDF.Vocabulary
     ( namespaceRDFD

File Swish/RDF/RDFRuleset.hs

     , varBindingId
     )
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeNSScopedName, namespaceToBuilder)
+import Swish.Utils.Namespace (Namespace, ScopedName, makeNSScopedName, namespaceToBuilder)
 import Swish.RDF.Vocabulary (swishName, namespaceRDF, namespaceRDFS)
 
 {-

File Swish/RDF/Rule.hs

        )
        where
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeScopedName)
+import Swish.Utils.Namespace (Namespace, makeNamespace, ScopedName, makeScopedName)
 import Swish.Utils.LookupMap (LookupEntryClass(..), LookupMap(..))
 import Swish.Utils.ShowM (ShowM(..))
 
 
 -- | The namespace @http:\/\/id.ninebynine.org\/2003\/Ruleset\/null@
 nullScope :: Namespace
-nullScope = Namespace (Just "null") nullScopeURI
+nullScope = makeNamespace (Just "null") nullScopeURI
 
 nullSN :: T.Text -> ScopedName
 nullSN = makeScopedName (Just "null") nullScopeURI

File Swish/RDF/Ruleset.hs

     )
 where
 
-import Swish.Utils.Namespace
-    ( Namespace(..)
-    , ScopedName
-    )
-
+import Swish.Utils.Namespace (Namespace, ScopedName)
 import Swish.RDF.Rule (Formula(..), Rule(..))
 
 import Swish.Utils.LookupMap

File Swish/RDF/Vocabulary.hs

     )
 where
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, getScopeLocal, getScopeNamespace, makeScopedName)
+import Swish.Utils.Namespace (Namespace, makeNamespace, ScopedName, getScopeLocal, getScopeNamespace, makeScopedName)
 
 import Data.Monoid (mappend, mconcat)
 import Data.Maybe (fromMaybe)
   let ustr = T.unpack utxt
       uri = fromMaybe (error ("Unable to convert " ++ ustr ++ " to a URI")) $
             parseURI ustr
-  in Namespace (Just p) uri
+  in makeNamespace (Just p) uri
 
 toNSU :: T.Text -> URI -> Namespace
-toNSU p = Namespace (Just p)
+toNSU p = makeNamespace (Just p)
 
 namespaceXsdType :: T.Text -> Namespace
 namespaceXsdType dtn = toNS ("xsd_" `mappend` dtn)

File Swish/Utils/Namespace.hs

 --------------------------------------------------------------------------------
 
 module Swish.Utils.Namespace
-    ( Namespace(..)
-    , makeNamespaceQName
+    ( Namespace
+    , makeNamespace, makeNamespaceQName
+      , getNamespacePrefix, getNamespaceURI, getNamespaceTuple
     -- , nullNamespace
     , ScopedName
     , getScopeNamespace, getScopeLocal
 -- |A NameSpace value consists of an optional prefix and a corresponding URI.
 --
 
-data Namespace = Namespace
+data Namespace = Namespace (Maybe T.Text) URI
+-- data Namespace = Namespace (Maybe T.Text) !URI
+                 
+{-                 
                  {
                    nsPrefix :: Maybe T.Text
                  , nsURI :: URI
                  }
+-}
                  
+getNamespacePrefix :: Namespace -> Maybe T.Text
+getNamespacePrefix (Namespace p _) = p
+
+getNamespaceURI :: Namespace -> URI
+getNamespaceURI (Namespace _ u) = u
+
+getNamespaceTuple :: Namespace -> (Maybe T.Text, URI)
+getNamespaceTuple (Namespace p u) = (p, u)
+
+-- | Equality is defined by the URI, not by the prefix
+-- (so the same URI with different prefixes will be
+-- considered to be equal).
 instance Eq Namespace where
   (Namespace _ u1) == (Namespace _ u2) = u1 == u2
 
     keyVal   (Namespace pre uri) = (pre,uri)
     newEntry (pre,uri)           = Namespace pre uri
 
+makeNamespace :: Maybe T.Text -> URI -> Namespace
+makeNamespace = Namespace
+
 makeNamespaceQName :: Namespace -> T.Text -> QName
 makeNamespaceQName (Namespace _ uri) = newQName uri
 
 getScopeNamespace (ScopedName _ ns _) = ns
 
 getScopePrefix :: ScopedName -> Maybe T.Text
-getScopePrefix = nsPrefix . getScopeNamespace
+getScopePrefix = getNamespacePrefix . getScopeNamespace
 
 getScopeURI :: ScopedName -> URI
-getScopeURI = nsURI . getScopeNamespace
+getScopeURI = getNamespaceURI . getScopeNamespace
 
 -- | This is not total since it will fail if the input string is not a valid URI.
 instance IsString ScopedName where
     (<=) = snLe
 
 instance Show ScopedName where
-    show (ScopedName _ n l) = case nsPrefix n of
+    show (ScopedName _ n l) = case getNamespacePrefix n of
       Just pre -> T.unpack $ mconcat [pre, ":", l]
-      _        -> "<" ++ show (nsURI n) ++ T.unpack l ++ ">"
+      _        -> "<" ++ show (getNamespaceURI n) ++ T.unpack l ++ ">"
 
 --  Scoped names are equal if their corresponding QNames are equal
 snEq :: ScopedName -> ScopedName -> Bool
 
 -- | Construct a ScopedName from a Namespace and local component
 makeNSScopedName :: Namespace -> T.Text -> ScopedName
-makeNSScopedName ns local = ScopedName (newQName (nsURI ns) local) ns local
+makeNSScopedName ns local = ScopedName (newQName (getNamespaceURI ns) local) ns local
 
 -- | This should never appear as a valid name
 nullScopedName :: ScopedName

File tests/N3FormatterTest.hs

     , resOwlSameAs
     )
 
-import Swish.Utils.Namespace (Namespace(..), makeNSScopedName, namespaceToBuilder)
+import Swish.Utils.Namespace (Namespace, makeNamespace, makeNSScopedName, namespaceToBuilder)
 
 import Swish.Utils.LookupMap
     ( LookupMap(..)
 toURI = fromJust . parseURI
 
 toNS :: T.Text -> String -> Namespace
-toNS p = Namespace (Just p) . toURI
+toNS p = makeNamespace (Just p) . toURI
 
 toRes :: Namespace -> T.Text -> RDFLabel
 toRes ns = Res . makeNSScopedName ns
 g1fu1 :: RDFGraph
 g1fu1 =
   mempty
-  { namespaces = makeLookupMap [basem, Namespace Nothing (toURI "file:///home/swish/photos/")]
+  { namespaces = makeLookupMap [basem, makeNamespace Nothing (toURI "file:///home/swish/photos/")]
   , statements = [arc sf meDepicts meMe, arc sf meHasURN su]
   }
   

File tests/N3ParserTest.hs

     )
 
 import Swish.Utils.Namespace (
-  Namespace(..)
+  Namespace, makeNamespace, getNamespaceURI
   , ScopedName
   , makeScopedName
   , makeNSScopedName
 dqn = (qnameFromURI . toURI) baseFile
 
 toNS :: T.Text -> String -> Namespace
-toNS p = Namespace (Just p) . toURI
+toNS p = makeNamespace (Just p) . toURI
 
 dbase, base1, base2, base3, base4, basea :: Namespace
-dbase = Namespace Nothing $ toURI "#"
+dbase = makeNamespace Nothing $ toURI "#"
 base1 = toNS "base1" "http://id.ninebynine.org/wip/2003/test/graph1/node/"
 base2 = toNS "base2" "http://id.ninebynine.org/wip/2003/test/graph2/node#"
 base3 = toNS "base3" "http://id.ninebynine.org/wip/2003/test/graph3/node"
 t07  = arc s3 p2 l3
 
 makeNewPrefixNamespace :: (T.Text,Namespace) -> Namespace
-makeNewPrefixNamespace (pre,ns) = Namespace (Just pre) (nsURI ns)
+makeNewPrefixNamespace (pre,ns) = makeNamespace (Just pre) (getNamespaceURI ns)
 
 dg1, dg2, dg3 :: RDFGraph
 dg1 = toRDFGraph [arc ds1 dp1 do1]
     xb3 = Res "http://example.org/ns/foo/b3"
     xc3 = Res "http://example.org/ns/foo/c3"
     
-    ns4 = Namespace Nothing $ toURI "http://example.org/ns/foo/bar#"
-    ns5 = Namespace Nothing $ toURI "http://example.org/ns2#"
+    ns4 = makeNamespace Nothing $ toURI "http://example.org/ns/foo/bar#"
+    ns5 = makeNamespace Nothing $ toURI "http://example.org/ns2#"
     mUN a b = Res (makeNSScopedName a b)
     xa4 = mUN ns4 "a4"
     xb4 = mUN ns4 "b4"
   "a:a a:b a:c ."
 
 nToB :: Namespace -> B.Builder
-nToB = B.fromString . show . nsURI
+nToB = B.fromString . show . getNamespaceURI
 
 --  Single statement using :name form
 simpleN3Graph_g1_03 :: B.Builder

File tests/RDFDatatypeXsdIntegerTest.hs

 import Swish.RDF.Ruleset (Ruleset(..), getRulesetRule)
 import Swish.RDF.Rule    (Formula(..), Rule(..), nullFormula, nullRule)
 import Swish.RDF.VarBinding (makeVarBinding)
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeScopedName, makeNSScopedName)
+import Swish.Utils.Namespace (getNamespaceURI, ScopedName, makeScopedName, makeNSScopedName)
 import Swish.RDF.Vocabulary (namespaceDefault)
 import Swish.Utils.LookupMap (LookupMap(..), mapFindMaybe)
 import Swish.Utils.ListHelpers (equiv)
 mkGraph :: B.Builder -> RDFGraph
 mkGraph gr = 
   let base = "@prefix : <" `mappend` (ns `mappend` "> . \n")
-      ns = B.fromString $ show $ nsURI namespaceDefault
+      ns = B.fromString $ show $ getNamespaceURI namespaceDefault
   in makeRDFGraphFromN3Builder (prefixXsdInteger `mappend` (base `mappend` gr))
 
 testRuleFwd :: String -> Maybe (Rule RDFGraph) -> B.Builder -> [B.Builder] -> Test

File tests/RDFGraphTest.hs

 
 import Swish.RDF.GraphClass (Label(..), arc)
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeNSScopedName, nullScopedName)
+import Swish.Utils.Namespace (Namespace, makeNamespace, getNamespaceURI, ScopedName, makeNSScopedName, nullScopedName)
 import Swish.Utils.QName (QName, qnameFromURI)
 
 import Swish.RDF.RDFGraph
 toURI s = fromMaybe (error $ "Error: unable to parse URI " ++ s) (parseURI s)
 
 toNS :: T.Text -> String -> Namespace
-toNS p = Namespace (Just p) . toURI
+toNS p = makeNamespace (Just p) . toURI
 
 -- TODO: basee and baseu had prefixes of "" and "?" before the conversion
 --       to using Maybe String. Now both are Nothing; is this semantically
 --       correct? Probably.
 
 basee, baseu, base1, base2, base3, base4 :: Namespace
-basee = Namespace Nothing $ toURI "http://example.com/a#"
-baseu = Namespace Nothing $ toURI "http://example.com/"
+basee = makeNamespace Nothing $ toURI "http://example.com/a#"
+baseu = makeNamespace Nothing $ toURI "http://example.com/"
 base1 = toNS "base1" base1Str
 base2 = toNS "base2" "http://id.ninebynine.org/wip/2003/test/graph2/node/"
 base3 = toNS "base3" "http://id.ninebynine.org/wip/2003/test/graph3/node"
 tt06 = arc st3 p1 l10
 
 makeNewPrefixNamespace :: (T.Text,Namespace) -> Namespace
-makeNewPrefixNamespace (pre,ns) = Namespace (Just pre) (nsURI ns)
+makeNewPrefixNamespace (pre,ns) = makeNamespace (Just pre) (getNamespaceURI ns)
 
 nslist :: LookupMap Namespace
 nslist = LookupMap $ map makeNewPrefixNamespace

File tests/RDFProofContextTest.hs

 import Swish.RDF.Ruleset (getContextAxiom, getContextRule)
 import Swish.RDF.Rule (Formula(..), Rule(..), nullFormula, nullRule)
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeNSScopedName, namespaceToBuilder)
+import Swish.Utils.Namespace (Namespace, makeNamespace, ScopedName, makeNSScopedName, namespaceToBuilder)
 import Swish.Utils.LookupMap (mapFindMaybe)
 
 import Swish.RDF.Vocabulary
 toURI = fromJust . parseURI
 
 toNS :: Maybe T.Text -> String -> Namespace
-toNS p = Namespace p . toURI
+toNS p = makeNamespace p . toURI
 
 mkPrefix :: Namespace -> B.Builder
 mkPrefix = namespaceToBuilder

File tests/RDFProofTest.hs

     )
 
 import Swish.RDF.Rule (Rule(..))
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeNSScopedName)
+import Swish.Utils.Namespace (Namespace, makeNamespace, ScopedName, makeNSScopedName)
 
 import Test.HUnit
     ( Test(TestCase,TestList)
 toURI = fromJust . parseURI
 
 toNS :: Maybe T.Text -> String -> Namespace
-toNS p = Namespace p . toURI
+toNS p = makeNamespace p . toURI
 
 --  test1:  simple query with URI, literal and blank nodes.
 

File tests/RDFQueryTest.hs

     , varFilterNE
     )
 
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeScopedName)
+import Swish.Utils.Namespace (Namespace, getNamespaceURI, ScopedName, makeScopedName)
 import Swish.RDF.Vocabulary (namespaceRDF, langName, swishName, rdfType, rdfXMLLiteral)
 import Swish.RDF.N3Parser (parseN3)
 import Swish.Utils.ListHelpers (equiv)
 baseex = toURI "http://example.org/"
 
 baserdf :: URI
-baserdf  = nsURI namespaceRDF
+baserdf  = getNamespaceURI namespaceRDF
 
 q_dattyp :: ScopedName
 q_dattyp = makeScopedName Nothing baseex "datatype"

File tests/RDFRulesetTest.hs

 
 import Swish.RDF.Rule (Formula(..), Rule(..), fwdCheckInference )
 import Swish.RDF.Vocabulary (namespaceRDF, namespaceRDFS, namespaceOWL, scopeRDF)
-import Swish.Utils.Namespace (Namespace(..), ScopedName, makeScopedName, makeNSScopedName, namespaceToBuilder)
+import Swish.Utils.Namespace (Namespace, makeNamespace, getNamespaceTuple, getNamespaceURI, ScopedName, makeScopedName, makeNSScopedName, namespaceToBuilder)
 
 import Test.HUnit
     ( Test(TestCase,TestList)
 testSameNamespace lab n1 n2 =
     TestCase ( assertBool ("testSameNamespace:"++lab) ((p1==p2)&&(u1==u2)) )
     where
-        p1 = nsPrefix n1
-        p2 = nsPrefix n2
-        u1 = nsURI n1
-        u2 = nsURI n2
+      (p1, u1) = getNamespaceTuple n1
+      (p2, u2) = getNamespaceTuple n2
 
 testScopedNameEq :: String -> Bool -> ScopedName -> ScopedName -> Test
 testScopedNameEq lab eq n1 n2 =
 ------------------------------------------------------------
 
 pref_rdf, pref_owl :: URI
-pref_rdf = nsURI namespaceRDF
-pref_owl = nsURI namespaceOWL
+pref_rdf = getNamespaceURI namespaceRDF
+pref_owl = getNamespaceURI namespaceOWL
 
 toURI :: String -> URI
 toURI = fromJust . parseURI
 
 toNS :: Maybe T.Text -> String -> Namespace
-toNS p = Namespace p . toURI
+toNS p = makeNamespace p . toURI
 
 ------------------------------------------------------------
 --  Define and manipulate rulesets