Author Archives: yogsma

Building a Saas application

This is a brainstorming post where I will jot down the ideas to build a saas application. Before we start, we have to go to basics.

What is Saas?

Software as a service (Saas) is a software delivery model. In this model, the software is served through subscription service. Saas has been popular for more than a decade now. In fact, the sales of such software have skyrocketed that building simple software has become easier. From project management to ordering healthy food, we can get any of these services through software with a subscription.

Now what do we want to build and how do we start?

Of course, this is not an easy question to answer in a single post. You have to go through trial and errors to build a viable product that people will use it. But also what and who are we targeting as an audience. There are a lot of broader areas to think about to build a product. That would make the entire process to build a software way too complex. So where do we start? The eternal question still remains.

Human psychology over the years has progressed and helped technology to build a lot of cool products. With AI has been knocking on our doors, what we build today, will be obsolete in the next ten years. Based on your own experience, what I have found, is that you look into your own daily life. When you go for grocery shopping when you talk to your friends, coworkers. The moment, you feel frustrated anything that is not in your control, that’s where you have something to build on.

I know it sounds ridiculously easy to write here in the post, but not easy when you are living life. What I am trying to point is, look at problems you or other human faces and if that problem can be solved through software, you have got a viable product idea.  At every pain point, the problem is an idea to build a product. Simple example – Elon Musk was driving on LA roads, he was caught in traffic which didn’t move for a long time. How do we improve our traffic? With increasing cars and population, this is almost going to be a nightmare in the future. He realized the problem and started a company called The Boring Company that would build underground tunnels for handling traffic.

If you are like me who works in a software company, it is easy to see through this dilemma to build a solution that can help you and other developers equally. But in a larger context, you can always go through different Saas services and hear the feedback from those services’ users. Any negative feedback is your path to build a product. Assuming we got the idea to build a Saas application, so how do we proceed further?

Post-idea discussion

Once we have a solid idea, we can think about building a minimum viable product which gives customers a chance to explore the product with minimum fuss. Less complex the product for customers to use intuitively, better will be their experiences and happier they will be to recommend your product to others.

You should work to create a minimum viable design. This will be an alpha version of the product. Getting alpha version out of the door in minimum time will give you a better idea of where to focus on scaling the product in the future. This will also save time and money.

Technology and Frameworks

Once we have the initial design of the minimum product, we can think of what technology and framework to use. What kind of infrastructure to use? Considering the less expensive options, the cloud is very popular to use to build a Saas product. This reduces the management of infrastructure while giving high availability and scalability. Amazon, Google, and Microsoft all these companies offer cloud solutions to build your application. Also if you want to scale your application in the future for data-intensive, the cloud is the best option to handle all kinds of load.

For backend, there are different frameworks available based on C#, Python, or Java. Since I have worked on Java, I vouch for Spring which offers a lot of flexibility and ease to add a lot of code easily. Of course, there is a learning curve if you have never used spring before. For the database, we have two major options, one is SQL based database or NoSQL. If it is data-intensive application, NoSQL makes more sense.

On the frontend side, angularjs offers a lot of ease to build a modern user interface to interact with backend.

Conclusion

There are a lot of other factors we have not considered in this discussion especially related to the performance and health of the application. Also, we didn’t discuss any major approaches to build the application. I hope this brainstorming post will give readers an idea of a saas application that they can build.

If you have an idea of saas application and you intend to build it, let me know how it goes for you. You can subscribe to my blog.

 

How to implement a chatbot in Java

So we are back on our discussion about chatbots. I will not talk about the basics of chatbots that I covered here. I will directly start showing how to implement a chatbot in Java. We are going to use AIML (Artificial Intelligence Markup Language) library for this implementation. This library is opensource and provided by google.

chatbot in Java

A maven project

As a first step, let’s create a maven project in Eclipse with groupId com.betterjavacode and artifactId as chatbot. Once the project is created, we can add ab.jar to project by adding the respective dependency in maven pom.xml  like below:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.betterjavacode</groupId>
  <artifactId>chatbot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>JavaChatbot</name>
  
  <dependencies>
    <dependency>
        <artifactId>com.google</artifactId>
        <groupId>Ab</groupId>
        <version>0.0.4.3</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/Ab.jar</systemPath>
    </dependency>
</dependencies>
</project>

Google library for AIML provides default AI rules to use to implement chatbot. We will add these rules in resources directory of our project. Copy bots folder from program-ab directory into resources folder.

Chatbot Program

Now we will write the chatbot program which will be part of main method. In simple terms, once we invoke this program through main method, it will be in an infinite loop. An input command will wait for user input and then based on our aiml library chatbot will answer to what an user had input.


