Пример #1
0
    def make_instance(self, instance_name: str = None, **slots) -> Instance:
        """Make a new Instance from this Class.

        Equivalent to the CLIPS (make-instance) function.

        """
        builder = environment_builder(self._env, 'instance')
        ret = lib.IBSetDefclass(builder, lib.DefclassName(self._ptr()))
        if ret != lib.IBE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)

        for slot, slot_val in slots.items():
            value = clips.values.clips_value(self._env, value=slot_val)

            ret = lib.IBPutSlot(builder, str(slot).encode(), value)
            if ret != PutSlotError.PSE_NO_ERROR:
                raise PUT_SLOT_ERROR[ret](slot)

        instance = lib.IBMake(
            builder,
            instance_name.encode() if instance_name is not None else ffi.NULL)
        if instance != ffi.NULL:
            return Instance(self._env, instance)
        else:
            raise CLIPSError(self._env, code=lib.FBError(self._env))
Пример #2
0
def classes(env: ffi.CData, names: (list, tuple)) -> iter:
    for name in names:
        defclass = lib.FindDefclass(env, name.encode())
        if defclass == ffi.NULL:
            raise CLIPSError(env)

        yield Class(env, name)
Пример #3
0
    def _ptr(self) -> ffi.CData:
        cls = lib.FindDefclass(self._env, self._cls)
        if cls == ffi.NULL:
            raise CLIPSError(self._env,
                             'Class <%s> not defined' % self._cls.decode())

        return cls
Пример #4
0
    def load(self, path: str, binary: bool = False):
        """Load a set of constructs into the CLIPS data base.

        If constructs were saved in binary format,
        the binary parameter should be set to True.

        Equivalent to the CLIPS (load) function.

        """
        if binary:
            if not lib.Bload(self._env, path.encode()):
                raise CLIPSError(self._env)
        else:
            ret = lib.Load(self._env, path.encode())
            if ret != lib.LE_NO_ERROR:
                raise CLIPSError(self._env, code=ret)
Пример #5
0
    def _ptr(self) -> ffi.CData:
        dfc = lib.FindDefinstances(self._env, self._name)
        if dfc == ffi.NULL:
            raise CLIPSError(self._env,
                             'DefinedInstances <%s> not defined' % self.name)

        return dfc
Пример #6
0
    def _ptr(self) -> ffi.CData:
        dfc = lib.FindDeffunction(self._env, self._name)
        if dfc == ffi.NULL:
            raise CLIPSError(self._env,
                             'Function <%s> not defined' % self.name)

        return dfc
Пример #7
0
def slot_value(env: ffi.CData, ist: ffi.CData, slot: str) -> type:
    value = clips.values.clips_value(env)

    ret = lib.DirectGetSlot(ist, slot.encode(), value)
    if ret != lib.GSE_NO_ERROR:
        raise CLIPSError(env, code=ret)

    return clips.values.python_value(env, value)
Пример #8
0
    def unmake(self):
        """This method is equivalent to delete except that it uses
        message-passing instead of directly deleting the instance.

        """
        ret = lib.UnmakeInstance(self._ist)
        if ret != lib.UIE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)
Пример #9
0
    def remove_breakpoint(self):
        """Remove a breakpoint for the Rule.

        Equivalent to the CLIPS (remove-break) function.

        """
        if not lib.RemoveBreak(self._env, self._ptr()):
            raise CLIPSError("No breakpoint set")
Пример #10
0
    def clear(self):
        """Clear the CLIPS environment.

        Equivalent to the CLIPS (clear) function.

        """
        if not lib.Clear(self._env):
            raise CLIPSError(self._env)
Пример #11
0
    def reset(self):
        """Reset the CLIPS environment.

        Equivalent to the CLIPS (reset) function.

        """
        if lib.Reset(self._env):
            raise CLIPSError(self._env)
