-------------------------------------------------------------------------------- -- $Id: LabelClass.hs,v 1.1 2004/07/01 15:25:36 graham Exp $ -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : LabelClass -- Copyright : (c) 2003, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- This module defines a Label class and the Arc datatype. -- -------------------------------------------------------------------------------- ------------------------------------------------------------ -- Define LDGraph, arc and related classes and types ------------------------------------------------------------ module RDF.Label.LabelClass ( Label(..) , Arc(..), arcSubj, arcPred, arcObj, arc, arcToTriple, arcFromTriple , Selector , hasLabel, arcLabels, arcNodes ) where import ListHelpers ( subset ) import FunctorM ( FunctorM(..) ) import List ( nub, union, (\\) ) --------------- -- Label class --------------- -- -- A label may have a fixed binding, which means that the label identifies (is) a -- particular graph node, and different such labels are always distinct nodes. -- Alternatively, a label may be unbound (variable), which means that it is a -- placeholder for an unknown node label. Unbound node labels are used as -- graph-local identifiers for indicating when the same node appears in -- several arcs. -- -- For the purposes of graph-isomorphism testing, fixed labels are matched when they -- are the same. Variable labels may be matched with any other variable label. -- Our definition of isomorphism (for RDF graphs) does not match variable labels -- with fixed labels. class (Eq lb, Show lb, Ord lb) => Label lb where labelIsVar :: lb -> Bool -- does this node have a variable binding? labelHash :: Int -> lb -> Int -- calculate hash of label using supplied seed getLocal :: lb -> String -- extract local id from variable node makeLabel :: String -> lb -- make label value given local id ------------ -- Arc type ------------ data Arc lb = Arc { asubj, apred, aobj :: lb } deriving Eq arcSubj :: Arc lb -> lb arcSubj = asubj arcPred :: Arc lb -> lb arcPred = apred arcObj :: Arc lb -> lb arcObj = aobj arc :: lb -> lb -> lb -> Arc lb arc s p o = Arc s p o arcToTriple :: Arc lb -> (lb,lb,lb) arcToTriple a = (asubj a,apred a,aobj a) arcFromTriple :: (lb,lb,lb) -> Arc lb arcFromTriple (s,p,o) = Arc s p o instance Ord lb => Ord (Arc lb) where compare (Arc s1 p1 o1) (Arc s2 p2 o2) = if cs /= EQ then cs else if cp /= EQ then cp else co where cs = compare s1 s2 cp = compare p1 p2 co = compare o1 o2 (Arc s1 p1 o1) <= (Arc s2 p2 o2) = if (s1 /= s2) then (s1 <= s2) else if (p1 /= p2) then (p1 <= p2) else (o1 <= o2) instance Functor Arc where -- fmap :: (lb -> l2) -> Arc lb -> Arc l2 fmap f (Arc s p o) = Arc (f s) (f p) (f o) instance FunctorM Arc where -- fmapM :: (lb -> m l2) -> Arc lb -> m (Arc l2) fmapM f (Arc s p o) = do { s' <- f s ; p' <- f p ; o' <- f o ; return $ Arc s' p' o' } instance (Show lb) => Show (Arc lb) where show (Arc lb1 lb2 lb3) = "("++(show lb1)++","++(show lb2)++","++(show lb3)++")" type Selector lb = Arc lb -> Bool hasLabel :: (Eq lb) => lb -> Arc lb -> Bool hasLabel lbv (Arc lb1 lb2 lb3) = (lbv==lb1) || (lbv==lb2) || (lbv==lb3) arcLabels :: Arc lb -> [lb] arcLabels (Arc lb1 lb2 lb3) = [lb1,lb2,lb3] arcNodes :: Arc lb -> [lb] arcNodes (Arc lb1 _ lb3) = [lb1,lb3] -------------------------------------------------------------------------------- -- -- Copyright (c) 2003, G. KLYNE. 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 -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/HaskellRDF/RDF/Label/LabelClass.hs,v $ -- $Author: graham $ -- $Revision: 1.1 $ -- $Log: LabelClass.hs,v $ -- Revision 1.1 2004/07/01 15:25:36 graham -- Moved Lavel and Vocabulary modules to separate directory -- -- Revision 1.1 2004/07/01 11:10:11 graham -- Separate RDFLabel from RDFGraph, and LabelClass from GraphClass, -- in preparation for implementing an RDF/XML parser that operates -- independently of the graph structure. --