Java @ Desk

Java @ Desk

Breaking

Saturday, September 15, 2018

Node JS Sort npm custom ArrayList using compare method

10:16 PM 0
Node JS Sort npm custom ArrayList using compare method

Arraylist is the collection in Node JS that stores the collection of objects. In below example we have a custom collection of ArrayList objects with two properties:
1) Name
2) Age

We will implement both the sort operations, one based on age and second based on name in ascending order. A simple collection of integer or string can be sorted using the sort() method. But in case of custom objects, we need to implement the compare function that defines the sorted algorithm.

We have implemented 2 different sort methods:
1) sortListByAge
2) sortListByName

First method implements the sort compare based on Age property and second implements based on Name. Here is the complete implementation.

/*jshint esversion: 6 */ 
var DELAY = 1000;
var ArrayList = require('arraylist');

var list = new ArrayList();
list.add([{ name: 'Edward', age: 21 },
   { name: 'Sharpe', age: 37 },
   { name: 'And', age: 45 },
   { name: 'The', age: -12 },
   { name: 'Magnetic', age: 13 },
   { name: 'Zeros', age: 37 }]);

module.exports = {
 sortListByAge() {
     return new Promise((resolve) => {
       setTimeout(() => {
        list.sort(function (a, b) {
         return a.age - b.age;
       });
      console.log("Sorted List By Age : " + list); 
         resolve(list);
       }, DELAY);
     });
   },
   sortListByName() {
      return new Promise((resolve) => {
        setTimeout(() => {
         list.sort(function(a, b) {
          var nameA = a.name.toUpperCase();
          var nameB = b.name.toUpperCase();
          if (nameA < nameB) {
            return -1;
          }
          if (nameA > nameB) {
            return 1;
          }
          return 0;
        });
        console.log("Sorted List By Name : " + list); 
          resolve(list);
        }, DELAY);
      });
    },
};


When we run above service, below output is generated
1) sortListByAge - [{"name":"The","age":-12},{"name":"Magnetic","age":13},{"name":"Edward","age":21},{"name":"Sharpe","age":37},{"name":"Zeros","age":37},{"name":"And","age":45}]
2) sortListByName - [{"name":"And","age":45},{"name":"Edward","age":21},{"name":"Magnetic","age":13},{"name":"Sharpe","age":37},{"name":"The","age":-12},{"name":"Zeros","age":37}]

Sunday, July 22, 2018

Drools Decision Table Set List To Pojo

6:56 AM 0
Drools Decision Table Set List To Pojo

In this post, we will learn how to set a list of values into a pojo field in Decision Table in the ACTION part. The values are set into the list field using the comma seperated values of Strings.

Consider a pojo class Person with following properties

1) age - Number
2) applyingFor - Pan/Adhaar/Passport(String)
3) documentsList - java.util.List - Documents required for application of Pan Card or Adhaar Card or Passport. It will be a String values seperated by comma

Following are the rules that are applied

1) If age > 18 and applyingFor = 'Pan Card', then documentsList = 'Birth Certificate', 'Photo'
2) If age > 18 and applyingFor = 'Adhaar Card', then documentsList = 'Birth Certificate', 'Pan Card', 'Light Bill', 'Photo'
3) If age > 18 and applyingFor = 'Pan Card', then documentsList = 'Adhaar Card', 'Pan Card', 'Photo'

This will be configured into a decision table as shown below


As shown, list is set using - $documentCheck.setDocumentList(Arrays.asList($param));

Thursday, March 29, 2018

Find Maximum LocalDateTime From ArrayList In Java 8

11:43 PM 0
Maximum date can be fetched from the List using the Collections interface.

Collections interface provide the methods to find the maximum value out of a collection of dates.

In this case we will pass ArrayList of LocalDateTime object that contains the dates.

Below is a sample implementation that retrieves the maximum date from a ArrayList -

