web/views/owl.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1263 01152fffd593
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
    12 from cubicweb.web.action import Action
    12 from cubicweb.web.action import Action
    13 from cubicweb.selectors import none_rset, match_view
    13 from cubicweb.selectors import none_rset, match_view
    14 
    14 
    15 _ = unicode
    15 _ = unicode
    16 
    16 
    17 OWL_CARD_MAP = {'1': '<rdf:type rdf:resource="&owl;FunctionalProperty"/>',                      
    17 OWL_CARD_MAP = {'1': '<rdf:type rdf:resource="&owl;FunctionalProperty"/>',
    18                 '?': '<owl:maxCardinality rdf:datatype="&xsd;int">1</owl:maxCardinality>',
    18                 '?': '<owl:maxCardinality rdf:datatype="&xsd;int">1</owl:maxCardinality>',
    19                 '+': '<owl:minCardinality rdf:datatype="&xsd;int">1</owl:minCardinality>',
    19                 '+': '<owl:minCardinality rdf:datatype="&xsd;int">1</owl:minCardinality>',
    20                 '*': ''
    20                 '*': ''
    21                 }
    21                 }
    22 
    22 
    35 
    35 
    36 OWL_OPENING_ROOT = u'''<?xml version="1.0" encoding="UTF-8"?>
    36 OWL_OPENING_ROOT = u'''<?xml version="1.0" encoding="UTF-8"?>
    37 <!DOCTYPE rdf:RDF [
    37 <!DOCTYPE rdf:RDF [
    38         <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    38         <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    39         <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    39         <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    40 ]>        
    40 ]>
    41 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    41 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    42     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    42     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    43     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    43     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    44     xmlns:owl="http://www.w3.org/2002/07/owl#"
    44     xmlns:owl="http://www.w3.org/2002/07/owl#"
    45     xmlns="http://logilab.org/owl/ontologies/%(appid)s#"
    45     xmlns="http://logilab.org/owl/ontologies/%(appid)s#"
    46     xmlns:%(appid)s="http://logilab.org/owl/ontologies/%(appid)s#"
    46     xmlns:%(appid)s="http://logilab.org/owl/ontologies/%(appid)s#"
    47     xmlns:base="http://logilab.org/owl/ontologies/%(appid)s">
    47     xmlns:base="http://logilab.org/owl/ontologies/%(appid)s">
    48 
    48 
    49   <owl:Ontology rdf:about="">
    49   <owl:Ontology rdf:about="">
    50     <rdfs:comment>
    50     <rdfs:comment>
    51     %(appid)s Cubicweb OWL Ontology                           
    51     %(appid)s Cubicweb OWL Ontology
    52     </rdfs:comment>
    52     </rdfs:comment>
    53   </owl:Ontology>'''
    53   </owl:Ontology>'''
    54 
    54 
    55 OWL_CLOSING_ROOT = u'</rdf:RDF>'
    55 OWL_CLOSING_ROOT = u'</rdf:RDF>'
    56 
    56 
    69         if writeprefix:
    69         if writeprefix:
    70             self.w(OWL_OPENING_ROOT % {'appid': self.schema.name})
    70             self.w(OWL_OPENING_ROOT % {'appid': self.schema.name})
    71         self.visit_schema(skipmeta=skipmeta)
    71         self.visit_schema(skipmeta=skipmeta)
    72         if writeprefix:
    72         if writeprefix:
    73             self.w(OWL_CLOSING_ROOT)
    73             self.w(OWL_CLOSING_ROOT)
    74         
    74 
    75     def visit_schema(self, skiprels=DEFAULT_SKIP_RELS, skipmeta=True):
    75     def visit_schema(self, skiprels=DEFAULT_SKIP_RELS, skipmeta=True):
    76         """get a layout for a whole schema"""
    76         """get a layout for a whole schema"""
    77         entities = sorted([eschema for eschema in self.schema.entities()
    77         entities = sorted([eschema for eschema in self.schema.entities()
    78                            if not eschema.is_final()])
    78                            if not eschema.is_final()])
    79         if skipmeta:
    79         if skipmeta:
    87             self.w(u'<!-- datatype property -->')
    87             self.w(u'<!-- datatype property -->')
    88             self.visit_property_object_schema(eschema)
    88             self.visit_property_object_schema(eschema)
    89 
    89 
    90     def visit_entityschema(self, eschema, skiprels=()):
    90     def visit_entityschema(self, eschema, skiprels=()):
    91         """get a layout for an entity OWL schema"""
    91         """get a layout for an entity OWL schema"""
    92         self.w(u'<owl:Class rdf:ID="%s">'% eschema)         
    92         self.w(u'<owl:Class rdf:ID="%s">'% eschema)
    93         self.w(u'<!-- relations -->')    
    93         self.w(u'<!-- relations -->')
    94         for rschema, targetschemas, role in eschema.relation_definitions():
    94         for rschema, targetschemas, role in eschema.relation_definitions():
    95             if rschema.type in skiprels:
    95             if rschema.type in skiprels:
    96                 continue
    96                 continue
    97             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
    97             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
    98                 continue
    98                 continue
   111  </owl:Restriction>
   111  </owl:Restriction>
   112 </rdfs:subClassOf>
   112 </rdfs:subClassOf>
   113 ''' % (label, cardtag))
   113 ''' % (label, cardtag))
   114 
   114 
   115         self.w(u'<!-- attributes -->')
   115         self.w(u'<!-- attributes -->')
   116               
   116 
   117         for rschema, aschema in eschema.attribute_definitions():
   117         for rschema, aschema in eschema.attribute_definitions():
   118             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   118             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   119                 continue
   119                 continue
   120             aname = rschema.type
   120             aname = rschema.type
   121             if aname == 'eid':
   121             if aname == 'eid':
   123             self.w(u'''<rdfs:subClassOf>
   123             self.w(u'''<rdfs:subClassOf>
   124   <owl:Restriction>
   124   <owl:Restriction>
   125    <owl:onProperty rdf:resource="#%s"/>
   125    <owl:onProperty rdf:resource="#%s"/>
   126    <rdf:type rdf:resource="&owl;FunctionalProperty"/>
   126    <rdf:type rdf:resource="&owl;FunctionalProperty"/>
   127   </owl:Restriction>
   127   </owl:Restriction>
   128 </rdfs:subClassOf>'''                         
   128 </rdfs:subClassOf>'''
   129                    % aname)
   129                    % aname)
   130         self.w(u'</owl:Class>')
   130         self.w(u'</owl:Class>')
   131     
   131 
   132     def visit_property_schema(self, eschema, skiprels=()):
   132     def visit_property_schema(self, eschema, skiprels=()):
   133         """get a layout for property entity OWL schema"""
   133         """get a layout for property entity OWL schema"""
   134         for rschema, targetschemas, role in eschema.relation_definitions():
   134         for rschema, targetschemas, role in eschema.relation_definitions():
   135             if rschema.type in skiprels:
   135             if rschema.type in skiprels:
   136                 continue
   136                 continue
   139             for oeschema in targetschemas:
   139             for oeschema in targetschemas:
   140                 label = rschema.type
   140                 label = rschema.type
   141                 self.w(u'''<owl:ObjectProperty rdf:ID="%s">
   141                 self.w(u'''<owl:ObjectProperty rdf:ID="%s">
   142  <rdfs:domain rdf:resource="#%s"/>
   142  <rdfs:domain rdf:resource="#%s"/>
   143  <rdfs:range rdf:resource="#%s"/>
   143  <rdfs:range rdf:resource="#%s"/>
   144 </owl:ObjectProperty>                                                
   144 </owl:ObjectProperty>
   145 ''' % (label, eschema, oeschema.type))
   145 ''' % (label, eschema, oeschema.type))
   146 
   146 
   147     def visit_property_object_schema(self, eschema):
   147     def visit_property_object_schema(self, eschema):
   148         for rschema, aschema in eschema.attribute_definitions():
   148         for rschema, aschema in eschema.attribute_definitions():
   149             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   149             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   155   <rdfs:domain rdf:resource="#%s"/>
   155   <rdfs:domain rdf:resource="#%s"/>
   156   <rdfs:range rdf:resource="%s"/>
   156   <rdfs:range rdf:resource="%s"/>
   157 </owl:DatatypeProperty>'''
   157 </owl:DatatypeProperty>'''
   158                    % (aname, eschema, OWL_TYPE_MAP[aschema.type]))
   158                    % (aname, eschema, OWL_TYPE_MAP[aschema.type]))
   159 
   159 
   160             
   160 
   161 class OWLABOXView(EntityView):
   161 class OWLABOXView(EntityView):
   162     '''This view represents a part of the ABOX for a given entity.'''
   162     '''This view represents a part of the ABOX for a given entity.'''
   163     
   163 
   164     id = 'owlabox'
   164     id = 'owlabox'
   165     title = _('owlabox')
   165     title = _('owlabox')
   166     templatable = False
   166     templatable = False
   167     content_type = 'application/xml' # 'text/xml'
   167     content_type = 'application/xml' # 'text/xml'
   168     
   168 
   169     def call(self):
   169     def call(self):
   170         self.w(OWL_OPENING_ROOT % {'appid': self.schema.name})
   170         self.w(OWL_OPENING_ROOT % {'appid': self.schema.name})
   171         for i in xrange(self.rset.rowcount):
   171         for i in xrange(self.rset.rowcount):
   172             self.cell_call(i, 0)
   172             self.cell_call(i, 0)
   173         self.w(OWL_CLOSING_ROOT)
   173         self.w(OWL_CLOSING_ROOT)
   174 
   174 
   175     def cell_call(self, row, col, skiprels=DEFAULT_SKIP_RELS):
   175     def cell_call(self, row, col, skiprels=DEFAULT_SKIP_RELS):
   176         self.wview('owlaboxitem', self.rset, row=row, col=col, skiprels=skiprels)
   176         self.wview('owlaboxitem', self.rset, row=row, col=col, skiprels=skiprels)
   177 
   177 
   178         
   178 
   179 class OWLABOXItemView(EntityView):
   179 class OWLABOXItemView(EntityView):
   180     '''This view represents a part of the ABOX for a given entity.'''    
   180     '''This view represents a part of the ABOX for a given entity.'''
   181     id = 'owlaboxitem'
   181     id = 'owlaboxitem'
   182     templatable = False
   182     templatable = False
   183     content_type = 'application/xml' # 'text/xml'
   183     content_type = 'application/xml' # 'text/xml'
   184 
   184 
   185     def cell_call(self, row, col, skiprels=DEFAULT_SKIP_RELS):
   185     def cell_call(self, row, col, skiprels=DEFAULT_SKIP_RELS):
   206             if rschema.type in skiprels:
   206             if rschema.type in skiprels:
   207                 continue
   207                 continue
   208             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   208             if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')):
   209                 continue
   209                 continue
   210             if role == 'object':
   210             if role == 'object':
   211                 attr = 'reverse_%s' % rschema.type 
   211                 attr = 'reverse_%s' % rschema.type
   212             else:
   212             else:
   213                 attr = rschema.type        
   213                 attr = rschema.type
   214             for x in getattr(entity, attr):
   214             for x in getattr(entity, attr):
   215                 self.w(u'<%s>%s %s</%s>' % (attr, x.id, x.eid, attr))
   215                 self.w(u'<%s>%s %s</%s>' % (attr, x.id, x.eid, attr))
   216         self.w(u'</%s>'% eschema)
   216         self.w(u'</%s>'% eschema)
   217 
   217 
   218 
   218 
   219 class DownloadOWLSchemaAction(Action):
   219 class DownloadOWLSchemaAction(Action):
   220     id = 'download_as_owl'
   220     id = 'download_as_owl'
   221     __select__ = none_rset() & match_view('schema')
   221     __select__ = none_rset() & match_view('schema')
   222     
   222 
   223     category = 'mainactions'
   223     category = 'mainactions'
   224     title = _('download schema as owl')
   224     title = _('download schema as owl')
   225    
   225 
   226     def url(self):
   226     def url(self):
   227         return self.build_url('view', vid='owl')
   227         return self.build_url('view', vid='owl')
   228