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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 524f8431dd91bd47d798334f77bed731e5525072
4
- data.tar.gz: 3c64436cf12b292c4c4da96b13f17f58ecb2a847
3
+ metadata.gz: 5fc3c07de9c4511d6a73debe9c7cef8041e1dc94
4
+ data.tar.gz: 149ffd4045fa9e0ce2ac3d0be842fbb7f563cdf3
5
5
  SHA512:
6
- metadata.gz: 3fff9269d46e83c0e95ae22418d2c0370f1ff9bb1563b2d19331236f573450460585f0ff5766e7ea0548d00c97b58e083180342eaa6957c0bb1d7434ee4a8990
7
- data.tar.gz: eba32c7218cb1e4a5ed1a3807501a5e5fe0afff65932e58995b0f0ab89c5710d274272d6b08b9c67c3853cbdc3722914d8e5f085cfd85705f9a258c991f6db9d
6
+ metadata.gz: 4aa10c32473c36e2d2b37692e160d6f5b6100b2c29f8c134107c4e15806c1061512542166ff7bceb01cd7699bc24423b2812432cede0a64319231e2e01770480
7
+ data.tar.gz: 046c120e5223007d4b999f59fa19fe3e251462c5936179f94a8d6d42d11502924ef19ea90bdbb97e77d486f550114ba7eb76aa80fd7b549a5c62e1519c910192
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rule-interface (1.0.2)
4
+ rule-interface (1.0.3)
5
5
  activesupport (~> 3)
6
6
  rest-client (~> 1)
7
7
 
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
  [![Build Status](https://travis-ci.org/NestAway/rule-interface.svg?branch=master)](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
- $ bundle
30
+ ```shell
31
+ bundle
32
+ ```
18
33
 
19
34
  Or install it yourself as:
20
35
 
21
- $ gem install rule-interface
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
- id: 123,
59
- email: 'yoman@manyo.com'
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.team_magic'
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 [MIT License](http://opensource.org/licenses/MIT).
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
- def hash_to_drool(data_hash:, namespace:, package:)
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] = 'session'
9
+ payload[:lookup] = session
8
10
  payload[:commands] = []
9
11
 
12
+ Thread.current[OUT_IDENTIFIER_ID] = 1
13
+
10
14
  #insert namespace object
11
- payload[:commands] << insert_object(
12
- data_object: {name: namespace},
13
- object_type: 'namespace',
14
- package: package,
15
- return_object: false
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
- insert_object[:insert]['out-identifier'] = "#{object_type}##{data_object[:id]}" if return_object
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:, namespace:, container:, package:)
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(
@@ -1,3 +1,3 @@
1
1
  module RuleInterface
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.0.3'.freeze
3
3
  end
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.2
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-05 00:00:00.000000000 Z
11
+ date: 2017-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport