Beispiel #1
0
    def new_instance(self, name):
        """Create a new raw instance from this Class."""
        ist = lib.EnvCreateRawInstance(self._env, self._cls, name.encode())
        if ist == ffi.NULL:
            raise CLIPSError(self._env)

        return Instance(self._env, ist)
Beispiel #2
0
    def __setitem__(self, slot, value):
        data = clips.data.DataObject(self._env)
        data.value = value

        if lib.EnvDirectPutSlot(self._env, self._ist, slot.encode(),
                                data.byref) == 0:
            raise CLIPSError(self._env)
Beispiel #3
0
    def unmake(self):
        """This method is equivalent to delete except that it uses
        message-passing instead of directly deleting the instance.

        """
        if lib.EnvUnmakeInstance(self._env, self._ist) != 1:
            raise CLIPSError(self._env)
Beispiel #4
0
def classes(env, names):
    for name in names:
        defclass = lib.EnvFindDefclass(env, name.encode())
        if defclass == ffi.NULL:
            raise CLIPSError(env)

        yield Class(env, defclass)
Beispiel #5
0
    def new_fact(self):
        """Create a new Fact from this template."""
        fact = lib.EnvCreateFact(self._env, self._tpl)
        if fact == ffi.NULL:
            raise CLIPSError(self._env)

        return new_fact(self._env, fact)
Beispiel #6
0
    def batch_star(self, path):
        """Evaluate the commands contained in the specific path.

        The Python equivalent of the CLIPS batch* command.

        """
        if lib.EnvBatchStar(self._env, path.encode()) != 1:
            raise CLIPSError(self._env)
Beispiel #7
0
    def remove_breakpoint(self):
        """Remove a breakpoint for the Rule.

        The Python equivalent of the CLIPS remove-break command.

        """
        if lib.EnvRemoveBreak(self._env, self._rule) != 1:
            raise CLIPSError("No breakpoint set")
Beispiel #8
0
    def find_message_handler(self, handler_name, handler_type='primary'):
        """Returns the MessageHandler given its name and type for this class."""
        ret = lib.EnvFindDefmessageHandler(
            self._env, self._cls, handler_name.encode(), handler_type.encode())
        if ret == 0:
            raise CLIPSError(self._env)

        return MessageHandler(self._env, self._cls, ret)
Beispiel #9
0
    def value(self, value):
        """Global value."""
        data = clips.data.DataObject(self._env)
        data.value = value

        if lib.EnvSetDefglobalValue(
                self._env, self.name.encode(), data.byref) != 1:
            raise CLIPSError(self._env)
Beispiel #10
0
    def refresh(self):
        """Refresh the Rule.

        The Python equivalent of the CLIPS refresh command.

        """
        if lib.EnvRefresh(self._env, self._rule) != 1:
            raise CLIPSError(self._env)
Beispiel #11
0
    def assert_string(self, string):
        """Assert a fact as string."""
        fact = lib.EnvAssertString(self._env, string.encode())

        if fact == ffi.NULL:
            raise CLIPSError(self._env)

        return new_fact(self._env, fact)
Beispiel #12
0
    def build(self, construct):
        """Build a single construct in CLIPS.

        The Python equivalent of the CLIPS build command.

        """
        if lib.EnvBuild(self._env, construct.encode()) != 1:
            raise CLIPSError(self._env)
Beispiel #13
0
    def assertit(self):
        """Assert the fact within the CLIPS environment."""
        if self.asserted:
            raise RuntimeError("Fact already asserted")

        lib.EnvAssignFactSlotDefaults(self._env, self._fact)

        if lib.EnvAssert(self._env, self._fact) == ffi.NULL:
            raise CLIPSError(self._env)
Beispiel #14
0
    def assertit(self):
        """Assert the fact within CLIPS."""
        data = clips.data.DataObject(self._env)
        data.value = list(self._multifield)

        if lib.EnvPutFactSlot(self._env, self._fact, ffi.NULL,
                              data.byref) != 1:
            raise CLIPSError(self._env)

        super(ImpliedFact, self).assertit()
Beispiel #15
0
    def undefine(self):
        """Undefine the Template.

        Python equivalent of the CLIPS undeftemplate command.

        The object becomes unusable after this method has been called.

        """
        if lib.EnvUndeftemplate(self._env, self._tpl) != 1:
            raise CLIPSError(self._env)
Beispiel #16
0
    def load(self, path):
        """Load a set of constructs into the CLIPS data base.

        The Python equivalent of the CLIPS load command.

        """
        ret = lib.EnvBload(self._env, path.encode())
        if ret != 1:
            ret = lib.EnvLoad(self._env, path.encode())
            if ret != 1:
                raise CLIPSError(self._env)
Beispiel #17
0
    def restore_instances(self, instances):
        """Restore a set of instances into the CLIPS data base.

        The Python equivalent of the CLIPS restore-instances command.

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

        """
        instances = instances.encode()

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

        return ret
Beispiel #18
0
    def save_facts(self, path, mode=SaveMode.LOCAL_SAVE):
        """Save the facts in the system to the specified file.

        The Python equivalent of the CLIPS save-facts command.

        """
        ret = lib.EnvSaveFacts(self._env, path.encode(), mode)
        if ret == -1:
            raise CLIPSError(self._env)

        return ret
Beispiel #19
0
    def load_facts(self, facts):
        """Load a set of facts into the CLIPS data base.

        The C equivalent of the CLIPS load-facts command.

        Facts can be loaded from a string or from a text file.

        """
        facts = facts.encode()

        if os.path.exists(facts):
            ret = lib.EnvLoadFacts(self._env, facts)
            if ret == -1:
                raise CLIPSError(self._env)
        else:
            ret = lib.EnvLoadFactsFromString(self._env, facts, -1)
            if ret == -1:
                raise CLIPSError(self._env)

        return ret
Beispiel #20
0
    def undefine(self):
        """Undefine the MessageHandler.

        Python equivalent of the CLIPS undefmessage-handler command.

        The object becomes unusable after this method has been called.

        """
        if lib.EnvUndefmessageHandler(self._env, self._cls, self._idx) != 1:
            raise CLIPSError(self._env)

        self._env = None
Beispiel #21
0
    def undefine(self):
        """Undefine the Rule.

        Python equivalent of the CLIPS undefrule command.

        The object becomes unusable after this method has been called.

        """
        if lib.EnvUndefrule(self._env, self._rule) != 1:
            raise CLIPSError(self._env)

        self._env = None
Beispiel #22
0
    def eval(self, construct):
        """Evaluate an expression returning its value.

        The Python equivalent of the CLIPS eval command.

        """
        data = clips.data.DataObject(self._env)

        if lib.EnvEval(self._env, construct.encode(), data.byref) != 1:
            raise CLIPSError(self._env)

        return data.value
Beispiel #23
0
    def load_instances(self, instances):
        """Load a set of instances into the CLIPS data base.

        The C equivalent of the CLIPS load-instances command.

        Instances can be loaded from a string,
        from a file or from a binary file.

        """
        instances = instances.encode()

        if os.path.exists(instances):
            ret = lib.EnvBinaryLoadInstances(self._env, instances)
            if ret == -1:
                ret = lib.EnvLoadInstances(self._env, instances)
                if ret == -1:
                    raise CLIPSError(self._env)
        else:
            ret = lib.EnvLoadInstancesFromString(self._env, instances, -1)
            if ret == -1:
                raise CLIPSError(self._env)

        return ret
Beispiel #24
0
    def save(self, path, binary=False):
        """Save a set of constructs into the CLIPS data base.

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

        The Python equivalent of the CLIPS load command.

        """
        if binary:
            ret = lib.EnvBsave(self._env, path.encode())
        else:
            ret = lib.EnvSave(self._env, path.encode())
        if ret == 0:
            raise CLIPSError(self._env)
Beispiel #25
0
    def __call__(self, arguments=''):
        """Call the CLIPS generic function.

        Function arguments must be provided as a string.

        """
        data = clips.data.DataObject(self._env)
        name = ffi.string(lib.EnvGetDefgenericName(self._env, self._gnc))
        args = arguments.encode() if arguments != '' else ffi.NULL

        if lib.EnvFunctionCall(self._env, name, args, data.byref) == 1:
            raise CLIPSError(self._env)

        return data.value
Beispiel #26
0
    def new_instance(self, name):
        """Create a new raw instance from this Class.

        No slot overrides or class default initializations
        are performed for the instance.

        This function bypasses message-passing.

        """
        ist = lib.EnvCreateRawInstance(self._env, self._cls, name.encode())
        if ist == ffi.NULL:
            raise CLIPSError(self._env)

        return Instance(self._env, ist)
Beispiel #27
0
    def __setitem__(self, key, value):
        if self.asserted:
            raise RuntimeError("Fact already asserted")

        data = clips.data.DataObject(self._env)
        data.value = value

        ret = lib.EnvPutFactSlot(self._env, self._fact,
                                 str(key).encode(), data.byref)
        if ret != 1:
            if key not in (s.name for s in self.template.slots()):
                raise KeyError("'%s' fact has not slot '%s'" %
                               (self.template.name, key))

            raise CLIPSError(self._env)
Beispiel #28
0
    def save_instances(self, path, binary=False, mode=SaveMode.LOCAL_SAVE):
        """Save the instances in the system to the specified file.

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

        The Python equivalent of the CLIPS save-instances command.

        """
        if binary:
            ret = lib.EnvBinarySaveInstances(self._env, path.encode(), mode)
        else:
            ret = lib.EnvSaveInstances(self._env, path.encode(), mode)
        if ret == 0:
            raise CLIPSError(self._env)

        return ret
Beispiel #29
0
    def make_instance(self, command):
        """Create and initialize an instance of a user-defined class.

        command must be a string in the form:

        (<instance-name> of <class-name> <slot-override>*)
        <slot-override> :== (<slot-name> <constant>*)

        Python equivalent of the CLIPS make-instance command.

        """
        ist = lib.EnvMakeInstance(self._env, command.encode())
        if ist == ffi.NULL:
            raise CLIPSError(self._env)

        return Instance(self._env, ist)
Beispiel #30
0
    def delete(self):
        """Remove the activation from the agenda."""
        if lib.EnvDeleteActivation(self._env, self._act) != 1:
            raise CLIPSError(self._env)

        self._env = None