use of de.prob.prolog.term.ListPrologTerm in project probparsers by bendisposto.
the class BindingGenerator method createBinding.
private static Map<String, PrologTerm> createBinding(final ListPrologTerm list) {
Map<String, PrologTerm> result;
result = new HashMap<String, PrologTerm>();
for (int i = 0; i < list.size(); i++) {
PrologTerm element = list.get(i);
if (element.isTerm()) {
CompoundPrologTerm binding = (CompoundPrologTerm) element;
if (binding.getArity() == 2 && "=".equals(binding.getFunctor())) {
extractBinding(result, binding);
} else
throw new IllegalArgumentException("Expected binding (=/2), but was " + binding.getFunctor() + "/" + String.valueOf(binding.getArity()));
} else
throw new IllegalArgumentException("Expected binding but was not a term");
}
return Collections.unmodifiableMap(result);
}
use of de.prob.prolog.term.ListPrologTerm in project prob2 by bendisposto.
the class CSPAssertionsCommand method processResult.
@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
ListPrologTerm prologTermResults = BindingGenerator.getList(bindings.get(RESULT_VARIABLE));
ListPrologTerm prologTermResultTraces = BindingGenerator.getList(bindings.get(RESULT_TRACES_VARIABLE));
for (PrologTerm term : prologTermResults) {
if (term != null) {
results.add(term.getFunctor());
}
}
for (PrologTerm term : prologTermResultTraces) {
if (term instanceof ListPrologTerm) {
resultTraces.add(BindingGenerator.getList(term));
}
}
}
use of de.prob.prolog.term.ListPrologTerm 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.ListPrologTerm in project prob2 by bendisposto.
the class GetTransitionDiagramCommand method processResult.
@Override
public void processResult(final ISimplifiedROMap<String, PrologTerm> bindings) {
// Result term is a list with two arguments [States,Transitions].
ListPrologTerm list = BindingGenerator.getList(bindings.get(STATE_SPACE));
extractStates(BindingGenerator.getList(list.getArgument(1)));
extractTransitions(BindingGenerator.getList(list.getArgument(2)));
}
use of de.prob.prolog.term.ListPrologTerm 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());
}
}
Aggregations