# $Id: N3GenProtocolMatrix.py,v 1.3 2002/12/11 13:51:49 graham Exp $ # # RDF protocol matrix report generator # # Uses an RDF-based description stored in an N3Model to generate # a protocol dependency table. # #--------+---------+---------+---------+---------+---------+---------+---------+ # # 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/N3GenProtocolMatrix.py,v $ # $Author: graham $ # $Revision: 1.3 $ #--------+---------+---------+---------+---------+---------+---------+---------+ # 1 2 3 4 5 6 7 8 import os import sys import string import StringIO from N3Exception import N3Exception from N3Node import N3Node from N3Model import N3Model, N3PrefixError from N3Report import N3Report, N3ReportError #--------+---------+---------+---------+---------+---------+---------+---------+ # Define N3GenProtocolMatrix package exception conditions # # Base class for exceptions # class N3GenProtocolMatrixError( N3Exception ): """ Base class for N3GenProtocolMatrix generation errors. Attributes: message -- explanation of the error """ def __init__(self, message): N3Exception.__init__( self, "N3GenProtocolMatrixError: "+message ) pass class N3CommandError( N3GenProtocolMatrixError ): """ Invalid command line argument supplied Attributes: message -- explanation of the error """ def __init__(self, message): N3GenProtocolMatrixError.__init__( self, "N3CommandError: "+message ) pass class N3QueryError( N3GenProtocolMatrixError ): """ Error in query structure Attributes: message -- explanation of the error """ def __init__(self, message): N3GenProtocolMatrixError.__init__( self, "N3QueryError: "+message ) pass class N3ReadError( N3GenProtocolMatrixError ): """ Error reading data files Attributes: message -- explanation of the error """ def __init__(self, message, file): N3GenProtocolMatrixError.__init__( self, "N3ReadError: "+message ) self.File = file pass class N3GenerateError( N3GenProtocolMatrixError ): """ Error generating registry files Attributes: message -- explanation of the error """ def __init__(self, message): N3GenProtocolMatrixError.__init__( self, "N3GenerateError: "+message ) pass #--------+---------+---------+---------+---------+---------+---------+---------+ # N3GenProtocolMatrix - message header registry generation functions # class N3GenProtocolMatrix: #--------------------------------------------------------------------------- # Registry generation model data #--------------------------------------------------------------------------- RegDef = ''' @prefix rdf: . @prefix rdfs: . @prefix foaf: . @prefix rep: . @prefix dc: . @prefix protocol: . @prefix prep: . prep:GenReport a rep:Report ; :- ( [ rep:cmd rep:if ; rep:defined "htmlname" ; rep:do prep:HTMLReport ] ) . prep:HTMLReport a rep:Report ; :- ( [ rep:cmd rep:open ; rep:chan "t" ; rep:file ( [ rep:var "htmlname" ] ) ] # [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Head ] [ rep:cmd rep:if ; rep:pattern prep:ThisPattern ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:ThisHead ] [ rep:do prep:GenProtoReport ] [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:ThisFoot ] ) ; rep:else ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:NoReport ] ) ] # [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Foot ] [ rep:cmd rep:close ; rep:chan "t" ] ) . prep:GenProtoReport :- ( [ rep:cmd rep:for ; rep:pattern prep:ProtoPattern ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto1 ] [ rep:cmd rep:for ; rep:pattern prep:ProtoTypePattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto2first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto2 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto2sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto2last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoSpecPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto3first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto3 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto3sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto3last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoUsesPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto4first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto4 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto4sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto4last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoCanUsePattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto5first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto5 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto5sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto5last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoUsedByPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:UsedByFirst ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:UsedByItem ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:UsedBySep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:UsedByLast ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoContainsPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto6first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto6 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto6sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto6last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoCanContainPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto7first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto7 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto7sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto7last ] ) ] [ rep:cmd rep:for ; rep:pattern prep:ProtoCannotContainPattern ; rep:first ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto8first ] ) ; rep:do ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto8 ] ) ; rep:sep ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto8sep ] ) ; rep:last ( [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto8last ] ) ] [ rep:cmd rep:write ; rep:chan "t" ; rep:data prep:Proto9 ] ) ] ) . #### query patterns #### # About the current document prep:ThisPattern :- ( [ rep:var "thisinfo" ] [ rep:and ( [ rep:uri rdfs:label ] [ rep:var "ThisLabel" ] ), ( [ rep:uri dc:author ] [ rep:var "ThisAuthor" ] ), ( [ rep:uri dc:date ] [ rep:var "ThisDate" ] ), ( [ rep:uri rdfs:comment ] [ rep:var "ThisComment" ] ) ] ) . # Protocol info pattern prep:ProtoPattern :- ( [ rep:var "protoinfo" ] [ rep:and ( [ rep:uri rdf:type ] [ rep:uri protocol:Protocol ] ), ( [ rep:uri rdfs:label ] [ rep:var "ProtoLabel" ] ), ( [ rep:uri rdfs:comment ] [ rep:var "ProtoDescr" ] ) ] ) . # Protocol type pattern prep:ProtoTypePattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:type ] [ rep:var "ProtoType" ] [ rep:uri rdfs:label ] [ rep:var "ProtoTypeLabel" ] ) . # Protocol specification pattern prep:ProtoSpecPattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:specification ] [ rep:var "ProtoSpec" ] ) . # Protocol uses pattern prep:ProtoUsesPattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:uses ] [ rep:var "ProtoUses" ] [ rep:uri rdfs:label ] [ rep:var "ProtoUsesLabel" ] ) . # Protocol can-use pattern prep:ProtoCanUsePattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:canUse ] [ rep:var "ProtoCanUse" ] [ rep:uri rdfs:label ] [ rep:var "ProtoCanUseLabel" ] ) . # Protocol used-by pattern prep:ProtoUsedByPattern :- ( [ rep:var "ProtoUsedBy" ] [ rep:and ( [ rep:uri protocol:uses ] [ rep:var "protoinfo" ] ), ( [ rep:uri rdfs:label ] [ rep:var "ProtoUsedByLabel" ] ) ] ) . # Protocol contains pattern prep:ProtoContainsPattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:contains ] [ rep:var "ProtoContains" ] [ rep:uri rdfs:label ] [ rep:var "ProtoContainsLabel" ] ) . # Protocol can-contain pattern prep:ProtoCanContainPattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:canContain ] [ rep:var "ProtoCanContain" ] [ rep:uri rdfs:label ] [ rep:var "ProtoCanContainLabel" ] ) . # Protocol cannot-contain pattern prep:ProtoCannotContainPattern :- ( [ rep:var "protoinfo" ] [ rep:uri protocol:cannotContain ] [ rep:var "ProtoCannotContain" ] [ rep:uri rdfs:label ] [ rep:var "ProtoCannotContainLabel" ] ) . #### output templates #### prep:ThisHead :- ( "" rep:nl "" "" rep:nl "" rep:nl "" [rep:var "ThisLabel"] "" rep:nl "" rep:nl "" rep:nl rep:nl "

