// $Id: checkmap.java,v 1.4 2003/02/06 18:46:40 mdean Exp $


class checkmap
{
    static String colors[] = { "red", "blue", "green", "yellow", "pink", "cyan", "white" };

    static java.util.Hashtable colorMap = new java.util.Hashtable(); 

    static String getColor(Object key)
    {
	String retval = (String) colorMap.get(key);
	if (retval == null)
	    {
		retval = colors[colorMap.size()];
		colorMap.put(key, retval);
	    }
	return retval;
    }

    public static void main(String args[])
	throws Exception
    {
	// create map
	com.hp.hpl.mesa.rdf.jena.model.Model map = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
	com.hp.hpl.mesa.rdf.jena.model.Resource mapResource = map.createResource();
	map.add(mapResource,
		com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
		map_ont.Map);
	com.hp.hpl.mesa.rdf.jena.model.Resource layer = map.createResource();
	map.add(layer,
		com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
		map_ont.DrawingLayer);

	// read in UCP
	com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
	model.read("http://www.daml.org/2001/09/countries/fips");
	model.read("file:ucp-2003.owl");

	// iterate over countries
	com.hp.hpl.mesa.rdf.jena.model.ResIterator countries = model.listSubjectsWithProperty(com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
											      fips_10_4_ont.Country);
	while (countries.hasNext())
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource country = countries.next();
		int counter = 0;
		com.hp.hpl.mesa.rdf.jena.model.StmtIterator iterator = model.listStatements(new com.hp.hpl.mesa.rdf.jena.common.SelectorImpl(null,
																	     model.createProperty("http://www.daml.org/2003/02/ucp/ucp-2003#country"), 
																	     country));
		com.hp.hpl.mesa.rdf.jena.model.Resource combatantCommand = null;
		while (iterator.hasNext())
		    {
			com.hp.hpl.mesa.rdf.jena.model.Statement statement = iterator.next();
			combatantCommand = statement.getSubject();
			counter++;
		    }
		if (counter != 1)
		    System.err.println(country + " has " + counter + " statements");

		String code = country.getProperty(fips_10_4_ont.code).getString();
		String factbookPath = "file:/daml/factbook/" + code + ".daml";
		try {
		    model.read(factbookPath);
		    com.hp.hpl.mesa.rdf.jena.model.Resource coordinates = country.getProperty(factbook_ont.geographicCoordinates).getResource();
		    String latitude = coordinates.getProperty(factbook_ont.latitude).getString();
		    String longitude = coordinates.getProperty(factbook_ont.longitude).getString();
		    com.hp.hpl.mesa.rdf.jena.model.Resource point = model.createResource();
		    map.add(point,
			    com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			    map_ont.Point);
		    com.hp.hpl.mesa.rdf.jena.model.Resource location = model.createResource();
		    map.add(location,
			    com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			    map_ont.Location);
		    map.add(location,
			    map_ont.latitude,
			    latitude);
		    map.add(location,
			    map_ont.longitude,
			    longitude);
		    map.add(point,
			    map_ont.location,
			    location);
		    map.add(point, // DAML Map currently seems to require
			    map_ont.label,
			    code);
		    map.add(point,
			    map_ont.color,
			    getColor(combatantCommand));
		    map.add(layer,
			    map_ont.object,
			    point);
		} catch (Exception e) {
		    System.err.println(factbookPath + ":  " + e);
		    e.printStackTrace();
		}
	    }

	// serialize map
	map.write(new java.io.PrintWriter(new java.io.FileOutputStream("checkmap.daml")));
    }
}
