wiki:FCIJavaExample

Here is a simple example of creating and operating an experiment within Java by means of FCI:

		package scenario;
		
		import java.util.ArrayList;
		import java.util.List;		
		import org.panlab.software.fci.core.AuthorizationKey;
		import org.panlab.software.fci.core.FCI;
		import org.panlab.software.fci.core.FCICredentials;
		import org.panlab.software.fci.core.ParameterValuePair;
		import org.panlab.software.fci.core.ResourceContext;
		import org.panlab.software.fci.core.ResourceGroup;
		import org.panlab.software.fci.core.ResourceProvider;
		import org.panlab.software.fci.core.ResourceProxy;
		import org.panlab.software.fci.core.ServiceType;
		
		public class myScenario {
			
			//TODO: Please enter here identity for panlab
			String _username_panlab ="ENTER USERNAME";
			String _password_panlab ="ENTER PASSWORD";
			
			FCI fci = FCI.getInstance();
			
			//for each imported office, we need a Resource Context	
			ResourceContext _context_panlab;
			
			public ResourceContext _return_context_panlab(){ 
				//credentials for panlab Office
				FCICredentials cred = new FCICredentials(_username_panlab, _password_panlab);
				AuthorizationKey authKey = fci.createAuthorizationKey(cred);
				ResourceContext _context_panlab = fci.createResourceContext("panlab", authKey);
				return _context_panlab;
			}
			
					
			//
			private ResourceProxy createResource_myecho(){
				//Create a service type by its name
				ServiceType service = _context_panlab.getServiceType("echo");
				
				//get a resource provider 
				ResourceProvider provider = _context_panlab.getResourceProviderByURI("site");
				
				//create Parameters of a resource
				List<ParameterValuePair> params = new ArrayList<ParameterValuePair>();
				ParameterValuePair p;
				p = new ParameterValuePair("sleeptime_ms", "2000");
				params.add(p);
				p = new ParameterValuePair("output", "");
				params.add(p);
				p = new ParameterValuePair("input", "HelloWorld");
				params.add(p);
				ResourceProxy resource_myecho = _context_panlab.createResourceProxy("myScenario", "echo_rp12_s12_or10782", provider, service, params);
				return  resource_myecho;
			}
			/**
			 * @param args
			 */
			public static void main(String[] args) {
				new myScenario();
		
			}
			
			//for each imported office, we need credentials
		
			public myScenario() {
				CreateContexts();
				CreateScenario();
			}
			
			public void CreateContexts(){
				_context_panlab = _return_context_panlab();
				
			}
		
			private void CreateScenario() {
		    //    Group (for grouping resources)
				ResourceGroup myGroup = FCI.getInstance().createResourceGroup("myGroup");
				//all creates				
				ResourceProxy resource_myecho = createResource_myecho();
				System.out.println("myecho resource GUID: " + resource_myecho.getGUID());
				myGroup.addResourceProxy(resource_myecho);
								 
				
//				System.out.println("Echo input = "+ resource_myecho.getParameterValueOfResource("input", true));
//				//assignments
//				// Update assignments for resources of group

				
				//example reads
				System.out.println("myecho sleeptime_ms = "+ resource_myecho.getParameterValueOfResource("sleeptime_ms", true));
				System.out.println("myecho output = "+ resource_myecho.getParameterValueOfResource("output", true));
				System.out.println("myecho input = "+ resource_myecho.getParameterValueOfResource("input", true));
				
				// Terminate the group..terminate any contained resources and release scenario
				myGroup.TearDownResources();
				}
		
			
		

Use the following to get an instance of FCI:

FCI fci = FCI.getInstance();

Then get a resource context for a specific resource broker/provider:

//for each imported office, we need a Resource Context	
ResourceContext _context_panlab;

The next thing is to connect to the provider/broker. In this case we will use our panlab credentials to connect to panlab. The FCI command createResourceContext it is used to connect to "panlab".

public ResourceContext _return_context_panlab(){ 
	//credentials for panlab Office
	FCICredentials cred = new FCICredentials(_username_panlab, _password_panlab);
	AuthorizationKey authKey = fci.createAuthorizationKey(cred);
	ResourceContext _context_panlab = fci.createResourceContext("panlab", authKey);
	return _context_panlab;
}

First is to create a group under which you can aggregate your resources by (createResourceGroup) and then go to create access for your required resource:

ResourceGroup myGroup = FCI.getInstance().createResourceGroup("myGroup");
	//all creates				
	ResourceProxy resource_myecho = createResource_myecho();
	System.out.println("myecho resource GUID: " + resource_myecho.getGUID());
	myGroup.addResourceProxy(resource_myecho);
{{{

}}}
Last modified 12 years ago Last modified on Nov 5, 2011, 10:24:35 PM