package com.learning;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FindMaxLocalDateTimeArrayList {

 public static void main(String args[]) throws ParseException {

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
  LocalDate ld = LocalDate.parse("2018-03-22T18:02:32.000Z", formatter);
  LocalDateTime ldt = LocalDateTime.of(ld, LocalDateTime.now().toLocalTime());

  List<LocalDateTime> localDateTimes = new ArrayList<>();
  localDateTimes.add(ldt);
  localDateTimes.add(LocalDateTime.of(LocalDate.parse("2018-03-25T18:02:32.000Z", formatter),
    LocalDateTime.now().toLocalTime()));
  localDateTimes.add(LocalDateTime.of(LocalDate.parse("2018-03-16T18:02:32.000Z", formatter),
    LocalDateTime.now().toLocalTime()));

  System.out.println(Collections.max(localDateTimes));
 }
}

Wednesday, March 28, 2018

Implement And Or Conditions in Drools Decision Table

3:02 AM 0
Implement And Or Conditions in Drools Decision Table

Drools rules can be defined in various formats one of which is in Decision Tables. Decision Tables rules are configured in an excel sheet in .xls format.

Decision Table is primarily used when there are rules that need to be configured for similar properties. For example, there is a rule implementation on age property of a user. Below are the examples:
If age > 18 && age < 22 then Do X
If age > 22 && age < 28 then Do Y
If age > 28 && age < 32 then Do Z
If age > 32 && age < 36 then Do A
If age > 36 && age < 48 then Do B
If age > 48 && age < 56 then Do C

If such rules are implemented in DRL files, then there will be lot of redundant code. Instead the same are configured in a decision table.

There are different components of a Decision table as mentioned below. The structure is similar to DRL file itself
1) Ruleset - Its similar to package from DRL file.
2) Import - To import comma separated Java packages that are used in rules.
3) Sequential - If set to true, rules will be fired in sequence from top to bottom.
4) Variables - One of more Global variables.
5) Functions - Functions used if any in rules.
6) Queries - Queries used if any in rules.

Rules configuration consists of below :
1) Rule Name
2) Description
3) CONDITION
4) ACTION

By default, there is a AND condition between the CONDITION columns in between the table. AND condition can also be implemented using conditions with comma seperated as shown below. OR condition is implemented using || between the conditions as shown in below screenshot Here is the snapshot of Decision Table that is used in the example.









User.java - Pojo used for rules
package com.pojo;

public class User {

 private String name;

 private String address;

 private Integer age;

 private boolean adult;

 private Integer rateOfInterest;
 
 private String bankAccounts;
 
 public String getBankAccounts() {
  return bankAccounts;
 }

 public void setBankAccounts(String bankAccounts) {
  this.bankAccounts = bankAccounts;
 }

 @Override
 public String toString() {
  return "Name - " + this.name + "\nAddress -" + this.address + "\nAge" + this.age + "\nAdult - " + this.adult
    + "\nRate Of Interest - " + this.rateOfInterest;
 }

 public Integer getRateOfInterest() {
  return rateOfInterest;
 }

 public void setRateOfInterest(Integer rateOfInterest) {
  this.rateOfInterest = rateOfInterest;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public boolean isAdult() {
  return adult;
 }

 public void setAdult(boolean adult) {
  this.adult = adult;
 }
}


Client File
package com.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.pojo.User;

public class DecisionTableClient {

 public static final void main(final String[] args) throws InterruptedException {
  KieSession kieSession = new DecisionTableClient().execute();

  User user = new User();
  user.setName("Andy");
  user.setAge(62);
  user.setAddress("Japan");
  user.setBankAccounts("Axis");
  kieSession.insert(user);
  kieSession.fireAllRules();
  System.out.println("\n");
  System.out.println(
    "User Object After Firing Rules - \n"
      + user.toString());
  kieSession.dispose();
 }