" [rep:var "ThisLabel"] "

" rep:nl "
" rep:nl "
Prepared by:
" [rep:var "ThisAuthor"] "
" rep:nl "
Date:
" [rep:var "ThisDate"] "
" rep:nl "
" rep:nl "

" [rep:var "ThisComment"] "

" rep:nl "
" rep:nl ) . prep:ThisFoot :- ( "" rep:nl "" rep:nl ) . prep:NoReport :- ( "" rep:nl "" "" rep:nl "" rep:nl "Empty report" rep:nl "" rep:nl "" rep:nl "

Nothing found to report.

" rep:nl "" rep:nl "" rep:nl ) . # Protocol table and name prep:Proto1 :- ( "

" [ rep:var "ProtoLabel" ] "

" rep:nl "" rep:nl ) . # Protocol type info prep:Proto2first :- ( "" ) . # Specification prep:Proto3first :- ( "" rep:nl ) . # Uses prep:Proto4first :- ( "" rep:nl ) . # Can-use prep:Proto5first :- ( "" rep:nl ) . # Used-by prep:UsedByFirst :- ( "" rep:nl ) . # Contains prep:Proto6first :- ( "" rep:nl ) . # Can-contain prep:Proto7first :- ( "" rep:nl ) . # Cannot-contain prep:Proto8first :- ( "" rep:nl ) . # Protocol description end table prep:Proto9 :- ( "" rep:nl "
Specification type: " rep:nl ) . prep:Proto2 :- ( [ rep:var "ProtoTypeLabel" ] ) . prep:Proto2sep :- ( ", " ) . prep:Proto2last :- ( "
Specification(s): " rep:nl ) . prep:Proto3 :- ( "" [ rep:var "ProtoSpec" ] "" ) . prep:Proto3sep :- ( "
" rep:nl ) . prep:Proto3last :- ( "
Uses: " rep:nl ) . prep:Proto4 :- ( "" [ rep:var "ProtoUsesLabel" ] "" ) . prep:Proto4sep :- ( "
" rep:nl ) . prep:Proto4last :- ( "
Can use: " rep:nl ) . prep:Proto5 :- ( "" [ rep:var "ProtoCanUseLabel" ] "" ) . prep:Proto5sep :- ( "
" rep:nl ) . prep:Proto5last :- ( "
Used by: " rep:nl ) . prep:UsedByItem :- ( "" [ rep:var "ProtoUsedByLabel" ] "" ) . prep:UsedBySep :- ( "
" rep:nl ) . prep:UsedByLast :- ( "
Contains or carries: " rep:nl ) . prep:Proto6 :- ( "" [ rep:var "ProtoContainsLabel" ] "" ) . prep:Proto6sep :- ( "
" rep:nl ) . prep:Proto6last :- ( "
Can contain or carry: " rep:nl ) . prep:Proto7 :- ( "" [ rep:var "ProtoCanContainLabel" ] "" ) . prep:Proto7sep :- ( "
" rep:nl ) . prep:Proto7last :- ( "
Cannot contain or carry: " rep:nl ) . prep:Proto8 :- ( "" [ rep:var "ProtoCannotContainLabel" ] "" ) . prep:Proto8sep :- ( "
" rep:nl ) . prep:Proto8last :- ( "
" [ rep:var "ProtoDescr" ] "
" ) . ''' Usage = ''' python N3GenProtocolMatrix.py options Options are: -i file,file,... input files -o dir output directory -t name generate HTML table to named file -e generate HTML entries to subdirectories ''' #--------------------------------------------------------------------------- # Setup methods #--------------------------------------------------------------------------- def __init__( self, argv ): """ Initialize and process command line options Parameters: argv list of command line arguments: -i file,file,... input files -t name generate HTML table to named file """ RegStr = StringIO.StringIO( N3GenProtocolMatrix.RegDef ) self.Model = N3Model( RegStr ) #### # self.Model.setPrefixTable( None ) # self.Model.read( RegStr ) #### self.Files = [] self.Table = None i = 1 while i < len(argv): if argv[i] == "-i": i += 1 if i >= len(argv): raise N3CommandError( "Input file(s) expected" ) for f in string.split(argv[i],","): if not os.access( f, os.R_OK ): raise N3CommandError( "Cannot read file "+f ) self.Files.append( f ) elif argv[i] == "-t": i += 1 if i >= len(argv): raise N3CommandError( "HTML output filename expected" ) t = argv[i] self.Table = t i += 1 # End while if not self.Model.parseOK(): raise N3QueryError( "Registry generator query error" ) return def readData( self ): """ Read data from files specified on command line. """ for f in self.Files: sys.stdout.write( "Reading file "+f+"...\n" ) s = open( f, "r" ) m = N3Model( s ) s.close() if not m.parseOK(): raise N3ReadError( "Invalid data file", f ) self.Model.merge( m ) return def makeReport( self, ReportNodeName ): """ Generate report files from model data. """ hrep_Report = self.Model.makeNode( ReportNodeName ) Report = N3Report( self.Model, hrep_Report ) VarBindings = { } if self.Table: VarBindings["htmlname"] = N3Node( lit=self.Table ) Report.generate( VarBindings ) return def debug( self, msg ): sys.stdout.write( "N3GenProtocolMatrix: "+msg+"\n" ) return # End of N3GenProtocolMatrix #--------+---------+---------+---------+---------+---------+---------+---------+ # Stand-alone test/command line operation code follows: # if __name__ == '__main__': # Process command line arguments sys.stdout.write( "Initializing\n" ) try: argv = sys.argv sys.stdout.write( "argv "+`argv`+", len="+`len(argv)`+"\n" ) if not argv or len(argv) <= 1: argv = [ "N3GenProtocolMatrix", "-i", "N3GenProtocolMatrixTest.n3", "-t", "N3GenProtocolMatrixTest.html" ] GenDocIssues = N3GenProtocolMatrix( argv ) except N3CommandError, e: sys.stdout.write( str(e)+"\n" ) sys.stdout.write( N3GenProtocolMatrix.Usage ) sys.exit( 1 ) except N3QueryError, e: sys.stdout.write( str(e)+"\n" ) sys.exit( 9 ) # Read model data sys.stdout.write( "Read data files\n" ) try: GenDocIssues.readData() except N3ReadError, e: sys.stdout.write( "Error reading file "+e.File+"\n" ) sys.stdout.write( str(e)+"\n" ) sys.exit( 2 ) except N3PrefixError, e: sys.stdout.write( str(e)+"\n" ) sys.exit( 2 ) # Generate registry data sys.stdout.write( "Generate protocol matrix\n" ) try: GenDocIssues.makeReport( "prep:GenReport" ) except N3GenerateError, e: sys.stdout.write( str(e)+"\n" ) sys.exit( 3 ) except N3ReportError, e: sys.stdout.write( str(e)+"\n" ) sys.exit( 3 ) sys.stdout.write( "Exiting\n" ) sys.exit( 0 ) #--------+---------+---------+---------+---------+---------+---------+---------+ # $Log: N3GenProtocolMatrix.py,v $ # Revision 1.3 2002/12/11 13:51:49 graham # Fix up some URI initialization problems # # Revision 1.2 2002/10/21 17:59:15 graham # Tweaked output format; added derived used-by entries and links # # Revision 1.1 2002/10/21 17:20:35 graham # Created new report generator utility to format a protocol cross-reference matrix as XHTML. #