1 /*
2  *  cDemePredicate.h
3  *  Avida
4  *
5  *  Created by Benjamin Beckmann on 5/18/09.
6  *  Copyright 2009-2011 Michigan State University. All rights reserved.
7  *
8  */
9 
10 /*
11  *  cOrgMessagePredicate.h
12  *  Avida
13  *
14  *  Copyright 2005-2006 Michigan State University. All rights reserved.
15  *  Copyright 1993-2003 California Institute of Technology.
16  *
17  *
18  *  This file is part of Avida.
19  *
20  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
21  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
22  *
23  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25  *
26  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
27  *  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #ifndef cDemePredicate_h
31 #define cDemePredicate_h
32 
33 #include <iostream>
34 #include <functional>
35 
36 #include "cResourceCount.h"
37 #include "cStats.h"
38 
39 class cAvidaContext;
40 
41 
42 /*! \brief An STL-compatible predicate on movement.  The intent here is to
43  provide a straightforward way to track arbitrary movement *wherever* they appear
44  in the population.  */
45 class cDemePredicate : public std::binary_function<cAvidaContext&, void*, bool>
46 {
47 	public:
~cDemePredicate()48 		virtual ~cDemePredicate() { }
49 		virtual bool operator()(cAvidaContext& ctx, void* arg = NULL) = 0;
50 		virtual void Print(std::ostream& out) = 0;
51 		virtual void Reset() = 0;
52 		virtual bool PreviouslySatisfied() = 0;
53 		virtual cString GetName() = 0;
54 		virtual void UpdateStats(cStats& stats) = 0;
55 };
56 
57 class cDemeResourceThresholdPredicate : public cDemePredicate {
58 private:
59 	cString demeResourceName;
60 	cString compareOperator;
61 	double resourceThresholdValue;
62 	bool previouslySatisfied;
63 
64 public:
cDemeResourceThresholdPredicate(cString resourceName,cString comparisonOperator,double threasholdValue)65 	cDemeResourceThresholdPredicate(cString resourceName, cString comparisonOperator, double threasholdValue) :
66 		demeResourceName(resourceName),
67 		compareOperator(comparisonOperator),
68 		resourceThresholdValue(threasholdValue),
69 		previouslySatisfied(false)
70 		{;}
71 
operator()72 	bool operator()(cAvidaContext& ctx, void* arg) {
73 		assert(arg != NULL);
74 		double resourceLevel = static_cast<cResourceCount*>(arg)->Get(ctx, static_cast<cResourceCount*>(arg)->GetResourceCountID(demeResourceName));
75 
76 		if(compareOperator == ">=") {
77 			if(resourceLevel >= resourceThresholdValue) {
78 				previouslySatisfied = true;
79 			}
80 		} else if(compareOperator == "<=") {
81 			if(resourceLevel <= resourceThresholdValue) {
82 				previouslySatisfied = true;
83 			}
84 		} else {
85 			std::cout << "Undefined operator " << compareOperator << ".  Exit!\n";
86 			exit(-1);
87 		}
88 
89 		return true;
90 	}
91 
Print(std::ostream & out)92 	void Print(std::ostream& out) {;}
Reset()93 	void Reset() { previouslySatisfied = false; }
PreviouslySatisfied()94 	bool PreviouslySatisfied() { return previouslySatisfied; }
GetName()95 	cString GetName() { return cString("cDemeResourceThreshold"); }
UpdateStats(cStats & stats)96 	void UpdateStats(cStats& stats) {
97 		cString name = demeResourceName + " " + compareOperator + cStringUtil::Stringf(" %f", resourceThresholdValue);
98 		stats.IncDemeResourceThresholdPredicate(name);
99 	}
100 };
101 
102 #endif
103