Wednesday, November 2, 2016

Upload XML data to MySQL or NoSQL

DefaultHandler UploadFileString //read servlet input GoogleSaveRestoreTypes SqlSaveRestoreTypes CreateRestoreTypes RestoreXmlHandler parameter SAXParser RestoreXML - Google Datastore RestoreXML - MySQL db connection data cache RestoreXml environment UploadFile servlet HttpServlet class reference diagram draw diagrams logically from specification Use the specification to generate/inject objects UploadFileString: getUploadedFile new RestoreXmlHandler data store: getLists SaveRestoreTypes: save data store: add CreateRestoreTypes: createType saxParser.parse(handler) RestoreXML:execute UploadFile:processRequest control flow append buf value to current refvar value characters current typename = element name yes no current typename is null current refvar = element name no yes root element is null root element = element startElement //input record format <Typename> <Refvar>refvalue</Refvar> .... </Typename> build current typename record clear refvar value map set current typename null yes no element name is current typename error no yes element is root element save the data endElement saxParser handler api

Friday, April 11, 2014

Implementation analysis of Dijkstra's algorithm - shortest path to single source

Algorithmic Analysis and object oriented design of Dijkstra's algorithm - shortest path to single source start Data model datatypes, public api used: Graph<N, W> Collection<Node<N, W>> getNodeList() Node<N, W> Collection<Edge<N, W>> getAdjList() Edge<N, W> W getWeight() Node<N, W> getTarget() ShortestD<N, W> void initMax() void setWeight(Node<N, W> t, W w) W getWeight(Node<N, W> node) Prev<N, W> void setPrevious(Node<N, W> t, Node<N, W> s) SPQueue<N, W> //Fib/bin heap impl Node<N, W> removeMin() void addDecrease(Node<N, W> t, W w) Processing data public api ShortestD<N, W> getDistance() SPQueue<N, W> getQueue() Prev<N, W> getPrev() Node<N, W> getSource() Graph<N, W> getGraph() initialize: distance[graph nodes] to previous[graph nodes] to none distance[source node] to 0 add source node to queue while queue is not empty exit loop distance[v] is shortest distance previous[v] specifies shortest path remove minimum distance node u from queue for each adjacent edge e of u do exit loop v is target node of e v new distance := distance[u] + e weight if (v new distance < distance[v]) set: distance[v] := v new distance previous[v] := u add/update v, v new distance in Q draw flowcharts logically from specification Use the specification to generate objects

Thursday, December 5, 2013

Hierarchical software design example

