Search in sources :

Example 76 with CompoundPrologTerm

use of de.prob.prolog.term.CompoundPrologTerm 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 77 with CompoundPrologTerm

use of de.prob.prolog.term.CompoundPrologTerm in project prob2 by bendisposto.

the class ExecuteModelCommand method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    PrologTerm prologTerm = bindings.get(TRANSITION_VARIABLE);
    CompoundPrologTerm cpt = BindingGenerator.getCompoundTerm(prologTerm, 4);
    Transition operation = Transition.createTransitionFromCompoundPrologTerm(statespace, cpt);
    resultTrace.add(operation);
    IntegerPrologTerm intPrologTerm = BindingGenerator.getInteger(bindings.get(EXECUTED_STEPS_VARIABLE));
    BigInteger bigInt = intPrologTerm.getValue();
    stepsExecuted = bigInt.intValue();
    switch(bindings.get(RESULT_VARIABLE).getFunctor()) {
        case "maximum_nr_of_steps_reached":
            this.result = ExecuteModelResult.MAXIMUM_NR_OF_STEPS_REACHED;
            break;
        case "deadlock":
            this.result = ExecuteModelResult.DEADLOCK;
            break;
        case "error":
            this.result = ExecuteModelResult.ERROR;
            break;
        case "internal_error":
            this.result = ExecuteModelResult.INTERNAL_ERROR;
            break;
        case "time_out":
            this.result = ExecuteModelResult.TIME_OUT;
            break;
        default:
            throw new AssertionError("Unexpected result of execute command.");
    }
}
Also used : Transition(de.prob.statespace.Transition) BigInteger(java.math.BigInteger) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) IntegerPrologTerm(de.prob.prolog.term.IntegerPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) IntegerPrologTerm(de.prob.prolog.term.IntegerPrologTerm)

Example 78 with CompoundPrologTerm

use of de.prob.prolog.term.CompoundPrologTerm in project prob2 by bendisposto.

the class UnsatRegularCoreCommand method processResult.

@Override
public void processResult(ISimplifiedROMap<String, PrologTerm> bindings) {
    CompoundPrologTerm compoundTerm = BindingGenerator.getCompoundTerm(bindings.get(RESULT_VARIABLE), 0);
    String code = compoundTerm.getFunctor();
    if (pred instanceof EventB) {
        core = new EventB(code, FormulaExpand.EXPAND);
    } else {
        core = new ClassicalB(code, FormulaExpand.EXPAND);
    }
}
Also used : EventB(de.prob.animator.domainobjects.EventB) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ClassicalB(de.prob.animator.domainobjects.ClassicalB)

Example 79 with CompoundPrologTerm

use of de.prob.prolog.term.CompoundPrologTerm in project prob2 by bendisposto.

the class WeakestPreconditionCommand method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    CompoundPrologTerm compoundTerm = BindingGenerator.getCompoundTerm(bindings.get(WEAKEST_PRECONDITION_VARIABLE), 0);
    String code = compoundTerm.getFunctor();
    if (predicate instanceof EventB) {
        result = new EventB(code, FormulaExpand.EXPAND);
    } else {
        result = new ClassicalB(code, FormulaExpand.EXPAND);
    }
}
Also used : EventB(de.prob.animator.domainobjects.EventB) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ClassicalB(de.prob.animator.domainobjects.ClassicalB)

Example 80 with CompoundPrologTerm

use of de.prob.prolog.term.CompoundPrologTerm in project prob2 by bendisposto.

the class EvalResult method getEvalResult.

/**
 * Translates the results from ProB into an {@link AbstractEvalResult}. This
 * is intended mainly for internal use, for developers who are writing
 * commands and want to translate them into an {@link AbstractEvalResult}.
 *
 * @param pt
 *            PrologTerm
 * @return {@link AbstractEvalResult} translation of pt
 */
public static AbstractEvalResult getEvalResult(PrologTerm pt) {
    if (pt instanceof ListPrologTerm) {
        /*
			 * If the evaluation was not successful, the result should be a
			 * Prolog list with the code on the first index and a list of errors
			 * This results therefore in a ComputationNotCompleted command
			 */
        ListPrologTerm listP = (ListPrologTerm) pt;
        ArrayList<String> list = new ArrayList<>();
        String code = listP.get(0).getFunctor();
        for (int i = 1; i < listP.size(); i++) {
            list.add(listP.get(i).getArgument(1).getFunctor());
        }
        return new ComputationNotCompletedResult(code, Joiner.on(",").join(list));
    } else if (pt.getFunctor().intern().equals("result")) {
        /*
			 * If the formula in question was a predicate, the result term will
			 * have the following form: result(Value,Solutions) where Value is
			 * 'TRUE','POSSIBLY TRUE', or 'FALSE' Solutions is then a list of
			 * triples bind(Name,Solution,PPSol) where Name is the name of the
			 * free variable calculated by ProB, Solution is the Prolog
			 * representation of the solution, and PPSol is the String pretty
			 * print of the solution calculated by Prolog.
			 *
			 * If the formula in question was an expression, the result term
			 * will have the following form: result(v(SRes,PRes),[],Code) where
			 * SRes is the string representation of the result calculated by
			 * ProB and PRes is the Prolog representation of the value.
			 *
			 * From this information, an EvalResult object is created.
			 */
        PrologTerm v = pt.getArgument(1);
        String value = v.getFunctor().intern();
        ListPrologTerm solutionList = BindingGenerator.getList(pt.getArgument(2));
        if (value.equals("TRUE") && solutionList.isEmpty()) {
            return TRUE;
        }
        if (value.equals("FALSE") && solutionList.isEmpty()) {
            return FALSE;
        }
        if (!value.equals("TRUE") && !value.equals("FALSE") && formulaCache.containsKey(value)) {
            return formulaCache.get(value);
        }
        if (v instanceof CompoundPrologTerm && v.getArity() == 2) {
            CompoundPrologTerm cpt = BindingGenerator.getCompoundTerm(v, 2);
            value = cpt.getArgument(1).getFunctor();
        }
        Map<String, String> solutions = solutionList.isEmpty() ? EMPTY_MAP : new HashMap<>();
        for (PrologTerm t : solutionList) {
            CompoundPrologTerm cpt = BindingGenerator.getCompoundTerm(t, 2);
            solutions.put(cpt.getArgument(1).getFunctor().intern(), cpt.getArgument(2).getFunctor().intern());
        }
        EvalResult res = new EvalResult(value, solutions);
        if (!value.equals("TRUE") && !value.equals("FALSE")) {
            formulaCache.put(value, res);
        }
        return res;
    } else if (pt.getFunctor().intern().equals("errors") && pt.getArgument(1).getFunctor().intern().equals("NOT-WELL-DEFINED")) {
        ListPrologTerm arg2 = BindingGenerator.getList(pt.getArgument(2));
        return new WDError(arg2.stream().map(PrologTerm::getFunctor).collect(Collectors.toList()));
    } else if (pt.getFunctor().intern().equals("errors") && pt.getArgument(1).getFunctor().intern().equals("IDENTIFIER(S) NOT YET INITIALISED; INITIALISE MACHINE FIRST")) {
        ListPrologTerm arg2 = BindingGenerator.getList(pt.getArgument(2));
        return new IdentifierNotInitialised(arg2.stream().map(PrologTerm::getFunctor).collect(Collectors.toList()));
    } else if (pt.getFunctor().intern().equals("enum_warning")) {
        return new EnumerationWarning();
    }
    throw new IllegalArgumentException("Unknown result type " + pt.toString());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CompoundPrologTerm (de.prob.prolog.term.CompoundPrologTerm)90 PrologTerm (de.prob.prolog.term.PrologTerm)79 ListPrologTerm (de.prob.prolog.term.ListPrologTerm)74 Test (org.junit.Test)62 IntegerPrologTerm (de.prob.prolog.term.IntegerPrologTerm)8 StructuredPrologOutput (de.prob.prolog.output.StructuredPrologOutput)7 ClassicalB (de.prob.animator.domainobjects.ClassicalB)6 VariablePrologTerm (de.prob.prolog.term.VariablePrologTerm)6 EventB (de.prob.animator.domainobjects.EventB)5 Transition (de.prob.statespace.Transition)5 ArrayList (java.util.ArrayList)4 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)3 EvalResult (de.prob.animator.domainobjects.EvalResult)3 IEvalElement (de.prob.animator.domainobjects.IEvalElement)3 ISimplifiedROMap (de.prob.parser.ISimplifiedROMap)3 ComputationNotCompletedResult (de.prob.animator.domainobjects.ComputationNotCompletedResult)2 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)2 BigInteger (java.math.BigInteger)2 Collections (java.util.Collections)2 BParser (de.be4.classicalb.core.parser.BParser)1