Tutorial: Building A Domain

Tutorials > Building A Domain > Part 3

Generating a domain

Now that we've defined our walls, actions, and propositional functions, we can tie everything together into a fully functional domain via the generateDomain() method.

public Domain generateDomain() {
	
	if(DOMAIN != null){
		return DOMAIN;
	}
	

	DOMAIN = new Domain();
	

	generateMap();
	
	//Creates a new Attribute object
	Attribute xatt = new Attribute(DOMAIN, ATTX, Attribute.AttributeType.DISC);
	xatt.setDiscValuesForRange(0, MAXX, 1);
	
	Attribute yatt = new Attribute(DOMAIN, ATTY, Attribute.AttributeType.DISC);
	yatt.setDiscValuesForRange(0, MAXY, 1);
	
	
	ObjectClass agentClass = new ObjectClass(DOMAIN, CLASSAGENT);
	agentClass.addAttribute(xatt);
	agentClass.addAttribute(yatt);
	
	ObjectClass goalClass = new ObjectClass(DOMAIN, CLASSGOAL);
	goalClass.addAttribute(xatt);
	goalClass.addAttribute(yatt);
	
	Action north = new NorthAction(ACTIONNORTH, DOMAIN, "");
	Action south = new SouthAction(ACTIONSOUTH, DOMAIN, "");
	Action east = new EastAction(ACTIONEAST, DOMAIN, "");
	Action west = new WestAction(ACTIONWEST, DOMAIN, "");
	
	PropositionalFunction atGoal = new AtGoalPF(PFATGOAL, DOMAIN,
			new String[]{CLASSAGENT, CLASSGOAL});
	
	return DOMAIN;
}
	
				

We begin by simply creating a generic insitantiation of a Domain object. We then define our X and Y attributes as distcrete values within the range of 0 and the maximum dimensions we defined in part 1 of this tutorial. Note that we only require one type of attribute despite the fact that they will be used to define both agent and goal objects. Next, we define two classes of objects from which agents and goals can be created. Of course, for our purposes there will only be one of each, but that is not known at this stage. The purpose of generateDomain() is to create all the elements necessary for the user to create a version of the domain to his/her own specficication. Finally, the action and goal classes we defined earlier are instatiated and attached to our specific domain. Note that this is also where we define what the parameters are for our propositional function.

Using the domain

At this point, all that remains to be done is to write a main function that will allow us to properly use and test our domain. to that end, add the following code.

public static void main(String[] args) {

	FourRoomsDomain frd = new FourRoomsDomain();
	
	Domain d = frd.generateDomain();
	
	State s = new State();
	
	s.addObject(new ObjectInstance(DOMAIN.getObjectClass(CLASSAGENT), CLASSAGENT+0));
	s.addObject(new ObjectInstance(DOMAIN.getObjectClass(CLASSGOAL), CLASSGOAL+0));

	ObjectInstance agent = s.getObjectsOfTrueClass(CLASSAGENT).get(0);
	agent.setValue(ATTX, 5);
	agent.setValue(ATTY, 5);

	ObjectInstance goal = s.getObjectsOfTrueClass(CLASSGOAL).get(0);
	goal.setValue(ATTX, 5);
	goal.setValue(ATTY, 5);

		
	TerminalExplorer exp = new TerminalExplorer(d);
	exp.addActionShortHand("n", ACTIONNORTH);
	exp.addActionShortHand("e", ACTIONEAST);
	exp.addActionShortHand("w", ACTIONWEST);
	exp.addActionShortHand("s", ACTIONSOUTH);
		
		
}
				

We first intialize our own FourRoomsDomain object as well as a regular Domain object using generateDomain(). Once this is done, we must create an initial state for our experiment to start in. This is done by first creating an blank State object and adding instances of the our agent and goal objects. In this case there is only one of each, but for domains with more than one object of the same type, be sure to give each ObjectInstance a unique name. We do this by appending a number to the end of the name ("agent0" for example). At this point, our agent and goal do not have functional coordinates, so we set them to our liking. Note that the getObjectsOfTrueClass() method returns a list of ObjectInstances and we are assuming that the first object in that list is the one we want. If you have more than one object of the same type, you may want to search for individual objects by name using getObject(). Finally, we end by initializing a terminal explorer for our domain object so that we can interact with it in real time.

And that's all! Running this main should create the domain and allow you to interact with it using the N, S, E, and W keys. A more robust version of the code found in this tutorial can be found in *insert wherever the distribution copy of FourRoomsDomain.java is*

End.