try {
            String resourcesPath = getResourcesPath();
            System.out.println(resourcesPath);
            MagicBooleans.trace_mode = TRACE_MODE;
            Bot bot = new Bot("Mr. Chatter", resourcesPath);
            Chat chatSession = new Chat(bot);
            bot.brain.nodeStats();
            String textLine = "";
            while (true) {
                System.out.println("human: ");
                textLine = IOUtils.readInputTextLine();
                if ((textLine == null) || (textLine.length() < 1))
                    textLine = MagicStrings.null_input;
                if (textLine.equals("q")) {
                    System.exit(0);
                } else if (textLine.equals("wq")) {
                    bot.writeQuit();
                    System.exit(0);
                } else {
                    String request = textLine;
                    if (MagicBooleans.trace_mode)
                        System.out.println("STATE=" + request + ":THAT=" + ((History) chatSession.thatHistory.get(0)).get(0) + ":TOPIC=" + chatSession.predicates.get("topic"));
                    String response = chatSession.multisentenceRespond(request);
                    while (response.contains("<"))
                        response = response.replace("<", "<"); while (response.contains(">")) response = response.replace(">", ">");
                    System.out.println("Robot : " + response);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

Now if we run this program, it will show us input to ask a question to the chatbot Mr. Chatter.

Conclusion

In this article, we showed how to add a chatbot in Java. Similarly, we can enhance this program by adding custom patterns that the chatbot can respond to.

References

Chatbot Implementation

Chatbots and more

What are chatbots? This is not the regular programming post, but more of a discussion post and where we are heading with our technology. Alexa, Google Home, Cortona, and the number of personal assistants are available at our perusal. With these kinds of products, we are slowly evolving into artificial intelligence-driven technology. A lot of manual labor jobs might be in danger in the near future. Politics aside, I am more interested to understand this topic from technology and humane perspective. While we still struggle with a lot of other ethical issues with existing technology, AI will only create social conundrums.

What I want to discuss in this post, is more about chatbots. You can think of this as a scribbling post. I am just bringing some ideas forefront to build a chatbot using java.

What are chatbots?

Chatbots are a crude version of personal assistants. Personal assistants help you in many ways, in-process saving you time, providing you leisure. The simplest version of these chatbots is those that answer your questions like “What’s the weather like today?”, a chatbot will connect to a weather website to find out today’s weather and then answer you accordingly. Similarly, on an eCommerce site, I can ask a question by typing “Where can I find this book?”, the chatbot will answer “In literature and short stories section”. A chatbot can also help build customer support, taking away the traditional customer support people. In a more advanced version, the same chatbots can build a library based on your likings, dislikings, answers, and provide more options for lifestyle.

Wikipedia definition says

A chatbot is a computer program that conducts a conversation via auditory or textual methods.

Chatbots are part of Artificial intelligence that has been popularized these days.

Design of chatbots

In this article, we will not be showing any code for chatbots, but we will be building chatbots in the next post. This is a post where we bring the idea of a chatbot into the design. As we discussed the definition of a chatbot, we will be building an agent that will chat with us in a natural language that we use for daily communication.

Me: “Hi Mr. Chatbot, how are you today?”

Mr. Chatbot: “I am fine, Mr. Mali. Thank You”

Me: “What’s the day today?”

Mr. Chatbot: “It’s Wednesday today.”

This is an example of a conversation about how a chatbot would answer. We will build a database that will have natural language processing ability to find out what question has been asked and based on that answer the question as accurately as possible. This chatbot is an experimental build-up.

Does it mean it can falter? Glad you ask that it definitely means it can answer erratic. But it’s ok in our experimentation world, even Google home had his off days.

We will need a chat engine and we will be using plain English to type our messages. We will use AIML (Artificial Intelligence Markup Language) to build this chatbot.

In conclusion, I would provide the implementation of this chatbot in the next few posts. We will have more discussion about chatbots in future articles. If you enjoyed this post, subscribe to my blog here.

 

References

  1. AIML
  2. Chatbot

 

Design Pattern – Adapter Pattern – Part VII

Till now, we have discussed all creational types of design patterns. In this post, we will be creating a demo about structural design patterns. In this series, our first design pattern is Adapter design pattern. As said, this design pattern is structural design pattern. This design pattern combines the capabilities of two independent interfaces. It basically acts like a bridge between two incompatible interfaces.

Easiest example to understand adapter pattern in real life is the electric socket in different continents provide different voltages. A traveler from Asia can use an adapter in Europe to get 240 V of electricity for electronic devices.

When to use Adapter Design Pattern?

Firstly, when a client expects different interface than available, at that time adapter pattern can help to convert a interface of a class into another interface that the client can use. However, Adapter pattern allows reuse of lot of code and it is one of the major reasons why it is most favorable among software engineering. Similarly, a real example you would find in JDK libraries of InputStreamReader and OutputStreamWriter.

How to use Adapter Design Pattern?

So in this implementation, we will show how to use Adapter design pattern. We have a traveler from Asia traveling in Europe, he wants to use his electronic device which needs 240 V of electricity from socket, but Europe only provides 120 V of electricity. We will design an adapter class that will convert 120 V of electricity to 240 V of electricity.

Our target class or client class is AsiaSocket, as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class AsiaSocket {

    public void provideElectricity() {
        System.out.println("Provide electricity of 240 V");
    }
}

It’s a simple class with a method provideElectricity.

Our adaptee class is EuropeSocket which implements an interface IEuropeSocket as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeSocket implements IEuropeSocket {

    public void getElectricity() {
        System.out.println("Get electricity of 120 V");
    }

}

Secondly, we will implement an Adapter class that will provide adapter between Europe and Asia Socket classes. This will look like below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeAsiaAdapter implements IEuropeSocket {

    AsiaSocket asiaSocket;

    public EuropeAsiaAdapter(AsiaSocket asiaSocket) {
        this.asiaSocket = asiaSocket;
    }

    public void getElectricity() {
        asiaSocket.provideElectricity();
    }

}

This class has a constructor to instantiate AsiaSocket and implements IEuropeSocket interface.

Now in our demo class, we will show how to use this adapter pattern.


package com.betterjavacode.designpatterns;

import com.betterjavacode.designpatterns.adapterexample.AsiaSocket;
import com.betterjavacode.designpatterns.adapterexample.EuropeAsiaAdapter;
import com.betterjavacode.designpatterns.adapterexample.EuropeSocket;
import com.betterjavacode.designpatterns.adapterexample.IEuropeSocket;

public class AdapterDemo {

    public static void main(String[] args) {
        EuropeSocket es = new EuropeSocket();

        AsiaSocket as = new AsiaSocket();

        IEuropeSocket europeAsiaAdapter = new EuropeAsiaAdapter(as);

        System.out.println(" Electricity in Asia ");
        as.provideElectricity();

        System.out.println(" Electricity in Europe ");
        es.getElectricity();

        System.out.println(" Electricity for Asia in Europe");
        europeAsiaAdapter.getElectricity();

    }

}

Therefore, if you run this demo class, the output will show that we will be getting electricity of 240 V for Asian electronic devices in Europe.

Download

In conclusion, we showed how to use the Adapter pattern. The demo code will be available on GitHub repository here.

References

  1. Adapter Design Pattern
  2. Adapter Design Pattern in Java

Design Pattern – Prototype Pattern – Part VI

In this post, I want to show how to use Prototype design pattern. If you want to read about previous posts related to design patterns, series of posts about design patterns are

  1. Introduction to design patterns
  2. Singleton Pattern
  3. Factory Pattern
  4. Abstract Factory Pattern
  5. Builder Pattern

The prototype design pattern will cover the creation design pattern that we have been writing about till now.

When to use?

Since this is a creational design pattern, this is used when decision is to reduce the creation cost of object in a standard way. There can be argument about how this is then different from abstract factory pattern. The key benefit of Prototype design pattern is that it optimizes the use case where multiple objects of same type will have mostly same data.

Major example is reading configuration data from a file/database over a network. Also if you want to hide the logic of creating new instance from the client.

How to use?

Firstly, in this pattern, there is an interface of Prototype that has method for clone and any concrete class implementing this interface, implements the method to clone the object.

 

public interface Car {

Car clone();

}

We have an interface Car which we will implement in our concrete classes.

package com.bettterjavacode.designpatterns.prototypeexample;

public class Toyota implements Car 
{

    private final String CARNAME = "Toyota";

    public Car clone() 
    {
       return new Toyota();
    }

    @Override
    public String toString() 
    {
      return CARNAME;
    }

}

 

We will have a factory class that will get us a prototype based on type of object we have passed. This will look like below:

package com.bettterjavacode.designpatterns.prototypeexample;

import java.util.HashMap;
import java.util.Map;

public class CarFactory
{

   private static final Map<String, Car> prototypes = new HashMap<String, Car>();

   static 
   {
     prototypes.put("toyota", new Toyota());
     prototypes.put("lexus", new Lexus());
     prototypes.put("bmw", new BMW());
   }

   public static Car getPrototype(String type) 
   {
      return prototypes.get(type).clone();
   }
}

 

Therefore, our demo class will pass the type of car as an argument to print the carname. That will look like below:

package com.betterjavacode.designpatterns;

import com.bettterjavacode.designpatterns.prototypeexample.Car;
import com.bettterjavacode.designpatterns.prototypeexample.CarFactory;

public class PrototypeDemo 
{
   public static void main(String[] args) 
   {
      if (args.length > 0) 
      {
         for (String type : args) 
         {
            Car prototype = CarFactory.getPrototype(type);
            if (prototype != null) 
            {
               System.out.println(prototype);
            }
         }
      } 
      else 
      {
         System.out.println(" Run again with arguments");
      }
   }
}

Conclusion

In conclusion, we showed how to use prototype design pattern. The code for this is available here

References

  1. Design Patterns – Prototype
  2. Prototype Pattern