Ejemplo n.º 1
0
def get_result_value(result, column):
    #====================================
    """
  Use a type information returned from a SPARQL query to cast a variable's
  value to an appropriate Python class.

  :param result (dict): A dictionary containing a result row as converted
    from a JSON formatted result set.
  :param column (str): The name of a variable in the result set.
  :return: The `value` field, cast according to the `type` and `datatype` fields.
  """
    from biosignalml.utils import isoduration_to_seconds
    NONE = {'value': '', 'type': 'literal', 'datatype': ''}
    r = result.get(column, NONE)
    rtype = r['type']
    value = r['value']
    if rtype == 'uri':
        return rdf.Uri(value)
    elif rtype == 'bnode':
        return rdf.BlankNode(value)
    elif rtype == 'typed-literal':
        dt = r['datatype']
        if dt == 'http://www.w3.org/2001/XMLSchema#dayTimeDuration':
            return isoduration_to_seconds(value)
        elif dt == 'http://www.w3.org/2001/XMLSchema#integer':
            return int(value)
        ## Extend...
    return value
Ejemplo n.º 2
0
 def get_metadata(self, uri):
     #---------------------------
     try:
         graph = rdf.Graph.create_from_resource(self._md_uri + str(uri),
                                                rdf.Format.RDFXML)
         if uri: graph.uri = rdf.Uri(uri)
         return graph
     except Exception, msg:
         raise IOError("Cannot get RDF for '%s' (%s)" % (uri, msg))
Ejemplo n.º 3
0
 def __init__(self, uri, metadata=None, **kwds):
 #----------------------------------------------
   self.metadata = AbstractObject.set_attributes(self, **kwds)
   '''Dictionary of property values with names not in :attr:`attributes` list.'''
   if metadata is not None:
     self.metadata.update(AbstractObject.set_attributes(self, **metadata))
   self.uri = (uri     if isinstance(uri, rdf.Uri) or uri is None  # None ==> Blank node
          else uri.uri if isinstance(uri, rdf.Node) and uri.is_resource()
          else rdf.Uri(str(uri).strip()))
   self.node = rdf.BlankNode() if uri is None else rdf.Resource(self.uri)
   self.graph = None
Ejemplo n.º 4
0
  def get_resources(self, rtype, rvars='?r', condition='',
                                 group=None, prefixes=None, graph=None, order=None, resource=None):
  #------------------------------------------------------------------------------------------------
    """
    Find resources of the given type and the most recent graphs that hold them.

    The resources found can be restricted by an optional SPARQL graph pattern.

    :param rtype: The type of resource to find. The SPARQL variable used for the
       resource is the first of the `rvars`.
    :param str rvars: Space separated SPARQL variables identifying the resource
       in the query along with any other variables to return values of. The first variable
       is usually that of the resource. Optional, defaults to `?r` unless `resource` is given.
    :param str condition: A SPARQL graph pattern for selecting resources. Optional.
    :param str group: Variables to group the results by. Optional.
    :param dict prefixes: Optional namespace prefixes for the SPARQL query.
    :param graph: The URI of a specific graph to search in, instead of finding
       the most recent. Optional.
    :param str order: The sort order of the SPARQL query. Optional.
    :param str resource: The SPARQL variable of the resource, if not in the `rvars` list.
       Optional.
    :return: A list of (graph_uri, resource_uri, optional_variable) tuples.
    :rtype: list[tuple(:class:`~biosignalml.rdf.Uri`, :class:`~biosignalml.rdf.Uri`)]
    """
    pfxdict = dict(bsml=BSML.prefix, prv=PRV.prefix)
    if prefixes: pfxdict.update(prefixes)
    varlist = [ var for var in rvars.split() if var[0] == '?' ]
    retvars = [ var[1:] for var in varlist ]
    if resource is None: resource = varlist[0]
    gv = rdf.sparqlstore.get_result_value   ## Shorten code
    NOVALUE = { 'value': None }  # For optional result variables
    if order is None: order = ' '.join(varlist)
    if graph is None:
      return [ (gv(r, 'g'), gv(r, retvars[0])) + tuple([gv(r, v) for v in retvars[1:]])
        for r in self.select('?g %(rvars)s',
          '''graph <%(pgraph)s> { ?g a <%(gtype)s> MINUS { [] prv:precededBy ?g }}
             graph ?g { { %(res)s a <%(rtype)s> . %(cond)s } }''',
          params=dict(pgraph=self._provenance_uri, gtype=self._graphtype,
                      res=resource, rtype=rtype, rvars=rvars, cond=condition),
          prefixes=pfxdict,
          group=group,
          order='?g %s' % order)
        ]
    else:
      return [ (rdf.Uri(str(graph)), gv(r, retvars[0])) + tuple([gv(r, v) for v in retvars[1:]])
        for r in self.select('%(rvars)s', '{ %(res)s a <%(rtype)s> . %(cond)s }',
          params=dict(res=resource, rtype=rtype, rvars=rvars, cond=condition),
          prefixes=pfxdict,
          graph=graph,
          order=order)
        ]
Ejemplo n.º 5
0
import sys
import biosignalml.rdf as rdf
from biosignalml.rdf.sparqlstore import Virtuoso


if __name__ == "__main__":
#=========================

  if len(sys.argv) < 4:
    print "Usage: %s store graph_uri graph_source [format]" % sys.argv[0]
    exit(1)
  if len(sys.argv) == 5: format = rdf.Format.format(sys.argv[4])
  else:                  format = rdf.Format.RDFXML

  store = Virtuoso(sys.argv[1])
  graph = rdf.Graph.create_from_resource(rdf.Uri(sys.argv[3]), format, base=rdf.Uri(sys.argv[3]))
  ##print graph.serialise(format=rdf.Format.TURTLE)
  store.extend_graph(sys.argv[2], graph.serialise(format=rdf.Format.RDFXML), format=rdf.Format.RDFXML)
Ejemplo n.º 6
0
if __name__ == '__main__':
    #=========================

    logging.basicConfig(format='%(asctime)s: %(message)s')
    logging.getLogger().setLevel('DEBUG')

    if len(sys.argv) < 3:
        print "Usage: %s REPOSITORY_URI BIOSIGNALML_HDF5_FILE" % sys.argv[0]
        sys.exit(1)

    store = BSMLUpdateStore(
        sys.argv[1],
        #                     FourStore('http://localhost:8083'))
        Virtuoso('http://localhost:8890'))

    rec = H5Recording.open(sys.argv[2])

    statements, format = rec.get_metadata()
    if statements is None: raise ValueError("No metadata in source file")

    # rec.dataset = ....  From repo? Parameter?? Default to filename's full path??
    #                     Move/rename/copy file ??
    dataset = rdf.Uri('file://' + os.path.abspath(sys.argv[2]))

    graph = rdf.Graph.create_from_string(rec.uri, statements, format)
    graph.set_subject_property(rec.uri, BSML.dataset, dataset)

    graph_uri = store.add_recording_graph(
        rec.uri, graph.serialise(), 'file://' + os.path.abspath(__file__))
    print '<%s> stored in <%s>' % (rec.uri, graph_uri)
Ejemplo n.º 7
0
 def __init__(self, base_uri, graphtype, sparqlstore):
 #----------------------------------------------------
   self.uri = rdf.Uri(base_uri)
   self._graphtype = graphtype
   self._sparqlstore = sparqlstore
   self._provenance_uri = self.uri + PROVENANCE_PATH