📄 higherorderfunction.java
字号:
case ID_ALL_OF: {
// param: boolean-function, single value, bag of same type
// return: boolean
// using the function, iterate through the bag, and if all
// of the bag elements match the single value, return
// true, otherwise return false
result = all(args[0], (BagAttribute)(args[1]), function, context);
break;
}
case ID_ANY_OF_ANY: {
// param: boolean-function, bag, bag of same type
// return: boolean
// apply the function to every combination of a single value from
// the first bag and a single value from the second bag, and if
// any evaluation is true return true, otherwise return false
result = new EvaluationResult(BooleanAttribute.getInstance(false));
Iterator it = ((BagAttribute)args[0]).iterator();
BagAttribute bag = (BagAttribute)(args[1]);
while (it.hasNext()) {
AttributeValue value = (AttributeValue)(it.next());
result = any(value, bag, function, context, false);
if (result.indeterminate())
return result;
if (((BooleanAttribute)(result.
getAttributeValue())).getValue())
break;
}
break;
}
case ID_ALL_OF_ANY: {
// param: boolean-function, bag, bag of same type
// return: boolean
// iterate through the first bag, and if for each of those values
// one of the values in the second bag matches then return true,
// otherwise return false
result = allOfAny((BagAttribute)(args[1]), (BagAttribute)(args[0]),
function, context);
break;
}
case ID_ANY_OF_ALL: {
// param: boolean-function, bag, bag of same type
// return: boolean
// iterate through the second bag, and if for each of those values
// one of the values in the first bag matches then return true,
// otherwise return false
result = anyOfAll((BagAttribute)(args[0]), (BagAttribute)(args[1]),
function, context);
break;
}
case ID_ALL_OF_ALL: {
// param: boolean-function, bag, bag of same type
// return: boolean
// iterate through the first bag, and for each of those values
// if every value in the second bag matches using the given
// function, then return true, otherwise return false
result = new EvaluationResult(BooleanAttribute.getInstance(true));
Iterator it = ((BagAttribute)args[0]).iterator();
BagAttribute bag = (BagAttribute)(args[1]);
while (it.hasNext()) {
AttributeValue value = (AttributeValue)(it.next());
result = all(value, bag, function, context);
if (result.indeterminate())
return result;
if (! ((BooleanAttribute)(result.
getAttributeValue())).getValue())
break;
}
break;
}
}
return result;
}
/**
* Checks that the given inputs are valid for this function.
*
* @param inputs a <code>List</code> of <code>Evaluatable</code>s
*
* @throws IllegalArgumentException if the inputs are invalid
*/
public void checkInputs(List inputs) throws IllegalArgumentException {
Object [] list = inputs.toArray();
// first off, check that we got the right number of paramaters
if (list.length != 3)
throw new IllegalArgumentException("requires three inputs");
// now, try to cast the first element into a function
Function function = null;
if (list[0] instanceof Function) {
function = (Function)(list[0]);
} else if (list[0] instanceof VariableReference) {
Expression xpr = ((VariableReference)(list[0])).
getReferencedDefinition().getExpression();
if (xpr instanceof Function)
function = (Function)xpr;
}
if (function == null)
throw new IllegalArgumentException("first arg to higher-order " +
" function must be a function");
// check that the function returns a boolean
if (! function.getReturnType().toString().
equals(BooleanAttribute.identifier))
throw new IllegalArgumentException("higher-order function must " +
"use a boolean function");
// get the two inputs
Evaluatable eval1 = (Evaluatable)(list[1]);
Evaluatable eval2 = (Evaluatable)(list[2]);
// the first arg might be a bag
if (secondIsBag && (! eval1.returnsBag()))
throw new IllegalArgumentException("first arg has to be a bag");
// the second arg must be a bag
if (! eval2.returnsBag())
throw new IllegalArgumentException("second arg has to be a bag");
// finally, we need to make sure that the given type will work on
// the given function
List args = new ArrayList();
args.add(eval1);
args.add(eval2);
function.checkInputsNoBag(args);
}
/**
* Checks that the given inputs are valid for this function if all
* inputs are considered to not be bags. This always throws an
* exception, since this function by definition must work on bags.
*
* @param inputs a <code>List</code> of <code>Evaluatable</code>s
*
* @throws IllegalArgumentException always
*/
public void checkInputsNoBag(List inputs) throws IllegalArgumentException {
throw new IllegalArgumentException("higher-order functions require " +
"use of bags");
}
/**
* Private helper function that performs the any function, but lets you
* swap the argument order (so it can be used by any-of-all)
*/
private EvaluationResult any(AttributeValue value, BagAttribute bag,
Function function, EvaluationCtx context,
boolean argumentsAreSwapped) {
return anyAndAllHelper(value, bag, function, context, false,
argumentsAreSwapped);
}
/**
* Private helper function that performs the all function
*/
private EvaluationResult all(AttributeValue value, BagAttribute bag,
Function function, EvaluationCtx context) {
return anyAndAllHelper(value, bag, function, context, true, false);
}
/**
* Private helper for any & all functions
*/
private EvaluationResult anyAndAllHelper(AttributeValue value,
BagAttribute bag,
Function function,
EvaluationCtx context,
boolean allFunction,
boolean argumentsAreSwapped) {
BooleanAttribute attr = BooleanAttribute.getInstance(allFunction);
Iterator it = bag.iterator();
while (it.hasNext()) {
List params = new ArrayList();
if (! argumentsAreSwapped) {
params.add(value);
params.add((AttributeValue)(it.next()));
} else {
params.add((AttributeValue)(it.next()));
params.add(value);
}
EvaluationResult result = function.evaluate(params, context);
if (result.indeterminate())
return result;
BooleanAttribute bool =
(BooleanAttribute)(result.getAttributeValue());
if (bool.getValue() != allFunction) {
attr = bool;
break;
}
}
return new EvaluationResult(attr);
}
/**
* any-of-all
*/
private EvaluationResult anyOfAll(BagAttribute anyBag, BagAttribute allBag,
Function function,
EvaluationCtx context) {
return allAnyHelper(anyBag, allBag, function, context, true);
}
/**
* all-of-any
*/
private EvaluationResult allOfAny(BagAttribute anyBag, BagAttribute allBag,
Function function,
EvaluationCtx context) {
return allAnyHelper(anyBag, allBag, function, context, false);
}
/**
* Private helper for the all-of-any and any-of-all functions
*/
private EvaluationResult allAnyHelper(BagAttribute anyBag,
BagAttribute allBag,
Function function,
EvaluationCtx context,
boolean argumentsAreSwapped) {
Iterator it = allBag.iterator();
while (it.hasNext()) {
AttributeValue value = (AttributeValue)(it.next());
EvaluationResult result =
any(value, anyBag, function, context, argumentsAreSwapped);
if (result.indeterminate())
return result;
if (! ((BooleanAttribute)(result.
getAttributeValue())).getValue())
return result;
}
return new EvaluationResult(BooleanAttribute.getTrueInstance());
}
/**
* Encodes this <code>HigherOrderFunction</code> into its XML
* representation and writes this encoding to the given
* <code>OutputStream</code> with no indentation.
*
* @param output a stream into which the XML-encoded data is written
*/
public void encode(OutputStream output) {
encode(output, new Indenter(0));
}
/**
* Encodes this <code>HigherOrderFunction</code> into its XML
* representation and writes this encoding to the given
* <code>OutputStream</code> with indentation.
*
* @param output a stream into which the XML-encoded data is written
* @param indenter an object that creates indentation strings
*/
public void encode(OutputStream output, Indenter indenter) {
PrintStream out = new PrintStream(output);
out.println(indenter.makeString() + "<Function FunctionId=\"" +
getIdentifier().toString() + "\"/>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -