def create_model(self, id=None, parents=None, label=None, graph=None): """Create a new model in this trace base. :param id: see :ref:`ktbs-resource-creation` :param parents: either None, or an iterable of models from which this model inherits :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.tace_model.TraceModelMixin`:class: """ # redefining built-in 'id' #pylint: disable-msg=W0622 trust = graph is None and id is None node = coerce_to_node(id, self.uri) if parents is None: parents = () # abstract API allows None if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.TraceModel)) if label: graph.add((node, SKOS.prefLabel, Literal(label))) for parent in parents: parent = coerce_to_uri(parent, self.uri) graph.add((node, KTBS.hasParentModel, parent)) uris = self.post_graph(graph, None, trust, node, KTBS.TraceModel) assert len(uris) == 1 return self.factory(uris[0], [KTBS.TraceModel])
def create_stored_trace(self, id=None, model=None, origin=None, default_subject=None, label=None, graph=None): """Create a new store trace in this trace base. :param id: see :ref:`ktbs-resource-creation` :param model: mode of the trace to create (required) :param origin: Typically a timestamp. It can be an opaque string, meaning that the precise time when the trace was collected is not known :param default_subject: The subject to set to new obsels when they do not specifify a subject :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.trace.StoredTraceMixin`:class: """ # redefining built-in 'id' #pylint: disable=W0622 # We somehow duplicate StoredTrace.complete_new_graph and # StoredTrace.check_new_graph here, but this is required if we want to # be able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if model is None: raise ValueError("model is mandatory") trust = graph is None and id is None node = coerce_to_node(id, self.uri) model = coerce_to_uri(model, self.uri) origin_isoformat = getattr(origin, "isoformat", None) if origin_isoformat: origin = origin_isoformat() if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.StoredTrace)) graph.add((node, KTBS.hasModel, model)) graph.add((node, KTBS.hasOrigin, Literal(origin))) if default_subject is not None: if not isinstance(default_subject, URIRef): default_subject = Literal(default_subject) graph.add((node, KTBS.hasDefaultSubject, default_subject)) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.StoredTrace) assert len(uris) == 1 return self.factory(uris[0], [KTBS.StoredTrace])
def create_method(self, id=None, parent=None, parameters=None, label=None, graph=None): """Create a new computed trace in this trace base. :param id: see :ref:`ktbs-resource-creation` :param parent: parent method (required) :param parameters: method parameters :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.method.MethodMixin`:class: """ # redefining built-in 'id' #pylint: disable-msg=W0622 # We somehow duplicate Method.check_new_graph here, but this is # required if we want to be able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if parent is None: raise ValueError("parent is mandatory") trust = graph is None and id is None node = coerce_to_node(id, self.uri) parent = coerce_to_uri(parent, self.uri) if parameters is None: parameters = {} if trust: if parent.startswith(self.uri): if not (parent, RDF.type, KTBS.Method) in self.state: raise InvalidDataError("Parent <%s> is not a Method" % parent) else: trust = False # could be built-in, let impl/server check if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.Method)) graph.add((node, KTBS.hasParentMethod, parent)) for key, value in parameters.iteritems(): if "=" in key: raise ValueError("Parameter name can not contain '=': %s", key) graph.add( (node, KTBS.hasParameter, Literal(u"%s=%s" % (key, value)))) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.Method) assert len(uris) == 1 return self.factory(uris[0], [KTBS.Method])
def create_method(self, id=None, parent=None, parameters=None, label=None, graph=None): """Create a new computed trace in this trace base. :param id: see :ref:`ktbs-resource-creation` :param parent: parent method (required) :param parameters: method parameters :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.method.MethodMixin`:class: """ # redefining built-in 'id' #pylint: disable-msg=W0622 # We somehow duplicate Method.check_new_graph here, but this is # required if we want to be able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if parent is None: raise ValueError("parent is mandatory") trust = graph is None and id is None node = coerce_to_node(id, self.uri) parent = coerce_to_uri(parent, self.uri) if parameters is None: parameters = {} if trust: if parent.startswith(self.uri): if not (parent, RDF.type, KTBS.Method) in self.state: raise InvalidDataError("Parent <%s> is not a Method" % parent) else: trust = False # could be built-in, let impl/server check if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.Method)) graph.add((node, KTBS.hasParentMethod, parent)) for key, value in parameters.iteritems(): if "=" in key: raise ValueError("Parameter name can not contain '=': %s", key) graph.add((node, KTBS.hasParameter, Literal(u"%s=%s" % (key, value)))) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.Method) assert len(uris) == 1 return self.factory(uris[0], [KTBS.Method])
def create_base(self, id=None, label=None, graph=None): """Create a new base in this kTBS. :param id: see :ref:`ktbs-resource-creation` :param label: TODO DOC explain :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.base.BaseMixin`:class: """ # redefining built-in 'id' #pylint: disable-msg=W0622 trust = graph is None and id is None node = coerce_to_node(id, self.uri) if graph is None: graph = Graph() graph.add((self.uri, KTBS.hasBase, node)) graph.add((node, RDF.type, KTBS.Base)) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.Base) assert len(uris) == 1 return self.factory(uris[0], [KTBS.Base])
def create_obsel(self, id=None, type=None, begin=None, end=None, subject=None, attributes=None, relations=None, inverse_relations=None, source_obsels=None, label=None, no_return=False): """ Creates a new obsel for the stored trace. :param id: see :ref:`ktbs-resource-creation`. :param type: Obsel type, defined by the Trace model. :param begin: Begin timestamp of the obsel, can be an int. :param end: End timestamp of the obsel, can be an int. :param subject: Subject of the obsel. :param attributes: explain. :param relations: explain. :param inverse_relations: explain. :param source_obsels: explain. :param label: explain. :param no_return: if True, None will be returned instead of the created obsek; this saves time and (in the case of a remote kTBS) network traffic :rtype: `ktbs.client.obsel.Obsel` """ # redefining built-in 'id' #pylint: disable=W0622 # We somehow duplicate Obsel.complete_new_graph and # Obsel.check_new_graph here, but this is required if we want to be # able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if type is None: raise ValueError("type is mandatory for obsel creation") if begin is None: begin = _NOW(UTC) if end is None: end = begin if subject is None: subject = self.get_default_subject() elif not isinstance(subject, URIRef): subject = Literal(subject) trust = False # TODO SOON decide if we can trust anything # this would imply verifying that begin and end are mutually consistent # knowing that they could be heterogeneous (int/datetime) graph = Graph() obs = coerce_to_node(id, self.uri) # return BNode if id is None type_uri = coerce_to_uri(type, self.uri) graph.add((obs, RDF.type, type_uri)) graph.add((obs, KTBS.hasTrace, self.uri)) if isinstance(begin, Integral): graph.add((obs, KTBS.hasBegin, Literal(int(begin)))) else: # will use KTBS.hasBeginDT begin_dt = begin if isinstance(begin, basestring): begin_dt = parse_date(begin_dt) elif isinstance(begin, datetime): if begin.tzinfo is None: begin = begin.replace(tzinfo=UTC) else: raise ValueError("Could not interpret begin %s", begin) graph.add((obs, KTBS.hasBeginDT, Literal(begin_dt))) if isinstance(end, Integral): graph.add((obs, KTBS.hasEnd, Literal(int(end)))) else: # will use KTBS.hasEndDT end_dt = end if isinstance(end_dt, basestring): end_dt = parse_date(end_dt) elif isinstance(end, datetime): if end.tzinfo is None: end = end.replace(tzinfo=UTC) else: raise ValueError("Could not interpret end %s", end) graph.add((obs, KTBS.hasEndDT, Literal(end_dt))) if subject is not None: graph.add((obs, KTBS.hasSubject, subject)) if attributes is not None: for key, val in attributes.items(): k_uri = coerce_to_uri(key) if isinstance(val, Node): v_node = val else: v_node = Literal(val) # TODO LATER do something if val is a list graph.add((obs, k_uri, v_node)) if relations is not None: for rtype, other in relations: rtype_uri = coerce_to_uri(rtype) other_uri = coerce_to_uri(other) graph.add((obs, rtype_uri, other_uri)) if inverse_relations is not None: for other, rtype in inverse_relations: other_uri = coerce_to_uri(other) rtype_uri = coerce_to_uri(rtype) graph.add((other_uri, rtype_uri, obs)) if source_obsels is not None: for src in source_obsels: s_uri = coerce_to_uri(src) graph.add((obs, KTBS.hasSourceObsel, s_uri)) if label is not None: graph.add((obs, RDFS.label, Literal(label))) uris = self.post_graph(graph, None, trust, obs, KTBS.Obsel) assert len(uris) == 1 self.obsel_collection.force_state_refresh() if not no_return: ret = self.factory(uris[0], [KTBS.Obsel]) assert isinstance(ret, ObselMixin) return ret
def create_computed_trace(self, id=None, method=None, parameters=None, sources=None, label=None, graph=None): """Create a new computed trace in this trace base. :param id: see :ref:`ktbs-resource-creation` :param method: method to apply for computation (required) :param parameters: parameters of the method :param sources: source traces to which the method is applied :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.trace.ComputedTraceMixin`:class: """ # redefining built-in 'id' #pylint: disable=W0622 # We somehow duplicate ComputedTrace.complete_new_graph and # ComputedTrace.check_new_graph here, but this is required if we want to # be able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if method is None: raise ValueError("method is mandatory") trust = graph is None and id is None node = coerce_to_node(id, self.uri) method = coerce_to_uri(method, self.uri) if sources is None: sources = () else: sources = [coerce_to_uri(i, self.uri) for i in sources] if parameters is None: parameters = {} if trust: # we need to check some integrity constrains, # because the graph may be blindly trusted if method.startswith(self.uri): if not (method, RDF.type, KTBS.Method) in self.state: raise InvalidDataError("<%s> is not a Method" % method) else: trust = False # could be built-in, let impl/server check for src in sources: if self.state.value(src, RDF.type) not in (KTBS.StoredTrace, KTBS.ComputedTrace): raise ValueError( "Source <%s> is not a trace of this base" % src) if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.ComputedTrace)) graph.add((node, KTBS.hasMethod, method)) for src in sources: graph.add((node, KTBS.hasSource, src)) for key, value in parameters.iteritems(): if "=" in key: raise ValueError("Parameter name can not contain '=': %s", key) graph.add( (node, KTBS.hasParameter, Literal(u"%s=%s" % (key, value)))) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.ComputedTrace) assert len(uris) == 1 return self.factory(uris[0], [KTBS.ComputedTrace])
def create_computed_trace(self, id=None, method=None, parameters=None, sources=None, label=None, graph=None): """Create a new computed trace in this trace base. :param id: see :ref:`ktbs-resource-creation` :param method: method to apply for computation (required) :param parameters: parameters of the method :param sources: source traces to which the method is applied :param label: explain. :param graph: see :ref:`ktbs-resource-creation` :rtype: `~.trace.ComputedTraceMixin`:class: """ # redefining built-in 'id' #pylint: disable=W0622 # We somehow duplicate ComputedTrace.complete_new_graph and # ComputedTrace.check_new_graph here, but this is required if we want to # be able to set _trust=True below. # Furthermore, the signature of this method makes it significantly # easier to produce a valid graph, so there is a benefit to this # duplication. if method is None: raise ValueError("method is mandatory") trust = graph is None and id is None node = coerce_to_node(id, self.uri) method = coerce_to_uri(method, self.uri) if sources is None: sources = () else: sources = [ coerce_to_uri(i, self.uri) for i in sources ] if parameters is None: parameters = {} if trust: # we need to check some integrity constrains, # because the graph may be blindly trusted if method.startswith(self.uri): if not (method, RDF.type, KTBS.Method) in self.state: raise InvalidDataError("<%s> is not a Method" % method) else: trust = False # could be built-in, let impl/server check for src in sources: if self.state.value(src, RDF.type) not in (KTBS.StoredTrace, KTBS.ComputedTrace): raise ValueError("Source <%s> is not a trace of this base" % src) if graph is None: graph = Graph() graph.add((self.uri, KTBS.contains, node)) graph.add((node, RDF.type, KTBS.ComputedTrace)) graph.add((node, KTBS.hasMethod, method)) for src in sources: graph.add((node, KTBS.hasSource, src)) for key, value in parameters.iteritems(): if "=" in key: raise ValueError("Parameter name can not contain '=': %s", key) graph.add((node, KTBS.hasParameter, Literal(u"%s=%s" % (key, value)))) if label: graph.add((node, SKOS.prefLabel, Literal(label))) uris = self.post_graph(graph, None, trust, node, KTBS.ComputedTrace) assert len(uris) == 1 return self.factory(uris[0], [KTBS.ComputedTrace])