示例#1
0
文件: fsa.py 项目: ktbs/ktbs
    def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag):
        """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels
        """
        source = computed_trace.source_traces[0]
        source_obsels = source.obsel_collection
        target_obsels = computed_trace.obsel_collection
        last_seen = cstate["last_seen"]

        fsa = FSA(cstate['fsa'], False) # do not check structure again
        if fsa.default_matcher is None:
            #fsa.default_matcher = "obseltype"              # <- doesn't work in fsa4streams v0.4
            fsa._structure['default_matcher'] = "obseltype" # <- workaround
        fsa.source = source
        fsa.target = computed_trace
        fsa.source_obsels_graph = source_obsels.state

        if monotonicity is STRICT_MON:
            LOG.debug("strictly temporally monotonic %s, reloading state", computed_trace)
            tokens = cstate['tokens']
            if tokens:
                fsa.load_tokens_from_dict(tokens)
        else:
            LOG.debug("NOT strictly temporally monotonic %s, restarting", computed_trace)
            passed_maxtime = False
            last_seen = None
            target_obsels._empty() # friend #pylint: disable=W0212

        source_uri = source.uri
        source_model_uri = source.model_uri
        source_state = source_obsels.state
        source_value = source_state.value
        target_uri = computed_trace.uri
        target_model_uri = computed_trace.model_uri
        target_add_graph = target_obsels.add_obsel_graph
        after = last_seen and URIRef(last_seen)

        with target_obsels.edit({"add_obsels_only":1}, _trust=True):
            for obs in source.iter_obsels(after=after, refresh="no"):
                last_seen = event = unicode(obs.uri)
                matching_tokens = fsa.feed(event, obs.end)
                for i, token in enumerate(matching_tokens):
                    state = KtbsFsaState(fsa, token['state'],
                                         source_model_uri, target_model_uri)
                    source_obsels = [ URIRef(uri) for uri in token['history_events']]
                    otype_uri = state.get_obsel_type()
                    LOG.debug("matched {} -> {}".format(source_obsels[-1], otype_uri))
                    if otype_uri is None:
                        continue

                    new_obs_uri = translate_node(source_obsels[-1], computed_trace,
                                                 source_uri, False)
                    if i > 0:
                        new_obs_uri = URIRef("{}-{}".format(new_obs_uri, i))
                    new_obs_graph = Graph()
                    new_obs_add = new_obs_graph.add
                    new_obs_add((new_obs_uri, KTBS.hasTrace, target_uri))
                    new_obs_add((new_obs_uri, RDF.type, otype_uri))
                    new_obs_add((new_obs_uri, KTBS.hasBegin, source_value(source_obsels[0], KTBS.hasBegin)))
                    new_obs_add((new_obs_uri, KTBS.hasEnd, source_value(source_obsels[-1], KTBS.hasEnd)))
                    for source_obsel in source_obsels:
                        new_obs_add((new_obs_uri, KTBS.hasSourceObsel, source_obsel))

                    attributes = state.get_attributes()
                    if attributes:
                        qvars = []
                        qwhere = [ '?obs <{}> ?end .'.format(KTBS.hasEnd) ]
                        for i, triple in enumerate(attributes):
                            var = '?v{}'.format(i)
                            qvars.append(var)
                            qwhere.append('OPTIONAL {{?obs <{}> {} .}}'
                                          .format(triple[1], var))
                        query = ('SELECT {} {{'
                                 '\nVALUES (?obs) {{\n(<{}>)\n}}'
                                 '\n{}\n'
                                 '}} ORDER BY ?end')\
                            .format(
                            ' '.join(qvars),
                            '>)\n(<'.join(source_obsels),
                            '\n'.join(qwhere),
                            )
                        results = source_state.query(query)
                        for i, triple in enumerate(attributes):
                            target_attr, _, aggr_func = triple
                            try:
                                val = aggr_func(results, i)
                                if val is not None:
                                    new_obs_add((new_obs_uri, target_attr, val))
                            except Exception, ex:
                                LOG.warn(ex.message)



                    target_add_graph(new_obs_graph)
