fromlogilab.mtconverterimportTransformError,html_escapefromcubicweb.common.viewimportStartupViewfromcubicweb.common.viewimportEntityView_=unicodeOWL_CARD_MAP={'1':'<rdf:type rdf:resource="&owl;FunctionalProperty"/>','?':'<owl:maxCardinality rdf:datatype="&xsd;int">1</owl:maxCardinality>','+':'<owl:minCardinality rdf:datatype="&xsd;int">1</owl:minCardinality>','*':''}OWL_TYPE_MAP={'String':'xsd:string','Datetime':'xsd:dateTime','Bytes':'xsd:byte','Float':'xsd:float','Boolean':'xsd:boolean','Int':'xsd:int','Date':'xsd:date','Time':'xsd:time','Password':'xsd:byte','Decimal':'xsd:decimal','Interval':'xsd:duration'}OWL_OPENING_ROOT=u'''<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE rdf:RDF [ <!ENTITY owl "http://www.w3.org/2002/07/owl#" > <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >]> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns="http://logilab.org/owl/ontologies/%(appid)s#" xmlns:%(appid)s="http://logilab.org/owl/ontologies/%(appid)s#" xmlns:base="http://logilab.org/owl/ontologies/%(appid)s"> <owl:Ontology rdf:about=""> <rdfs:comment>%(appid)s Cubicweb OWL Ontology </rdfs:comment> </owl:Ontology>'''OWL_CLOSING_ROOT=u'</rdf:RDF>'DEFAULT_SKIP_RELS=frozenset(('is','is_instance_of','identity','owned_by','created_by'))classOWLView(StartupView):"""This view export in owl format schema database. It is the TBOX"""id='owl'title=_('owl')templatable=Falsecontent_type='application/xml'# 'text/xml'defcall(self,writeprefix=True):skipmeta=int(self.req.form.get('skipmeta',True))ifwriteprefix:self.w(OWL_OPENING_ROOT%{'appid':self.schema.name})self.visit_schema(skipmeta=skipmeta)ifwriteprefix:self.w(OWL_CLOSING_ROOT)defvisit_schema(self,skiprels=DEFAULT_SKIP_RELS,skipmeta=True):"""get a layout for a whole schema"""entities=sorted([eschemaforeschemainself.schema.entities()ifnoteschema.is_final()])ifskipmeta:entities=[eschemaforeschemainentitiesifnoteschema.meta]self.w(u'<!-- classes definition -->')foreschemainentities:self.visit_entityschema(eschema,skiprels)self.w(u'<!-- property definition -->')self.visit_property_schema(eschema,skiprels)self.w(u'<!-- datatype property -->')self.visit_property_object_schema(eschema)defvisit_entityschema(self,eschema,skiprels=()):"""get a layout for an entity OWL schema"""self.w(u'<owl:Class rdf:ID="%s">'%eschema)self.w(u'<!-- relations -->')forrschema,targetschemas,roleineschema.relation_definitions():ifrschema.typeinskiprels:continueifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueforoeschemaintargetschemas:label=rschema.typeifrole=='subject':card=rschema.rproperty(eschema,oeschema,'cardinality')[0]else:card=rschema.rproperty(oeschema,eschema,'cardinality')[1]cardtag=OWL_CARD_MAP[card]ifcardtag:self.w(u'''<rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="#%s"/>%s </owl:Restriction></rdfs:subClassOf>'''%(label,cardtag))self.w(u'<!-- attributes -->')forrschema,aschemaineschema.attribute_definitions():ifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueaname=rschema.typeifaname=='eid':continueself.w(u'''<rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="#%s"/> <rdf:type rdf:resource="&owl;FunctionalProperty"/> </owl:Restriction></rdfs:subClassOf>'''%aname)self.w(u'</owl:Class>')defvisit_property_schema(self,eschema,skiprels=()):"""get a layout for property entity OWL schema"""forrschema,targetschemas,roleineschema.relation_definitions():ifrschema.typeinskiprels:continueifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueforoeschemaintargetschemas:label=rschema.typeself.w(u'''<owl:ObjectProperty rdf:ID="%s"> <rdfs:domain rdf:resource="#%s"/> <rdfs:range rdf:resource="#%s"/></owl:ObjectProperty> '''%(label,eschema,oeschema.type))defvisit_property_object_schema(self,eschema):forrschema,aschemaineschema.attribute_definitions():ifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueaname=rschema.typeifaname=='eid':continueself.w(u'''<owl:DatatypeProperty rdf:ID="%s"> <rdfs:domain rdf:resource="#%s"/> <rdfs:range rdf:resource="%s"/></owl:DatatypeProperty>'''%(aname,eschema,OWL_TYPE_MAP[aschema.type]))classOWLABOXView(EntityView):'''This view represents a part of the ABOX for a given entity.'''id='owlabox'title=_('owlabox')templatable=Falsecontent_type='application/xml'# 'text/xml'defcall(self):self.w(OWL_OPENING_ROOT%{'appid':self.schema.name})self.wview('owl',None,writeprefix=False)foriinxrange(self.rset.rowcount):self.cell_call(i,0)self.w(OWL_CLOSING_ROOT)defcell_call(self,row,col,skiprels=DEFAULT_SKIP_RELS):self.wview('owlaboxitem',self.rset,row=row,col=col,skiprels=skiprels)classOWLABOXItemView(EntityView):'''This view represents a part of the ABOX for a given entity.'''id='owlaboxitem'templatable=Falsecontent_type='application/xml'# 'text/xml'defcell_call(self,row,col,skiprels=DEFAULT_SKIP_RELS):entity=self.complete_entity(row,col)eschema=entity.e_schemaself.w(u'<%s rdf:ID="%s">'%(eschema,entity.eid))self.w(u'<!--attributes-->')forrschema,aschemaineschema.attribute_definitions():ifrschema.typeinskiprels:continueifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueaname=rschema.typeifaname=='eid':continuetry:attr=entity.printable_value(aname,format='text/plain')ifattr:self.w(u'<%s>%s</%s>'%(aname,html_escape(attr),aname))exceptTransformError:passself.w(u'<!--relations -->')forrschema,targetschemas,roleineschema.relation_definitions():ifrschema.typeinskiprels:continueifnot(rschema.has_local_role('read')orrschema.has_perm(self.req,'read')):continueifrole=='object':attr='reverse_%s'%rschema.typeelse:attr=rschema.typeforxingetattr(entity,attr):self.w(u'<%s>%s%s</%s>'%(attr,x.id,x.eid,attr))self.w(u'</%s>'%eschema)