@Override protected ActionSpecProgram getNewProgram(File file) throws IOException { return new ActionSpecProgram(this.registry, new InputStreamPosition(0, 0, 0, 0, file.getCanonicalPath())); }
@SuppressWarnings("unchecked") public List<DatabaseFormula> getBeliefs() { return (List<DatabaseFormula>) getItems(UseCase.BELIEFS); }
/** * 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); }
/** * 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()); } } } }
@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; }
break; case ACTIONSPEC: asItems.addAll(((ActionSpecProgram) program).getActionSpecifications()); break; case MODULE:
/** * 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 }
getProgram().addActionSpecification(action);
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); } } }
ActionSpecProgram program = spec.getProgram(); this.actionSpecDefinitions.add(program); for (UserSpecAction action : program.getActionSpecifications()) { this.actionDefinitions.add(action);
@SuppressWarnings("unchecked") public List<DatabaseFormula> getKnowledge() { return ((List<DatabaseFormula>) getItems(UseCase.KNOWLEDGE)); }
case ACTIONSPEC: ActionSpecProgram actionspecs = (ActionSpecProgram) this.program.getRegistry().getProgram(file); for (UserSpecAction actionspec : actionspecs.getActionSpecifications()) { String signature = actionspec.getSignature(); Symbol symbol = new ActionSymbol(signature, actionspec, actionspec.getSourceInfo());