Search in sources :

Example 26 with ListPrologTerm

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);
}
Also used : CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) CompoundPrologTerm(de.prob.prolog.term.CompoundPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm) VariablePrologTerm(de.prob.prolog.term.VariablePrologTerm) IntegerPrologTerm(de.prob.prolog.term.IntegerPrologTerm)

Example 27 with ListPrologTerm

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));
        }
    }
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm) PrologTerm(de.prob.prolog.term.PrologTerm) ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 28 with ListPrologTerm

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());
    }
}
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 29 with ListPrologTerm

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)));
}
Also used : ListPrologTerm(de.prob.prolog.term.ListPrologTerm)

Example 30 with ListPrologTerm

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

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