Monday, December 5, 2011

Generate XSD with Trang and Parsing XML with JAXB

The JAXB binding compiler helps us to generate Java classes from a given XSD. It generates a collection of Java classes based on the provided XSD.
First you need to have the xml file to be parsed from JAXB.
The sample i am going to use is info.xml and it is as follows:

Trang can be used to infer an XSD from an XML document. The steps are as follows:

1. Download the Trang distribution zip file and extract it to a location in your machine.
2. Go to the root folder of Trang in the command prompt and type the command to generate XSD from the given XML file as mentioned below:

java -jar trang.jar info.xml info.xsd

(Note: I have copied the sample xml file to the Trang root directory and i am expeting the output xsd file to be created in the same place)

The generated XSD is as follows:

Now in order to parse the info.xml file we can use JAXB to input the generated info.xsd and generate the classes.

The command line tool "xjc" can be used to run the JAXB compiler. The command to generate the classes is as follows:

xjc info.xsd -p com.pu.jaxb.sample

The output of the above command will be a folder structure "com.pu.jaxb.sample" with 4 classes:
ObjectFactory.java
Education.java
Info.java
Institute.java

In order to Unmarshall the info.xml file i have written the code segment depicted below:


Now with the use of info instance you can access all the values defined in XML file.

Monday, July 11, 2011

How to check whether a given graph is two colorable in a way that no two neighbors have the same color???



This is the result:

First you need to represent the graph with adjacency matrix as follows:
(Also you can represent the graph with adj ency list as well )

0,1,0,0,1,0,0
1,0,0,0,0,1,0
0,0,0,1,0,1,0
0,0,1,0,0,0,1
1,0,0,0,0,0,0
0,1,1,0,0,0,0
0,0,0,1,0,0,0

Save this as a CSV file.This is the input to my program. I have used BFS algorithm for the implementation:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ColourFinder {

private static Integer[][] aMatrix = null;
private static int size = 0;
private static Map nodes = new HashMap();
private static List queue = new ArrayList();

public static enum Colour {
NO_COLOR, BLACK, RED
}

/**
* @param args
*/
public static void main(String[] args) {
String path = null;
boolean run = true;
while (run) {
System.out
.print("Enter csv file path containing the graph representation in ajacency matrix: ");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
try {
path = br.readLine();
run = false;
} catch (IOException e) {
System.out.println("Error occured! Please try again." + e);
}
}

setMatrix(path);
createNodes();
BFS();
}

private static void BFS() {
Node node;
Node ajacentNode;
Colour colour;
Integer[] aMatrixRow;
Colour nbrColour;

node = nodes.get(1);
node.setColour(Colour.RED);
System.out
.println("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n Source node: "
+ node);
enqueue(node);
while (queue.size() != 0) {
node = dequeue();
System.out
.println("=======================================================\n Current node: "
+ node);
colour = node.getColour();
aMatrixRow = aMatrix[node.getNodeNo() - 1];
for (int i = 0; i < aMatrixRow.length; i++) {
if (aMatrixRow[i] == 1) {
ajacentNode = nodes.get(i + 1);
System.out.println("Neighbor node initial colour: "
+ ajacentNode);
nbrColour = ajacentNode.getColour();
if (!nbrColour.equals(colour)) {
if (nbrColour.equals(Colour.NO_COLOR)) {
CheckColours(ajacentNode, colour);
enqueue(ajacentNode);
System.out.println("Neighbor node after coloured: "
+ ajacentNode);
} else {
System.out
.println("Neighbor node already has opposite colour: "
+ ajacentNode);
}
} else {
System.out
.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n Neighbor node colour is simillar to it's neighbor "
+ ajacentNode);
System.out
.println("Therefore Graph is not 2-colorable");
System.exit(0);
}
}
}
}
System.out
.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n Graph is 2-colorable");
}

private static void CheckColours(Node ajacentNode, Colour colour) {
switch (colour) {
case RED:
ajacentNode.setColour(Colour.BLACK);
break;
case BLACK:
ajacentNode.setColour(Colour.RED);
break;
}
}

private static void enqueue(Node node) {
queue.add(node);
}

private static Node dequeue() {
Node node = queue.get(0);
queue.remove(0);
return node;
}

private static void createNodes() {
Node node;
for (int i = 0; i < size; i++) {
node = new Node();
node.setColour(Colour.NO_COLOR);
node.setNodeNo(i + 1);
nodes.put(i + 1, node);
}

}

private static void setMatrix(String path) {

int j = 0;
String[] row;
File file;
Scanner scan;
try {
file = new File(path);
scan = new Scanner(file);
System.out.println("Ajacency Matrix for the graph:\n");
while (scan.hasNext()) {
System.out.println((size++) + "| " + scan.next());
}
aMatrix = new Integer[size][size];

scan = new Scanner(file);

while (scan.hasNext()) {
row = scan.next().split(",");
if (size == row.length) {

for (int i = 0; i < row.length; i++) {
aMatrix[j][i] = new Integer(row[i]);
}
j++;

} else {
System.out.println("Incorrect ajacency matrix in csv file "
+ file.getAbsolutePath());
aMatrix = null;
System.exit(0);
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}

}

public static class Node {
private Colour colour;
private int nodeNo;

public Colour getColour() {
return colour;
}

public void setColour(Colour colour) {
this.colour = colour;
}

public int getNodeNo() {
return nodeNo;
}

public void setNodeNo(int nodeNo) {
this.nodeNo = nodeNo;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return nodeNo + "->" + colour;
}
}

}