Commit 0b19939 1 parent d0862d2 commit 0b19939 Copy full SHA for 0b19939
File tree 5 files changed +149
-0
lines changed
5 files changed +149
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+
5
+ <modelVersion >4.0.0</modelVersion >
6
+
7
+ <artifactId >drools-backward-chaining</artifactId >
8
+ <version >1.0</version >
9
+ <name >drools-backward-chaining</name >
10
+
11
+ <parent >
12
+ <groupId >com.baeldung</groupId >
13
+ <artifactId >parent-modules</artifactId >
14
+ <version >1.0.0-SNAPSHOT</version >
15
+ </parent >
16
+
17
+ <properties >
18
+ <runtime .version>6.4.0.Final</runtime .version>
19
+ </properties >
20
+
21
+ <dependencies >
22
+ <dependency >
23
+ <groupId >org.kie</groupId >
24
+ <artifactId >kie-api</artifactId >
25
+ <version >${runtime.version} </version >
26
+ </dependency >
27
+ <dependency >
28
+ <groupId >org.drools</groupId >
29
+ <artifactId >drools-core</artifactId >
30
+ <version >${runtime.version} </version >
31
+ </dependency >
32
+ <dependency >
33
+ <groupId >org.drools</groupId >
34
+ <artifactId >drools-decisiontables</artifactId >
35
+ <version >${runtime.version} </version >
36
+ </dependency >
37
+ </dependencies >
38
+ </project >
Original file line number Diff line number Diff line change
1
+ package com .baeldung ;
2
+
3
+ import org .kie .api .KieServices ;
4
+ import org .kie .api .runtime .KieContainer ;
5
+ import org .kie .api .runtime .KieSession ;
6
+
7
+ import com .baeldung .model .Beatle ;
8
+
9
+ public class BackwardChainingBeatles {
10
+ public static void main (String [] args ) {
11
+
12
+ KieServices ks = KieServices .Factory .get ();
13
+ KieContainer kContainer = ks .getKieClasspathContainer ();
14
+ KieSession kSession = kContainer .newKieSession ("ksession-backward-chaining" );
15
+ // drools session base on the xml configuration (<strong>kmodule.xml</strong>)
16
+
17
+ // graph population
18
+ kSession .insert (new Beatle ("Starr" , "drums" ));
19
+ kSession .insert (new Beatle ("McCartney" , "bass" ));
20
+ kSession .insert (new Beatle ("Lennon" , "guitar" ));
21
+ kSession .insert (new Beatle ("Harrison" , "guitar" ));
22
+
23
+ kSession .insert ("Ringo" ); // invoke the rule that calls the query implentation of backward chaining
24
+ kSession .fireAllRules (); // invoke all the rules
25
+
26
+ }
27
+
28
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .model ;
2
+
3
+ import org .kie .api .definition .type .Position ;
4
+
5
+ public class Beatle {
6
+
7
+ @ Position (0 )
8
+ private String lastName ;
9
+ @ Position (1 )
10
+ private String instrument ;
11
+
12
+ public Beatle (String lastName , String instrument ) {
13
+ this .lastName = lastName ;
14
+ this .instrument = instrument ;
15
+ }
16
+
17
+ public String getInstrument () {
18
+ return instrument ;
19
+ }
20
+
21
+ public void setInstrument (String instrument ) {
22
+ this .instrument = instrument ;
23
+ }
24
+
25
+ public String getLastName () {
26
+ return lastName ;
27
+ }
28
+
29
+ public void setLastName (String lastName ) {
30
+ this .lastName = lastName ;
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <kmodule xmlns =" http://jboss.org/kie/6.0.0/kmodule" >
3
+ <kbase name =" backward_chaining" packages =" backward_chaining" >
4
+ <ksession name =" ksession-backward-chaining" />
5
+ </kbase >
6
+ </kmodule >
Original file line number Diff line number Diff line change
1
+ package com.baeldung
2
+
3
+ import com.baeldung.model.Beatle;
4
+
5
+
6
+ query whichBeatle(String lastName, String instrument)
7
+ Beatle(lastName, instrument;)
8
+ or
9
+ (Beatle(lastName, null;)
10
+ and
11
+ whichBeatle(null, instrument;)) //recursive call to the function that allows to search in a derivation tree structure
12
+ end
13
+
14
+ rule "Ringo"
15
+ when
16
+ String(this == "Ringo")
17
+ whichBeatle("Starr", "drums";)
18
+ then
19
+ System.out.println("The beatle is Ringo Starr");
20
+ end
21
+
22
+ rule "Paul"
23
+ when
24
+ String(this == "Paul")
25
+ whichBeatle("McCartney", "bass";)
26
+ then
27
+ System.out.println("The beatle is Paul McCartney");
28
+ end
29
+
30
+ rule "John"
31
+ when
32
+ String(this == "John")
33
+ whichBeatle("Lennon", "guitar";)
34
+ then
35
+ System.out.println("The beatle is John Lennon");
36
+ end
37
+
38
+ rule "George"
39
+ when
40
+ String(this == "George")
41
+ whichBeatle("Harrison", "guitar";)
42
+ then
43
+ System.out.println("The beatle is George Harrison");
44
+ end
You can’t perform that action at this time.
0 commit comments