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