merged specgen changes into same branch as spec changes. A mercurial administration issue.
authorHenry Story <henry.story@bblfish.net>
Wed, 16 Nov 2011 14:30:03 +0100
changeset 189 799504dd986b
parent 186 d629f4b9630d (current diff)
parent 184 271899970981 (diff)
child 190 c87c211832e2
merged specgen changes into same branch as spec changes. A mercurial administration issue.
--- a/ontologies/specgen/libvocab.py	Mon Nov 14 15:48:52 2011 +0100
+++ b/ontologies/specgen/libvocab.py	Wed Nov 16 14:30:03 2011 +0100
@@ -1,6 +1,14 @@
 #!/usr/bin/env python
 
-# total rewrite. --danbri
+# modifications and improvements: Sergio Fernández, October 2011
+#		+ owl:equivalentClass
+#		+ skos:editorialNote
+#		+ rdfs:Datatype
+#		+ fixed curies generation
+#		+ general markup improvements 
+#
+#	Copyright 2011 W3C WebID XG <http://www.w3.org/2005/Incubator/webid/>
+#
 #
 # modifications and extensions: Bob Ferris, July 2010
 #		+ multiple property and class types 
@@ -10,7 +18,10 @@
 #		+ inverse properties (explicit and anonymous)
 #		+ sub properties
 #
-#		Copyright 2010 Bob Ferris <http://smiy.wordpress.com/author/zazi0815/>
+#	Copyright 2010 Bob Ferris <http://smiy.wordpress.com/author/zazi0815/>
+#
+#
+# total rewrite. --danbri
 #
 # Usage:
 #
@@ -226,6 +237,22 @@
   		return(True)
 
 
+
+# A Python class representing an RDFS datatype
+#
+
+class Datatype(Term):
+
+	# OK OK but how are we SUPPOSED to do this stuff in Python OO?. Stopgap.
+	def is_property(self):
+		# print "Class.is_property called on "+self
+		return(False)
+
+  	def is_class(self):
+  		# print "Class.is_class called on "+self
+  		return(False)
+
+
 # A python class representing (a description of) some RDF vocabulary
 #
 class Vocab(object):
@@ -236,7 +263,12 @@
 		self.dir = dir
 		self.filename = os.path.join(dir, f)
 		# print "ontology file name ", self.filename 
-		self.graph.parse(self.filename)
+		if (self.filename.endswith(".n3")):
+			self.graph.parse(self.filename, publicID=uri, format="n3")
+		elif(self.filename.endswith(".rdf")):
+			self.graph.parse(self.filename, publicID=uri, format="xml")
+		else:
+			self.graph.parse(self.filename, publicID=uri)
 		self.terms = []
 		self.uterms = []
 		# should also load translations here?
@@ -244,7 +276,8 @@
 
 		##if f != None:    
 		##  self.index()
-		self.ns_list = { "http://www.w3.org/1999/02/22-rdf-syntax-ns#"   : "rdf",
+		self.ns_list = { 
+					"http://www.w3.org/1999/02/22-rdf-syntax-ns#"   : "rdf",
 					"http://www.w3.org/2000/01/rdf-schema#"         : "rdfs",
 					"http://www.w3.org/2002/07/owl#"                : "owl",
 					"http://www.w3.org/2001/XMLSchema#"             : "xsd",
@@ -265,7 +298,7 @@
 					"http://www.w3.org/ns/auth/cert#"               : "cert",
 					"http://www.w3.org/ns/auth/rsa#"                : "rsa"
 		}
-
+		self.prefixes = dict((v,k) for (k,v) in self.ns_list.items())
 
 
   	def addShortName(self, sn):
@@ -307,8 +340,10 @@
   		self.terms = []
   		self.properties = []
   		self.classes = []
+		self.datatypes = []
   		tmpclasses = []
   		tmpproperties = []
+		tmpdatatypes = []
 
   		g = self.graph
   		# extend query for different property types 
@@ -339,9 +374,38 @@
   				self.classes.append(c)
   				tmpclasses.append(str(c))
 
+  		query = """	
+					PREFIX owl: <%s>	
+					PREFIX vs: <%s>
+					PREFIX skos: <%s>
+					SELECT ?x ?l ?c ?ec ?vs ?en
+					WHERE {
+						?x rdf:type     rdfs:Datatype ;
+						   rdfs:label   ?l ;
+						   rdfs:comment ?c .
+						OPTIONAL { ?x owl:equivalentClass ?ec }
+						OPTIONAL { ?x vs:term_status ?vs }
+						OPTIONAL { ?x skos:editorialNote ?en }
+					}
+				""" % (self.prefixes["owl"], self.prefixes["vs"], self.prefixes["skos"])
+  		relations = g.query(query)
+  		for (term, label, comment, cls, status, note) in relations:
+  			d = Datatype(term)
+			d.type = "http://www.w3.org/2000/01/rdf-schema#Datatype"
+  			d.label = str(label)
+  			d.comment = str(comment)
+			d.equivalentClass = str(cls) if cls else None
+			d.status = str(status) if status else None
+			d.note = str(note)  if note else None
+  			self.terms.append(d)
+  			if (not str(d) in tmpdatatypes):
+  				self.datatypes.append(d)
+  				tmpdatatypes.append(str(d))
+
   		self.terms.sort(key = operator.attrgetter('id'))
   		self.classes.sort(key = operator.attrgetter('id'))
   		self.properties.sort(key = operator.attrgetter('id'))
+		self.datatypes.sort(key = operator.attrgetter('id'))
 
   		# http://www.w3.org/2003/06/sw-vocab-status/ns#"
   		query = 'SELECT ?x ?vs WHERE { ?x <http://www.w3.org/2003/06/sw-vocab-status/ns#term_status> ?vs }'
@@ -425,14 +489,17 @@
   		"""Builds the A-Z list of terms"""
   		c_ids = []
   		p_ids = []
-  		for p in self.properties:
-  			p_ids.append(str(p.id))
+		d_ids = []
   		for c in self.classes:
   			c_ids.append(str(c.id))
+  		for p in self.properties:
+  			p_ids.append(str(p.id))
+  		for d in self.datatypes:
+  			d_ids.append(str(d.id))
   		c_ids.sort()
   		p_ids.sort()
-  		return (c_ids, p_ids)
-
+		d_ids.sort()
+  		return (c_ids, p_ids, d_ids)
 
 
 class VocabReport(object):
@@ -485,22 +552,28 @@
 
   	def az(self):
   		"""AZ List for html doc"""
-  		c_ids, p_ids = self.vocab.azlist()
-  		az = """<div class="azlist">"""
-  		az = """%s\n<p>Classes: |""" % az
-  		# print c_ids, p_ids
-  		for c in c_ids:
-  			# speclog("Class "+c+" in az generation.")
-  			az = """%s <a href="#%s">%s</a> | """ % (az, str(c).replace(" ", ""), c)
+  		c_ids, p_ids, d_ids = self.vocab.azlist()
+  		# print c_ids, p_ids, d_ids
 
-  		az = """%s\n</p>""" % az
-  		az = """%s\n<p>Properties: |""" % az
+		az = ""
+		
+		azs = []
+  		for c in c_ids:
+  			azs.append("""<a href="#%s">%s</a>""" % (str(c).strip(), c))
+  		az = """%s\n<p><span style="font-weight: bold;">Classes:</span> %s </p> \n""" % (az, " | ".join(azs))
+
+  		azs = []
   		for p in p_ids:
-  			# speclog("Property "+p+" in az generation.")
-  			az = """%s <a href="#%s">%s</a> | """ % (az, str(p).replace(" ", ""), p)
-  		az = """%s\n</p>""" % az
-  		az = """%s\n</div>""" % az
-  		return(az)
+  			azs.append("""<a href="#%s">%s</a>""" % (str(p).strip(), p))
+  		az = """%s\n<p><span style="font-weight: bold;">Properties:</span> %s </p> \n""" % (az, " | ".join(azs))
+
+		if (len(d_ids)>0):
+			azs = []
+	  		for d in d_ids:
+	  			azs.append("""<a href="#%s">%s</a>""" % (str(d).strip(), d))
+			az = """%s\n<p><span style="font-weight: bold;">Datatypes:</span> %s </p>\n""" % (az, " | ".join(azs))
+
+  		return """<div class="azlist">%s\n</div>""" % az
 
   	def termlist(self):
   		"""Term List for html doc"""
@@ -510,14 +583,14 @@
   		archaicTxt = ''
 
   		queries = ''
-  		c_ids, p_ids = self.vocab.azlist()
+  		c_ids, p_ids, d_ids = self.vocab.azlist()
   		tl = """<div class="termlist">"""
   		tl = """%s\n<div class='termdetails'><br />\n\n""" % tl
 
 
   		# danbri hack 20100101 removed: href="http://www.w3.org/2003/06/sw-vocab-status/ns#%s" pending discussion w/ libby and leigh re URIs
 
-  		# first classes, then properties
+  		# first classes, then properties, and datatypoes at the end
   		eg = """<div class="specterm" id="%s" about="%s" typeof="%s">
   			<h4>%s: %s</h4> 
   			<em property="rdfs:label" >%s</em> - <span property="rdfs:comment" >%s</span> <br />
@@ -870,7 +943,6 @@
   		## then add the whole thing to the main tl string
   		tl = tl + "<h3>Classes</h3>\n"
   		tl = "%s %s" % (tl, stableTxt + "\n" + testingTxt + "\n" + unstableTxt + "\n" + archaicTxt)
-  		tl = tl + "<h3>Properties</h3>\n"
 
   		# properties
   		stableTxt = ''
@@ -1080,7 +1152,7 @@
   				contentStr = "%s %s" % (contentStr, termStr)
 
   			if contentStr != "":
-  				propertyIsDefinedBy = "%s <dd> %s </dd></tr>" % (startStr, contentStr)
+  				propertyIsDefinedBy = "%s <dd> %s </dd>" % (startStr, contentStr)
 
 
   			# rdf property
@@ -1193,9 +1265,56 @@
   			if((term.status == None) or (term.status == "") or (term.status == "unknown")):
   				archaicTxt = archaicTxt + zz
 
-  		## then add the whole thing to the main tl string
+		## then add the whole thing to the main tl string
+  		tl = tl + "<h3>Properties</h3>\n"
   		tl = "%s %s" % (tl, stableTxt + "\n" + testingTxt + "\n" + unstableTxt + "\n" + archaicTxt)
-  		##    tl = "%s %s" % (tl, zz)
+
+
+		if (len(self.vocab.datatypes) > 0):
+
+	  		# properties
+	  		stableTxt = ''
+	  		testingTxt = ''
+	  		unstableTxt = ''
+	  		archaicTxt = ''
+
+			#datatypes
+			for term in self.vocab.datatypes:
+				sn = self.vocab.niceName(term.uri)
+	  			s = termlink(s)
+
+				equivalentClass = ""
+				if (term.equivalentClass):
+					cls = Term(term.equivalentClass)
+	  				startStr = "Equivalent Class:"
+	  				curie = "%s:%s" % (self.vocab.ns_list[cls.ns], cls.id)
+	  				termStr = '<a href="#%s">%s</a>' % (cls.id, curie)
+	  				equivalentClass = "<dt> %s </dt> \n <dd> %s </dd>" % (startStr, termStr)
+
+				editorialNote = ""
+				if (term.note):
+	  				startStr = "Editorial Note:"
+	  				termStr = term.note
+	  				editorialNote = "<dt> %s </dt> \n <dd> %s </dd>" % (startStr, termStr)
+
+				zz = eg % (term.id, term.uri, term.type, "Datatype", sn, term.label, term.comment, term.status, "", equivalentClass + editorialNote, s, term.id, term.id)
+
+	  			## we add to the relevant string - stable, unstable, testing or archaic
+	  			if(term.status == "stable"):
+	  				stableTxt = stableTxt + zz
+	  			if(term.status == "testing"):
+	  				testingTxt = testingTxt + zz
+	  			if(term.status == "unstable"):
+	  				unstableTxt = unstableTxt + zz
+	  			if(term.status == "archaic"):
+	  				archaicTxt = archaicTxt + zz
+	  			if((term.status == None) or (term.status == "") or (term.status == "unknown")):
+	  				archaicTxt = archaicTxt + zz
+
+
+	  		## then add the whole thing to the main tl string
+			tl = tl + "<h3>Datatypes</h3>\n"
+	  		tl = "%s %s" % (tl, stableTxt + "\n" + testingTxt + "\n" + unstableTxt + "\n" + archaicTxt)
 
   		## ensure termlist tag is closed
   		return(tl + "\n" + queries + "</div>\n</div>")
--- a/ontologies/specgen/specgen6.py	Mon Nov 14 15:48:52 2011 +0100
+++ b/ontologies/specgen/specgen6.py	Wed Nov 16 14:30:03 2011 +0100
@@ -1,15 +1,23 @@
 #!/usr/bin/env python
 
-# This is a draft rewrite of specgen5, the family of scripts (originally
-# in Ruby, then Python) that are used with the Counter Ontology, Ordered Lists
-# Ontology and Info Service Ontology.
-# This version is based on danbri's specgen version (specgen5) and heavily extended
-# by Bob Ferris in July 2010 (the main extensions are in libvocab.py). This version is
-# a bit more FOAF independent ;)
+# specgen6-webid:
+#
+#	Fork by the W3C WebID XG, trying to improve the tool for the concrete
+#	scope of the group ontologies (cert and rsa), but keeping all changes 
+#	ready for a general purpose usage.
+#
+#	Copyright 2011 W3C WebID XG <http://www.w3.org/2005/Incubator/webid/>
 #
 # spegen6:
 #
-#   Copyright 2010 Bob Ferris <http://smiy.wordpress.com/author/zazi0815/>
+#	This is a draft rewrite of specgen5, the family of scripts (originally
+#	in Ruby, then Python) that are used with the Counter Ontology, Ordered Lists
+#	Ontology and Info Service Ontology.
+#	This version is based on danbri's specgen version (specgen5) and heavily extended
+#	by Bob Ferris in July 2010 (the main extensions are in libvocab.py). This version is
+#	a bit more FOAF independent ;)
+#
+#	Copyright 2010 Bob Ferris <http://smiy.wordpress.com/author/zazi0815/>
 #
 # specgen5:
 #