 public KieSession execute() {
  KieServices ks = KieServices.Factory.get();
  KieContainer kc = ks.getKieClasspathContainer();
  KieSession kieSession = kc.newKieSession("DecisionTableKIESession");
  return kieSession;
 }

}

Friday, March 23, 2018

Spring Boot Configure Server Port and Context Path

5:53 AM 0
Spring Boot Configure Server Port and Context Path

By default, in Spring Boot the context path is "/" and server port is 8080.

In order to configure the context path or server port, application.properties need to be configured.

applicatin.properties is located in src/main/resources folder. Following entries are required to configure.

server.port=8088
server.contextPath=/myContectPath

There is one more way to update the context path while running the application from command line. Below is the code for the same -

java -jar -Dserver.contextPath=/myContectPath mySpringBoot.jar

Wednesday, March 21, 2018

Activation Group in Decision Table in Drools CSV Example

8:20 AM 0
Activation Group in Decision Table in Drools CSV Example

Drools rules can be defined in various formats one of which is in Decision Tables. Decision Tables rules are configured in an excel sheet in .xls format.

Decision Table is primarily used when there are rules that need to be configured for similar properties. For example, there is a rule implementation on age property of a user. Below are the examples:
If age > 18 && age < 22 then Do X
If age > 22 && age < 28 then Do Y
If age > 28 && age < 32 then Do Z
If age > 32 && age < 36 then Do A
If age > 36 && age < 48 then Do B
If age > 48 && age < 56 then Do C

If such rules are implemented in DRL files, then there will be lot of redundant code. Instead the same are configured in a decision table.

There are different components of a Decision table as mentioned below. The structure is similar to DRL file itself
1) Ruleset - Its similar to package from DRL file.
2) Import - To import comma separated Java packages that are used in rules.
3) Sequential - If set to true, rules will be fired in sequence from top to bottom.
4) Variables - One of more Global variables.
5) Functions - Functions used if any in rules.
6) Queries - Queries used if any in rules.

Rules configuration consists of below :
1) Rule Name
2) Description
3) CONDITION
4) ACTION

Along with above attributes, an Activation Group is added between the CONDITION and ACTION. Activation Group makes sure only one rule should fire under the activation group. In the below example, we will see the usage of Activation group in decision tables to create rules.

Here is the snapshot of Decision Table that is used in the example.






User.java - Pojo used for rules
package com.pojo;

public class User {

 private String name;

 private String address;

 private Integer age;

 private boolean adult;

 private Integer rateOfInterest;
 
 private String bankAccounts;
 
 public String getBankAccounts() {
  return bankAccounts;
 }

 public void setBankAccounts(String bankAccounts) {
  this.bankAccounts = bankAccounts;
 }

 @Override
 public String toString() {
  return "Name - " + this.name + "\nAddress -" + this.address + "\nAge" + this.age + "\nAdult - " + this.adult
    + "\nRate Of Interest - " + this.rateOfInterest;
 }

 public Integer getRateOfInterest() {
  return rateOfInterest;
 }

 public void setRateOfInterest(Integer rateOfInterest) {
  this.rateOfInterest = rateOfInterest;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public boolean isAdult() {
  return adult;
 }

 public void setAdult(boolean adult) {
  this.adult = adult;
 }
}


Client File
package com.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.pojo.User;

public class DecisionTableClient {

 public static final void main(final String[] args) throws InterruptedException {
  KieSession kieSession = new DecisionTableClient().execute();

  User user = new User();
  user.setName("Andy");
  user.setAge(62);
  user.setAddress("Japan");
  user.setBankAccounts("Axis");
  kieSession.insert(user);
  kieSession.fireAllRules();
  System.out.println("\n");
  System.out.println(
    "User Object After Firing Rules - \n"
      + user.toString());
  kieSession.dispose();
 }

 public KieSession execute() {
  KieServices ks = KieServices.Factory.get();
  KieContainer kc = ks.getKieClasspathContainer();
  KieSession kieSession = kc.newKieSession("DecisionTableKIESession");
  return kieSession;
 }

}

Monday, March 19, 2018

Drools Decision Table CSV Example

5:40 AM 0
Drools Decision Table CSV Example

Drools rules can be defined in various formats one of which is in Decision Tables. Decision Tables rules are configured in an excel sheet in .xls format.

Decision Table is primarily used when there are rules that need to be configured for similar properties. For example, there is a rule implementation on age property of a user. Below are the examples:
If age > 18 && age < 22 then Do X
If age > 22 && age < 28 then Do Y
If age > 28 && age < 32 then Do Z
If age > 32 && age < 36 then Do A
If age > 36 && age < 48 then Do B
If age > 48 && age < 56 then Do C

If such rules are implemented in DRL files, then there will be lot of redundant code. Instead the same are configured in a decision table.

There are different components of a Decision table as mentioned below. The structure is similar to DRL file itself
1) Ruleset - Its similar to package from DRL file.
2) Import - To import comma separated Java packages that are used in rules.
3) Sequential - If set to true, rules will be fired in sequence from top to bottom.
4) Variables - One of more Global variables.
5) Functions - Functions used if any in rules.
6) Queries - Queries used if any in rules.

