When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it - this is knowledge. -Confucius-

The gift of fantasy has meant more to me than my talent for absorbing positive knowledge. -Albert Einstein-

Science investigates religion interprets. Science gives man knowledge which is power religion gives man wisdom which is control. -Martin Luther King, Jr.-

Those who have knowledge, don't predict. Those who predict, don't have knowledge. -Lao Tzu-

Our treasure lies in the beehive of our knowledge. We are perpetually on the way thither, being by nature winged insects and honey gatherers of the mind. -Friedrich Nietzsche-

Friday, September 19, 2014

Convert Sinhala Word-Net to Text Files



There are 3 main projects you have to configure before create the word-net flat file system for Sinhala. 1) Sinhala-WordNet-MongoDBToText 2) Sinhala-WordNet-API 3) Sinhala-WordNet-CSS When getting the Sinhala-WordNet-CSS project from the git chose not the master branch the branch but the individual_commits It has 5 projects 1) Sinhala-WordNet-CSS-Common 2) Sinhala-WordNet-CSS-DAL 3) Sinhala-WordNet-CSS-JWNL 4) Sinhala-WordNet-CSS-Model 5) Sinhala-WordNet-CSS-Util 6) Sinhala-WordNet-CSS-Web But the important one is the "Sinhala-WordNet-CSS-DAL" Okay now to successfully generate the flat files (i.e. to do MongoDB to Text transformation) we need to configure the below 3 projects 1) Sinhala-WordNet-CSS-DAL 2) Sinhala-WordNet-API 3) Sinhala-WordNet-MongoDBToText First locate the file "file_properties.xml" in Sinhala-WordNet-API-master\SinhalaWordNetAPI\src\extjwnl\src\main\resources\net\sf\extjwnl In the XML file find the Replace the value "XXXXXXXX" with the full file path to "Sinhala-WordNet-API-master\SinhalaWordNetAPI\data\clean-file" Then go to Sinhala-WordNet-API-master\SinhalaWordNetAPI\src\utilities\src\main\java\net\sf\extjwnl\utilities and find the "shakshara.java" file. Find the below code block, String propsFile = "XXXXXX"; and replace the value "XXXXXX" with the full path to "file_properties.xml" file. Now Run the App.java in "Sinhala-WordNet-MongoDBToText" project (in Sinhala-WordNet-MongotoText\SinhalaWordNetMongotoText\src\main\java\org\sinhala\wordnet\DBconvert\core) If it prints "done" or else run successfully then check the folder "Sinhala-WordNet-API-master\SinhalaWordNetAPI\data\clean-file" . there will be the flat files. you can view them by clicking and open through an editor

Thursday, September 4, 2014

The Senate Bus problem - Java Solution



Problem : This problem was originally based on the Senate bus at Wellesley College. Riders come to a bus stop and wait for a bus. When the bus arrives, all the waiting riders invoke boardBus, but anyone who arrives while the bus is boarding has to wait for the next bus. The capacity of the bus is 50 people; if there are more than 50 people waiting, some will have to wait for the next bus. When all the waiting riders have boarded, the bus can invoke depart. If the bus arrives when there are no riders, it should depart immediately. Puzzle: Write synchronization code that enforces all of these constraints.
 

import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *               Concurrent Programming
 *                       
 *           
 */
public class BusProblem {

    private static Counter waiting = new Counter(0);
    private static Semaphore mutex = new Semaphore(1);
    private static Semaphore bus = new Semaphore(0);
    private static Semaphore boarded = new Semaphore(0);

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of Riders (suggest a value larger than 100) : ");
        int n = sc.nextInt();
        System.out.println("");
        
        // Demonstrate the behaviour of the Bus. this thread's run method will keep running
        new Bus(n,waiting, mutex, bus, boarded).start();
        
        // We created 'n'number of riders to  demonstrate the behaviour of the riders pool 
        for (int i = 0; i < n; i++) {
            new Rider(i, waiting, mutex, bus, boarded).start();
        }

    }
}

class Bus extends Thread {

    private Counter waiting;
    private Semaphore mutex;
    private Semaphore bus;
    private Semaphore boarded;
    
    private int countNumberOfRidersWent;

    public Bus(int countNumberOfRidersWent,Counter waiting, Semaphore mutex, Semaphore bus, Semaphore boarded) {
        this.waiting = waiting;
        this.mutex = mutex;
        this.bus = bus;
        this.boarded = boarded;
        this.countNumberOfRidersWent=countNumberOfRidersWent;
    }

