001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.function.v1;
009:
010: //base classes
011: import java.math.BigDecimal;
012:
013: //project specific classes
014: import org.jfolder.common.SelfArgumentlessConstructor;
015: import org.jfolder.common.StandardDataTypes;
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.function.BaseCommonFunctionCompatibilityTag;
018: import org.jfolder.common.function.WebPageTagInstanceViewContext;
019: import org.jfolder.common.function.WebPageTagInstanceViewContextInfo;
020: import org.jfolder.common.tagging.AppraiseConceptTagContext;
021: import org.jfolder.common.tagging.ConceptTagCharacteristic;
022: import org.jfolder.common.tagging.ConceptTagConstraint;
023: import org.jfolder.common.tagging.ConceptTagConstraintHolder;
024: import org.jfolder.common.tagging.ConceptTagFlagsHelper;
025: import org.jfolder.common.tagging.ConceptTagHelper;
026: import org.jfolder.common.tagging.DetermineClassConceptTagContext;
027: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
028: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
029: import org.jfolder.common.tagging.DynamicConceptTagConstraintContext;
030: import org.jfolder.common.tagging.InitializeConceptTagContext;
031: import org.jfolder.common.tagging.ReturnableConceptTag;
032: import org.jfolder.common.tagging.SelectionCriteriaForReturnableConceptTag;
033: import org.jfolder.common.tagging.StaticConceptTagConstraintContext;
034: import org.jfolder.common.tagging.StudioConceptTagCreParInfoContext;
035: import org.jfolder.common.tagging.StudioConceptTagInstanceInfoContext;
036: import org.jfolder.common.tagging.StudioConceptTagTypeInfoContext;
037: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
038: import org.jfolder.common.utils.misc.CommonExpressions;
039: import org.jfolder.common.utils.misc.MiscHelper;
040:
041: //other classes
042:
043: public class CommonFunctionMathExponentTag extends
044: BaseCommonFunctionCompatibilityTag implements
045: SelfArgumentlessConstructor {
046:
047: //characteristics
048: private ConceptTagCharacteristic baseCtchar = null;
049: private ConceptTagCharacteristic factorCtchar = null;
050:
051: //base details
052: private final static String BASE_CTCON = "base";
053: private final static String BASE_TITLE = "Base";
054: private final static String BASE_SHORT_DESCRIPTION = "The number to be multiplied by itself "
055: + "('Base' is a fancy term for this)";
056:
057: //factor details
058: private final static String FACTOR_CTCON = "factor";
059: private final static String FACTOR_TITLE = "Factor";
060: private final static String FACTOR_SHORT_DESCRIPTION = "The number of times the base will be multiplied by itself - "
061: + "('Factor' is a fancy term for this)";
062:
063: public void initialize(InitializeConceptTagContext inIctc) {
064:
065: //
066: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
067: .newInstance(inIctc);
068:
069: //constrain "base"
070: ConceptTagConstraint baseCtcon = ConceptTagConstraint
071: .newInstance(BASE_CTCON, rootCtconh);
072: baseCtcon.setTitle(BASE_TITLE);
073: baseCtcon.setShortDescription(BASE_SHORT_DESCRIPTION);
074: //
075: StaticConceptTagConstraintContext baseSctcc = StaticConceptTagConstraintContext
076: .newInstance(baseCtcon);
077: baseSctcc.setRegularExpression(CommonExpressions.DECIMAL);
078: //
079: DynamicConceptTagConstraintContext baseDctcc = DynamicConceptTagConstraintContext
080: .newInstance(baseCtcon);
081: baseDctcc.getValidator().allowReturnOfOnlyDecimal();
082: baseDctcc.getValidator().filterDirectChildOfRootAndHtml();
083: //
084: baseSctcc.addDefaultValue("1");
085:
086: //constrain "factor"
087: ConceptTagConstraint factorCtcon = ConceptTagConstraint
088: .newInstance(FACTOR_CTCON, rootCtconh);
089: factorCtcon.setTitle(this .FACTOR_TITLE);
090: factorCtcon.setShortDescription(this .FACTOR_SHORT_DESCRIPTION);
091: //
092: StaticConceptTagConstraintContext factorSctcc = StaticConceptTagConstraintContext
093: .newInstance(factorCtcon);
094: factorSctcc.setRegularExpression(CommonExpressions.DECIMAL);
095: //
096: DynamicConceptTagConstraintContext factorDctcc = DynamicConceptTagConstraintContext
097: .newInstance(factorCtcon);
098: factorDctcc.getValidator().allowReturnOfOnlyDecimal();
099: factorDctcc.getValidator().filterDirectChildOfRootAndHtml();
100: //
101: factorSctcc.addDefaultValue("1");
102:
103: //
104: ConceptTagFlagsHelper
105: .pushNonIterativeCommonFunctionDepth(inIctc);
106: inIctc.initialize();
107: ConceptTagFlagsHelper
108: .popNonIterativeCommonFunctionDepth(inIctc);
109:
110: //initialize characteristics
111: this .baseCtchar = baseCtcon.getCharacteristic();
112: this .factorCtchar = factorCtcon.getCharacteristic();
113: }
114:
115: public void appraise(AppraiseConceptTagContext inActc) {
116:
117: if (inActc instanceof StudioConceptTagInstanceInfoContext) {
118:
119: StudioConceptTagInstanceInfoContext sctiic = (StudioConceptTagInstanceInfoContext) inActc;
120:
121: sctiic.setInstanceTitle("Exponent");
122: sctiic
123: .setInstanceDescription("Computes one value raised to the power of another");
124: } else if (inActc instanceof StudioConceptTagTypeInfoContext) {
125:
126: StudioConceptTagTypeInfoContext scttic = (StudioConceptTagTypeInfoContext) inActc;
127:
128: scttic.setTypeTitle("Exponent");
129: scttic
130: .setTypeDescription("Computes one value raised to the power of another");
131: } else if (inActc instanceof WebPageTagInstanceViewContext) {
132:
133: WebPageTagInstanceViewContext wptivc = (WebPageTagInstanceViewContext) inActc;
134:
135: WebPageTagInstanceViewContextInfo info = wptivc
136: .getInfoAsNonHtml();
137: info.setNonHtmlContent("MathExp - Not Impl");
138: //wptivc.getInfo().setStartContent("");
139: //wptivc.getInfo().setMiddleContent("");
140: //wptivc.getInfo().setEndContent("");
141: } else if (inActc instanceof SelectionCriteriaForReturnableConceptTag) {
142:
143: SelectionCriteriaForReturnableConceptTag scfrct = (SelectionCriteriaForReturnableConceptTag) inActc;
144:
145: scfrct.canReturnDecimal();
146: scfrct.declareFormatAsPlain();
147: } else if (inActc instanceof StudioConceptTagCreParInfoContext) {
148:
149: StudioConceptTagCreParInfoContext scpc = (StudioConceptTagCreParInfoContext) inActc;
150: }
151: }
152:
153: public void determineValueAndClass(
154: DetermineValueAndClassConceptTagContext inDvacctc) {
155:
156: try {
157: ValueAndClassForConceptTag vacfct = null;
158:
159: //ComponentsForReturnableScriptTag wcfrt =
160: // ScriptTagHelper.getComponentsForReturnableScriptTag(inRvacc);
161:
162: //base
163: BigDecimal base = null;
164: if (this .baseCtchar.isStatic(0, inDvacctc)) {
165: base = new BigDecimal(this .baseCtchar.getValueAsString(
166: 0, inDvacctc));
167: } else {
168: ValueAndClassForConceptTag baseVac = this .baseCtchar
169: .getDetermineValueAndClass(0, inDvacctc);
170: base = MiscHelper.fromNumberToBigDecimal(baseVac
171: .getValue());
172: }
173:
174: //factor
175: BigDecimal factor = null;
176: if (this .factorCtchar.isStatic(0, inDvacctc)) {
177: factor = new BigDecimal(this .factorCtchar
178: .getValueAsString(0, inDvacctc));
179: } else {
180: ValueAndClassForConceptTag factorVac = this .factorCtchar
181: .getDetermineValueAndClass(0, inDvacctc);
182: factor = MiscHelper.fromNumberToBigDecimal(factorVac
183: .getValue());
184: }
185:
186: double result = Math.pow(base.doubleValue(), factor
187: .doubleValue());
188: BigDecimal bdResult = new BigDecimal(result);
189: vacfct = ValueAndClassForConceptTag.newDecimal(bdResult);
190:
191: inDvacctc.setDetermineValueAndClass(vacfct);
192: } catch (DetermineValueAndClassConceptTagException dvaccte) {
193: inDvacctc.setDetermineException(dvaccte);
194: }
195: }
196:
197: public void determineClass(DetermineClassConceptTagContext inDcctc) {
198: inDcctc.setDetermineClass(StandardDataTypes.getDecimalClass());
199: }
200:
201: public Object createUninitializedSelf() {
202: return new CommonFunctionMathExponentTag();
203: }
204: }
|