# $Id: N3Statement.py,v 1.6 2002/05/01 15:43:49 graham Exp $ # # RDF statement class for use with N3 parser and model # #--------+---------+---------+---------+---------+---------+---------+---------+ # # Copyright (c) 2002, G. KLYNE # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # #--------+---------+---------+---------+---------+---------+---------+---------+ # $Source: /file/cvsdev/PythonN3/N3Statement.py,v $ # $Author: graham $ # $Revision: 1.6 $ $Date: 2002/05/01 15:43:49 $ #--------+---------+---------+---------+---------+---------+---------+---------+ # 1 2 3 4 5 6 7 8 #import os #import re import sys import string import StringIO from N3Node import N3Node #--------+---------+---------+---------+---------+---------+---------+---------+ # N3Statement - represents any statement in the graph. # class N3Statement: def __init__( self, Subj, Prop, Obj ): """ Construct statement from subject, property, object nodes. """ self.Prop = Prop if Prop.isReversed(): self.Subj = Obj self.Obj = Subj else: self.Subj = Subj self.Obj = Obj return def __eq__( self, Stmt ): """ Compare for equality with another statement. Statements are equal if they have equal subject, property and object values. """ return ( ( self.Subj == Stmt.Subj ) and ( self.Prop == Stmt.Prop ) and ( self.Obj == Stmt.Obj ) ) def __ne__( self, Stmt ): """ Compare for inequality with another statement. (Complement of __eq__ above.) """ return not self.__eq__( Stmt ) def __str__( self ): return ( str(self.Subj) + " " + str(self.Prop) + " " + str(self.Obj) + " ." ) def __repr__( self ): return ( "'" + str(self.Subj) + " " + str(self.Prop) + " " + str(self.Obj) + " . '\n" ) def getSubject( self ): """ Return subject node of statement """ return self.Subj def getProperty( self ): """ Return property node for statement """ return self.Prop def getObject( self ): """ Return object node of statement """ return self.Obj def match( self, Subj, Prop, Obj ): """ Test for match with supplied subject, property and object. Values supplied as None match anything. """ if Subj: if Subj != self.Subj: return 0 if Prop: if Prop != self.Prop: return 0 if Obj: if Obj != self.Obj: return 0 return 1 def getStmtDisplay( self, PrefixTable ): """ Return convenient description for the statement, using the supplied prefix table. """ return ( self.Subj.getNodeName( PrefixTable ) + " " + self.Prop.getNodeName( PrefixTable ) + " " + self.Obj.getNodeName( PrefixTable ) + " ." ) # End of N3Statement #--------+---------+---------+---------+---------+---------+---------+---------+ # # Stand-alone test code follows: # if __name__ == '__main__': # Some basic tests sys.stdout.write( "Node tests\n" ) Node1 = N3Node() sys.stdout.write( "Node 1: "+str(Node1)+"\n" ) Node2 = N3Node( uri='http://example.org/base/local' ) sys.stdout.write( "Node 2: "+str(Node2)+"\n" ) Node3 = N3Node( ns='http://example.org/base/', name='local' ) sys.stdout.write( "Node 3: "+str(Node3)+"\n" ) Node4 = N3Node( lit='a string' ) sys.stdout.write( "Node 4: "+str(Node4)+"\n" ) if Node1 == Node1: sys.stdout.write( "Node1 == Node1 (OK)\n" ) else: sys.stdout.write( "Node1 != Node1 (Error)\n" ) if Node2 == Node3: sys.stdout.write( "Node2 == Node3 (OK)\n" ) else: sys.stdout.write( "Node2 != Node3 (Error)\n" ) sys.stdout.write( "Statement tests\n" ) Stmt1 = N3Statement( Node1, Node2, Node4 ) sys.stdout.write( "Stmt 1: "+str(Stmt1)+"\n" ) Stmt2 = N3Statement( Node1, Node3, Node4 ) sys.stdout.write( "Stmt 2: "+str(Stmt2)+"\n" ) if Stmt1 == Stmt2: sys.stdout.write( "Stmt1 == Stmt2 (OK)\n" ) else: sys.stdout.write( "Stmt1 != Stmt2 (Error)\n" ) sys.stdout.write( "Exiting\n" ) #--------+---------+---------+---------+---------+---------+---------+---------+ # # $Log: N3Statement.py,v $ # Revision 1.6 2002/05/01 15:43:49 graham # Cleanup small details # # Revision 1.5 2002/04/25 19:00:13 graham # Update CVS keyword fields # # Revision 1.4 2002/04/25 18:53:25 graham # Add copyright and disclaimer notices # # Revision 1.3 2002/04/24 14:36:33 graham # Save edits # # Revision 1.2 2002/04/23 17:09:57 graham # Basic query and report generation works # # Revision 1.1 2002/04/19 22:07:39 graham # Separated into smaller modules; basic N3Model functions working # # Revision 1.3 2002/04/18 07:39:42 graham # Added TODO # # Revision 1.2 2002/04/17 23:41:31 graham # Basic N3 parser is working #