Search in sources :

Example 1 with EvalResult

use of de.prob.animator.domainobjects.EvalResult in project prob2 by bendisposto.

the class CbcSolveCommand method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    PrologTerm idList = bindings.get(IDENTIFIER_LIST);
    if (idList instanceof ListPrologTerm) {
        for (PrologTerm id : (ListPrologTerm) idList) {
            freeVariables.add(id.getFunctor());
        }
    }
    PrologTerm prologTerm = bindings.get(EVALUATE_TERM_VARIABLE);
    assert prologTerm instanceof CompoundPrologTerm;
    String functor = prologTerm.getFunctor();
    if ("time_out".equals(functor)) {
        result = new ComputationNotCompletedResult(evalElement.getCode(), "time out");
    }
    if ("contradiction_found".equals(functor)) {
        result = EvalResult.FALSE;
    }
    if ("solution".equals(functor)) {
        ListPrologTerm solutionBindings = BindingGenerator.getList(prologTerm.getArgument(BINDINGS));
        if (solutionBindings.isEmpty()) {
            result = EvalResult.TRUE;
            return;
        }
        Map<String, String> solutions = new HashMap<>();
        for (PrologTerm b : solutionBindings) {
            CompoundPrologTerm t = (CompoundPrologTerm) b;
            solutions.put(t.getArgument(VAR_NAME).getFunctor(), t.getArgument(PRETTY_PRINT).getFunctor());
        }
        result = new EvalResult("TRUE", solutions);
    }
    if ("no_solution_found".equals(functor)) {
        result = new ComputationNotCompletedResult(evalElement.getCode(), "no solution found (but one might exist)");
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) ComputationNotCompletedResult(de.prob.animator.domainobjects.ComputationNotCompletedResult) HashMap(java.util.HashMap) EvalResult(de.prob.animator.domainobjects.EvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm)

Example 2 with EvalResult

use of de.prob.animator.domainobjects.EvalResult in project prob2 by bendisposto.

the class EvaluateFormulasCommandTest method testProcessResult.

@Test
public void testProcessResult() throws Exception {
    IEvalElement element = new ClassicalB("1<3", FormulaExpand.EXPAND);
    final CompoundPrologTerm lpt = mk_result("true");
    ISimplifiedROMap<String, PrologTerm> m1 = new ISimplifiedROMap<String, PrologTerm>() {

        @Override
        public PrologTerm get(final String key) {
            return lpt;
        }
    };
    EvaluateFormulaCommand command = new EvaluateFormulaCommand(element, "root");
    command.processResult(m1);
    AbstractEvalResult value = command.getValue();
    assertEquals(((EvalResult) value).getValue(), "true");
    assertEquals(((EvalResult) value).getSolutions().get("a"), "3");
}
Also used : EvalResult(de.prob.animator.domainobjects.EvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) IEvalElement(de.prob.animator.domainobjects.IEvalElement) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ClassicalB(de.prob.animator.domainobjects.ClassicalB) ISimplifiedROMap(de.prob.parser.ISimplifiedROMap) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) Test(org.junit.Test)

Example 3 with EvalResult

use of de.prob.animator.domainobjects.EvalResult in project prob2 by bendisposto.

the class PersistentTransition method addValuesToDestState.

private void addValuesToDestState(Map<IEvalElement, AbstractEvalResult> map) {
    if (destState == null) {
        destState = new HashMap<>();
    }
    for (Map.Entry<IEvalElement, AbstractEvalResult> entry : map.entrySet()) {
        if (entry.getValue() instanceof EvalResult) {
            EvalResult evalResult = (EvalResult) entry.getValue();
            destState.put(entry.getKey().getCode(), evalResult.getValue());
        }
    }
}
Also used : EvalResult(de.prob.animator.domainobjects.EvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) IEvalElement(de.prob.animator.domainobjects.IEvalElement) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with EvalResult

use of de.prob.animator.domainobjects.EvalResult in project prob2 by bendisposto.

