@Override public MentalStateCondition getPrecondition() { return this.actionSpec.getPrecondition(); }
public ActionPreCondition getFullPreCondition() { return this.actionSpec.getFullPreCondition(); } }
@SuppressWarnings("unchecked") public List<DatabaseFormula> getKnowledge() { return ((List<DatabaseFormula>) getItems(UseCase.KNOWLEDGE)); }
@Override public UserSpecAction applySubst(Substitution substitution) { // Apply substitution to action parameters, pre- and post-condition. List<Term> parameters = new ArrayList<>(getParameters().size()); for (Term parameter : getParameters()) { parameters.add(parameter.applySubst(substitution)); } ActionPreCondition precondition = (getFullPreCondition() == null) ? null : getFullPreCondition().applySubst(substitution); ActionPostCondition positivePostcondition = (getPositivePostcondition() == null) ? null : getPositivePostcondition().applySubst(substitution); ActionPostCondition negativePostcondition = (getNegativePostcondition() == null) ? null : getNegativePostcondition().applySubst(substitution); return new UserSpecAction(getName(), parameters, isExternal(), precondition, positivePostcondition, negativePostcondition, getSourceInfo()); }
/** * Extracts relevant info for validation. */ private void processInfo() { // Extract relevant info from referenced files. this.knowledge.addAll(this.program.getKnowledge()); this.beliefs.addAll(this.program.getBeliefs()); // Extract relevant info from action specifications. for (UserSpecAction action : this.program.getActionSpecifications()) { for (MentalLiteral literal : getBeliefLiterals(action.getPrecondition())) { this.beliefQueries.add(literal.getFormula()); } if (action.getPositivePostcondition() != null) { this.beliefQueries.add(action.getPositivePostcondition().getPostCondition().toQuery()); } if (action.getNegativePostcondition() != null) { this.beliefQueries.add(action.getNegativePostcondition().getPostCondition().toQuery()); } } } }
protected void reportUnusedVariables() { for (UserSpecAction spec : this.program.getActionSpecifications()) { List<Var> vars = new LinkedList<>(); try { CognitiveKR ckr = getFirstPass().getCognitiveKR(); if (spec.getPrecondition() != null) { for (MentalLiteral literal : spec.getPrecondition().getAllLiterals()) { vars.addAll(ckr.getAllVariables(literal.getFormula())); } } if (spec.getNegativePostcondition() != null) { vars.addAll(ckr.getAllVariables(spec.getNegativePostcondition().getPostCondition())); } if (spec.getPositivePostcondition() != null) { vars.addAll(ckr.getAllVariables(spec.getPositivePostcondition().getPostCondition())); } Set<Var> unique = new LinkedHashSet<>(vars); unique.removeAll(spec.getParameters()); for (Var var : unique) { int occurences = Collections.frequency(vars, var); if (occurences < 2) { getFirstPass().reportWarning(ActionSpecWarning.VARIABLE_UNUSED, var.getSourceInfo(), var.toString()); } } } catch (ParserException e) { getFirstPass().reportParsingException(e); } } }
/** * Add an action specification to this action specification program. * * @param actionSpec * An action specification. */ public void addActionSpecification(UserSpecAction action) { register(action); if (action.getFullPreCondition() != null) { register(action.getPrecondition()); } if (action.getPositivePostcondition() != null) { register(action.getPositivePostcondition()); } if (action.getNegativePostcondition() != null) { register(action.getNegativePostcondition()); } this.actions.add(action); }
public ActionPreCondition applySubst(Substitution subst) { return new ActionPreCondition((getPreCondition() == null) ? null : getPreCondition().applySubst(subst), getSignatures(), getSourceInfo()); }
public ActionPostCondition applySubst(Substitution subst) { return new ActionPostCondition((getPostCondition() == null) ? null : getPostCondition().applySubst(subst), isNegative(), getSourceInfo()); }
@Override public Action convert(UserSpecAction action) throws MSTTranslationException { LinkedList<Parameter> parameters = new LinkedList<>(); for (Term term : action.getParameters()) { parameters.add(convert(term)); } return new Action(action.getName(), parameters); }
/** * @return A {@link MentalStateCondition} of the form "bel(precondition)" that * represents the precondition of this action. */ @Override public MentalStateCondition getPrecondition() { // Create mental state condition of the form "self.bel(precondition)". if (this.precondition == null) { return new MentalStateCondition(null, null); } else { List<MentalFormula> formulalist = new ArrayList<>(1); formulalist.add(new BelLiteral(true, new Selector(SelectorType.SELF, this.precondition.getSourceInfo()), this.precondition.getPreCondition(), this.precondition.getSignatures(), this.precondition.getSourceInfo())); return new MentalStateCondition(formulalist, this.precondition.getSourceInfo()); } }
/** * Creates a new {@link UserSpecCallAction}, which will focus the agent's * attention to a certain module when executed. * * @param spec * the user spec action that should be called. * @param parameters * the parameters, exactly as used at the caller's side. These * still have to be substituted at runtime against the actual * variable values at that time and then renamed into the space * of the {@link #actionSpec}. */ public UserSpecCallAction(UserSpecAction spec, List<Term> parameters, SourceInfo info) { super(spec.getName(), info); for (Term term : parameters) { addParameter(term); } this.actionSpec = spec; }
@Override public Object visitSpecifications(SpecificationsContext ctx) { // Process use clauses. for (UseclauseContext useclausectx : ctx.useclause()) { visitUseclause(useclausectx); } // VALIDATE: Check which (single) KR language is used. // Based on KR file references only. // TODO: duplicates if (getProgram().resolveKRInterface()) { // Process action specifications. for (ActionspecContext actionspecctx : ctx.actionspec()) { visitActionspec(actionspecctx); } } else { if (getProgram().getReferencedKRFiles().isEmpty()) { reportError(ActionSpecError.KR_COULDNOT_RESOLVE_NO_KR_USECLAUSES, ctx); } else { reportError(ActionSpecError.KR_COULDNOT_RESOLVE_DIFFERENT_KRS_USED, ctx); } } return null; }
@Override protected ActionSpecProgram getNewProgram(File file) throws IOException { return new ActionSpecProgram(this.registry, new InputStreamPosition(0, 0, 0, 0, file.getCanonicalPath())); }
/** * Applies the substitution to the parameters of this action and to the * target module associated with the action. Assumes a target module has * been associated with this action. * * @return Instantiated module call action, where free variables in * parameters have been substituted with terms from the * substitution. */ @Override public Action<Term> applySubst(Substitution substitution) { // Apply substitution to parameters. List<Term> parameters = new ArrayList<>(getParameters().size()); for (Term term : getParameters()) { parameters.add(term.applySubst(substitution)); } // Create new focus action with instantiated parameters. return new UserSpecCallAction((getSpecification() == null) ? null : getSpecification().applySubst(substitution), parameters, getSourceInfo()); }
/** * Performs the validation and resolution of references by a walk over the * program structure. */ @Override public void validate() { preProcess(); if (!checkKRIuse() || this.program.getRegistry().hasSyntaxError()) { return; } processInfo(); validateKR(); reportUnusedVariables(); }
@Override public Void visitUseclause(UseclauseContext ctx) { for (RefContext ref : ctx.ref()) { // Get reference. String reference = visitRef(ref); // Create use clause and resolve reference. UseClause useClause = new UseClause(reference, UseCase.KNOWLEDGE, getPathRelativeToSourceFile(""), getSourceInfo(ref)); List<URI> files = useClause.resolveReference(); if (files.isEmpty()) { reportError(ActionSpecError.REFERENCE_COULDNOT_FIND, ref, reference); } else if (files.size() > 1) { reportError(ActionSpecError.REFERENCE_COULDNOT_RESOLVE, ref, reference); } // Add use clause to module. if (!getProgram().addUseClause(useClause)) { reportError(ActionSpecError.REFERENCE_DUPLICATE, ref, reference); } } return null; // Java says must return something even when Void }
@Override public ActionPostCondition visitPostdel(PostdelContext ctx) { String krFragment = (ctx.KR_BLOCK() == null) ? "" : removeLeadTrailCharacters(ctx.KR_BLOCK().getText()); Update post = null; try { CognitiveKR ckr = getCognitiveKR(); SourceInfo info = getSourceInfo(ctx.KR_BLOCK()); info = (info == null) ? null : new InputStreamPosition(info.getLineNumber(), info.getCharacterPosition() + 1, info.getStartIndex() + 1, info.getStopIndex() - 1, info.getSource()); post = ckr.visitBeliefDelete(krFragment, info); reportEmbeddedLanguageErrors(ckr); } catch (ParserException e) { reportParsingException(e); } return (post == null) ? null : new ActionPostCondition(post, true, getSourceInfo(ctx)); }
@SuppressWarnings("unchecked") public List<DatabaseFormula> getBeliefs() { return (List<DatabaseFormula>) getItems(UseCase.BELIEFS); }