Ejemplo n.º 1
0
    def get_annotations(self):
        #-------------------------
        """
    Return all Annotations about the recording.

    :rtype: list of bsml:Annotations
    """
        sparql = """PREFIX bsml: <%(bsml)s>
                PREFIX rdfs: <%(rdfs)s>
                PREFIX dct: <%(dct)s>
                PREFIX prv: <%(prv)s>
                PREFIX tl: <%(tl)s>

                select ?ann ?about ?stype ?comment ?tag ?created
                       ?source ?tm ?start ?duration ?timeline
                where {
                    ?ann a bsml:Annotation
                    minus { [] prv:precededBy ?ann }
                       {
                         ?ann dct:subject ?about .
                         ?about a ?stype .
                         optional { ?ann rdfs:comment ?comment }
                         optional { ?ann dct:created ?created }
                         optional { ?ann bsml:tag ?tag }
                         { ?ann dct:subject <%(subject)s> }
                         union
                         { ?about a bsml:Segment ;
                             dct:source <%(subject)s> ;
                             dct:source ?source ;
                             bsml:time ?tm .
                             ?tm tl:timeline ?timeline .
                           { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration }
                           union
                           { ?tm a bsml:Instant ; tl:at ?start }
                         }
                       }
                     }""" % dict(subject=self._rec_uri,
                                 bsml=BSML.prefix,
                                 rdfs=RDFS.prefix,
                                 dct=DCT.prefix,
                                 prv=PRV.prefix,
                                 tl=TL.prefix)
        anns = [[
            str(a[0].uri),
            str(a[1].uri),
            str(a[2]), a[3].value, a[5].value,
            str(a[6].uri),
            str(a[7].uri), a[8].value,
            None if a[9] in ['', None] else a[9].value,
            str(a[10].uri)
        ] for a in self.query(sparql)]
        anns.sort(key=operator.itemgetter(7, 1, 0))  # start, about, uri
        annotations = []
        for a in anns:
            ann = Annotation(a[0], about=a[1], comment=a[3], created=a[4])
            if a[2] == str(BSML.Segment):
                time = TemporalEntity.create(a[6], a[7], a[8], timeline=a[9])
                ann.about = Segment(a[1], source=a[5], time=time)
            annotations.append(ann)
        return annotations
Ejemplo n.º 2
0
    def get_annotations(self, subject, graph_uri=None):
        #--------------------------------------------------
        """
    Return all Annotations about a subject.

    This is significantly faster (approx 15x) then getting a list of URIs
    (fast) and then creating Annotations (slow).

    Code could be used as a template for general abstract object creation
    based on PropertyMap and bypassing internal use of RDF -- instead map
    directly to and from SPARQL.

    :param subject: The URI of the subject.
    :param graph_uri: An optional URI of the graph to query.
    :rtype: list of bsml:Annotations
    """
        anns = self.get_resources(
            BSML.Annotation,
            rvars=
            '?ann ?about ?stype ?comment ?tag ?created ?source ?tm ?start ?duration ?timeline',
            condition='''minus { [] prv:precededBy ?ann }
                       {
                         ?ann dct:subject ?about .
                         ?about a ?stype .
                         optional { ?ann rdfs:comment ?comment }
                         optional { ?ann dct:created ?created }
                         optional { ?ann bsml:tag ?tag }
                         { ?ann dct:subject <%(subject)s> }
                         union
                         { ?about a bsml:Segment ;
                             dct:source <%(subject)s> ;
                             dct:source ?source ;
                             bsml:time ?tm .
                             ?tm tl:timeline ?timeline .
                           { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration }
                           union
                           { ?tm a bsml:Instant ; tl:at ?start }
                         }
                       }''' % dict(subject=subject),
            prefixes=dict(bsml=BSML.prefix,
                          dct=DCT.prefix,
                          prv=PRV.prefix,
                          tl=TL.prefix),
            graph=graph_uri,
            #       order = '?start',  ## Virtuoso doesn't correctly sort xsd:dayTimeDuration
        )
        anns.sort(key=operator.itemgetter(9, 2, 1))  # start, about, uri
        annotations = []
        for a in anns:
            ann = Annotation(a[1], about=a[2], comment=a[4], created=a[6])
            if str(a[3]) == str(BSML.Segment):
                time = TemporalEntity.create(
                    a[8],
                    float(a[9]),
                    duration=None if a[10] in ['', None] else float(a[10]),
                    timeline=a[11])
                ann.about = Segment(a[2], source=a[7], time=time)
            annotations.append(ann)
        return annotations
Ejemplo n.º 3
0
  r.save_metadata_to_graph(g)

  s = MyRecording.create_from_graph('http://example.org/uri1', g)
  assert(r.metadata_as_string(rdf.Format.TURTLE) == s.metadata_as_string(rdf.Format.TURTLE))
  s.comment='From graph'

  user = '******'

  a1 = 'http://example.org/annotation/1'
  a2 = 'http://example.org/annotation/2'
  a3 = 'http://example.org/annotation/3'

  t1 = 'http://example.org/onto#tag1'
  t2 = 'http://example.org/onto#tag2'
  t3 = 'http://example.org/onto#tag3'
  a = Annotation.Note(a1, s.uri, user, 'A test recording...')
  b = Annotation.Tag(a2, s.uri, user, t1)
  c = Annotation(a3, s.uri, user, tags=[t2, t3], text='Multiple tags')

  #print a.metadata_as_string(rdf.Format.TURTLE)
  #print b.metadata_as_string(rdf.Format.TURTLE)
  #print c.metadata_as_string(rdf.Format.TURTLE)

  c.save_metadata_to_graph(g)
  d = Annotation.create_from_graph(a3, g)
  assert(c.metadata_as_string(rdf.Format.TURTLE) == d.metadata_as_string(rdf.Format.TURTLE))

  s.metadata['zz'] = [ a, b, c ]
  print s.metadata_as_string(rdf.Format.TURTLE)