the class EvalstoreEvalCommand method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    CompoundPrologTerm term = (CompoundPrologTerm) bindings.get(RESULT_VAR);
    // most fields are about
    if (term.hasFunctor("interrupted", 0)) {
        this.result = new EvalstoreResult(false, true, evalstoreId, null, Collections.emptyList());
    } else if (term.hasFunctor("timeout", 0)) {
        this.result = new EvalstoreResult(true, false, evalstoreId, null, Collections.emptyList());
    } else if (term.hasFunctor("errors", 1)) {
        final ListPrologTerm args = (ListPrologTerm) term.getArgument(1);
        final List<String> errors = args.stream().map(arg -> PrologTerm.atomicString(arg.getArgument(1))).collect(Collectors.toList());
        final String error = errors.isEmpty() ? "unspecified error" : errors.get(0);
        final AbstractEvalResult er = new ComputationNotCompletedResult(evalElement.getCode(), error);
        this.result = new EvalstoreResult(false, false, evalstoreId, er, Collections.emptyList());
    } else if (term.hasFunctor("ok", 4)) {
        // first argument ignored
        final String valueStr = PrologTerm.atomicString(term.getArgument(2));
        final ListPrologTerm ids = (ListPrologTerm) term.getArgument(3);
        final List<String> newIdentifiers = PrologTerm.atomicStrings(ids);
        final long storeId = ((IntegerPrologTerm) term.getArgument(4)).getValue().longValue();
        final EvalResult er = new EvalResult(valueStr, Collections.emptyMap());
        this.result = new EvalstoreResult(false, false, storeId, er, newIdentifiers);
    } else {
        // I don't now which
        throw new IllegalStateException("Unexpected es_eval result: " + term.getFunctor() + "/" + term.getArity());
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) ComputationNotCompletedResult(de.prob.animator.domainobjects.ComputationNotCompletedResult) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) EvalResult(de.prob.animator.domainobjects.EvalResult) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) Collectors(java.util.stream.Collectors) ISimplifiedROMap(de.prob.parser.ISimplifiedROMap) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) List(java.util.List) IEvalElement(de.prob.animator.domainobjects.IEvalElement) PrologTerm(de.prob.prolog.term.PrologTerm) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) Collections(java.util.Collections) IntegerPrologTerm(de.prob.prolog.term.IntegerPrologTerm) ComputationNotCompletedResult(de.prob.animator.domainobjects.ComputationNotCompletedResult) EvalResult(de.prob.animator.domainobjects.EvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) List(java.util.List) IntegerPrologTerm(de.prob.prolog.term.IntegerPrologTerm)

Example 5 with EvalResult

use of de.prob.animator.domainobjects.EvalResult in project prob2 by bendisposto.

the class RuleResult method transformCounterExamples.

private void transformCounterExamples(AbstractEvalResult abstractEvalResult) {
    EvalResult evalCurrent = (EvalResult) abstractEvalResult;
    TranslatedEvalResult translatedResult = null;
    try {
        translatedResult = evalCurrent.translate();
    } catch (Exception e) {
        /*- fall back solution if the result can not be parsed (e.g. {1,...,1000}) 
			 * should not not happen because MAX_DISPLAY_SET is set to -1 
			 * and hence, no truncated terms are delivered by ProBCore
			 * */
        final String message = evalCurrent.getValue().replaceAll("\"", "");
        counterExamples.add(new CounterExample(1, message));
        return;
    }
    if (translatedResult.getValue() instanceof de.prob.translator.types.Set) {
        de.prob.translator.types.Set set = (de.prob.translator.types.Set) translatedResult.getValue();
        for (final BObject object : set) {
            if (object instanceof Tuple) {
                final Tuple tuple = (Tuple) object;
                de.prob.translator.types.Number first = (de.prob.translator.types.Number) tuple.getFirst();
                int errorType = first.intValue();
                de.prob.translator.types.String second = (de.prob.translator.types.String) tuple.getSecond();
                String message = second.getValue();
                counterExamples.add(new CounterExample(errorType, message));
            } else {
                throw new AssertionError();
            }
        }
    } else if (translatedResult.getValue() instanceof de.prob.translator.types.Sequence) {
        de.prob.translator.types.Sequence sequence = (de.prob.translator.types.Sequence) translatedResult.getValue();
        for (int i = 1; i <= sequence.size(); i++) {
            de.prob.translator.types.String value = (de.prob.translator.types.String) sequence.get(i);
            String message = value.getValue();
            counterExamples.add(new CounterExample(i, message));
        }
    } else {
        // fall back: should not happen
        counterExamples.add(new CounterExample(1, evalCurrent.getValue()));
    }
}
Also used : BObject(de.prob.translator.types.BObject) Set(java.util.Set) EvalResult(de.prob.animator.domainobjects.EvalResult) TranslatedEvalResult(de.prob.animator.domainobjects.TranslatedEvalResult) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) TranslatedEvalResult(de.prob.animator.domainobjects.TranslatedEvalResult) Tuple(de.prob.translator.types.Tuple)

Aggregations

AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)5 EvalResult (de.prob.animator.domainobjects.EvalResult)5 IEvalElement (de.prob.animator.domainobjects.IEvalElement)3 CompoundPrologTerm (de.prob.prolog.term.CompoundPrologTerm)3 ListPrologTerm (de.prob.prolog.term.ListPrologTerm)3 PrologTerm (de.prob.prolog.term.PrologTerm)3 ComputationNotCompletedResult (de.prob.animator.domainobjects.ComputationNotCompletedResult)2 ISimplifiedROMap (de.prob.parser.ISimplifiedROMap)2 HashMap (java.util.HashMap)2 ClassicalB (de.prob.animator.domainobjects.ClassicalB)1 TranslatedEvalResult (de.prob.animator.domainobjects.TranslatedEvalResult)1 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)1 IntegerPrologTerm (de.prob.prolog.term.IntegerPrologTerm)1 BObject (de.prob.translator.types.BObject)1 Tuple (de.prob.translator.types.Tuple)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1