Пример #12
0
    def batch_star(self, path: str):
        """Evaluate the commands contained in the specific path.

        Equivalent to the CLIPS (batch*) function.

        """
        if lib.BatchStar(self._env, path.encode()) != 1:
            raise CLIPSError(self._env)
Пример #13
0
    def build(self, construct: str):
        """Build a single construct in CLIPS.

        Equivalent to the CLIPS (build) function.

        """
        ret = lib.Build(self._env, construct.encode())
        if ret != lib.BE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)
Пример #14
0
    def undefine(self):
        """Undefine the Class.

        Equivalent to the CLIPS (undefclass) command.

        The object becomes unusable after this method has been called.

        """
        if not lib.Undefclass(self._ptr(), self._env):
            raise CLIPSError(self._env)
Пример #15
0
    def undefine(self):
        """Undefine the DefinedInstances.

        Equivalent to the CLIPS (undefinstances) function.

        The object becomes unusable after this method has been called.

        """
        if not lib.Undefinstances(self._ptr(), self._env):
            raise CLIPSError(self._env)
Пример #16
0
    def undefine(self):
        """Undefine the MessageHandler.

        Equivalent to the CLIPS (undefmessage-handler) function.

        The object becomes unusable after this method has been called.

        """
        if not lib.UndefmessageHandler(self._ptr(), self._idx, self._env):
            raise CLIPSError(self._env)
Пример #17
0
    def find_message_handler(
            self,
            name: str,
            handler_type: str = 'primary') -> 'MessageHandler':
        """Returns the MessageHandler given its name and type."""
        ident = lib.FindDefmessageHandler(self._ptr(), name.encode(),
                                          handler_type.encode())
        if ident == 0:
            raise CLIPSError(self._env)

        return MessageHandler(self._env, self.name, ident)
Пример #18
0
    def default_value(self) -> type:
        """The default value for this Slot.

        Equivalent to the CLIPS (slot-default-value) function.

        """
        value = clips.values.clips_value(self._env)

        if lib.SlotDefaultValue(self._ptr(), self._name, value):
            return clips.values.python_value(self._env, value)
        else:
            raise CLIPSError(self._env)
Пример #19
0
    def cardinality(self) -> tuple:
        """A tuple containing the cardinality for this Slot.

        Equivalent to the CLIPS slot-cardinality function.

        """
        value = clips.values.clips_value(self._env)

        if lib.SlotCardinality(self._ptr(), self._name, value):
            return clips.values.python_value(self._env, value)
        else:
            raise CLIPSError(self._env)
Пример #20
0
    def sources(self) -> tuple:
        """A tuple containing the names of the Class sources for this Slot.

        Equivalent to the CLIPS (slot-sources) function.

        """
        value = clips.values.clips_value(self._env)

        if lib.SlotSources(self._ptr(), self._name, value):
            return clips.values.python_value(self._env, value)
        else:
            raise CLIPSError(self._env)
Пример #21
0
    def range(self) -> tuple:
        """A tuple containing the numeric range for this Slot.

        Equivalent to the CLIPS (slot-range) function.

        """
        value = clips.values.clips_value(self._env)

        if lib.SlotRange(self._ptr(), self._name, value):
            return clips.values.python_value(self._env, value)
        else:
            raise CLIPSError(self._env)
Пример #22
0
    def allowed_values(self) -> tuple:
        """A tuple containing the allowed values for this Slot.

        Equivalent to the CLIPS (slot-allowed-values) function.

        """
        value = clips.values.clips_value(self._env)

        if lib.SlotAllowedValues(self._ptr(), self._name, value):
            return clips.values.python_value(self._env, value)
        else:
            raise CLIPSError(self._env)
Пример #23
0
    def restore_instances(self, instances: str) -> int:
        """Restore a set of instances into the CLIPS data base.

        Equivalent to the CLIPS (restore-instances) function.

        Instances can be passed as a set of strings or as a file.

        """
        instances = instances.encode()

        if os.path.exists(instances):
            ret = lib.RestoreInstances(self._env, instances)
            if ret == -1:
                raise CLIPSError(self._env)
        else:
            ret = lib.RestoreInstancesFromString(self._env, instances,
                                                 len(instances))
            if ret == -1:
                raise CLIPSError(self._env)

        return ret
