import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.aop.PojoCache;
import propagation.*;
import propagation.impl.*;

//
// Beanshell script to run the aop tree cache demo
//
PojoCache createCache()
{
   tree = new PojoCache();
   config = new PropertyConfigurator();
   // configure tree cache.
   config.configure(tree, "META-INF/replSync-service.xml");
   tree.startService();  // kick start tree cache

   return tree;
}

PropagationManager initPm() throws Exception {
   pm_ = new PropagationManagerImpl();

   pm_.setRootNode("Japan");

   pm_.addNode("Japan", "Tokyo");   // Tokyo station

   pm_.addNode("Japan.Tokyo", "WindSensor1");   // Wind sensor device
   pm_.addStateItem("Japan.Tokyo.WindSensor1", 1000, "power supply", 1040); // power supply
   pm_.addStateItem("Japan.Tokyo.WindSensor1", 1001, "sensor unit", 1040); // sensor unit

   pm_.addNode("Japan.Tokyo", "RainSensor1");   // rain sensor device
   pm_.addStateItem("Japan.Tokyo.RainSensor1", 1002, "power supply", 1040); // power supply
   pm_.addStateItem("Japan.Tokyo.RainSensor1", 1003, "sensor unit", 1040); // sensor unit

   pm_.addNode("Japan", "Yokohama");   // Yokohama station

   pm_.addNode("Japan.Yokohama", "WindSensor2"); // wind sensor device
   pm_.addStateItem("Japan.Yokohama.WindSensor2", 1000, "power supply", 1040); // power supply
   pm_.addStateItem("Japan.Yokohama.WindSensor2", 1001, "sensor unit", 1040); // sensor unit

   pm_.addNode("Japan.Yokohama", "RainSensor2"); // rain sensor device
   pm_.addStateItem("Japan.Yokohama.RainSensor2", 1002, "power supply", 1040); // power supply
   pm_.addStateItem("Japan.Yokohama.RainSensor2", 1003, "sensor unit", 1040); // sensor unit

   pm_.createNode("WindSummary", "WindSummary"); // summary node for wind sensors in this network
   pm_.setUpperNode("WindSummary", "Japan.Tokyo.WindSensor1"); // assoication
   pm_.setUpperNode("WindSummary", "Japan.Yokohama.WindSensor2"); // association

   pm_.createNode("RainSummary", "RainSummary"); // summary node for rain sensor in this network.
   pm_.setUpperNode("RainSummary", "Japan.Tokyo.RainSensor1"); // association
   pm_.setUpperNode("RainSummary", "Japan.Yokohama.RainSensor2"); // association

   return pm_;
}

void printStatus(String msg, PropagationManager pm) {
   System.out.println("---------------------------------------------");
   System.out.println(msg);
   System.out.println("---------------------------------------------");
   pm.printNodes();
   System.out.println("\n\n");
}

// Start here
show();  // verbose mode

PojoCache tree = createCache();

PropagationManager pm = tree.getObject("/monitor");

// Output
printStatus("Initial state", pm);

// tree.stopService(); // will stop the cache

