Beispiel #1
0
 def get_streams_with_defaults(self, streams):
     streams_with_defaults = []
     for s in streams:
         stream_name = s.stream
         if not stream_name:
             stream_name = self.get_default_stream(s.name)
         streams_with_defaults.append(
             mltypes.MLStreamType(s.name, stream_name))
     return streams_with_defaults
Beispiel #2
0
    def _preselect(self, selected, candidates):
        '''
        _preselect - select all the streams that doesn't have multiple contexts

        The method modifies the lists in place, moving elements from 'candidates' into 'selected'.
        The streams that have multiple contexts will remain in 'candidates' for backtracking later.

        :param selected: the map of selected elements
        :param candidates: the remaining candidate pool map
        '''
        # Pool of contexts explored so far
        ctx_pool = selected[:]
        ctx_pool.extend(candidates)

        # Process stack: extended as dependencies are explored
        stack = candidates[:]
        candidates.clear()

        while stack:
            s = stack.pop()

            if len(self._indexer._get_contexts_for_stream(s[0],
                                                          ctx_pool)) == 1:
                # There is only a single context for this stream so we'll pick it
                if s not in selected:
                    self._indexer.add_group(s, selected)
                deps = s[0].get_dependencies()[0]
                dep_mods = [
                    m for m in deps.get_runtime_modules()
                    if m not in RESERVED_STREAMS
                ]
                for m in dep_mods:
                    try:
                        dep_str = deps.get_runtime_streams(m)[0]
                    except:
                        # No stream specified. Any stream will do
                        if next(
                            (c[0]
                             for c in ctx_pool if c[0].get_module_name() == m),
                                False):
                            # Already has an alternative in the pool
                            continue
                        dep_str = None

                    # Add the dependencies to the stack for further processing
                    dep_ctx = self._proc.get_stream_contexts(
                        mltypes.MLStreamType(m, dep_str))
                    for c in dep_ctx:
                        self._indexer.add(c, stack)
                        self._indexer.add(c, ctx_pool)
            else:
                # Multiple contexts available for the stream.
                # Will be later resolved with backtracking
                if s not in candidates:
                    self._indexer.add_group(s, candidates)
Beispiel #3
0
    def pick_stream(self, s_type: mltypes.MLStreamType):
        if s_type.name in MLLibmodProc.RESERVED_STREAMS:
            # Reserved streams are always enabled
            return

        if self._is_stream_enabled(s_type):
            enabled_stream = self._enabled_stream_modules[s_type.name]
            if enabled_stream.get_stream_name() == s_type.stream:
                # Stream already enabled, nothing to do
                return
            else:
                # Another stream of the same module already enabled
                conflicting = [
                    mltypes.MLStreamType(s_type.name, enabled_stream.get_stream_name()).to_obj(),
                    s_type.to_obj()
                ]
                raise mlerrcode.MlConflictingStreams("Conflicting streams").set_data("streams", conflicting)

        all_deps = set()  # type: ignore
        allContexts = self.get_stream_contexts(s_type=s_type)
        if not allContexts:
            raise mlerrcode.MlModuleNotFound("Module not found").set_data("streams", [s_type.to_obj()])
        for c in allContexts:
            all_deps = all_deps.union(self.get_stream_dependencies(c))

        enabledDeps = []
        for d in all_deps:
            enabledDeps.append((d, self.get_actual_stream(d)))

        for ctx in allContexts:
            currDeps = self.get_dep_streams(ctx)
            if all(i in enabledDeps for i in currDeps):
                for dstream in currDeps:
                    self.pick_stream(s_type=mltypes.MLStreamType(name=dstream[0], streamname=dstream[1]))

                self.enable_stream(ctx)
                self._streams.append(ctx)
            else:
                # Return s_type
                raise mlerrcode.MlDependencyResolutionError("Dependencies cannot be resolved").set_data("streams", [s_type.to_obj()])
Beispiel #4
0
    def _get_deps(self, ctx):
        '''
        _get_deps - get the list of contexts of the first-level dependencies for a specified stream context

        :param ctx: a context object
        '''
        all_deps = []
        dep_mods = self._indexer.get_dep_streams(ctx)
        for (module, stream) in dep_mods:
            all_deps.extend(
                self._proc.get_stream_contexts(
                    mltypes.MLStreamType(module, stream)))
        return all_deps
Beispiel #5
0
    def get_default_stream(self, name: str):
        if self._mod_index is None:
            raise mlerrcode.MlGeneralException("Module index not found")

        module = self._mod_index.get_module(name)

        if not module:
            raise mlerrcode.MlModuleNotFound(
                "Module {} not found".format(name)).set_data(
                    "streams", [mltypes.MLStreamType(name, "").to_obj()])

        defaults = module.get_defaults()
        if defaults:
            return defaults.get_default_stream()

        return module.get_all_streams()[0].get_stream_name()
Beispiel #6
0
    def _get_deps(self, ctx):
        '''
        _get_deps - get the list of contexts of the first-level dependencies for a specified stream context

        :param ctx: a context object
        '''
        all_deps = []
        deps = ctx.get_dependencies()[0]
        dep_mods = [
            m for m in deps.get_runtime_modules() if m not in RESERVED_STREAMS
        ]
        for m in dep_mods:
            try:
                stream = deps.get_runtime_streams(m)[0]
            except:
                # No stream specified. Any stream will do
                stream = None
            all_deps.extend(
                self._proc.get_stream_contexts(mltypes.MLStreamType(m,
                                                                    stream)))
        return all_deps
Beispiel #7
0
 def pick_default_stream(self, s_type: mltypes.MLStreamType):
     s_type = mltypes.MLStreamType(s_type.name, self.get_default_stream(s_type.name))
     self.pick_stream(s_type=s_type)