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());
}
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);
}
}
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());
}
}
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)));
}
}
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));
}
}
Aggregations