added support for datatypes
authorSergio Fernandez <sergio.fernandez@fundacionctic.org>
Thu, 20 Oct 2011 11:28:10 +0200
changeset 180 2816bade4b4f
parent 179 318b65255b9a
child 181 c784a76907c7
added support for datatypes
ontologies/specgen/libvocab.py
--- a/ontologies/specgen/libvocab.py	Thu Oct 20 09:45:43 2011 +0200
+++ b/ontologies/specgen/libvocab.py	Thu Oct 20 11:28:10 2011 +0200
@@ -226,6 +226,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):
@@ -271,7 +287,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):
@@ -313,8 +329,10 @@
   		self.terms = []
   		self.properties = []
   		self.classes = []
+		self.datatypes = []
   		tmpclasses = []
   		tmpproperties = []
+		tmpdatatypes = []
 
   		g = self.graph
   		# extend query for different property types 
@@ -345,9 +363,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 }'
@@ -431,14 +478,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):
@@ -491,21 +541,28 @@
 
   	def az(self):
   		"""AZ List for html doc"""
-  		c_ids, p_ids = self.vocab.azlist()
+  		c_ids, p_ids, d_ids = self.vocab.azlist()
   		az = """<div class="azlist">"""
-  		az = """%s\n<p>Classes: |""" % az
+  		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)
+  		az = """%s\n</p>""" % az
 
-  		az = """%s\n</p>""" % az
-  		az = """%s\n<p>Properties: |""" % az
+  		az = "%s\n<p>Properties: |" % az
   		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
+  		az = "%s\n</p>" % az
+
+		if (len(d_ids)>0):
+	  		az = "%s\n<p>Datatypes: |" % az
+	  		for d in d_ids:
+	  			az = """%s <a href="#%s">%s</a> | """ % (az, str(d).replace(" ", ""), d)
+	  		az = "%s\n</p>" % az
+
+  		az = "%s\n</div>" % az
   		return(az)
 
   	def termlist(self):
@@ -516,14 +573,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 />
@@ -876,7 +933,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 = ''
@@ -1086,7 +1142,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
@@ -1199,9 +1255,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>")