diff --git a/easy-rules/pom.xml b/easy-rules/pom.xml new file mode 100644 index 0000000000..b74b16f34c --- /dev/null +++ b/easy-rules/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + com.baeldung.easyrules + easy-rules + 1.0.0-SNAPSHOT + + + + org.jeasy + easy-rules-core + 3.0.0 + + + \ No newline at end of file diff --git a/easy-rules/src/main/java/com/baeldung/easyrules/HelloWorldRule.java b/easy-rules/src/main/java/com/baeldung/easyrules/HelloWorldRule.java new file mode 100644 index 0000000000..5448eabf2a --- /dev/null +++ b/easy-rules/src/main/java/com/baeldung/easyrules/HelloWorldRule.java @@ -0,0 +1,20 @@ +package com.baeldung.easyrules; + +import org.jeasy.rules.annotation.Action; +import org.jeasy.rules.annotation.Condition; +import org.jeasy.rules.annotation.Rule; + +@Rule(name = "Hello World rule", description = "Always say hello world") +public class HelloWorldRule { + + @Condition + public boolean when() { + return true; + } + + @Action + public void then() throws Exception { + System.out.println("hello world"); + } + +} diff --git a/easy-rules/src/main/java/com/baeldung/easyrules/Launcher.java b/easy-rules/src/main/java/com/baeldung/easyrules/Launcher.java new file mode 100644 index 0000000000..427e3eace0 --- /dev/null +++ b/easy-rules/src/main/java/com/baeldung/easyrules/Launcher.java @@ -0,0 +1,21 @@ +package com.baeldung.easyrules; + +import org.jeasy.rules.api.Facts; +import org.jeasy.rules.api.Rules; +import org.jeasy.rules.api.RulesEngine; +import org.jeasy.rules.core.DefaultRulesEngine; + +public class Launcher { + public static void main(String... args) { + // create facts + Facts facts = new Facts(); + + // create rules + Rules rules = new Rules(); + rules.register(new HelloWorldRule()); + + // create a rules engine and fire rules on known facts + RulesEngine rulesEngine = new DefaultRulesEngine(); + rulesEngine.fire(rules, facts); + } +} diff --git a/openl-tablets/pom.xml b/openl-tablets/pom.xml new file mode 100644 index 0000000000..77b9f47b38 --- /dev/null +++ b/openl-tablets/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + com.baeldung.openltablets + openl-tablets + 0.0.1-SNAPSHOT + + UTF-8 + UTF-8 + 1.8 + + + + org.openl + org.openl.core + 5.19.4 + + + org.openl.rules + org.openl.rules + 5.19.4 + + + \ No newline at end of file diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/model/Case.java b/openl-tablets/src/main/java/com/baeldung/openltablets/model/Case.java new file mode 100644 index 0000000000..f9f5f4bd5f --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/model/Case.java @@ -0,0 +1,23 @@ +package com.baeldung.openltablets.model; + +public class Case { + + private User user; + private int hourOfDay; + + public User getUser() { + return user; + } + + public void setUser(final User user) { + this.user = user; + } + + public int getHourOfDay() { + return hourOfDay; + } + + public void setHourOfDay(final int hourOfDay) { + this.hourOfDay = hourOfDay; + } +} diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/model/Greeting.java b/openl-tablets/src/main/java/com/baeldung/openltablets/model/Greeting.java new file mode 100644 index 0000000000..5dc7bcd117 --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/model/Greeting.java @@ -0,0 +1,20 @@ +package com.baeldung.openltablets.model; + +public enum Greeting { + + GOOD_MORNING("Good Morning"), + GOOD_AFTERNOON("Good Afternoon"), + GOOD_EVENING("Good Evening"), + GOOD_NIGHT("Good Night"); + + private final String literal; + + private Greeting(final String literal) { + this.literal = literal; + } + + public String getLiteral() { + return literal; + } + +} \ No newline at end of file diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/model/User.java b/openl-tablets/src/main/java/com/baeldung/openltablets/model/User.java new file mode 100644 index 0000000000..8e45487497 --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/model/User.java @@ -0,0 +1,14 @@ +package com.baeldung.openltablets.model; + +public class User { + + private String name; + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/rules/IRule.java b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/IRule.java new file mode 100644 index 0000000000..857a6433ef --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/IRule.java @@ -0,0 +1,7 @@ +package com.baeldung.openltablets.rules; + +import com.baeldung.openltablets.model.Case; + +public interface IRule { + void helloUser(final Case aCase, final Response response); +} \ No newline at end of file diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Main.java b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Main.java new file mode 100644 index 0000000000..34f5c48ed1 --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Main.java @@ -0,0 +1,31 @@ +package com.baeldung.openltablets.rules; + + +import java.time.LocalDateTime; + +import org.openl.rules.runtime.RulesEngineFactory; +import org.openl.runtime.EngineFactory; + +import com.baeldung.openltablets.model.Case; +import com.baeldung.openltablets.model.User; + +public class Main { + private IRule instance; + + public static void main(String[] args) { + Main rules = new Main(); + User user = new User(); + user.setName("Donald Duck"); + Case aCase = new Case(); + aCase.setUser(user); + aCase.setHourOfDay(23); + rules.process(aCase); + } + + public void process(Case aCase) { + final EngineFactory engineFactory = new RulesEngineFactory(getClass().getClassLoader() + .getResource("openltablets/HelloUser.xls"), IRule.class); + instance = engineFactory.newEngineInstance(); + instance.helloUser(aCase, new Response()); + } +} diff --git a/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Response.java b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Response.java new file mode 100644 index 0000000000..27fa634866 --- /dev/null +++ b/openl-tablets/src/main/java/com/baeldung/openltablets/rules/Response.java @@ -0,0 +1,24 @@ +package com.baeldung.openltablets.rules; + +import java.util.HashMap; +import java.util.Map; + +public class Response { + private String result; + private Map map = new HashMap<>(); + + public Response() { } + + public String getResult() { + return result; + } + + public void setResult(final String s) { + result = s; + } + + public Map getMap() { + return map; + } + +} diff --git a/openl-tablets/src/main/resources/openltablets/HelloUser.xls b/openl-tablets/src/main/resources/openltablets/HelloUser.xls new file mode 100644 index 0000000000..1e85d0ce2d Binary files /dev/null and b/openl-tablets/src/main/resources/openltablets/HelloUser.xls differ diff --git a/rulebook/pom.xml b/rulebook/pom.xml new file mode 100644 index 0000000000..2a42e36d93 --- /dev/null +++ b/rulebook/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + com.baeldung.rulebook + rulebook + 1.0.0-SNAPSHOT + + + + com.deliveredtechnologies + rulebook-core + 0.6.2 + + + \ No newline at end of file diff --git a/rulebook/src/main/java/com/baeldung/rulebook/HelloWorldRule.java b/rulebook/src/main/java/com/baeldung/rulebook/HelloWorldRule.java new file mode 100644 index 0000000000..c09772a3c6 --- /dev/null +++ b/rulebook/src/main/java/com/baeldung/rulebook/HelloWorldRule.java @@ -0,0 +1,17 @@ +package com.baeldung.rulebook; + +import com.deliveredtechnologies.rulebook.lang.RuleBookBuilder; +import com.deliveredtechnologies.rulebook.model.RuleBook; + +public class HelloWorldRule { + public RuleBook defineHelloWorldRules() { + + return RuleBookBuilder.create() + .addRule(rule -> rule.withNoSpecifiedFactType() + .then(f -> System.out.print("Hello "))) + .addRule(rule -> rule.withNoSpecifiedFactType() + .then(f -> System.out.println("World"))) + .build(); + + } +} diff --git a/rulebook/src/main/java/com/baeldung/rulebook/Launcher.java b/rulebook/src/main/java/com/baeldung/rulebook/Launcher.java new file mode 100644 index 0000000000..57965457ec --- /dev/null +++ b/rulebook/src/main/java/com/baeldung/rulebook/Launcher.java @@ -0,0 +1,12 @@ +package com.baeldung.rulebook; + +import com.deliveredtechnologies.rulebook.FactMap; + +public class Launcher { + + public static void main(String[] args) { + HelloWorldRule ruleBook = new HelloWorldRule(); + ruleBook.defineHelloWorldRules() + .run(new FactMap<>()); + } +} diff --git a/spring-core/src/main/java/com/baeldung/autowired/TypesOfBeanInjectionSpring.java b/spring-core/src/main/java/com/baeldung/autowired/TypesOfBeanInjectionSpring.java new file mode 100644 index 0000000000..ca6018a21e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/autowired/TypesOfBeanInjectionSpring.java @@ -0,0 +1,70 @@ +package com.baeldung.autowired; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Service; + +@SpringBootApplication +public class TypesOfBeanInjectionSpring { + private final UserService userService; + + @Autowired // the @Autowired can even be omitted, in case there's only one explicit constructor + public TypesOfBeanInjectionSpring(UserService userService) { + this.userService = userService; + } + + public static void main(String[] args) { + SpringApplication.run(TypesOfBeanInjectionSpring.class, args); + } + + @Bean + CommandLineRunner runIt() { + return args -> { + userService.listUsers() + .stream() + .forEach(System.out::println); + }; + } +} + +class User { + private String name; + + public User(String name) { + this.name = name; + } + + // getters and setters ... + public String getName() { + return this.name; + } + + public String toString() { + return name; + } + +} + +interface UserService { + List listUsers(); +} + +@Service +class UserServiceImpl implements UserService { + + @Override + public List listUsers() { + ArrayList users = new ArrayList<>(3); + users.add(new User("Snoopy")); + users.add(new User("Woodstock")); + users.add(new User("Charlie Brown")); + return users; + } + +} diff --git a/spring-core/src/test/java/com/baeldung/autowired/TypesOfBeanInjectionSpringIntegrationTest.java b/spring-core/src/test/java/com/baeldung/autowired/TypesOfBeanInjectionSpringIntegrationTest.java new file mode 100644 index 0000000000..206a062a57 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/autowired/TypesOfBeanInjectionSpringIntegrationTest.java @@ -0,0 +1,25 @@ +package com.baeldung.autowired; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class TypesOfBeanInjectionSpringIntegrationTest { + @Autowired + UserService userService; + + private static final String[] expected = new String[] { "Snoopy", "Woodstock", "Charlie Brown" }; + + @Test + public void givenDI_whenInjectObject_thenUserNamesAreListed() { + Assert.assertArrayEquals(expected, userService.listUsers() + .stream() + .map(User::getName) + .toArray()); + } +}