本文整理汇总了Java中com.sri.ai.expresso.helper.Expressions类的典型用法代码示例。如果您正苦于以下问题:Java Expressions类的具体用法?Java Expressions怎么用?Java Expressions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Expressions类属于com.sri.ai.expresso.helper包,在下文中一共展示了Expressions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: make
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
/**
* Make an if then else expression, returning the then or else branches directly if condition is trivial,
* or the condition (or its negation) if the branches are trivial.
*/
public static Expression make(Expression condition, Expression thenBranch, Expression elseBranch) {
if (condition.equals(true)) {
return thenBranch;
}
if (condition.equals(false)) {
return elseBranch;
}
if (thenBranch.equals(true) && elseBranch.equals(false)) {
return condition;
}
// if (thenBranch.equals(elseBranch)) { // breaking some code even though it should not; we want to have it eventually
// return thenBranch;
// }
// if (thenBranch.equals(false) && elseBranch.equals(true)) { // this may violate normalization routines in which 'not' is moved in
// return Not.make(condition);
// }
Expression result = Expressions.makeExpressionOnSyntaxTreeWithLabelAndSubTrees(FunctorConstants.IF_THEN_ELSE, condition, thenBranch, elseBranch);
return result;
}
示例2: solve
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
@Override
public SolverResult solve(String solveRequestId, ModelLanguage modelLanguage, String model, String evidenceQuery)
throws Exception {
if (modelLanguage != ModelLanguage.HOGMv1) {
throw new UnsupportedOperationException(modelLanguage.name() + " is currently not supported by this solver.");
}
Translator inputToUAITranslator = TranslatorFactory.newTranslator(modelLanguage, ModelLanguage.UAI);
// NOTE: This trick is dependent on the input model being HOGMv1
String hogmv1Model = model + "\nrandom UAIQuery : Boolean;\nif "+evidenceQuery+" then UAIQuery else not UAIQuery;\n";
VECCallResult partitionResult = callVECPR("Partition Function "+solveRequestId, inputToUAITranslator, hogmv1Model, new Reader[] {new StringReader(hogmv1Model)});
VECCallResult evidenceResult = callVECPR("Evidence "+solveRequestId, inputToUAITranslator, hogmv1Model+"\nUAIQuery;", new Reader[] {new StringReader(hogmv1Model), new StringReader("UAIQuery")});
Double probabilityResult = Math.pow(10, evidenceResult.resultLog10) / Math.pow(10, partitionResult.resultLog10);
SolverResult result = new SolverResult(
Math.max(partitionResult.translationTookMS, evidenceResult.translationTookMS),
Math.max(partitionResult.vecProcessTookMS, evidenceResult.vecProcessTookMS),
probabilityResult.isNaN() ? null : Expressions.makeSymbol(probabilityResult)
);
return result;
}
示例3: testCountingFormulaTerm
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
@Test
public void testCountingFormulaTerm() {
String string;
// Add support for counting formulas, i.e. | X in People : X = ann or X = brian or X = dan |
string = "sort People : 10000000, ann, brian, con, dan;\n"
+"random lucky : Boolean;\n"
+"random winner : People;\n"
+"if lucky then winner = con else winner = con 1/| X in People : X = ann or X = brian or X = dan |;";
test(string, expected(Expressions.parse("sort(People, 10000000, {ann, brian, con, dan})"),
null,
Expressions.parse("tuple(randomVariable(lucky, 0, Boolean), randomVariable(winner, 0, People))"),
Expressions.parse("if lucky then (if winner = con then 1 else 0) else (if winner = con then 1 / | X in People : X = ann or X = brian or X = dan | else 1 - 1 / | X in People : X = ann or X = brian or X = dan |)")));
string = "sort People : 10000000, ann, brian, con, dan;\n"
+"random lucky : Boolean;\n"
+"random winner : People;\n"
+"if lucky then winner = con else winner = con 1/| X in People, Y in People : X = ann or Y = dan |;";
test(string, expected(Expressions.parse("sort(People, 10000000, {ann, brian, con, dan})"),
null,
Expressions.parse("tuple(randomVariable(lucky, 0, Boolean), randomVariable(winner, 0, People))"),
Expressions.parse("if lucky then (if winner = con then 1 else 0) else (if winner = con then 1 / | X in People, Y in People : X = ann or Y = dan | else 1 - 1 / | X in People, Y in People : X = ann or Y = dan |)")));
}
示例4: simplify
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
public static Expression simplify(Expression expression) {
if (expression.get(0).equals(expression.get(1))) {
return Expressions.TRUE;
}
if (expression.get(0).equals(Expressions.TRUE)) {
return expression.get(1);
}
else if (expression.get(0).equals(Expressions.FALSE)) {
return Not.make(expression.get(1));
}
if (expression.get(1).equals(Expressions.TRUE)) {
return expression.get(0);
}
else if (expression.get(1).equals(Expressions.FALSE)) {
return Not.make(expression.get(0));
}
return expression;
}
示例5: constructComponentMap
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
private static Map<Expression, List<Pair<Expression, Integer>>> constructComponentMap(Map<Expression, TupleType> freeVariablesOfTupleType, Expression expression, Context context) {
Map<Expression, List<Pair<Expression, Integer>>> result = new LinkedHashMap<>();
for (Map.Entry<Expression, TupleType> freeVariableOfTupleType : freeVariablesOfTupleType.entrySet()) {
Expression freeVariable = freeVariableOfTupleType.getKey();
TupleType freeVariableTupleType = freeVariableOfTupleType.getValue();
List<Pair<Expression, Integer>> components = new ArrayList<>();
int tupleArity = freeVariableTupleType.getArity();
for (int i = 1; i <= tupleArity; i++) {
String proposedComponentVariableName = freeVariable.toString()+i;
Expression componentVariable = Expressions.makeUniqueVariable(proposedComponentVariableName, expression, context);
components.add(new Pair<>(componentVariable, i));
}
result.put(freeVariable, components);
}
return result;
}
示例6: testScientificOutput
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
@Test
public void testScientificOutput() {
//
// Positive
Assert.assertEquals("1.2E-7", Expressions.makeSymbol(0.000000123).toString());
Assert.assertEquals("1E-9", Expressions.makeSymbol(0.000000001).toString());
Assert.assertEquals("1E6", Expressions.makeSymbol(1000000.013).toString());
Assert.assertEquals("1E6", Expressions.makeSymbol(1000000.016).toString());
Assert.assertEquals("1E6", Expressions.makeSymbol(new Rational(10000009, 10)).toString()); // 1000000.9
Assert.assertEquals("1.23457E6", Expressions.makeSymbol(1234567.1).toString());
Assert.assertEquals("1.23457E6", Expressions.makeSymbol(1234567.9).toString());
//
// Negative
Assert.assertEquals("-1.2E-7", Expressions.makeSymbol(-0.000000123).toString());
Assert.assertEquals("-1E-9", Expressions.makeSymbol(-0.000000001).toString());
Assert.assertEquals("-1E6", Expressions.makeSymbol(-1000000.013).toString());
Assert.assertEquals("-1E6", Expressions.makeSymbol(-1000000.016).toString());
Assert.assertEquals("-1E6", Expressions.makeSymbol(new Rational(-10000009, 10)).toString()); // 1000000.9
Assert.assertEquals("-1.23457E6", Expressions.makeSymbol(-1234567.1).toString());
Assert.assertEquals("-1.23457E6", Expressions.makeSymbol(-1234567.9).toString());
}
示例7: testSimpleRecursiveRewriter
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
@Test
public void testSimpleRecursiveRewriter() {
RewriterFromStepMaker rewriter =
(Expression e, Context c) -> {
if (Expressions.isNumber(e)) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
}
return new Solution(e);
};
Expression initial;
Expression expected;
initial = parse("7");
expected = parse("8");
runTest(rewriter, initial, expected, map());
initial = parse("f(9,g(8,7,6))");
expected = parse("f(10,g(9,8,7))");
runTest(rewriter, initial, expected, map());
initial = parse("(6)(9,g(8,7,6))");
expected = parse("(7)(10,g(9,8,7))");
runTest(rewriter, initial, expected, map());
}
示例8: getQuantifiers
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
void getQuantifiers(Expression expr, List<Pair<Expression, Map<Expression, ConstantDeclaration>>> quantifiersWithScope, Map<Expression, ConstantDeclaration> currentScope) {
if (isQuantifiedExpression(expr)) {
Map<Expression, ConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
quantifierScope.putAll(getQuantifiedExpressionScope(expr));
quantifiersWithScope.add(new Pair<>(expr, quantifierScope));
Expression bodyExpression = getQuantifiedExpressionBody(expr);
if (bodyExpression != null) {
getQuantifiers(bodyExpression, quantifiersWithScope, quantifierScope);
}
}
if (Expressions.isFunctionApplicationWithArguments(expr)) {
expr.getArguments().forEach(arg -> getQuantifiers(arg, quantifiersWithScope, currentScope));
}
}
示例9: getNonConstantRandomFunctions
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
void getNonConstantRandomFunctions(Expression expr, List<Pair<Expression, Map<Expression, ConstantDeclaration>>> nonConstantRandomFunctionsWithScope, Map<Expression, ConstantDeclaration> currentScope) {
if (Expressions.isFunctionApplicationWithArguments(expr) &&
!isDeclaredConstantFunctor(expr.getFunctorOrSymbol(), currentScope) &&
!isDeclaredRandomFunctor(expr.getFunctorOrSymbol())) {
nonConstantRandomFunctionsWithScope.add(new Pair<>(expr, currentScope));
}
if (Expressions.isFunctionApplicationWithArguments(expr)) {
expr.getArguments().forEach(arg -> getNonConstantRandomFunctions(arg, nonConstantRandomFunctionsWithScope, currentScope));
}
else if (isQuantifiedExpression(expr)) {
Map<Expression, ConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
quantifierScope.putAll(getQuantifiedExpressionScope(expr));
Expression bodyExpression = getQuantifiedExpressionBody(expr);
if (bodyExpression != null) {
getNonConstantRandomFunctions(bodyExpression, nonConstantRandomFunctionsWithScope, quantifierScope);
}
}
}
示例10: definiteIntegral
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
/**
* This method will return the polynomial equivalent to:<br>
* <pre>
* Q.replace(variable, end) - Q.replace(variable, start)
* </pre>
* where 'Q = indefiniteIntegral(polynomial, variable)'
*
* @param polynomial
* the polynomial the definite integral is to be found for.
* @param variable
* the variable integration is with respect to.
* @param start
* the starting limit of the integral.
* @param end
* the ending limit of the integral.
* @return the definite integral of the polynomial for the given limits.
*/
public static Polynomial definiteIntegral(Polynomial polynomial, Expression variable, Expression start, Expression end, Predicate<Expression> isVariable) {
Polynomial q = indefiniteIntegral(polynomial, variable);
Set<Expression> variableSet = new LinkedHashSet<>(q.getVariables()); // Note: will include variable due to calling indefiniteIntegral
variableSet.addAll(Expressions.freeVariables(start, isVariable));
variableSet.addAll(Expressions.freeVariables(end, isVariable));
// if (!isNumber(start) && !isPositiveOrNegativeInfinity(start)) {
// variableSet.add(start);
// }
// if (!isNumber(end) && !isPositiveOrNegativeInfinity(end)) {
// variableSet.add(end);
// }
List<Expression> variables = new ArrayList<>(variableSet);
Polynomial minuendPolynomial = replaceFactor(q, variable, end, variables);
Polynomial subtrahendPolynomial = replaceFactor(q, variable, start, variables);
Polynomial result = minuendPolynomial.minus(subtrahendPolynomial);
return result;
}
示例11: getRandomVariableDeclaration
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
/**
*
* @return an expression representing the full random variable declaration.
*/
public Expression getRandomVariableDeclaration() {
// Lazy initialize this attribute
if (randomVariableDeclaration == null) {
List<Expression> declarationArgs = new ArrayList<Expression>();
declarationArgs.add(name);
declarationArgs.add(arity);
declarationArgs.addAll(parameters);
declarationArgs.add(range);
randomVariableDeclaration = Expressions.makeExpressionOnSyntaxTreeWithLabelAndSubTrees(
FUNCTOR_RANDOM_VARIABLE_DECLARATION,
declarationArgs.toArray());
}
return randomVariableDeclaration;
}
示例12: 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);
}
}
示例13: convertGenericTableToInstance
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
public static Expression convertGenericTableToInstance(FunctionTable functionTable, Expression genericFunctionTableExpr, List<Integer> instanceVarIdxs) {
Expression result = genericFunctionTableExpr;
Context context = new TrueContext();
for (int i = 0; i < functionTable.numberVariables(); i++) {
// Replace the generic variable name with the correct instance name
result = SyntacticSubstitute.replace(result, Expressions.makeSymbol(genericVariableName(i)), Expressions.makeSymbol(instanceVariableName(instanceVarIdxs.get(i))), context);
int varCardinality = functionTable.cardinality(i);
for (int c = 0; c < varCardinality; c++) {
// Replace the generic constants with constants for the variable index (if they differ)
Expression genericConstant = Expressions.makeSymbol(genericConstantValueForVariable(c, i, varCardinality));
Expression instanceConstant = Expressions.makeSymbol(instanceConstantValueForVariable(c, instanceVarIdxs.get(i), varCardinality));
if (!genericConstant.equals(instanceConstant)) {
result = SyntacticSubstitute.replace(result, genericConstant, instanceConstant, context);
}
}
}
return result;
}
示例14: simplify
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
/**
* Simplify a unary minus application if possible.
* @param expression
* @return
*/
public static Expression simplify(Expression expression) {
if (expression.get(0).hasFunctor(MINUS) && expression.get(0).numberOfArguments() == 1) { // double minus
Expression argumentsArgument = expression.get(0).get(0);
if (argumentsArgument.hasFunctor(MINUS) && argumentsArgument.numberOfArguments() == 1) {
return simplify(expression);
}
else {
return argumentsArgument;
}
}
else if (expression.get(0).getValue() instanceof Number) {
return Expressions.makeSymbol(expression.get(0).rationalValue().negate());
}
return expression;
}
示例15: simplify
import com.sri.ai.expresso.helper.Expressions; //导入依赖的package包/类
static public Expression simplify(Expression expression) {
Expression result = expression;
Expression first = expression.get(0);
Expression second = expression.get(1);
if (Expressions.isNumber(first)) {
Rational firstValue = first.rationalValue();
if (Expressions.isNumber(second)) {
Rational secondValue = second.rationalValue();
result = Expressions.makeSymbol(firstValue.subtract(secondValue));
}
else if (firstValue.isZero()) {
result = Expressions.apply("-", second);
}
}
else if (Expressions.isNumber(second) && second.rationalValue().isZero()) {
result = first;
}
return result;
}