PageRenderTime 10ms CodeModel.GetById 6ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/farmR/R/Farm-class.R

https://code.google.com/p/javawfm/
R | 56 lines | 35 code | 9 blank | 12 comment | 7 complexity | 4dd903d9438b10c03d70829445d4f431 MD5 | raw file
 1# Implementation of the Farm class #
 2#
 3####################################
 4
 5
 6################### Constructor #######################
 7# Create a new Farm object from its economic parameters
 8#######################################################
 9Farm <- function(farm.params=defaultArableFarmParameters(),obj.params=NULL) { 
10	farm=new("Farm")
11	if ( class(farm.params)=="FarmParameters"){
12		farm@model=.jnew("jfm/r/SimpleFarmRepresentation",document(farm.params))
13	} else {
14		farm@model=.jnew("jfm/r/SimpleFarmRepresentation",farm.params)
15	}
16	
17	if ( !is.null(obj.params)){
18		set(farm,obj.params)
19	}
20	
21	farm@cropNames=.jcall(farm@model,"[Ljava/lang/String;","cropNames")   
22	farm
23}
24
25####### Functions for setting particular parameters #####
26#
27#########################################################
28# Set the multi objective preferences for a farm object. #
29set.mou <- function(farm,mou){
30	if ( class(mou)=="ObjectiveParameters"){
31		.jcall(model(farm),"V","createAndApplyMOU",document(mou))
32	} else {
33		.jcall(model(farm),"V","createAndApplyMOU",mou)
34	}
35  farm
36}
37
38# Set input variables required for sugarbeet production #
39set.sugarbeet <- function(sfarm,coords,SBFactories,haulagePerTonnePerKm,maxSBHaulageDistance){
40
41  sugarDists=spDistsN1(coordinates(SBFactories),coords,longlat=TRUE)
42  minDist=min(sugarDists)
43  if ( minDist > maxSBHaulageDistance ){
44    haulagePerTonnePerKm=10000
45  }
46  .jcall(model(sfarm),"V","setDistanceFromSugarbeetFactory",minDist,haulagePerTonnePerKm)
47}
48
49# Make the set.mou function into a generic so it can take a jobjRef or a character arg
50if (!isGeneric("set")){
51  setGeneric("set", function(farm,input,...) standardGeneric("set"))
52}
53setMethod("set", signature(farm="Farm",input="jobjRef"), function(farm,input) set.mou(farm,input)) 
54setMethod("set", signature(farm="Farm",input="ObjectiveParameters"), function(farm,input) set.mou(farm,input)) 
55setMethod("set", signature(farm="Farm",input="character"), function(farm,input) set.mou(farm,input))
56