package spcl.data;

import spcl.data.*;
import java.util.*;
import java.io.*;

public class thisZonemyPolicyInstaller extends PolicyInstaller {

	public thisZonemyPolicyInstaller( ) {
	}

	public void doInstall() throws PolicyException {
		if (null==policyLoader) 
			throw new PolicyException("No policy loader defined.", 
			  PolicyException.NULL_LOADER);
		final Zone zone = new Zone( "thisZone" );
		final Policy policy = new Policy( "thisZone.myPolicy" );

		final Principal default_p = new Principal( "default_p" );
		policy.setDefaultPrincipal( default_p );

		final Rule thisZone_myPolicy_default_p_1 = new Rule( "thisZone.myPolicy.default_p.1" );
		thisZone_myPolicy_default_p_1.setActive( true );
		thisZone_myPolicy_default_p_1.setAccess( spcl.data.Rule.DENY );
		thisZone_myPolicy_default_p_1.setAction( spcl.data.Rule.STAR );
		thisZone_myPolicy_default_p_1.setOwner( default_p );
		default_p.addRule( thisZone_myPolicy_default_p_1 );

		final SystemObject fridge = new SystemObject( "fridge" );
		policy.addObject( fridge);
		final Action thisZone_myPolicy_fridge_b = new Action( "b" );
		thisZone_myPolicy_fridge_b.setActionPattern( "");
		fridge.addAction(thisZone_myPolicy_fridge_b);

		final SystemObject O = new SystemObject( "O" );
		policy.addObject( O);
		final Variable thisZone_myPolicy_O_q = new Variable("q", Variable.NUMBER, "3");
		O.addVariable(thisZone_myPolicy_O_q);
		final Action thisZone_myPolicy_O_a = new Action( "a" );
		thisZone_myPolicy_O_a.setActionPattern( "get");
		O.addAction(thisZone_myPolicy_O_a);
		final Action thisZone_myPolicy_O_c = new Action( "c" );
		thisZone_myPolicy_O_c.setActionPattern( "zba");
		O.addAction(thisZone_myPolicy_O_c);

		final Group friends = new Group( "friends" );
		policy.addGroup( friends );

		final Rule thisZone_myPolicy_friends_2 = new Rule( "thisZone.myPolicy.friends.2" );
		thisZone_myPolicy_friends_2.setActive( true );
		thisZone_myPolicy_friends_2.setAccess( spcl.data.Rule.DENY );
		thisZone_myPolicy_friends_2.setAction( spcl.data.Rule.STAR );
		thisZone_myPolicy_friends_2.setOwner( friends );
		friends.addRule( thisZone_myPolicy_friends_2 );

		final Group enemies = new Group( "enemies" );
		policy.addGroup( enemies );

		final Principal john = new Principal( "john" );
		policy.addPrincipal( john );
		friends.addMember(john);
		enemies.addMember(john);

		final Principal P1 = new Principal( "P1" );
		policy.addPrincipal( P1 );

		final Rule thisZone_myPolicy_P1_5 = new Rule( "thisZone.myPolicy.P1.5" );
		thisZone_myPolicy_P1_5.setActive( true );
		thisZone_myPolicy_P1_5.setAccess( spcl.data.Rule.ALLOW );
		thisZone_myPolicy_P1_5.setAction( thisZone_myPolicy_O_a );
		thisZone_myPolicy_P1_5.setObject( O );
		thisZone_myPolicy_P1_5.setOwner( P1 );
		P1.addRule( thisZone_myPolicy_P1_5 );
		thisZone_myPolicy_P1_5.addCondition(thisZone_myPolicy_O_q,"<", "4");

		final Rule thisZone_myPolicy_P1_5_3 = new Rule( "thisZone.myPolicy.P1.5.3" );
		thisZone_myPolicy_P1_5_3.setActive( false );
		thisZone_myPolicy_P1_5_3.setAccess( spcl.data.Rule.DENY );
		thisZone_myPolicy_P1_5_3.setAction( spcl.data.Rule.STAR );
		thisZone_myPolicy_P1_5_3.setObject( fridge );
		thisZone_myPolicy_P1_5_3.setOwner( john );
		john.addRule( thisZone_myPolicy_P1_5_3 );

		thisZone_myPolicy_P1_5.addFiring(
			new Firing(){
				public void doFire() {
					if (policyLoader.resolveCondition( spcl.data.PolicyLoader.SYSTEM_TIME, ">", "10:00 am")) {
						thisZone_myPolicy_P1_5_3.setActive( true );
					}
				}
			}
		);

		final Rule thisZone_myPolicy_P1_5_4 = new Rule( "thisZone.myPolicy.P1.5.4" );
		thisZone_myPolicy_P1_5_4.setActive( false );
		thisZone_myPolicy_P1_5_4.setAccess( spcl.data.Rule.ALLOW );
		thisZone_myPolicy_P1_5_4.setAction( spcl.data.Rule.STAR );
		thisZone_myPolicy_P1_5_4.setObject( O );
		thisZone_myPolicy_P1_5_4.setOwner( enemies );
		enemies.addRule( thisZone_myPolicy_P1_5_4 );

		thisZone_myPolicy_P1_5.addFiring(
			new Firing(){
				public void doFire() {
					if (!policyLoader.resolveCondition( spcl.data.PolicyLoader.SYSTEM_TIME, ">", "10:00 am")) {
						thisZone_myPolicy_P1_5_4.setActive( true );
					}
				}
			}
		);

		zone.attach( policy );
		policyLoader.load( zone );
	}
}
