def mint_uri_from_label(label, target, uri=None, suffix=""): """ Mint a URI for a resource posted to `target` based on `label`. :param label: the label for the resource to create :param target: the resource "containing" the resource to create :param uri: if provided, will be used instead (must be fresh) :param suffix: if provided, will be added to the end of the URI :return: a URI not present in `target.state` :rtype: rdflib.URIRef :raise: InvalidDataError if `uri` is provided and not acceptable """ if uri is not None: uri = coerce_to_uri(uri, target.uri) if not check_new(target.state, uri): raise InvalidDataError("URI already in use <%s>" % uri) if not uri.startswith(target.uri): raise InvalidDataError( "URI is wrong <%s> (did you forget a leading '#'?)" % uri) else: label = label.lower() prefix = target.uri if prefix[-1] != "/": prefix = "%s#" % prefix prefix = "%s%s" % (prefix, _NON_ALPHA.sub("-", label)) uri = URIRef("%s%s" % (prefix, suffix)) if not check_new(target.state, uri): prefix = "%s-" % prefix uri = make_fresh_uri(target.state, prefix, suffix) return uri
def check_new_obs(uri): return check_new(_target_obsels, uri)
def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag): """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels """ if cstate['passed_maxtime'] and monotonicity is STRICT_MON: return source = computed_trace.source_traces[0] source_obsels = source.obsel_collection target_obsels = computed_trace.obsel_collection mintime = cstate["mintime"] maxtime = cstate["maxtime"] otypes = cstate["otypes"] bgp = cstate["bgp"] passed_maxtime = cstate["passed_maxtime"] last_seen = cstate["last_seen"] if otypes: otypes = set( URIRef(i) for i in otypes ) begin = mintime if monotonicity is NOT_MON: LOG.debug("non-monotonic %s", computed_trace) passed_maxtime = False last_seen = None target_obsels._empty() # friend #pylint: disable=W0212 elif monotonicity is STRICT_MON: LOG.debug("strictly temporally monotonic %s", computed_trace) if last_seen: begin = last_seen elif monotonicity is PSEUDO_MON: LOG.debug("pseudo temporally monotonic %s", computed_trace) if last_seen: begin = last_seen - source.get_pseudomon_range() else: LOG.debug("non-temporally monotonic %s", computed_trace) source_uri = source.uri target_uri = computed_trace.uri source_state = source_obsels.state source_triples = source_state.triples target_contains = target_obsels.state.__contains__ target_add_graph = target_obsels.add_obsel_graph check_new_obs = lambda uri, g=target_obsels.state: check_new(g, uri) with target_obsels.edit({"add_obsels_only":1}, _trust=True): for obs in source.iter_obsels(begin=begin, bgp=bgp, refresh="no"): last_seen = obs.begin if maxtime: if obs.end > maxtime: LOG.debug("--- passing maxtime on %s", obs) passed_maxtime = True break if otypes: obs_uri = obs.uri obs_state = obs.state for otype in otypes: if (obs_uri, RDF.type, otype) in obs_state: break else: # goes with the for (NOT the if) LOG.debug("--- dropping %s", obs) continue new_obs_uri = translate_node(obs.uri, computed_trace, source_uri, False) if target_contains((new_obs_uri, KTBS.hasTrace, target_uri)): LOG.debug("--- skipping %s", new_obs_uri) continue # already added LOG.debug("--- keeping %s", obs) 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, KTBS.hasSourceObsel, obs.uri)) for _, pred, obj in source_triples((obs.uri, None, None)): if pred == KTBS.hasTrace or pred == KTBS.hasSourceObsel: continue new_obj = translate_node(obj, computed_trace, source_uri, False, check_new_obs) if new_obj is None: continue # skip relations to nodes that are filtered out or not created yet new_obs_add((new_obs_uri, pred, new_obj)) for subj, pred, _ in source_triples((None, None, obs.uri)): if pred == KTBS.hasTrace or pred == KTBS.hasSourceObsel: continue new_subj = translate_node(subj, computed_trace, source_uri, False, check_new_obs) if new_subj is None: continue # skip relations from nodes that are filtered out or not created yet new_obs_add((new_subj, pred, new_obs_uri)) target_add_graph(new_obs_graph) cstate["passed_maxtime"] = passed_maxtime cstate["last_seen"] = last_seen
def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag): """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels """ if cstate['passed_maxtime'] and monotonicity is STRICT_MON: return source = computed_trace.source_traces[0] source_obsels = source.obsel_collection target_obsels = computed_trace.obsel_collection mintime = cstate["mintime"] maxtime = cstate["maxtime"] otypes = cstate["otypes"] if otypes: otypes = set(URIRef(i) for i in otypes) bgp = cstate["bgp"] passed_maxtime = cstate["passed_maxtime"] try: last_seen_u = cstate["last_seen_u"] if last_seen_u: last_seen_u = URIRef(last_seen_u) last_seen_b = cstate["last_seen_b"] except KeyError: # might be the cstate of an older version of 'filter' last_seen_b = cstate.pop("last_seen", None) last_seen_u = None if monotonicity is STRICT_MON: monotonicity = PSEUDO_MON # ensures a save recovery begin = mintime after = None if monotonicity is NOT_MON: LOG.debug("non-monotonic %s", computed_trace) passed_maxtime = False last_seen_u = last_seen_b = None target_obsels._empty() # friend #pylint: disable=W0212 elif monotonicity is STRICT_MON: LOG.debug("strictly temporally monotonic %s", computed_trace) if last_seen_u: after = last_seen_u elif monotonicity is PSEUDO_MON: LOG.debug("pseudo temporally monotonic %s", computed_trace) if last_seen_b is not None: begin = last_seen_b - source.get_pseudomon_range() else: LOG.debug("non-temporally monotonic %s", computed_trace) if otypes: filter_otypes = ', '.join(otype.n3() for otype in otypes) bgp = (bgp or '') + '''?obs a ?_filter_otype_. FILTER(?_filter_otype_ in (%s))''' % filter_otypes source_uri = source.uri target_uri = computed_trace.uri source_state = source_obsels.state target_contains = target_obsels.state.__contains__ target_add_graph = target_obsels.add_obsel_graph check_new_obs = lambda uri, g=target_obsels.state: check_new(g, uri) source_obsels = source.iter_obsels(after=after, begin=begin, end=maxtime, bgp=bgp, refresh="no") with target_obsels.edit({"add_obsels_only": 1}, _trust=True): for obs in source_obsels: new_obs_uri = translate_node(obs.uri, computed_trace, source_uri, False) if monotonicity is not STRICT_MON\ and target_contains((new_obs_uri, KTBS.hasTrace, target_uri)): LOG.debug("--- already seen %s", new_obs_uri) continue # already added LOG.debug("--- keeping %s", obs) new_obs_graph = copy_obsel( obs.uri, computed_trace, source, new_obs_uri=new_obs_uri, check_new_obs=check_new_obs, ) target_add_graph(new_obs_graph) for obs in source.iter_obsels(begin=begin, reverse=True, limit=1): # iter only once on the last obsel, if any last_seen_u = obs.uri last_seen_b = obs.begin passed_maxtime = (maxtime is not None and obs.end > maxtime) cstate["passed_maxtime"] = passed_maxtime if last_seen_u is not None: last_seen_u = unicode(last_seen_u) cstate["last_seen_u"] = last_seen_u cstate["last_seen_b"] = last_seen_b
def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag): """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels """ if cstate['passed_maxtime'] and monotonicity is STRICT_MON: return source = computed_trace.source_traces[0] source_obsels = source.obsel_collection target_obsels = computed_trace.obsel_collection mintime = cstate["mintime"] maxtime = cstate["maxtime"] otypes = cstate["otypes"] bgp = cstate["bgp"] passed_maxtime = cstate["passed_maxtime"] last_seen = cstate["last_seen"] if otypes: otypes = set(URIRef(i) for i in otypes) begin = mintime if monotonicity is NOT_MON: LOG.debug("non-monotonic %s", computed_trace) passed_maxtime = False last_seen = None target_obsels._empty() # friend #pylint: disable=W0212 elif monotonicity is STRICT_MON: LOG.debug("strictly temporally monotonic %s", computed_trace) if last_seen: begin = last_seen elif monotonicity is PSEUDO_MON: LOG.debug("pseudo temporally monotonic %s", computed_trace) if last_seen: begin = last_seen - source.get_pseudomon_range() else: LOG.debug("non-temporally monotonic %s", computed_trace) source_uri = source.uri target_uri = computed_trace.uri source_state = source_obsels.state source_triples = source_state.triples target_contains = target_obsels.state.__contains__ target_add_graph = target_obsels.add_obsel_graph check_new_obs = lambda uri, g=target_obsels.state: check_new(g, uri) with target_obsels.edit({"add_obsels_only": 1}, _trust=True): for obs in source.iter_obsels(begin=begin, bgp=bgp, refresh="no"): last_seen = obs.begin if maxtime: if obs.end > maxtime: LOG.debug("--- passing maxtime on %s", obs) passed_maxtime = True break if otypes: obs_uri = obs.uri obs_state = obs.state for otype in otypes: if (obs_uri, RDF.type, otype) in obs_state: break else: # goes with the for (NOT the if) LOG.debug("--- dropping %s", obs) continue new_obs_uri = translate_node(obs.uri, computed_trace, source_uri, False) if target_contains((new_obs_uri, KTBS.hasTrace, target_uri)): LOG.debug("--- skipping %s", new_obs_uri) continue # already added LOG.debug("--- keeping %s", obs) 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, KTBS.hasSourceObsel, obs.uri)) for _, pred, obj in source_triples((obs.uri, None, None)): if pred == KTBS.hasTrace or pred == KTBS.hasSourceObsel: continue new_obj = translate_node(obj, computed_trace, source_uri, False, check_new_obs) if new_obj is None: continue # skip relations to nodes that are filtered out or not created yet new_obs_add((new_obs_uri, pred, new_obj)) for subj, pred, _ in source_triples((None, None, obs.uri)): if pred == KTBS.hasTrace or pred == KTBS.hasSourceObsel: continue new_subj = translate_node(subj, computed_trace, source_uri, False, check_new_obs) if new_subj is None: continue # skip relations from nodes that are filtered out or not created yet new_obs_add((new_subj, pred, new_obs_uri)) target_add_graph(new_obs_graph) cstate["passed_maxtime"] = passed_maxtime cstate["last_seen"] = last_seen
def do_compute_obsels(self, computed_trace, cstate, monotonicity, diag): """I implement :meth:`.abstract.AbstractMonosourceMethod.do_compute_obsels """ if cstate['passed_maxtime'] and monotonicity is STRICT_MON: return source = computed_trace.source_traces[0] source_obsels = source.obsel_collection target_obsels = computed_trace.obsel_collection mintime = cstate["mintime"] maxtime = cstate["maxtime"] otypes = cstate["otypes"] if otypes: otypes = set( URIRef(i) for i in otypes ) bgp = cstate["bgp"] passed_maxtime = cstate["passed_maxtime"] try: last_seen_u = cstate["last_seen_u"] if last_seen_u: last_seen_u = URIRef(last_seen_u) last_seen_b = cstate["last_seen_b"] except KeyError: # might be the cstate of an older version of 'filter' last_seen_b = cstate.pop("last_seen", None) last_seen_u = None if monotonicity is STRICT_MON: monotonicity = PSEUDO_MON # ensures a save recovery begin = mintime after = None if monotonicity is NOT_MON: LOG.debug("non-monotonic %s", computed_trace) passed_maxtime = False last_seen_u = last_seen_b = None target_obsels._empty() # friend #pylint: disable=W0212 elif monotonicity is STRICT_MON: LOG.debug("strictly temporally monotonic %s", computed_trace) if last_seen_u: after = last_seen_u elif monotonicity is PSEUDO_MON: LOG.debug("pseudo temporally monotonic %s", computed_trace) if last_seen_b is not None: begin = last_seen_b - source.get_pseudomon_range() else: LOG.debug("non-temporally monotonic %s", computed_trace) if otypes: filter_otypes = ', '.join( otype.n3() for otype in otypes ) bgp = (bgp or '') + '''?obs a ?_filter_otype_. FILTER(?_filter_otype_ in (%s))''' % filter_otypes source_uri = source.uri target_uri = computed_trace.uri source_state = source_obsels.state target_contains = target_obsels.state.__contains__ target_add_graph = target_obsels.add_obsel_graph check_new_obs = lambda uri, g=target_obsels.state: check_new(g, uri) source_obsels = source.iter_obsels(after=after, begin=begin, end=maxtime, bgp=bgp, refresh="no") with target_obsels.edit({"add_obsels_only":1}, _trust=True): for obs in source_obsels: new_obs_uri = translate_node(obs.uri, computed_trace, source_uri, False) if monotonicity is not STRICT_MON\ and target_contains((new_obs_uri, KTBS.hasTrace, target_uri)): LOG.debug("--- already seen %s", new_obs_uri) continue # already added LOG.debug("--- keeping %s", obs) new_obs_graph = copy_obsel(obs.uri, computed_trace, source, new_obs_uri=new_obs_uri, check_new_obs=check_new_obs, ) target_add_graph(new_obs_graph) for obs in source.iter_obsels(begin=begin, reverse=True, limit=1): # iter only once on the last obsel, if any last_seen_u = obs.uri last_seen_b = obs.begin passed_maxtime = (maxtime is not None and obs.end > maxtime) cstate["passed_maxtime"] = passed_maxtime if last_seen_u is not None: last_seen_u = unicode(last_seen_u) cstate["last_seen_u"] = last_seen_u cstate["last_seen_b"] = last_seen_b
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 bgps = cstate["bgps"] last_seen_u = cstate["last_seen_u"] if last_seen_u: last_seen_u = URIRef(last_seen_u) last_seen_b = cstate["last_seen_b"] begin = None after = None if monotonicity is NOT_MON: LOG.debug("non-monotonic %s", computed_trace) last_seen_u = last_seen_b = None target_obsels._empty() # friend #pylint: disable=W0212 elif monotonicity is STRICT_MON: LOG.debug("strictly temporally monotonic %s", computed_trace) if last_seen_u: after = last_seen_u elif monotonicity is PSEUDO_MON: LOG.debug("pseudo temporally monotonic %s", computed_trace) if last_seen_b is not None: begin = last_seen_b - source.get_pseudomon_range() else: LOG.debug("non-temporally monotonic %s", computed_trace) source_uri = source.uri target_uri = computed_trace.uri source_state = source_obsels.state target_contains = target_obsels.state.__contains__ target_add_graph = target_obsels.add_obsel_graph check_new_obs = lambda uri, g=target_obsels.state: check_new(g, uri) inserted = set() with target_obsels.edit({"add_obsels_only": 1}, _trust=True): for _rank, new_type, bgp in bgps: new_type = URIRef(new_type) select = source_obsels.build_select(after=after, bgp=bgp) query_str = "PREFIX ktbs: <%s#> %s" % (KTBS_NS_URI, select) tuples = list(source_obsels.state.query(query_str)) for obs_uri, in tuples: new_obs_uri = translate_node(obs_uri, computed_trace, source_uri, False) if monotonicity is PSEUDO_MON\ and target_contains((new_obs_uri, KTBS.hasTrace, target_uri))\ or new_obs_uri in inserted: # could be either because of pseudo-monotony, # or because a BGP with higher priority already matched LOG.debug("--- already seen %s", new_obs_uri) continue # already added new_obs_graph = copy_obsel( obs_uri, computed_trace, source, new_obs_uri=new_obs_uri, check_new_obs=check_new_obs, ) new_obs_graph.set((new_obs_uri, RDF.type, new_type)) target_add_graph(new_obs_graph) inserted.add(new_obs_uri) for obs in source.iter_obsels(begin=begin, reverse=True, limit=1): # iter only once on the last obsel, if any last_seen_u = obs.uri last_seen_b = obs.begin if last_seen_u is not None: last_seen_u = unicode(last_seen_u) cstate["last_seen_u"] = last_seen_u cstate["last_seen_b"] = last_seen_b