Searching for a Member of the House

Once the graph is complete you can search for the given node and get its characteristics. In the exercise you will search for a member of the House and get his or her characteristics.

Prerequisites

See the topic Building the House of Bonaparte for the previous procedure.

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.tibco.tgdb.connection.TGConnection;
import com.tibco.tgdb.connection.TGConnectionFactory;
import com.tibco.tgdb.model.TGAttribute;
import com.tibco.tgdb.model.TGEdge;
import com.tibco.tgdb.model.TGEntity;
import com.tibco.tgdb.model.TGGraphObjectFactory;
import com.tibco.tgdb.model.TGKey;
import com.tibco.tgdb.model.TGNode;

/**
 * Search for a member in the House of Bonaparte graph 
 * and display the member attributes and children
 * 
 * Usage : java SearchGraph -memberName "Carlo Bonaparte" 
 * 
 */
public class SearchGraph {
	
	static String url = "tcp://127.0.0.1:8222";
	static String user = "napoleon";
	static String pwd = "bonaparte";
	
	static String memberName = "Napoleon Bonaparte";
	
	public static void main(String[] args) throws Exception {
		
		for (int i=0; i<args.length; i++) {
			if (args[i].equals("-memberName"))
				memberName = args[i+1];
		}
		
		TGConnection conn = null;
		try {
			conn = TGConnectionFactory.getInstance().createConnection(url, user, pwd, null);
			conn.connect();

			TGGraphObjectFactory gof = conn.getGraphObjectFactory();
			if (gof == null) {
				throw new Exception("Graph object not found");
			}

			conn.getGraphMetadata(true);
			
			TGKey houseKey = gof.createCompositeKey("houseMemberType");
			
			houseKey.setAttribute("memberName", memberName);
			System.out.printf("Searching for member '%s'...\n",memberName);
      		TGEntity houseMember = conn.getEntity(houseKey, null);
      		if (houseMember != null) {
      			SimpleDateFormat simpleFormat = new SimpleDateFormat("dd MMM yyyy");
      			System.out.printf("House member '%s' found\n",houseMember.getAttribute("memberName").getAsString());
      			for (TGAttribute attr : houseMember.getAttributes()) {
      				if (attr.getValue() == null)
      					System.out.printf("\t%s: %s\n", attr.getAttributeType().getName(), "");
      				else
      					System.out.printf("\t%s: %s\n", attr.getAttributeType().getName(), (attr.getValue() instanceof Calendar)?(simpleFormat.format(((Calendar)attr.getValue()).getTime())):attr.getValue());
      			}
      			for (TGEdge relation : ((TGNode)houseMember).getEdges()) {
      				if (relation.getVertices()[0].getAttribute("memberName") == houseMember.getAttribute("memberName") && relation.getAttribute("relType").getAsString().equals("child")) 
      					System.out.printf("\tchild: %s\n", relation.getVertices()[1].getAttribute("memberName").getAsString());	
      			}
      		} else {
      			System.out.printf("House member '%s' not found", memberName);
      		}
		}
		finally {
			if (conn != null)
				conn.disconnect();
		}
	}
}

Procedure

  1. Save the earlier provided Java code in a file named SearchGraph.java.
  2. Compile the code using the command javac -cp <tgdb_home>/lib/tgdb-client.jar SearchGraph.java
  3. Run the compiled file using the command java -cp <tgdb_home>/lib/tgdb-client.jar:. SearchGraph -memberName "Napoleon Bonaparte"
    The output of the program provides details of the member:
    Searching for member 'Napoleon Bonaparte'...
    House member 'Napoleon Bonaparte' found
    reignStart: 22 Jun 1815
    houseHead: false
    yearDied: 1821
    crownName: Napoleon I
    crownTitle: Emperor of the French
    memberName: Napoleon Bonaparte
    yearBorn: 1769
    reignEnd: 22 Jun 1815
    child: Francois Bonaparte
    
    Additionally, you can also search for other members of the House by passing another name using the parameter: SearchGraph -memberName "Carlo Bonaparte".
    Attention:
    • See how a TGKey was first defined and a node was received using the getEntity() call.
    • See how by using the node (member) searched, the edges (relation) were retrieved using the getEdges() call to get to the children of the searched node (member).

What to do next

See the topic Updating a Member of the House for the next procedure