예제 #1
0
파일: pipe.py 프로젝트: HuaiQiu/ktbs
    def _prepare_source_and_params(computed_trace, diag):
        """I check and prepare the data required by the method.

        I return the unique source of the computed trace, and a dict of
        useful parameters converted to the expected datatype. If this can not
        be done, I return ``(None, None)``.

        I also populate `diag` with error/warning messages.
        """
        sources = computed_trace.source_traces
        params = computed_trace.parameters_as_dict
        critical = False

        if len(sources) != 1:
            msg = "Method ktbs:pipe expects exactly one source"
            LOG.error(msg)
            diag.append(msg)
            critical = True

        if "methods" not in params:
            msg = "Method ktbs:pipe requires parameter methods"
            LOG.error(msg)
            diag.append(msg)
            critical = True

        method_uris = [i for i in params['methods'].split(' ') if i != '']
        if len(method_uris) < 1:
            msg = "Method ktbs:pipe must have at least one method"
            LOG.error(msg)
            diag.append(msg)
            critical = True
        methods = []
        for uri in method_uris:
            m = factory(uri)
            if len(method_uris) < 1:
                msg = "Sub-method <{}> is not accessible".format(uri)
                LOG.error(msg)
                diag.append(msg)
                critical = True
            methods.append(m)
        method_params = [{} for i in methods]
        ret_params = {
            'methods_uris': method_uris,
            'methods': methods,
            'method_params': method_params,
        }

        if len(params) > 1:
            diag.append("WARN: Method ktbs:pipe does not support "
                        "additional parameters yet")
            # TODO implement a way to dispatch parameters to submethods

        if critical:
            return None, None
        else:
            return sources[0], ret_params
예제 #2
0
파일: parallel.py 프로젝트: HuaiQiu/ktbs
    def compute_trace_description(self, computed_trace):
        """I implement :meth:`.interface.IMethod.compute_trace_description`.
        """
        diag = Diagnosis("parallel.compute_trace_description")
        params = computed_trace.parameters_as_dict
        critical = False

        if len(computed_trace.source_traces) != 1:
            msg = "Method ktbs:parallel expects exactly one source"
            LOG.error(msg)
            diag.append(msg)
            critical = True

        critical, fusion_params = \
            _FUSION_IMPL._get_fusion_parameters(params, diag, critical)
        if "methods" not in params:
            msg = "Method ktbs:parallel requires parameter methods"
            LOG.error(msg)
            diag.append(msg)
            critical = True
        else:
            method_uris = [
                i for i in params.pop('methods').split(' ') if i != ''
            ]
            if len(method_uris) < 1:
                msg = "Method ktbs:parellel must have at least one method"
                LOG.error(msg)
                diag.append(msg)
                critical = True
            methods = []
            method_params = []
            for uri in method_uris:
                m = factory(uri)
                if len(method_uris) < 1:
                    msg = "Sub-method <{}> is not accessible".format(uri)
                    LOG.warning(msg)
                    diag.append("WARN: " + msg)
                else:
                    methods.append(m)
                    method_params.append({})
            parallel_params = {
                'methods_uris': method_uris,
                'methods': methods,
                'method_params': method_params,
            }
            if len(params) > 1:
                diag.append("Method ktbs:parallel does not support "
                            "additional parameters yet")
                # TODO implement a way to dispatch parameters to submethods

            if not critical:
                sources = self._prepare_intermediate_traces(
                    computed_trace, parallel_params)
                _FUSION_IMPL._do_compute_trace_description(
                    computed_trace, sources, fusion_params, diag)
        _FUSION_IMPL._init_cstate(computed_trace, diag)
예제 #3
0
파일: parallel.py 프로젝트: ktbs/ktbs
    def compute_trace_description(self, computed_trace):
        """I implement :meth:`.interface.IMethod.compute_trace_description`.
        """
        diag = Diagnosis("parallel.compute_trace_description")
        params = computed_trace.parameters_as_dict
        critical = False

        if len(computed_trace.source_traces) != 1:
            msg = "Method ktbs:parallel expects exactly one source"
            LOG.error(msg)
            diag.append(msg)
            critical = True

        critical, fusion_params = \
            _FUSION_IMPL._get_fusion_parameters(params, diag, critical)
        if "methods" not in params:
            msg = "Method ktbs:parallel requires parameter methods"
            LOG.error(msg)
            diag.append(msg)
            critical = True
        else:
            method_uris = [i for i in params.pop('methods').split(' ') if i != '']
            if len(method_uris) < 1:
                msg = "Method ktbs:parellel must have at least one method"
                LOG.error(msg)
                diag.append(msg)
                critical = True
            methods = []
            method_params = []
            for uri in method_uris:
                m = factory(uri)
                if len(method_uris) < 1:
                    msg = "Sub-method <{}> is not accessible".format(uri)
                    LOG.warning(msg)
                    diag.append("WARN: "+msg)
                else:
                    methods.append(m)
                    method_params.append({})
            parallel_params = {
                'methods_uris': method_uris,
                'methods': methods,
                'method_params': method_params,
            }
            if len(params) > 1:
                diag.append("Method ktbs:parallel does not support "
                            "additional parameters yet")
                # TODO implement a way to dispatch parameters to submethods

            if not critical:
                sources = self._prepare_intermediate_traces(computed_trace,
                                                            parallel_params)
                _FUSION_IMPL._do_compute_trace_description(computed_trace, sources,
                                                           fusion_params, diag)
        _FUSION_IMPL._init_cstate(computed_trace, diag)
예제 #4
0
파일: client.py 프로젝트: ktbs/ktbs
def get_ktbs(uri):
    """I return the root of a kTBS.

    :param basestring uri: the URI of this kTBS
    :rtype: :class:`ktbs.api.ktbs_root.KtbsRootMixin`

    This assumes that the kTBS already exists, either as a remore server or as
    a local service. If your goal is to *create* a local kTBS service, you must
    use :func:`ktbs.engine.service.make_ktbs` instead.
    """
    ret = factory(uri, [KTBS.KtbsRoot])
    assert isinstance(ret, ktbs.api.ktbs_root.KtbsRootMixin)
    return ret
예제 #5
0
def get_ktbs(uri):
    """I return the root of a kTBS.

    :param basestring uri: the URI of this kTBS
    :rtype: :class:`ktbs.api.ktbs_root.KtbsRootMixin`

    This assumes that the kTBS already exists, either as a remore server or as
    a local service. If your goal is to *create* a local kTBS service, you must
    use :func:`ktbs.engine.service.make_ktbs` instead.
    """
    ret = factory(uri, [KTBS.KtbsRoot])
    assert isinstance(ret, ktbs.api.ktbs_root.KtbsRootMixin)
    return ret
예제 #6
0
def parse_json(content, base_uri=None, encoding="utf-8", graph=None):
    """I parse RDF content from kTBS-specific JSON.

    See :func:`rdfrest.parse.parse_rdf_xml` for prototype
    documentation.
    """
    if graph is None:
        graph = Graph()
    if encoding.lower() != "utf-8":
        content = content.decode(encoding).encode("utf-8")
    try:
        json_data = loads(content)
        obsel_context = False
        if isinstance(json_data, list):
            # this is a list of obsels ; embed it in correct
            json_data = {
                "@id": base_uri,
                "obsels": json_data,
            }
            obsel_context = True
        elif json_data.get("@type") == "Base":
            json_data.setdefault(u"inRoot", unicode(base_uri))

        elif json_data.get("@type") in ("StoredTrace", "ComputedTrace",
                                        "DataGraph", "TraceModel", "Method"):
            json_data.setdefault(u"inBase", unicode(base_uri))

        elif "@graph" in json_data:
            # this is a TraceModel
            # @graph must be a non-empty list,
            # with the first item representing the trace model
            json_data["@graph"][0].setdefault(u"inBase", unicode(base_uri))
        elif ((json_data.get("hasObselList") is None)
              and (json_data.get("hasTraceStatistics") is None)
              and (json_data.get("hasBuiltinMethod") is None)):
            # must be an obsel
            obsel_context = True
            json_data.setdefault(u"hasTrace", unicode(base_uri))

        # add context if needed
        if "@context" not in json_data:
            if not obsel_context:
                json_data["@context"] = CONTEXT_URI
            else:
                model_uri = factory(base_uri, [KTBS.AbstractTrace]).model_uri
                if model_uri[-1] not in {"/", "#"}:
                    model_uri += "#"
                json_data["@context"] = [
                    CONTEXT_URI,
                    {
                        "m": unicode(model_uri)
                    },
                ]

        # ... then parse!
        normalized_json = normalize(json_data, pylod_options(base_uri))
        # Do not use "nt" as format as it works only with latin-1
        graph.parse(data=normalized_json, format="n3")

    except Exception, ex:
        raise ParseError(ex.message or str(ex))
예제 #7
0
def parse_json(content, base_uri=None, encoding="utf-8", graph=None):
    """I parse RDF content from kTBS-specific JSON.

    See :func:`rdfrest.parse.parse_rdf_xml` for prototype
    documentation.
    """
    if graph is None:
        graph = Graph()
    if encoding.lower() != "utf-8":
        content = content.decode(encoding).encode("utf-8")
    try:
        json_data = loads(content)
        obsel_context = False
        if isinstance(json_data, list):
            # this is a list of obsels ; embed it in correct
            json_data = {
                "@id": base_uri,
                "obsels": json_data,
            }
            obsel_context = True
        elif json_data.get("@type") == "Base":
            json_data.setdefault(u"inRoot", unicode(base_uri))

        elif json_data.get("@type") in ("StoredTrace",
                                        "ComputedTrace",
                                        "DataGraph",
                                        "TraceModel",
                                        "Method"):
            json_data.setdefault(u"inBase", unicode(base_uri))

        elif "@graph" in json_data:
            # this is a TraceModel
            # @graph must be a non-empty list,
            # with the first item representing the trace model
            json_data["@graph"][0].setdefault(u"inBase", unicode(base_uri))
        elif ((json_data.get("hasObselList") is None)
              and
              (json_data.get("hasTraceStatistics") is None)
              and
              (json_data.get("hasBuiltinMethod") is None)):
            # must be an obsel
            obsel_context = True
            json_data.setdefault(u"hasTrace", unicode(base_uri))

        # add context if needed
        if "@context" not in json_data:
            if not obsel_context:
                json_data["@context"] = CONTEXT_URI
            else:
                model_uri = factory(base_uri, [KTBS.AbstractTrace]).model_uri
                if model_uri[-1] not in { "/", "#" }:
                    model_uri += "#"
                json_data["@context"] = [
                    CONTEXT_URI,
                    { "m": unicode(model_uri) },
                ]


        # ... then parse!
        normalized_json = normalize(json_data, pylod_options(base_uri))
        # Do not use "nt" as format as it works only with latin-1
        graph.parse(data=normalized_json, format="n3")

    except Exception, ex:
        raise ParseError(ex.message or str(ex))