application model Problem: Print customer names from a given file. Solution: Step 1: Create logical classes(data + tasks + services) to perform the pieces of the problem solution. --logical implementation of functional requirements. public class CustomerData { //process data private LinkedList<Customer> customerList; public CustomerData() { customerList = new LinkedList<Customer>(); } public final LinkedList<Customer> getCustomerList() { return customerList; } public final void addCustomer(Customer c) { getCustomerList().add(c); } } public class ReadCustomers { //service public readCustomers() { FileReader fr = new FileReader(getFilename()); BufferedReader br = new BufferedReader(fr); String name = br.readLine(); while (name != null) { Customer customer = new Customer(name); addCustomer(customer); name = br.readLine(); } } } public class PrintCustomers { //controller task public printCustomers() { for (Customer customer : getCustomerList()) { printCustomer(customer); } } } public class PrintCustomer { //terminal task public printCustomer(Customer customer) { System.out.println(customer.getName()); } } public class CustomersTask { //controller task public printCustomersTask() { readCustomers(); printCustomers(); } public static void main(String args[]) { String filename = args[0]; ........ CustomersTask pct = new CustomersTask(....); pct.printCustomersTask(); } } customersTask main printCustomers readCustomers filename injection printCustomer customerData Object dependency diagram To make the application complete, do the following 2.Connect these classes to make an extensible structure 3.Create object instances 4.write linking code from caller to callee Step 2. Create a structure by creating a set of connector (context) classes and then attatch the logical classes to these nodes, to create an extensible hierarchical structure. CustomersTask CustomersTaskContext ReadCustomers PrintCustomers PrintCustomersContext PrintCustomer class structure diagram public class PrintCustomersContext { //Connection example private CustomersTaskContext customersTaskContext; public PrintCustomersContext(CustomersTaskContext ctc) { this.customersTaskContext = ctc; } public final CustomersTaskContext getCustomersTaskContext() { return customersTaskContext; } } Step 3. Add object composition code to the classes. main CustomersTask CustomersTaskContext CustomerData ReadCustomers PrintCustomers PrintCustomersContext PrintCustomer class structure with object composition diagram public class CustomersTask { //object composition example private CustomersTaskContext customersTaskContext; private PrintCustomersContext printCustomersContext; private ReadCustomers readCustomers; private PrintCustomers printCustomers; public CustomersTask(CustomersTaskContext ctc) { this.customersTaskContext = ctc; printCustomersContext = new PrintCustomersContext(ctc); readCustomers = new ReadCustomers(ctc); printCustomers = new PrintCustomers(ctc); } } Step 4. Add the linking code. Here is the complete code.
public class CustomersTask {

    private CustomersTaskContext customersTaskContext;
    private PrintCustomersContext printCustomersContext;
    private ReadCustomers readCustomers;
    private PrintCustomers printCustomers;

    public CustomersTask(CustomersTaskContext customersTaskContext) {
        this.customersTaskContext = customersTaskContext;
        customersTaskInit();
    }

    public final void customersTaskInit() {
        printCustomersContext 
                = new PrintCustomersContext(getCustomersTaskContext());
        readCustomers = new ReadCustomers(getCustomersTaskContext());
        printCustomers = new PrintCustomers(getPrintCustomersContext());
    }

    public final void printCustomersTask() throws IOException {
        readCustomers();
        printCustomers();
    }

    public static void main(String args[]) {
        try {
            String filename = args[0];
            CustomersTaskContext ctc = new CustomersTaskContext();
            ctc.init(filename);
            CustomersTask ct = new CustomersTask(ctc);
            ct.printCustomersTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }

    private PrintCustomersContext getPrintCustomersContext() {
        return printCustomersContext;
    }

    private PrintCustomers getPrintCustomers() {
        return printCustomers;
    }

    private void printCustomers() {
        getPrintCustomers().printCustomers();
    }

    private ReadCustomers getReadCustomers() {
        return readCustomers;
    }

    private void readCustomers() throws IOException {
        getReadCustomers().readCustomers();
    }
}
public class PrintCustomers {

    private PrintCustomersContext printCustomersContext;
    private PrintCustomer printCustomer;

    public PrintCustomers(PrintCustomersContext printCustomersContext) {
        this.printCustomersContext = printCustomersContext;
        printCustomersInit();
    }

    public final void printCustomersInit() {
        printCustomer = new PrintCustomer(getPrintCustomersContext());
    }

    public final void printCustomers() {
        for (Customer customer : getCustomerList()) {
            printCustomer(customer);
        }
    }

    private PrintCustomersContext getPrintCustomersContext() {
        return printCustomersContext;
    }

    private PrintCustomer getPrintCustomer() {
        return printCustomer;
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return getPrintCustomersContext().getCustomersTaskContext();
    }

    private void printCustomer(Customer customer) {
        getPrintCustomer().printCustomer(customer);
    }

    private CustomerData getCustomerData() {
        return getCustomersTaskContext().getCustomerData();
    }

    private LinkedList getCustomerList() {
        return getCustomerData().getCustomerList();
    }
}
© Ravindra Mahajan
public class CustomersTaskContext {

    private String filename;
    private CustomerData customerData;

    public CustomersTaskContext() {
    }

    public void init(String filename) {
        this.filename = filename;
        this.customerData = new CustomerData();
    }

    public final String getFilename() {
        return filename;
    }

    public final CustomerData getCustomerData() {
        return customerData;
    }
}
public class ReadCustomers {

    private CustomersTaskContext customersTaskContext;

    public ReadCustomers(CustomersTaskContext customersTaskContext) {
        this.customersTaskContext = customersTaskContext;
    }

    public final void readCustomers() throws IOException {
        FileReader fr = new FileReader(getFilename());
        BufferedReader br = new BufferedReader(fr);
        String name = br.readLine();
        while (name != null) {
            Customer customer = new Customer(name);
            addCustomer(customer);
            name = br.readLine();
        }
        br.close();
    }

    private CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }

    private String getFilename() {
        return getCustomersTaskContext().getFilename();
    }

    private CustomerData getCustomerData() {
        return getCustomersTaskContext().getCustomerData();
    }

    private boolean addCustomer(Customer customer) {
        return getCustomerData().addCustomer(customer);
    }
}
public class PrintCustomersContext {

    private CustomersTaskContext customersTaskContext;

    public PrintCustomersContext(CustomersTaskContext ctc) {
        this.customersTaskContext = ctc;
    }

    public final CustomersTaskContext getCustomersTaskContext() {
        return customersTaskContext;
    }
}
public class PrintCustomer {

    private PrintCustomersContext printCustomersContext;

    public PrintCustomer(PrintCustomersContext pcc) {
        this.printCustomersContext = pcc;
    }

    public final void printCustomer(Customer customer) {
        System.out.println(customer.getName());
    }
}
public class CustomerData {

    private LinkedList customerList;

    public CustomerData() {
        customerList = new LinkedList();
    }

    public boolean addCustomer(Customer c) {
        return getCustomerList().add(c);
    }

    public LinkedList getCustomerList() {
        return customerList;
    }
}
public class Customer {

    private String name;

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}