    @Override
    public void run() {

        while (true) {
            try {
                mutex.acquire();  // bus gets the mutex and holds it throughout the boarding process
                System.out.println("\nBus locked the BusStop");
            } catch (InterruptedException ex) {
                Logger.getLogger(Bus.class.getName()).log(Level.SEVERE, null, ex);
            }
            int n = Math.min(waiting.getCount(), 50);
            
            // remove the below line to get the infinite loop , and then remove block 'check'
            countNumberOfRidersWent-=n; // this is to easily demonstrate the problem
            System.out.println("Available Passengers = " + waiting.getCount() +"  and "+ n+ " will be boarded...");
            for (int i = 0; i < n; i++) {
                bus.release();  // signal bus is ready to get a passenger in
                try {
                    boarded.acquire(); // rider has boarded
                } catch (InterruptedException ex) {
                    Logger.getLogger(Bus.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //When all the riders have boarded, the bus updates waiting
            waiting.setCount(Math.max(waiting.getCount() - 50, 0));
            
            mutex.release(); // release the mutex

            System.out.println("Bus depart...\n");
            
            // block 'check'
            if(countNumberOfRidersWent==0){
                break;
            }
        }
    }
}

class Rider extends Thread {

    private int id;
    private Counter waiting;
    private Semaphore mutex;
    private Semaphore bus;
    private Semaphore boarded;

    public Rider(int id, Counter waiting, Semaphore mutex, Semaphore bus, Semaphore boarded) {
        this.waiting = waiting;
        this.mutex = mutex;
        this.bus = bus;
        this.boarded = boarded;
        this.id = id;
    }

    @Override
    public void run() {

        try {
            mutex.acquire();
        } catch (InterruptedException ex) {
            Logger.getLogger(Rider.class.getName()).log(Level.SEVERE, null, ex);
        }
        waiting.incrementCount();
        mutex.release();
        try {
            bus.acquire(); // waiting for the bus
        } catch (InterruptedException ex) {
            Logger.getLogger(Rider.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("rider got in to the bus..");
        boarded.release(); // got in to the bus

    }
}

// We cannot simply have a integer type variable for share amonge the threads. So we have to create 
// an object which can be shared among the threads. it is the Counter 
class Counter {

    private int count;

    public Counter(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    // reasign a value to the counter
    public void setCount(int count) {
        this.count = count;
    }

    // add 1 to the present value
    public void incrementCount() {
        this.count = ++count;
    }
}

 
==================================================================================================
     Concurrent Programming

==================================================================================================

class "BusProblem" contains the main method. 

If you are using the command prompt :
1) compile it using : javac BusProblem.java
2) run it using : java BusProblem
  - give the input parameter (rider count)
  
NOTE : the program is implemented to stop after all riders boarded to the bus. If you don't want that to happen just disable that by removing 

countNumberOfRidersWent-=n;  in line 69

line 87,88,89
// block 'check'
            if(countNumberOfRidersWent==0){
                break;
            }  
   
==================================================================================================

Wednesday, September 3, 2014

Simple Java Concurrent Program to handle a Thread Pool Using Semaphores




in a program there are multiple resources. And multiple users may be trying to access them at the same time. This will lead to various sort of problems which we have to consider when trying to create that application. That is the basic idea of concurrent process management. So the main objective to protect resources from multiple user access at once. to do that we may need to use locks and semaphores as we learn in the theories. Java has built in facility to create semaphores. like java.util.concurrent.Semaphore here is a simple example of the functionality of semaphore. Problem : there are equal number of threads which prints A and which print B. We need to print the pattern ABABAB....
 

import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Handle {

    // declare and initialize the semaphores
    private static Semaphore s1 = new Semaphore(0);
    private static Semaphore s2 = new Semaphore(1);
    
    public static void main(String[] args) {
        // make the threads and make them start.
        for (int i = 0; i < 10; i++) {
            new P1(s1, s2).start();
        }
        for (int i = 0; i < 10; i++) {
            new P2(s1, s2).start();
        }
    }
}

class P1 extends Thread {

    private Semaphore s1;
    private Semaphore s2;

    public P1(Semaphore s1, Semaphore s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public void run() {
        try {
            s2.acquire();// --1
        } catch (InterruptedException ex) {
            Logger.getLogger(P1.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.print("A");
        s1.release();
    }
}

class P2 extends Thread {

    private Semaphore s1;
    private Semaphore s2;

    public P2(Semaphore s1, Semaphore s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public void run() {
        try {
            s1.acquire();
        } catch (InterruptedException ex) {
            Logger.getLogger(P2.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.print("B");
        s2.release();
    }
}