Search in sources :

Example 31 with ListPrologTerm

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

Example 32 with ListPrologTerm

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

the class ExpandedFormula method init.

public void init(final CompoundPrologTerm cpt) {
    name = cpt.getArgument(1).getFunctor();
    fields.put("name", escapeUnicode(name));
    // Value
    PrologTerm v = cpt.getArgument(2);
    value = getValue(v);
    fields.put("value", value instanceof String ? escapeUnicode((String) value) : value);
    fields.put("hasError", hasError);
    // Children
    id = cpt.getArgument(3).getFunctor();
    fields.put("id", id);
    ListPrologTerm list = BindingGenerator.getList(cpt.getArgument(4));
    if (!list.isEmpty()) {
        children = new ArrayList<>();
        List<Object> childrenFields = new ArrayList<>();
        for (PrologTerm prologTerm : list) {
            ExpandedFormula expandedFormula = new ExpandedFormula(BindingGenerator.getCompoundTerm(prologTerm, 4));
            children.add(expandedFormula);
            childrenFields.add(expandedFormula.getFields());
        }
        fields.put("children", childrenFields);
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) ArrayList(java.util.ArrayList) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 33 with ListPrologTerm

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

the class GetCurrentPreferencesCommand method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    ListPrologTerm prefs = BindingGenerator.getList(bindings.get(PREFERENCES_VARIABLE));
    for (PrologTerm prologTerm : prefs) {
        CompoundPrologTerm cpt = BindingGenerator.getCompoundTerm(prologTerm, 2);
        preferences.put(cpt.getArgument(1).getFunctor(), cpt.getArgument(2).getFunctor());
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 34 with ListPrologTerm

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

the class GetEnableMatrixCommand method processResult.

// yes('.'(=('Matrix','.'(enable_rel(new,del,enable_edges(ok,ok,false,false)),[])),[]))
// enable_edges(Enable,KeepEnabled,Disable,KeepDisabled)
@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    ListPrologTerm elements = (ListPrologTerm) bindings.get(MATRIX_VAR);
    matrix.clear();
    for (PrologTerm term : elements) {
        EventPair key = new EventPair(term.getArgument(1).getFunctor(), term.getArgument(2).getFunctor());
        matrix.put(key, new EnableMatixEntry(term.getArgument(3)));
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 35 with ListPrologTerm

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

the class GetMachineOperationInfos method processResult.

@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
    for (PrologTerm prologTerm : BindingGenerator.getList(bindings, RESULT_VARIABLE)) {
        final String opName = prologTerm.getArgument(1).getFunctor();
        final List<String> outputParameterNames = ((ListPrologTerm) prologTerm.getArgument(2)).stream().map(PrologTerm::getFunctor).collect(Collectors.toList());
        final List<String> parameterNames = ((ListPrologTerm) prologTerm.getArgument(3)).stream().map(PrologTerm::getFunctor).collect(Collectors.toList());
        operationInfos.add(new OperationInfo(opName, parameterNames, outputParameterNames));
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Aggregations

ListPrologTerm (de.prob.prolog.term.ListPrologTerm)38 PrologTerm (de.prob.prolog.term.PrologTerm)33 CompoundPrologTerm (de.prob.prolog.term.CompoundPrologTerm)27 Test (org.junit.Test)11 IntegerPrologTerm (de.prob.prolog.term.IntegerPrologTerm)6 VariablePrologTerm (de.prob.prolog.term.VariablePrologTerm)4 ArrayList (java.util.ArrayList)4 Transition (de.prob.statespace.Transition)3 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)2 ComputationNotCompletedResult (de.prob.animator.domainobjects.ComputationNotCompletedResult)2 EvalResult (de.prob.animator.domainobjects.EvalResult)2 IEvalElement (de.prob.animator.domainobjects.IEvalElement)2 StateError (de.prob.animator.domainobjects.StateError)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DotEdge (de.prob.animator.domainobjects.DotEdge)1 DotNode (de.prob.animator.domainobjects.DotNode)1 ProBPreference (de.prob.animator.domainobjects.ProBPreference)1 ASTCategory (de.prob.animator.prologast.ASTCategory)1 ASTFormula (de.prob.animator.prologast.ASTFormula)1