Rules configuration consists of below :
1) Rule Name
2) Description
3) CONDITION
4) ACTION

In the below example, we will see the usage of decision tables to create rules.

Here is the snapshot of Decision Table that is used in the example.




User.java - Pojo used for rules
package com.pojo;

public class User {

 private String name;

 private String address;

 private Integer age;

 private boolean adult;

 private Integer rateOfInterest;
 
 private String bankAccounts;
 
 public String getBankAccounts() {
  return bankAccounts;
 }

 public void setBankAccounts(String bankAccounts) {
  this.bankAccounts = bankAccounts;
 }

 @Override
 public String toString() {
  return "Name - " + this.name + "\nAddress -" + this.address + "\nAge" + this.age + "\nAdult - " + this.adult
    + "\nRate Of Interest - " + this.rateOfInterest;
 }

 public Integer getRateOfInterest() {
  return rateOfInterest;
 }

 public void setRateOfInterest(Integer rateOfInterest) {
  this.rateOfInterest = rateOfInterest;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public boolean isAdult() {
  return adult;
 }

 public void setAdult(boolean adult) {
  this.adult = adult;
 }
}


Client File
package com.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.pojo.User;

public class DecisionTableClient {

 public static final void main(final String[] args) throws InterruptedException {
  KieSession kieSession = new DecisionTableClient().execute();

  User user = new User();
  user.setName("Andy");
  user.setAge(62);
  user.setAddress("Japan");
  user.setBankAccounts("Axis");
  kieSession.insert(user);
  kieSession.fireAllRules();
  System.out.println("\n");
  System.out.println(
    "User Object After Firing Rules - \n"
      + user.toString());
  kieSession.dispose();
 }

 public KieSession execute() {
  KieServices ks = KieServices.Factory.get();
  KieContainer kc = ks.getKieClasspathContainer();
  KieSession kieSession = kc.newKieSession("DecisionTableKIESession");
  return kieSession;
 }

}

Friday, March 9, 2018

Spring Boot Executable Jar File

3:21 AM 0
Spring Boot Executable Jar File

In this post, we will learn how to create executable Jar file in Spring Boot through Maven. The Jar file that we create through Maven is not an executable jar file i.e. it does not contains nested jars or external jars added through pom.xml in maven.

The jar file created using maven is not compatible enough to run from command line. In order to run from command line, the jar must include all the nested jars too.

To create an executable jar, spring-boot-maven-plugin need to be added in pom.xml along with the execution configuration repackage goal as shown below
<plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <version>1.4.0.RELEASE</version>
 <executions>
  <execution>
   <goals>
    <goal>repackage</goal>
   </goals>
  </execution>
 </executions>
</plugin>


The original structure of target folder includes the below structure. It clearly shows, the final jar that is created is only of 8KB i.e. it does not include any nested jars required by the developed classed.

Once the above execution is added, the structure will be changed as shown below


There will be a .jar.original file that is similar to JAR created through 1st mode. The second Jar file is the one that includes all the nested jars required to run the application.

If we open the Jar file using ZIP structure will contain below folders
1) BOOT-INF - It contains external libs and project classes.
2) META-INF
3) org.springframework.boot.loader - It includes all the load configuration.

The above execution needs to be manually added in plugin only if spring-boot-starter-parent is not included in the project POM. If spring-boot-starter-parent is added then spring-boot-maven-plugin can be configured without repackage goal.