Пример #24
0
    def eval(self, expression: str) -> type:
        """Evaluate an expression returning its value.

        Equivalent to the CLIPS (eval) function.

        """
        value = clips.values.clips_value(self._env)

        ret = lib.Eval(self._env, expression.encode(), value)
        if ret != lib.EE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)

        return clips.values.python_value(self._env, value)
Пример #25
0
    def modify_slots(self, **slots):
        """Modify one or more slot values of the Instance.

        Instance must exist within the CLIPS engine.

        Equivalent to the CLIPS (modify-instance) function.

        """
        modifier = environment_modifier(self._env, 'instance')
        ret = lib.IMSetInstance(modifier, self._ist)
        if ret != lib.IME_NO_ERROR:
            raise CLIPSError(self._env, code=ret)

        for slot, slot_val in slots.items():
            value = clips.values.clips_value(self._env, value=slot_val)

            ret = lib.IMPutSlot(modifier, str(slot).encode(), value)
            if ret != PutSlotError.PSE_NO_ERROR:
                raise PUT_SLOT_ERROR[ret](slot)

        if lib.IMModify(modifier) is ffi.NULL:
            raise CLIPSError(self._env, code=lib.IMError(self._env))
Пример #26
0
    def save(self, path: str, binary=False):
        """Save a set of constructs into the CLIPS data base.

        If binary is True, the constructs will be saved in binary format.

        Equivalent to the CLIPS (load) function.

        """
        if binary:
            ret = lib.Bsave(self._env, path.encode())
        else:
            ret = lib.Save(self._env, path.encode())
        if ret == 0:
            raise CLIPSError(self._env)
Пример #27
0
    def call(self, function: str, *arguments) -> type:
        """Call the CLIPS function with the given arguments."""
        value = clips.values.clips_value(self._env)
        builder = environment_builder(self._env, 'function')

        lib.FCBReset(builder)
        for argument in arguments:
            lib.FCBAppend(builder,
                          clips.values.clips_value(self._env, value=argument))

        ret = lib.FCBCall(builder, function.encode(), value)
        if ret != lib.FCBE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)

        return clips.values.python_value(self._env, value)
Пример #28
0
    def _assert_is_active(self):
        """As the engine does not provide means to find activations,
        the existence of the pointer in the activations list is tested instead.

        """
        activations = []
        activation = lib.GetNextActivation(self._env, ffi.NULL)

        while activation != ffi.NULL:
            activations.append(activation)
            activation = lib.GetNextActivation(self._env, activation)

        if self._act not in activations:
            raise CLIPSError(self._env,
                             "Activation %s not in the agenda" % self.name)
Пример #29
0
    def error_state(self) -> Union[None, CLIPSError]:
        """Get the CLIPS environment error state.

        Equivalent to the CLIPS (get-error) function.

        """
        value = clips.values.clips_udf_value(self._env)

        lib.GetErrorFunction(self._env, ffi.NULL, value)
        state = clips.values.python_value(self._env, value)

        if isinstance(state, clips.Symbol):
            return None
        else:
            return CLIPSError(self._env, message=state)
Пример #30
0
    def __call__(self, *arguments):
        """Call the CLIPS function with the given arguments."""
        value = clips.values.clips_value(self._env)
        builder = environment_builder(self._env, 'function')

        lib.FCBReset(builder)
        for argument in arguments:
            lib.FCBAppend(builder,
                          clips.values.clips_value(self._env, value=argument))

        ret = lib.FCBCall(builder, lib.DeffunctionName(self._ptr()), value)
        if ret != lib.FCBE_NO_ERROR:
            raise CLIPSError(self._env, code=ret)

        return clips.values.python_value(self._env, value)