本文整理汇总了Java中com.sri.ai.expresso.helper.Expressions.hasFunctor方法的典型用法代码示例。如果您正苦于以下问题:Java Expressions.hasFunctor方法的具体用法?Java Expressions.hasFunctor怎么用?Java Expressions.hasFunctor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sri.ai.expresso.helper.Expressions
的用法示例。
在下文中一共展示了Expressions.hasFunctor方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractGeneralizedVariables
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
private static void extractGeneralizedVariables(Expression polynomialExpression, Set<Expression> generalizedVariables) {
if (Expressions.isSymbol(polynomialExpression)) {
if (!Expressions.isNumber(polynomialExpression)) {
generalizedVariables.add(polynomialExpression);
}
}
else if (Expressions.hasFunctor(polynomialExpression, PLUS_FUNCTOR) ||
Expressions.hasFunctor(polynomialExpression, MINUS_FUNCTOR) ||
Expressions.hasFunctor(polynomialExpression, TIMES_FUNCTOR) ||
Expressions.hasFunctor(polynomialExpression, DIVISION_FUNCTOR) ||
Expressions.hasFunctor(polynomialExpression, EXPONENTIATION_FUNCTOR)) {
for (Expression arg : polynomialExpression.getArguments()) {
extractGeneralizedVariables(arg, generalizedVariables);
}
}
else {
// An unknown functor or other type of expression not expected
// by a standard polynomial expression
generalizedVariables.add(polynomialExpression);
}
}
示例2: simplify
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Equality.isEquality(expression) && expression.numberOfArguments() == 2) {
Expression union = null;
Expression emptySet = null;
for (Expression arg: expression.getArguments()) {
if (Expressions.hasFunctor(arg, FunctorConstants.UNION)) {
union = arg;
}
else if (Sets.isEmptySet(arg)) {
emptySet = arg;
}
}
if (union != null && emptySet != null) {
List<Expression> conjuncts = new ArrayList<>();
for (Expression unionArg : union.getArguments()) {
Expression equality = Equality.make(unionArg, emptySet);
conjuncts.add(equality);
}
result = And.make(conjuncts);
}
}
return result;
}
示例3: isIntensionalUnion
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static boolean isIntensionalUnion(Expression expression) {
boolean result =
Expressions.hasFunctor(expression, FunctorConstants.INTENSIONAL_UNION)
&& expression.numberOfArguments() == 1
&& isIntensionalMultiSet(expression.get(0));
return result;
}
示例4: simplify
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Expressions.hasFunctor(expression, FunctorConstants.IN) && expression.numberOfArguments() == 2) {
Expression t = expression.get(0);
Expression set = expression.get(1);
if (Sets.isIntensionalUnion(set)) {
IntensionalSet intensionalSet = (IntensionalSet) set.get(0);
Expression tElementOfPhi = Expressions.apply(FunctorConstants.IN, t, intensionalSet.getHead());
Expression existsBody = And.make(intensionalSet.getCondition(), tElementOfPhi);
result = ThereExists.make(intensionalSet.getIndexExpressions(), existsBody);
}
}
return result;
}
示例5: simplify
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Expressions.hasFunctor(expression, FunctorConstants.IN) && expression.numberOfArguments() == 2) {
Expression t = expression.get(0);
Expression set = expression.get(1);
if (Sets.isExtensionalSet(set)) {
// if empty set
if (set.numberOfArguments() == 0) {
result = Expressions.FALSE;
}
else if (set.numberOfArguments() == 1) {
// (t = t<sub>1</sub>)
result = Equality.make(t, set.get(0));
}
else { // > 1 element to test
// (t = t<sub>1</sub>)
Expression tEqualsT1 = Equality.make(t, set.get(0));
// {t<sub>2</sub>,…,t<sub>n</sub>}
Expression tailSet = ExtensionalSets.makeOfSameTypeAs(set, set.getArguments().subList(1, set.numberOfArguments()));
// t ∈ {t<sub>2</sub>,…,t<sub>n</sub>}
Expression tElementOfTailSet = Expressions.apply(FunctorConstants.IN, t, tailSet);
// (t = t<sub>1</sub>) ∨ t ∈ {t<sub>2</sub>,…,t<sub>n</sub>}
result = Or.make(tEqualsT1, tElementOfTailSet);
}
}
}
return result;
}
示例6: simplify
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Expressions.hasFunctor(expression, FunctorConstants.GET)
&& expression.numberOfArguments() == 2) {
Expression tupleArg = expression.get(0);
Expression indexArg = expression.get(1);
if (Tuple.isTuple(tupleArg)) {
// NOTE: Tuple's are indexed starting from 1 not 0.
// TODO - do we really want this ^^^
if (Expressions.isNumber(indexArg)) {
// We have a constant
int constantIndex = indexArg.intValueExact();
if (constantIndex > 0 && constantIndex <= tupleArg.numberOfArguments()) {
result = tupleArg.get(constantIndex -1); // NOTE: the API is 0 based.
}
else {
throw new IndexOutOfBoundsException("Index "+indexArg+" is out of bounds on "+tupleArg);
}
}
else {
// Construct a conditional
result = constructConditional(tupleArg, indexArg, 0);
}
}
}
return result;
}
示例7: simplify
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Expressions.hasFunctor(expression, FunctorConstants.SET)
&& expression.numberOfArguments() == 3) {
Expression tupleArg = expression.get(0);
Expression indexArg = expression.get(1);
Expression setToArg = expression.get(2);
if (Tuple.isTuple(tupleArg)) {
// NOTE: Tuple's are indexed starting from 1 not 0.
// TODO - do we really want this ^^^
if (Expressions.isNumber(indexArg)) {
// We have a constant
int constantIndex = indexArg.intValueExact();
if (constantIndex > 0 && constantIndex <= tupleArg.numberOfArguments()) {
result = tupleArg.set(constantIndex -1, setToArg); // NOTE: the API is 0 based.
}
else {
throw new IndexOutOfBoundsException("Index "+indexArg+" is out of bounds on "+tupleArg);
}
}
else {
// Construct a conditional
result = constructConditional(tupleArg, indexArg, setToArg, 0);
}
}
}
return result;
}
示例8: isIntegerIntervalReference
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
public static boolean isIntegerIntervalReference(Expression reference) {
boolean result = false;
try {
if (Expressions.hasFunctor(reference, FunctorConstants.INTEGER_INTERVAL)) {
if (reference.numberOfArguments() == 2 && reference.get(0).intValueExact() <= reference.get(1).intValueExact()) {
result = true;
}
}
}
catch (Throwable t) {
// ignore
}
return result;
}
示例9: isRealInterval
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
private static boolean isRealInterval(String functor, Expression reference) {
boolean result = false;
try {
if (Expressions.hasFunctor(reference, functor)) {
if (reference.numberOfArguments() == 2 && reference.get(0).doubleValue() <= reference.get(1).doubleValue()) {
result = true;
}
}
}
catch (Throwable t) {
// ignore
}
return result;
}
示例10: makeRandomVariableDeclaration
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
/**
* Make a random variable declaration object. Will default the arity of the
* random variable declaration to 0 and range to be of sort 'Boolean' if not
* specified in the expression passed in.
*
* @param expression
* an expression in the form of a random variable declaration.
* @return a RandomVariableDeclaration object corresonding to the expression
* passed in.
*/
public static HOGMRandomVariableDeclaration makeRandomVariableDeclaration(
Expression expression) {
HOGMRandomVariableDeclaration declaration = null;
if (Expressions.hasFunctor(expression, FUNCTOR_RANDOM_VARIABLE_DECLARATION)) {
int numArgs = expression.numberOfArguments();
if (numArgs > 0) {
// Extract arguments
Expression name = expression.get(0);
Expression arity = Expressions.ZERO;
if (numArgs >= 2) {
arity = expression.get(1);
}
Expression[] parametersAndRange = new Expression[0];
if (numArgs > 2) {
parametersAndRange = new Expression[numArgs - 2];
for (int i = 2; i < numArgs; i++) {
parametersAndRange[i - 2] = expression.get(i);
}
}
else {
parametersAndRange = new Expression[0];
}
declaration = new HOGMRandomVariableDeclaration(name, arity, parametersAndRange);
}
}
if (declaration == null) {
throw new IllegalArgumentException(
"Not a legal definition of a random variable declartion:" + expression);
}
return declaration;
}
示例11: makeConstantDeclaration
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
/**
* Make a constant declaration object. Will default the arity of the
* constant declaration to 0 and range to be of sort 'Boolean' if not
* specified in the expression passed in.
*
* @param expression
* an expression in the form of a constant declaration.
* @return a ConstantDeclaration object corresponding to the expression
* passed in.
*/
public static ConstantDeclaration makeConstantDeclaration(
Expression expression) {
ConstantDeclaration declaration = null;
if (Expressions.hasFunctor(expression, FUNCTOR_CONSTANT_DECLARATION)) {
int numArgs = expression.numberOfArguments();
if (numArgs > 0) {
// Extract arguments
Expression name = expression.get(0);
Expression arity = Expressions.ZERO;
if (numArgs >= 2) {
arity = expression.get(1);
}
Expression[] parametersAndRange = new Expression[0];
if (numArgs > 2) {
parametersAndRange = new Expression[numArgs - 2];
for (int i = 2; i < numArgs; i++) {
parametersAndRange[i - 2] = expression.get(i);
}
}
else {
parametersAndRange = new Expression[0];
}
declaration = new ConstantDeclaration(name, arity, parametersAndRange);
}
}
if (declaration == null) {
throw new IllegalArgumentException(
"Not a legal definition of a constant declartion:" + expression);
}
return declaration;
}
示例12: make
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
private static Monomial make(List<Expression> numericConstantsAndTerms) {
Rational numericFactor = Rational.ONE;
Map<Expression, Rational> factorToPower = new LinkedHashMap<>();
for (Expression numericConstantOrTerm : numericConstantsAndTerms) {
if (Expressions.isNumber(numericConstantOrTerm)) {
numericFactor = numericFactor.multiply(numericConstantOrTerm.rationalValue());
}
else { // Is a term
Expression factor = numericConstantOrTerm;
Rational power = Rational.ONE;
boolean attemptFlattening = false;
// Handle case where factor is negated, e.g.: -x
if (factor.hasFunctor(MINUS) && factor.numberOfArguments() == 1) {
factor = factor.get(0);
// i.e. same as having an explicit constant '-1' multiplicand in the expression
numericFactor = numericFactor.negate();
attemptFlattening = true;
}
// If exponentiation using a constant integer exponent then we need to extract the factor and the power
if (Expressions.hasFunctor(factor, Exponentiation.EXPONENTIATION_FUNCTOR)) {
Expression simplifiedPower = simplifyExponentIfPossible(factor.get(1));
if (isLegalExponent(simplifiedPower)) {
power = simplifiedPower.rationalValue();
// The factor is actually the base of the exponentiation
factor = factor.get(0);
attemptFlattening = true;
}
else if (!simplifiedPower.equals(factor.get(1))) {
// Use the simplified version of the non legal exponent in the factor
// i.e. is a non numeric factor where the exponent has been simplified
// as best as possible.
factor = apply(EXPONENTIATION_FUNCTOR, factor.get(0), simplifiedPower);
}
}
// Handle nested *'s arguments
if (factor.hasFunctor(TIMES)) {
attemptFlattening = true;
}
// We attempt flattening if we were/are able to simplify the factor in some way
if (attemptFlattening) {
// Treat the factor as a Monomial and merge it in
// This lets you handle nested monomial expressions
// in a simplified/recursive manner.
Monomial factorAsMonomial = make(Times.getMultiplicands(factor));
// Need to raise to the current power
factorAsMonomial = factorAsMonomial.exponentiate(power.intValue());
numericFactor = numericFactor.multiply(factorAsMonomial.getNumericFactor());
List<Expression> factors = factorAsMonomial.getOrderedNonNumericFactors();
List<Rational> powers = factorAsMonomial.getPowersOfNonNumericFactors();
int factorSize = factors.size();
for (int i = 0; i < factorSize; i++) {
updateFactorToPowerMap(factorToPower, factors.get(i), powers.get(i));
}
}
else {
updateFactorToPowerMap(factorToPower, factor, power);
}
}
}
Monomial result = null;
if (numericFactor.equals(Rational.ZERO)) {
result = ZERO;
}
else {
List<Expression> orderedFactors = new ArrayList<>(factorToPower.keySet());
Collections.sort(orderedFactors, _factorComparator);
List<Rational> orderedPowers = new ArrayList<>(orderedFactors.size());
orderedFactors.forEach(factor -> orderedPowers.add(factorToPower.get(factor)));
result = make(numericFactor, orderedFactors, orderedPowers);
}
return result;
}
示例13: makeSortDeclaration
import com.sri.ai.expresso.helper.Expressions; //导入方法依赖的package包/类
/**
* Make a sort declaration object. Will set the size of the sort to the
* string valued symbol "Unknown" and the known constants to the empty set
* {} if not specified in the expression passed in.
*
* @param expression
* an expression in the form of a sort declaration definition.
* @return a SortDeclaration object corresponding to the expression passed
* in.
* @throws IllegalArgumentException
* if the expression is not a legal sort declaration.
*/
public static HOGMSortDeclaration makeSortDeclaration(Expression expression) {
HOGMSortDeclaration declaration = null;
// Check if an in-built first.
for (int i = 0; i < IN_BUILT_SORTS.length; i++) {
if (IN_BUILT_SORTS[i].getSortDeclaration().equals(expression)) {
declaration = IN_BUILT_SORTS[i];
break;
}
}
// If not an in-built
if (declaration == null
&& Expressions.hasFunctor(expression, FUNCTOR_SORT_DECLARATION)) {
int numArgs = expression.numberOfArguments();
if (numArgs >= 1 && numArgs <= 3) {
// Extract arguments
Expression name = expression.get(0);
Expression size = null;
if (numArgs >= 2) {
size = expression.get(1);
}
else {
size = UNKNOWN_SIZE;
}
Expression constants = null;
if (numArgs >= 3) {
constants = expression.get(2);
}
else {
constants = ExtensionalSets.makeEmptySetExpression();
}
declaration = new HOGMSortDeclaration(name, size, constants);
}
}
if (declaration == null) {
throw new IllegalArgumentException(
"Not a legal definition of a sort declartion:" + expression);
}
return declaration;
}