rule-interface 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +77 -12
- data/lib/rule-interface/converter.rb +23 -13
- data/lib/rule-interface/rule_executor.rb +2 -2
- data/lib/rule-interface/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fc3c07de9c4511d6a73debe9c7cef8041e1dc94
|
4
|
+
data.tar.gz: 149ffd4045fa9e0ce2ac3d0be842fbb7f563cdf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4aa10c32473c36e2d2b37692e160d6f5b6100b2c29f8c134107c4e15806c1061512542166ff7bceb01cd7699bc24423b2812432cede0a64319231e2e01770480
|
7
|
+
data.tar.gz: 046c120e5223007d4b999f59fa19fe3e251462c5936179f94a8d6d42d11502924ef19ea90bdbb97e77d486f550114ba7eb76aa80fd7b549a5c62e1519c910192
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,22 @@
|
|
1
1
|
# RuleInterface
|
2
2
|
|
3
|
-
A ruby interface communicate with Drools
|
3
|
+
A ruby interface to convert and communicate with Drools
|
4
4
|
|
5
5
|
[](https://travis-ci.org/NestAway/rule-interface)
|
6
6
|
|
7
|
+
## Use case
|
8
|
+
|
9
|
+
- Using [Drools](https://www.drools.org/)
|
10
|
+
- Want a easy way to convert the data to Drools format and send
|
11
|
+
- Stateless session
|
12
|
+
- Agenda group implementation for stateless session (We call it namespace)
|
13
|
+
|
14
|
+
## Expectation
|
15
|
+
|
16
|
+
Currently this Gem is designed for stateless session.
|
17
|
+
|
18
|
+
Using this you can pass a set of data (facts in Drools) and get the same data back with the modification happended (based on your rule) in Drools
|
19
|
+
|
7
20
|
## Installation
|
8
21
|
|
9
22
|
Add this line to your application's Gemfile:
|
@@ -14,11 +27,15 @@ gem 'rule-interface'
|
|
14
27
|
|
15
28
|
And then execute:
|
16
29
|
|
17
|
-
|
30
|
+
```shell
|
31
|
+
bundle
|
32
|
+
```
|
18
33
|
|
19
34
|
Or install it yourself as:
|
20
35
|
|
21
|
-
|
36
|
+
```shell
|
37
|
+
gem install rule-interface
|
38
|
+
```
|
22
39
|
|
23
40
|
## Configuration
|
24
41
|
|
@@ -53,19 +70,67 @@ RuleInterface.execute!(
|
|
53
70
|
name: 'Bla2'
|
54
71
|
}
|
55
72
|
],
|
56
|
-
user:
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
}
|
61
|
-
]
|
73
|
+
user: { # Array only if multiple objects, we handle it
|
74
|
+
id: 123,
|
75
|
+
email: 'yoman@manyo.com'
|
76
|
+
}
|
62
77
|
},
|
63
|
-
namespace: :invensense,
|
64
78
|
container: 'team_magic_v1.2.3',
|
65
|
-
package: 'com.myteam.
|
79
|
+
package: 'com.myteam.test',
|
80
|
+
namespace: :test,
|
81
|
+
session: 'blah'
|
66
82
|
)
|
67
83
|
```
|
68
84
|
|
85
|
+
### Arguments explaind
|
86
|
+
|
87
|
+
#### data_hash
|
88
|
+
Used to send data to Drools.
|
89
|
+
|
90
|
+
**Syntax**:
|
91
|
+
Inside root hash, define the data class name in ruby style as the key and put the value as an array of objects or a signle object
|
92
|
+
|
93
|
+
Let's say the data class name (ruby style) you put as `line_item`, this will get converted to `{package}.LineItem`
|
94
|
+
|
95
|
+
Object is a key value pair (Nested objects are not supported right now). And `id` should be an uniq identifire for the class and `id` is optional, if passed we'll return the object back
|
96
|
+
|
97
|
+
#### container
|
98
|
+
Container name of the KIE server
|
99
|
+
|
100
|
+
#### package
|
101
|
+
The package name of your JAVA models (fact class) created. And this package name will get automatically added to your data_hash models
|
102
|
+
|
103
|
+
#### namespace
|
104
|
+
> Optional argument
|
105
|
+
|
106
|
+
As stateless session doesn't support agenda group feature in Drools, we build namespace as a hack for it
|
107
|
+
|
108
|
+
Create a fact class in your Drools project as shown below
|
109
|
+
|
110
|
+
```
|
111
|
+
Namespace {
|
112
|
+
:name
|
113
|
+
}
|
114
|
+
```
|
115
|
+
|
116
|
+
**Example rule**:
|
117
|
+
```
|
118
|
+
rule "rule 1"
|
119
|
+
when
|
120
|
+
Namespace(name == "campine_1")
|
121
|
+
Product(amount > 200)
|
122
|
+
then
|
123
|
+
// Do something here
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
And you pass `campine_1` as your namesapce
|
128
|
+
|
129
|
+
#### session
|
130
|
+
> Optional argument
|
131
|
+
|
132
|
+
Default session name in the Gem is `session`.
|
133
|
+
|
69
134
|
## Development
|
70
135
|
|
71
136
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -79,5 +144,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
79
144
|
|
80
145
|
## License
|
81
146
|
|
82
|
-
The gem is available as open source under the terms of the [
|
147
|
+
The gem is available as open source under the terms of the [Apache-2.0](https://opensource.org/licenses/Apache-2.0).
|
83
148
|
|
@@ -2,20 +2,27 @@ module RuleInterface
|
|
2
2
|
module Converter
|
3
3
|
extend self
|
4
4
|
|
5
|
-
|
5
|
+
OUT_IDENTIFIER_ID = :rule_interface_out_identifier_id
|
6
|
+
|
7
|
+
def hash_to_drool(data_hash:, namespace:, package:, session:)
|
6
8
|
payload = {}
|
7
|
-
payload[:lookup] =
|
9
|
+
payload[:lookup] = session
|
8
10
|
payload[:commands] = []
|
9
11
|
|
12
|
+
Thread.current[OUT_IDENTIFIER_ID] = 1
|
13
|
+
|
10
14
|
#insert namespace object
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
if namespace
|
16
|
+
payload[:commands] << insert_object(
|
17
|
+
data_object: {name: namespace},
|
18
|
+
object_type: 'namespace',
|
19
|
+
package: package,
|
20
|
+
return_object: false
|
21
|
+
)
|
22
|
+
end
|
17
23
|
|
18
24
|
data_hash.each do |object_type, data_objects|
|
25
|
+
data_objects = [data_objects] unless data_objects.is_a? Array
|
19
26
|
data_objects.each do |data_object|
|
20
27
|
payload[:commands] << insert_object(
|
21
28
|
data_object: data_object,
|
@@ -43,10 +50,6 @@ module RuleInterface
|
|
43
50
|
private
|
44
51
|
|
45
52
|
def insert_object(data_object:, object_type:, package:, return_object: true)
|
46
|
-
if return_object && data_object[:id].blank?
|
47
|
-
raise Error::CommonError, "Object must have an id attribute: #{data_object.to_json}"
|
48
|
-
end
|
49
|
-
|
50
53
|
data_object = data_object.inject({}) do |m, (k,v)|
|
51
54
|
m[k.to_s.camelize(:lower).to_sym] = v; m
|
52
55
|
end
|
@@ -55,7 +58,14 @@ module RuleInterface
|
|
55
58
|
insert_object = {}
|
56
59
|
insert_object[:insert] = {}
|
57
60
|
insert_object[:insert]['return-object'] = return_object
|
58
|
-
|
61
|
+
|
62
|
+
out_identifier_id = data_object[:id]
|
63
|
+
if out_identifier_id.blank?
|
64
|
+
out_identifier_id = "RA_#{Thread.current[OUT_IDENTIFIER_ID]}"
|
65
|
+
Thread.current[OUT_IDENTIFIER_ID] += 1
|
66
|
+
end
|
67
|
+
|
68
|
+
insert_object[:insert]['out-identifier'] = "#{object_type}##{out_identifier_id}" if return_object
|
59
69
|
|
60
70
|
object = {}
|
61
71
|
object_name = "#{package}.#{object_type}"
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RuleInterface
|
2
2
|
module RuleExecutor
|
3
|
-
def execute!(data_hash:,
|
4
|
-
payload = Converter.hash_to_drool(data_hash: data_hash, namespace: namespace, package: package)
|
3
|
+
def execute!(data_hash:, container:, package:, namespace: nil, session: 'session')
|
4
|
+
payload = Converter.hash_to_drool(data_hash: data_hash, namespace: namespace, package: package, session: session)
|
5
5
|
response = parsed_connection!(container: container, method: "post", payload: payload)
|
6
6
|
if response[:type] && response[:type] == "SUCCESS"
|
7
7
|
Converter.drool_to_hash(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rule-interface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ranveer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|