
mentalState.Result Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of msInterface Show documentation
Show all versions of msInterface Show documentation
Defines an interface and generic functionality of a mental state, independent from a specific KR language that is used to represent the content of a mental state.
package mentalState;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import krTools.language.DatabaseFormula;
import mentalState.error.MSTTranslationException;
import mentalState.translator.Translator;
/**
* Result contains the changes on a database after it was modified: which
* formulas were added and deleted, what is the new focus.
*
*/
abstract public class Result {
private final BASETYPE base;
private String focus;
private final List added;
private final List removed;
protected Result(BASETYPE base, String focus) {
this.base = base;
this.focus = focus;
this.added = new LinkedList<>();
this.removed = new LinkedList<>();
}
public void added(List dbfs) {
this.added.addAll(dbfs);
}
public void added(DatabaseFormula dbf) {
this.added.add(dbf);
}
public void removed(List dbfs) {
this.removed.addAll(dbfs);
}
public void removed(DatabaseFormula dbf) {
this.removed.add(dbf);
}
public BASETYPE getBaseType() {
return this.base;
}
public String getFocus() {
return this.focus;
}
public List getAdded() {
return this.added;
}
public List getRemoved() {
return this.removed;
}
public void merge(Result other) {
if (getBaseType() == other.getBaseType() && (getFocus() == null || getFocus().equals(other.getFocus()))) {
this.focus = other.getFocus();
added(other.getAdded());
removed(other.getRemoved());
} else {
throw new RuntimeException("incompatible: " + getBaseType() + "," + other.getBaseType() + " and "
+ getFocus() + "," + other.getFocus() + ".");
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (!this.added.isEmpty()) {
String added = (this.base == BASETYPE.GOALBASE) ? "adopted" : "inserted";
builder.append(added).append(" ").append(listToString(this.added)).append(" into ");
}
if (!this.removed.isEmpty()) {
if (builder.length() > 0) {
builder.append("and ");
}
String removed = (this.base == BASETYPE.GOALBASE) ? "dropped" : "deleted";
builder.append(removed).append(" ").append(listToString(this.removed)).append(" from ");
}
if (builder.length() < 1) {
builder.append("empty update on ");
}
String focus = this.focus.startsWith("(") ? this.focus.substring(1, this.focus.length() - 1) : this.focus;
builder.append(this.base).append(" ").append(focus).append(".");
return builder.toString();
}
protected abstract Translator getTranslator();
protected char[] listToString(List formulas) {
List> list;
switch (this.base) {
case PERCEPTBASE:
Translator translator1 = getTranslator();
if (translator1 == null) {
list = formulas;
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy