Search in sources :

Example 26 with StringJoiner

use of java.util.StringJoiner in project enhydrator by AdamBien.

the class PojoSink method checkConventions.

static void checkConventions(Class parent) {
    Field[] declaredFields = parent.getDeclaredFields();
    int counter = 0;
    StringJoiner joiner = new StringJoiner(",");
    for (Field field : declaredFields) {
        final Class<?> type = field.getType();
        if (type.isAssignableFrom(Collection.class)) {
            joiner.add(field.getName());
            counter++;
        }
    }
    if (counter > 1) {
        throw new IllegalStateException("Multiple (" + counter + ") collection fields with names " + joiner.toString() + "");
    }
}
Also used : Field(java.lang.reflect.Field) StringJoiner(java.util.StringJoiner)

Example 27 with StringJoiner

use of java.util.StringJoiner in project aic-expresso by aic-sri-international.

the class TypeTest method checkTypeIteration.

public void checkTypeIteration(Type type, String... expectedLines) {
    StringJoiner expected = new StringJoiner("\n", type.getName() + "\n", "");
    for (String expectedLine : expectedLines) {
        expected.add(expectedLine);
    }
    StringJoiner actual = new StringJoiner("\n", type.getName() + "\n", "");
    type.iterator().forEachRemaining(actualLine -> actual.add(actualLine.toString()));
    Assert.assertEquals(expected.toString(), actual.toString());
}
Also used : StringJoiner(java.util.StringJoiner)

Example 28 with StringJoiner

use of java.util.StringJoiner in project aic-praise by aic-sri-international.

the class HOGModelGroundingTest method test.

/**
	 * @throws AssertionError
	 */
@Test
public void test() throws AssertionError {
    long start = System.currentTimeMillis();
    StringJoiner sj = new StringJoiner("\n");
    sj.add("sort People : 10, Putin;");
    sj.add("sort Countries : 10, USA, Russia;");
    sj.add("random country : Countries;");
    sj.add("random president : People;");
    sj.add("random communism : Boolean;");
    sj.add("random democracy : Boolean;");
    sj.add("random votePutin : 1..15;");
    sj.add("if country = Russia then if president = Putin then communism else not communism else if democracy then not communism else communism;");
    sj.add("if country = Russia then if votePutin > 5 then president = Putin else not president = Putin;");
    HOGMParserWrapper parser = new HOGMParserWrapper();
    ParsedHOGModel parsedModel = parser.parseModel(sj.toString());
    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
    List<Expression> evidence = new ArrayList<>();
    evidence.add(Expressions.parse("communism"));
    StringJoiner outputBuffer = new StringJoiner("");
    HOGModelGrounding.ground(factorsAndTypes, evidence, new // NOTE: an example listener that outputs in the UAI format
    HOGModelGrounding.Listener() {

        int numberVariables;

        StringJoiner preamble = new StringJoiner("");

        StringJoiner functionTables = new StringJoiner("");

        List<Pair<Integer, Integer>> evidence = new ArrayList<>();

        @Override
        public void numberGroundVariables(int number) {
            this.numberVariables = number;
            preamble.add("MARKOV\n");
            preamble.add("" + number + "\n");
        }

        @Override
        public void groundVariableCardinality(int variableIndex, int cardinality) {
            preamble.add("" + cardinality);
            if (variableIndex == (numberVariables - 1)) {
                preamble.add("\n");
            } else {
                preamble.add(" ");
            }
        }

        @Override
        public void numberFactors(int number) {
            preamble.add("" + number + "\n");
        }

        @Override
        public void factorParticipants(int factorIndex, int[] variableIndexes) {
            preamble.add("" + variableIndexes.length);
            for (int i = 0; i < variableIndexes.length; i++) {
                preamble.add(" " + variableIndexes[i]);
            }
            preamble.add("\n");
        }

        @Override
        public void factorValue(int numberFactorValues, boolean isFirstValue, boolean isLastValue, Rational value) {
            if (isFirstValue) {
                functionTables.add("\n" + numberFactorValues + "\n");
            } else {
                functionTables.add(" ");
            }
            functionTables.add("" + value.doubleValue());
            if (isLastValue) {
                functionTables.add("\n");
            }
        }

        @Override
        public void evidence(int variableIndex, int valueIndex) {
            evidence.add(new Pair<>(variableIndex, valueIndex));
        }

        @Override
        public void groundingComplete() {
            long end = System.currentTimeMillis() - start;
            outputBuffer.add("--- MODEL ---\n");
            outputBuffer.add(preamble.toString());
            outputBuffer.add(functionTables.toString());
            outputBuffer.add("--- EVIDENCE ---\n");
            outputBuffer.add("" + evidence.size());
            for (Pair<Integer, Integer> evidenceAssignment : evidence) {
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.first.toString());
                outputBuffer.add(" ");
                outputBuffer.add(evidenceAssignment.second.toString());
            }
            System.out.println(outputBuffer.toString());
            System.out.println("\nTime ta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5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5\n" + "--- EVIDENCE ---\n" + "1 2 1";
    assertEquals(expected, outputBuffer.toString());
}
Also used : ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) Rational(com.sri.ai.util.math.Rational) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) HOGModelGrounding(com.sri.ai.praise.lang.grounded.model.HOGModelGrounding) Expression(com.sri.ai.expresso.api.Expression) StringJoiner(java.util.StringJoiner) Pair(com.sri.ai.util.base.Pair) Test(org.junit.Test)

Example 29 with StringJoiner

use of java.util.StringJoiner in project aic-praise by aic-sri-international.

the class HOGMQueryError method toString.

@Override
public String toString() {
    StringJoiner sj = new StringJoiner("");
    if (context == Context.UNKNOWN) {
        sj.add("General Error: ");
    } else if (context == Context.QUERY) {
        sj.add("Error in Query ");
    } else if (context == Context.MODEL) {
        sj.add("Error in Model ");
    }
    if (context != Context.UNKNOWN) {
        sj.add("at Line " + line + ": ");
    }
    sj.add(errorMessage);
    if (throwable != null) {
        sj.add("\n");
        sj.add(ExceptionUtils.getStackTrace(throwable));
    }
    return sj.toString();
}
Also used : StringJoiner(java.util.StringJoiner)

Example 30 with StringJoiner

use of java.util.StringJoiner in project aic-expresso by aic-sri-international.

the class FunctionType method iterator.

@Override
public Iterator<Expression> iterator() {
    if (!(getCodomain().isDiscrete() && getArgumentTypes().stream().allMatch(Type::isFinite))) {
        throw new Error("Only function types with finite argument types and a discrete codomain can be enumerated.");
    }
    if (cachedIterateRegistry == null) {
        // Pre-compute
        cachedIterateRegistry = new DefaultRegistry();
        int numCodomainValues = argumentTypes.stream().map(Type::cardinality).map(Expression::rationalValue).reduce(Rational.ONE, Rational::multiply).intValue();
        cachedIterateRegistry = cachedIterateRegistry.add(getCodomain());
        Expression codomainTypeExpression = parse(getCodomain().getName());
        codomainVariables = new ArrayList<>(numCodomainValues);
        Map<Expression, Expression> symbolsAndTypes = new LinkedHashMap<>();
        for (int i = 0; i < numCodomainValues; i++) {
            Expression coDomainVariableI = makeSymbol("C" + (i + 1));
            codomainVariables.add(coDomainVariableI);
            symbolsAndTypes.put(coDomainVariableI, codomainTypeExpression);
        }
        List<Expression> argVariables = new ArrayList<>();
        for (int i = 0; i < getArgumentTypes().size(); i++) {
            cachedIterateRegistry = cachedIterateRegistry.add(getArgumentTypes().get(i));
            argVariables.add(makeSymbol("A" + (i + 1)));
            symbolsAndTypes.put(argVariables.get(i), parse(getArgumentTypes().get(i).getName()));
        }
        cachedIterateRegistry = cachedIterateRegistry.setSymbolsAndTypes(symbolsAndTypes);
        StringJoiner lambdaApplicationPrefix = new StringJoiner(", ", "(lambda ", " : ");
        for (Expression argVar : argVariables) {
            lambdaApplicationPrefix.add(argVar + " in " + symbolsAndTypes.get(argVar));
        }
        AssignmentsIterator assignmentsIterator = new AssignmentsIterator(argVariables, cachedIterateRegistry);
        StringJoiner lambdaApplicationBody = new StringJoiner(" else ", "", ")");
        AtomicInteger counter = new AtomicInteger(0);
        assignmentsIterator.forEachRemaining(assignment -> {
            if (counter.incrementAndGet() != numCodomainValues) {
                StringJoiner condition = new StringJoiner(" and ", "if ", " then C" + counter);
                for (int i = 0; i < argVariables.size(); i++) {
                    Expression argVariable = argVariables.get(i);
                    condition.add(argVariable + " = " + assignment.get(argVariable));
                }
                lambdaApplicationBody.add(condition.toString());
            } else {
                lambdaApplicationBody.add("C" + numCodomainValues);
            }
        });
        genericLambda = parse(lambdaApplicationPrefix.toString() + lambdaApplicationBody.toString());
    }
    return FunctionIterator.functionIterator(new AssignmentsIterator(codomainVariables, cachedIterateRegistry), assignment -> {
        Expression lambda = genericLambda;
        for (int i = 0; i < codomainVariables.size(); i++) {
            Expression codomainVariable = codomainVariables.get(i);
            lambda = lambda.replaceFirstOccurrence(codomainVariable, assignment.get(codomainVariable), cachedIterateRegistry);
        }
        return lambda;
    });
}
Also used : Expression(com.sri.ai.expresso.api.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) ArrayList(java.util.ArrayList) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) StringJoiner(java.util.StringJoiner) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

StringJoiner (java.util.StringJoiner)94 ArrayList (java.util.ArrayList)20 List (java.util.List)10 HashSet (java.util.HashSet)6 Map (java.util.Map)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 ClassName (com.squareup.javapoet.ClassName)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 Expression (com.sri.ai.expresso.api.Expression)3 Attribute (io.requery.meta.Attribute)3 Scanner (java.util.Scanner)3 RaptorColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle)2 Range (com.facebook.presto.spi.predicate.Range)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2 MaterializedRow (com.facebook.presto.testing.MaterializedRow)2