Search in sources :

Example 86 with PrologTerm

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

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

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

the class LoadBProjectCommand method printLoadTerm.

private void printLoadTerm(IPrologTermOutput pto) {
    StructuredPrologOutput parserOutput = new StructuredPrologOutput();
    this.rml.printAsProlog(parserOutput);
    nodeIdMapping = this.rml.getNodeIdMapping();
    for (PrologTerm term : parserOutput.getSentences()) {
        pto.printTerm(term);
    }
}
Also used : StructuredPrologOutput(de.prob.prolog.output.StructuredPrologOutput) PrologTerm(de.prob.prolog.term.PrologTerm)

Example 89 with PrologTerm

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

the class GetMachineStructureCommand method makeASTNode.

private static PrologASTNode makeASTNode(PrologTerm node) {
    if ("formula".equals(node.getFunctor())) {
        final PrologTerm term = node.getArgument(1);
        final PrologTerm prettyPrintTerm = node.getArgument(2);
        // It's not clear if this is the intended behavior, or if they should have been converted to atoms in the Prolog code.
        if (!prettyPrintTerm.isAtom() && !prettyPrintTerm.isNumber()) {
            throw new IllegalArgumentException("Formula pretty print must be an atom or number: " + prettyPrintTerm);
        }
        final String prettyPrint = prettyPrintTerm.getFunctor();
        return new ASTFormula(term, prettyPrint);
    } else if ("category".equals(node.getFunctor())) {
        final String name = node.getArgument(1).getFunctor();
        final List<String> infos = PrologTerm.atomicStrings((ListPrologTerm) node.getArgument(2));
        final List<PrologASTNode> subnodes = buildAST(BindingGenerator.getList(node.getArgument(3)));
        final boolean expanded = infos.contains("expanded");
        final boolean propagated = infos.contains("propagated");
        return new ASTCategory(subnodes, name, expanded, propagated);
    } else {
        throw new AssertionError("Unknown node type: " + node.getFunctor());
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) ASTCategory(de.prob.animator.prologast.ASTCategory) ASTFormula(de.prob.animator.prologast.ASTFormula) List(java.util.List) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 90 with PrologTerm

use of de.prob.prolog.term.PrologTerm 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

PrologTerm (de.prob.prolog.term.PrologTerm)103 ListPrologTerm (de.prob.prolog.term.ListPrologTerm)86 CompoundPrologTerm (de.prob.prolog.term.CompoundPrologTerm)85 Test (org.junit.Test)64 IntegerPrologTerm (de.prob.prolog.term.IntegerPrologTerm)11 StructuredPrologOutput (de.prob.prolog.output.StructuredPrologOutput)10 VariablePrologTerm (de.prob.prolog.term.VariablePrologTerm)8 Transition (de.prob.statespace.Transition)5 IEvalElement (de.prob.animator.domainobjects.IEvalElement)4 ArrayList (java.util.ArrayList)4 CheckBooleanPropertyCommand (de.prob.animator.command.CheckBooleanPropertyCommand)3 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)3 EvalResult (de.prob.animator.domainobjects.EvalResult)3 ISimplifiedROMap (de.prob.parser.ISimplifiedROMap)3 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)3 RecursiveMachineLoader (de.be4.classicalb.core.parser.analysis.prolog.RecursiveMachineLoader)2 LtlParseException (de.be4.ltl.core.parser.LtlParseException)2 ClassicalB (de.prob.animator.domainobjects.ClassicalB)2 ComputationNotCompletedResult (de.prob.animator.domainobjects.ComputationNotCompletedResult)2 StateError (de.prob.animator.domainobjects.StateError)2