package com.naveensundarg.planner.inducers; import com.naveensundarg.planner.*; import com.naveensundarg.planner.utils.Commons; import com.naveensundarg.planner.utils.PlanningProblem; import com.naveensundarg.shadow.prover.representations.formula.Formula; import com.naveensundarg.shadow.prover.representations.value.Compound; import com.naveensundarg.shadow.prover.representations.value.Value; import com.naveensundarg.shadow.prover.representations.value.Variable; import com.naveensundarg.shadow.prover.utils.Sets; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * Created by naveensundarg on 12/19/17. */ public class SimpleInducer implements Inducer { @Override public PlanMethod induce(PlanningProblem planningProblem, State start, Goal goal, Plan plan) { List actionList = plan.getActions(); List actionCompounds = actionList.stream().map(Action::getShorthand).collect(Collectors.toList()); Set backgroundFormula = planningProblem.getBackground().stream().collect(Collectors.toSet()); Set initFormula = start.getFormulae().stream().collect(Collectors.toSet()); Set goalFormula = goal.getGoalState().getFormulae().stream().collect(Collectors.toSet()); Set values = actionCompounds.stream(). map(Compound::getArguments).map(Arrays::stream).map(x->x.collect(Collectors.toSet())) .reduce(Sets.newSet(), Sets::union) .stream().filter(Value::isConstant).collect(Collectors.toSet()); Map valueVariableMap = Commons.makeVariables(values); Function,Set> getRelevantFormula = formulae -> { return formulae.stream(). filter(formula -> Sets.difference(formula.valuesPresent().stream().filter(Value::isConstant).collect(Collectors.toSet()), values).isEmpty() && !Sets.intersection(values, formula.valuesPresent()).isEmpty()). collect(Collectors.toSet()); }; Set relevantBackgroundFormula = getRelevantFormula.apply(backgroundFormula); Set relevantInitFormula = getRelevantFormula.apply(initFormula); Set relevantGoalFormula = getRelevantFormula.apply(goalFormula); Commons.generalize(valueVariableMap, relevantBackgroundFormula); Commons.generalize(valueVariableMap, relevantInitFormula); Commons.generalize(valueVariableMap, relevantGoalFormula); System.out.println(relevantBackgroundFormula); System.out.println(relevantInitFormula); System.out.println(relevantGoalFormula); //PlanMethod(Set goalPreconditions, Set backGroundStatePreconditions, List freeVariables, List actionCompounds return new PlanMethod(Commons.generalize(valueVariableMap, relevantGoalFormula), Sets.union(Commons.generalize(valueVariableMap, relevantBackgroundFormula), Commons.generalize(valueVariableMap, relevantInitFormula)), new ArrayList<>(valueVariableMap.values()), actionCompounds.stream().map(x-> (Compound) x.generalize(valueVariableMap)). collect(Collectors.toList())); } }