示例#2
0
文件: fsa.py 项目: HuaiQiu/ktbs
    def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag):
        """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels
        """
        source = computed_trace.source_traces[0]
        source_obsels = source.obsel_collection
        target_obsels = computed_trace.obsel_collection
        last_seen = cstate["last_seen"]

        fsa = FSA(cstate['fsa'], False)  # do not check structure again
        if fsa.default_matcher is None:
            #fsa.default_matcher = "obseltype"              # <- doesn't work in fsa4streams v0.4
            fsa._structure['default_matcher'] = "obseltype"  # <- workaround
        fsa.source = source
        fsa.target = computed_trace
        fsa.source_obsels_graph = source_obsels.state

        if monotonicity is STRICT_MON:
            LOG.debug("strictly temporally monotonic %s, reloading state",
                      computed_trace)
            tokens = cstate['tokens']
            if tokens:
                fsa.load_tokens_from_dict(tokens)
        else:
            LOG.debug("NOT strictly temporally monotonic %s, restarting",
                      computed_trace)
            passed_maxtime = False
            last_seen = None
            target_obsels._empty()  # friend #pylint: disable=W0212

        source_uri = source.uri
        source_model_uri = source.model_uri
        source_state = source_obsels.state
        source_value = source_state.value
        target_uri = computed_trace.uri
        target_model_uri = computed_trace.model_uri
        target_add_graph = target_obsels.add_obsel_graph
        after = last_seen and URIRef(last_seen)

        with target_obsels.edit({"add_obsels_only": 1}, _trust=True):
            for obs in source.iter_obsels(after=after, refresh="no"):
                last_seen = event = unicode(obs.uri)
                matching_tokens = fsa.feed(event, obs.end)
                for i, token in enumerate(matching_tokens):
                    state = KtbsFsaState(fsa, token['state'], source_model_uri,
                                         target_model_uri)
                    source_obsels = [
                        URIRef(uri) for uri in token['history_events']
                    ]
                    otype_uri = state.get_obsel_type()
                    LOG.debug("matched {} -> {}".format(
                        source_obsels[-1], otype_uri))
                    if otype_uri is None:
                        continue

                    new_obs_uri = translate_node(source_obsels[-1],
                                                 computed_trace, source_uri,
                                                 False)
                    if i > 0:
                        new_obs_uri = URIRef("{}-{}".format(new_obs_uri, i))
                    new_obs_graph = Graph()
                    new_obs_add = new_obs_graph.add
                    new_obs_add((new_obs_uri, KTBS.hasTrace, target_uri))
                    new_obs_add((new_obs_uri, RDF.type, otype_uri))
                    new_obs_add((new_obs_uri, KTBS.hasBegin,
                                 source_value(source_obsels[0],
                                              KTBS.hasBegin)))
                    new_obs_add((new_obs_uri, KTBS.hasEnd,
                                 source_value(source_obsels[-1], KTBS.hasEnd)))
                    for source_obsel in source_obsels:
                        new_obs_add(
                            (new_obs_uri, KTBS.hasSourceObsel, source_obsel))

                    attributes = state.get_attributes()
                    if attributes:
                        qvars = []
                        qwhere = ['?obs <{}> ?end .'.format(KTBS.hasEnd)]
                        for i, triple in enumerate(attributes):
                            var = '?v{}'.format(i)
                            qvars.append(var)
                            qwhere.append('OPTIONAL {{?obs <{}> {} .}}'.format(
                                triple[1], var))
                        query = ('SELECT {} {{'
                                 '\nVALUES (?obs) {{\n(<{}>)\n}}'
                                 '\n{}\n'
                                 '}} ORDER BY ?end')\
                            .format(
                            ' '.join(qvars),
                            '>)\n(<'.join(source_obsels),
                            '\n'.join(qwhere),
                            )
                        results = source_state.query(query)
                        for i, triple in enumerate(attributes):
                            target_attr, _, aggr_func = triple
                            try:
                                val = aggr_func(results, i)
                                if val is not None:
                                    new_obs_add(
                                        (new_obs_uri, target_attr, val))
                            except Exception, ex:
                                LOG.warn(ex.message)

                    target_add_graph(new_obs_graph)