A Simpleton Example
import org.datamanager.kernel.Simpleton;
import org.datamanager.kernel.Entity;
import org.datamanager.kernel.Pool;
import org.datamanager.constraint.Constraint;
import org.datamanager.constraint.ValueClassIsConstraint;
import org.datamanager.event.DataManagerEvent;
import org.datamanager.event.SetValueEvent;
import org.datamanager.exception.DataManagerException;
import org.datamanager.entityvalue.IntegerEntityValue;
import org.datamanager.entityvalue.StringEntityValue;
public class ExampleSimpleton extends Simpleton implements EventHandler
{
public void process()
{
try
{
//fetch the set of all entities in the default pool
//and just die if there are only 0 or 1 entities
Pool pool = Pool.getDefaultPool();
Entity[] entityList = pool.search(Constraint.NONE);
if (entityList.length < 2)
return;
Entity entity1 = entityList[0];
Entity entity2 = entityList[1];
//let me know whenever anyone changes entity1
ValueClassIsConstraint eventConstraint =
new ValueClassIsConstraint(SetValueEvent.class);
entity1.subscribe(this, eventConstraint);
//alter entity1's value
//and make entity2 an attribute of entity1
entity1.setValue(new IntegerEntityValue(5));
entity1.attach(entity2);
//create a new entity to hold a string
//and link it in as an attribute of entity2
Entity entity3 = Entity.create("my new entity");
entity3.setValue(new StringEntityValue("my marker"));
entity2.attach(entity3);
//note that entity3 can now be found by searches on the pool
//even though we haven't added it to the pool
//stop subscribing to entity1
entity1.unsubscribe(this);
}
catch (DataManagerException exception)
{
//do something when an exception happens
System.out.println(exception + " happened in kernel");
/*
Currently the kernel supports only one catchall
exception class, DataManagerException
, a subclass of
java.lang.Exception
,
and it (so far) carries no new methods of its own.
For now, simply put all your code in one try-catch block
and catch all DataManagerExceptions
.
*/
}
}
public void handle(DataManagerEvent event)
{
//someone changed entity1's value...
System.out.println(((Entity) event.getSource()).getName() +
" sent me an event");
}
}