001: package org.osbl.agent.model.action;
002:
003: import ognl.Ognl;
004: import ognl.OgnlException;
005:
006: import org.osbl.agent.model.Action;
007: import org.osbl.agent.model.Rule;
008: import org.osbl.agent.model.RuleContext;
009:
010: /**
011: * The Class SetPropertyAction.
012: *
013: * @author Sebastian Nozzi.
014: */
015: public class SetPropertyAction implements Action {
016:
017: /** The bean meta class name. */
018: private String beanMetaClassName;
019:
020: /** The property meta name. */
021: private String propertyMetaName;
022:
023: /** The value to set. */
024: private Object valueToSet;
025:
026: /**
027: * Gets the bean meta class name.
028: *
029: * @return the bean meta class name
030: */
031: public String getBeanMetaClassName() {
032: return beanMetaClassName;
033: }
034:
035: /**
036: * Sets the bean meta class name.
037: *
038: * @param beanMetaClassName the new bean meta class name
039: */
040: public void setBeanMetaClassName(String beanMetaClassName) {
041: this .beanMetaClassName = beanMetaClassName;
042: }
043:
044: /**
045: * Gets the property meta name.
046: *
047: * @return the property meta name
048: */
049: public String getPropertyMetaName() {
050: return propertyMetaName;
051: }
052:
053: /**
054: * Sets the property meta name.
055: *
056: * @param propertyMetaName the new property meta name
057: */
058: public void setPropertyMetaName(String propertyMetaName) {
059: this .propertyMetaName = propertyMetaName;
060: }
061:
062: /**
063: * Gets the value to set.
064: *
065: * @return the value to set
066: */
067: public Object getValueToSet() {
068: return valueToSet;
069: }
070:
071: /**
072: * Sets the value to set.
073: *
074: * @param valueToSet the new value to set
075: */
076: public void setValueToSet(Object valueToSet) {
077: this .valueToSet = valueToSet;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.osbl.agent.model.Action#execute(org.osbl.agent.model.RuleContext)
082: */
083: public void execute(RuleContext context) {
084:
085: // "subject" is the instance that will have a property changed
086: Object subject = context.getTargetObject();
087:
088: try {
089: // Use OGNL to set a value, using "subject" as our root object.
090: // "valueToSet" is a field in this class.
091:
092: // Since "valueToSet" can, in turn, be an expression itself, we evaluate
093: // it with Ognl, and then pass the computed value to setValue...
094:
095: // This allows things like "preis <- preis + 1"
096:
097: if (valueToSet instanceof String) {
098: System.out.println("this.valueToSet is: "
099: + this .valueToSet);
100: // treat this.valueToSet as an expression, explicitly calling getValue(String, Object)
101: // since there is also a getValue(Object, Object) which is NOT the one we want.
102: Object valueToSet = Ognl.getValue(
103: (String) this .valueToSet, context, subject);
104: Ognl.setValue(propertyMetaName, context, subject,
105: valueToSet);
106: } else {
107: // valueToSet could be anything
108:
109: Ognl.setValue(propertyMetaName, context, subject,
110: valueToSet);
111: }
112:
113: } catch (OgnlException e) {
114: // TODO Auto-generated catch block
115: e.printStackTrace();
116: }
117: }
118:
119: /* (non-Javadoc)
120: * @see java.lang.Object#equals(java.lang.Object)
121: */
122: public boolean equals(Object obj) {
123:
124: if (this == obj)
125: return true;
126:
127: if (obj instanceof SetPropertyAction == false)
128: return false;
129:
130: SetPropertyAction other = (SetPropertyAction) obj;
131:
132: if (!Rule.propertyEquals(beanMetaClassName,
133: other.beanMetaClassName))
134: return false;
135:
136: if (!Rule.propertyEquals(propertyMetaName,
137: other.propertyMetaName))
138: return false;
139:
140: if (!Rule.propertyEquals(valueToSet, other.valueToSet))
141: return false;
142:
143: return true;
144: }
145:
146: }
|