forked from Keysight/clipsgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.go
87 lines (73 loc) · 2.31 KB
/
modules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package clips
// #cgo CFLAGS: -I ../../clips_source
// #cgo LDFLAGS: -L ../../clips_source -l clips -lm
// #include <clips/clips.h>
import "C"
/*
Copyright 2020 Keysight Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
import (
"fmt"
"strings"
"unsafe"
)
// Module represents a CLIPS module
type Module struct {
env *Environment
modptr unsafe.Pointer
}
// CurrentModule returns the current module of the env
func (env *Environment) CurrentModule() *Module {
modptr := C.EnvGetCurrentModule(env.env)
return createModule(env, modptr)
}
// SetModule sets the current module for the CLIPS env
func (env *Environment) SetModule(module *Module) {
C.EnvSetCurrentModule(env.env, module.modptr)
}
// Modules returns the list of modulesb
func (env *Environment) Modules() []*Module {
modptr := C.EnvGetNextDefmodule(env.env, nil)
ret := make([]*Module, 0, 10)
for modptr != nil {
ret = append(ret, createModule(env, modptr))
modptr = C.EnvGetNextDefmodule(env.env, modptr)
}
return ret
}
// FindModule returns the module with the given name
func (env *Environment) FindModule(name string) (*Module, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
modptr := C.EnvFindDefmodule(env.env, cname)
if modptr == nil {
return nil, NotFoundError(fmt.Errorf(`Module "%s" not found`, name))
}
return createModule(env, modptr), nil
}
func createModule(env *Environment, modptr unsafe.Pointer) *Module {
return &Module{
env: env,
modptr: modptr,
}
}
// Equal returns true if the other module references the same CLIPS module
func (m *Module) Equal(other *Module) bool {
return m.modptr == other.modptr
}
func (m *Module) String() string {
module := C.EnvGetDefmodulePPForm(m.env.env, m.modptr)
return strings.TrimRight(C.GoString(module), "\n")
}
// Name returns the name of this module
func (m *Module) Name() string {
name := C.EnvGetDefmoduleName(m.env.env, m.modptr)
return C.GoString(name)
}