Skip to content

Commit

Permalink
Refactor: Removed the QueryEngine
Browse files Browse the repository at this point in the history
Functionality moved into MentalState
  • Loading branch information
andreasschmidtjensen committed Feb 17, 2015
1 parent bac0092 commit 0bbb309
Show file tree
Hide file tree
Showing 42 changed files with 375 additions and 457 deletions.
13 changes: 6 additions & 7 deletions framework/src/java/aorta/AgentState.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import alice.tuprolog.Var;
import aorta.kr.KBType;
import aorta.kr.MentalState;
import aorta.kr.QueryEngine;
import aorta.kr.language.model.Metamodel;
import aorta.kr.util.FormulaQualifier;
import aorta.msg.IncomingOrganizationalMessage;
Expand Down Expand Up @@ -69,7 +68,7 @@ public AortaBridge getBridge() {
}

@Override
public void insertTerm(QueryEngine engine, Struct term, KBType type) {
public void insertTerm(Struct term, KBType type) {
boolean insert = true;
if (bridge != null) {
switch (type) {
Expand All @@ -83,11 +82,11 @@ public void insertTerm(QueryEngine engine, Struct term, KBType type) {
}

if (insert) {
super.insertTerm(engine, term, type);
super.insertTerm(term, type);
}
}

public void insertMessage(QueryEngine engine, IncomingOrganizationalMessage msg) {
public void insertMessage(IncomingOrganizationalMessage msg) {
Struct om = (Struct) msg.getMessage();
Struct contents = (Struct) om.getArg(0);
boolean insert = true;
Expand All @@ -111,14 +110,14 @@ public void insertMessage(QueryEngine engine, IncomingOrganizationalMessage msg)
}

if (insert) {
engine.insert(getMentalState(), contents);
getMentalState().insert(contents);
logger.fine("insertMessage(" + msg + ")");
setChanged(true);
}
}

@Override
public void removeTerm(QueryEngine engine, Struct term, KBType type) {
public void removeTerm(Struct term, KBType type) {
boolean remove = true;
if (bridge != null) {
switch (type) {
Expand All @@ -132,7 +131,7 @@ public void removeTerm(QueryEngine engine, Struct term, KBType type) {
}

if (remove) {
super.removeTerm(engine, term, type);
super.removeTerm(term, type);
}
}

Expand Down
8 changes: 3 additions & 5 deletions framework/src/java/aorta/AortaAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

import aorta.kr.MentalState;
import aorta.kr.QueryEngine;
import aorta.kr.language.model.Metamodel;
import aorta.kr.util.FormulaQualifier;
import aorta.msg.OutgoingOrganizationalMessage;
Expand Down Expand Up @@ -78,12 +77,12 @@ public AgentState getState() {

public void addAgentToBeliefs(String agentName) {
Struct qualified = FormulaQualifier.qualifyStruct(new Struct("agent", new Struct(agentName)), KBType.BELIEF);
state.insertTerm(new QueryEngine(), qualified);
state.insertTerm(qualified);
}

public void removeAgentFromBeliefs(String agentName) {
Struct qualified = FormulaQualifier.qualifyStruct(new Struct("agent", new Struct(agentName)), KBType.BELIEF.getType());
state.removeTerm(new QueryEngine(), qualified);
state.removeTerm(qualified);
}

public Strategy getStrategy() {
Expand All @@ -110,8 +109,7 @@ public List<Struct>[] handlePercept(Percept p) {
if (p.obsPropChanges()) {
ArtifactObsProperty prop = (ArtifactObsProperty) p.getPropChanged()[0];
List<Struct> orgStructs = (List<Struct>) prop.getValue();
QueryEngine engine = new QueryEngine();
return engine.mergeKBs(state.getMentalState(), KBType.ORGANIZATION, orgStructs);
return state.getMentalState().mergeKBs(KBType.ORGANIZATION, orgStructs);
}
} catch (NullPointerException ex) {
// event in percept is null if no percept... no way to check it
Expand Down
5 changes: 2 additions & 3 deletions framework/src/java/aorta/reasoning/MessageFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package aorta.reasoning;

import aorta.AgentState;
import aorta.kr.QueryEngine;
import aorta.msg.IncomingOrganizationalMessage;
import aorta.ts.rules.Check;

Expand All @@ -18,9 +17,9 @@ public class MessageFunction {
public MessageFunction() {
}

public AgentState process(QueryEngine engine, IncomingOrganizationalMessage message, AgentState state) {
public AgentState process(IncomingOrganizationalMessage message, AgentState state) {
AgentState newState = state;
newState.insertMessage(engine, message);
newState.insertMessage(message);
state.notifyTermAdded(new Check().getName(), message.getMessage());
return newState;
}
Expand Down
7 changes: 3 additions & 4 deletions framework/src/java/aorta/reasoning/action/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import alice.tuprolog.Term;
import aorta.AORTAException;
import aorta.AgentState;
import aorta.kr.QueryEngine;
import aorta.logging.Logger;

public abstract class Action {
Expand All @@ -13,14 +12,14 @@ public abstract class Action {
public Action() {
}

public final AgentState execute(QueryEngine engine, Term option, AgentState state) throws AORTAException {
final AgentState result = executeAction(engine, option, state);
public final AgentState execute(Term option, AgentState state) throws AORTAException {
final AgentState result = executeAction(option, state);
if (result != null) {
logger.finest("[" + state.getAgent().getName() + "] Executing action: " + this);
}
return result;
}

protected abstract AgentState executeAction(QueryEngine engine, Term option, AgentState state) throws AORTAException;
protected abstract AgentState executeAction(Term option, AgentState state) throws AORTAException;

}
12 changes: 5 additions & 7 deletions framework/src/java/aorta/reasoning/action/CommitAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import aorta.AgentState;
import aorta.kr.KBType;
import aorta.kr.MentalState;
import aorta.kr.QueryEngine;
import aorta.kr.util.FormulaQualifier;
import aorta.tracer.Tracer;
import aorta.ts.TransitionNotPossibleException;
import java.util.logging.Level;
import aorta.logging.Logger;
import aorta.ts.rules.ActionExecution;
Expand All @@ -30,28 +28,28 @@ public Term getObjective() {
}

@Override
protected AgentState executeAction(QueryEngine engine, Term option, AgentState state) throws AORTAException {
protected AgentState executeAction(Term option, AgentState state) throws AORTAException {
AgentState newState = state;

MentalState ms = state.getMentalState();

final Term clonedObjTerm = Term.createTerm(objective.toString());

String bef = clonedObjTerm.toString();
engine.unify(ms, clonedObjTerm, state.getBindings());
ms.unify(clonedObjTerm, state.getBindings());

Term belTerm = FormulaQualifier.qualifyTerm(clonedObjTerm, KBType.BELIEF);
Term goalTerm = FormulaQualifier.qualifyTerm(clonedObjTerm, KBType.GOAL);

Term test = Term.createTerm("\\+ " + belTerm + ", \\+ " + goalTerm);

SolveInfo result = engine.solve(ms, test);
SolveInfo result = ms.solve(test);

logger.log(Level.FINEST, "Attempting to commit: " + result.isSuccess());
if (result.isSuccess()) {
state.addBindings(result);

engine.unify(ms, clonedObjTerm, state.getBindings());
ms.unify(clonedObjTerm, state.getBindings());

Struct asStruct;
if (clonedObjTerm instanceof Var) {
Expand All @@ -61,7 +59,7 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
}

ActionExecution tr = new ActionExecution();
tr.add(newState, engine, asStruct, KBType.GOAL);
tr.add(newState, asStruct, KBType.GOAL);

logger.fine("[" + state.getAgent().getName() + "/" + state.getAgent().getCycle() + "] Executing action: commit(" + asStruct + ")");
Tracer.queue(state.getAgent().getName(), "commit(" + asStruct + ")");
Expand Down
10 changes: 4 additions & 6 deletions framework/src/java/aorta/reasoning/action/DeactAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import aorta.kr.KBType;
import aorta.kr.MentalState;
import aorta.kr.util.FormulaQualifier;
import aorta.kr.QueryEngine;
import aorta.kr.language.MetaLanguage;
import aorta.tracer.Tracer;
import aorta.ts.TransitionNotPossibleException;
import aorta.logging.Logger;
import aorta.ts.rules.ActionExecution;
import cartago.CartagoException;
Expand All @@ -31,7 +29,7 @@ public Term getRole() {
}

@Override
protected AgentState executeAction(QueryEngine engine, Term option, AgentState state)
protected AgentState executeAction(Term option, AgentState state)
throws AORTAException {
AgentState newState = state;

Expand All @@ -45,15 +43,15 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
Term term = FormulaQualifier.qualifyTerm(reaDef, KBType.ORGANIZATION.getType());
MentalState ms = state.getMentalState();

SolveInfo result = engine.solve(ms, term);
SolveInfo result = ms.solve(term);

logger.finest("Attempting to deact: " + result.isSuccess());

if (result.isSuccess()) {
state.addBindings(result);

Term qualified = FormulaQualifier.qualifyTerm(reaDef, KBType.ORGANIZATION.getType());
engine.unify(ms, qualified, state.getBindings());
ms.unify(qualified, state.getBindings());

if (!qualified.isGround()) {
throw new AORTAException("Cannot execute action: term '" + qualified + "' is not ground.");
Expand All @@ -74,7 +72,7 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
}
} else {
ActionExecution tr = new ActionExecution();
tr.remove(newState, engine, (Struct) qualified);
tr.remove(newState, (Struct) qualified);

logger.fine("[" + state.getAgent().getName() + "] Executing action: deact(" + qualified + ")");
Tracer.queue(state.getAgent().getName(), "deact(" + qualified + ")");
Expand Down
10 changes: 4 additions & 6 deletions framework/src/java/aorta/reasoning/action/DropAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import aorta.AgentState;
import aorta.kr.KBType;
import aorta.kr.MentalState;
import aorta.kr.QueryEngine;
import aorta.kr.util.FormulaQualifier;
import aorta.tracer.Tracer;
import aorta.ts.TransitionNotPossibleException;
import aorta.logging.Logger;
import aorta.ts.rules.ActionExecution;

Expand All @@ -30,21 +28,21 @@ public Term getObjective() {
}

@Override
protected AgentState executeAction(QueryEngine engine, Term option, AgentState state) throws AORTAException {
protected AgentState executeAction(Term option, AgentState state) throws AORTAException {
AgentState newState = state;

final Term clonedObjTerm = Term.createTerm(objective.toString());

// Check that it is already a goal
Term goalTerm = FormulaQualifier.qualifyTerm(clonedObjTerm, KBType.GOAL.getType(), true);
MentalState ms = state.getMentalState();
SolveInfo result = engine.solve(ms, goalTerm);
SolveInfo result = ms.solve(goalTerm);

logger.finest("Attempting to drop: " + result.isSuccess());
if (result.isSuccess()) {
state.addBindings(result);

engine.unify(ms, clonedObjTerm, state.getBindings());
ms.unify(clonedObjTerm, state.getBindings());

if (!clonedObjTerm.isGround()) {
throw new AORTAException("Cannot execute action: term '" + clonedObjTerm + "' is not ground.");
Expand All @@ -57,7 +55,7 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
}

ActionExecution tr = new ActionExecution();
tr.remove(newState, engine, asStruct, KBType.GOAL);
tr.remove(newState, asStruct, KBType.GOAL);
logger.fine("[" + state.getAgent().getName() + "] Executing action: drop(" + asStruct + ")");
Tracer.queue(state.getAgent().getName(), "drop(" + asStruct + ")");
}
Expand Down
18 changes: 8 additions & 10 deletions framework/src/java/aorta/reasoning/action/EnactAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import aorta.AgentState;
import aorta.kr.KBType;
import aorta.kr.MentalState;
import aorta.kr.QueryEngine;
import aorta.kr.language.MetaLanguage;
import aorta.kr.util.FormulaQualifier;
import aorta.tracer.Tracer;
import aorta.ts.TransitionNotPossibleException;
import aorta.logging.Logger;
import aorta.ts.rules.ActionExecution;
import cartago.CartagoException;
Expand All @@ -32,7 +30,7 @@ public Term getRole() {
}

@Override
protected AgentState executeAction(QueryEngine engine, Term option, AgentState state) throws AORTAException {
protected AgentState executeAction(Term option, AgentState state) throws AORTAException {
AgentState newState = state;

MentalState ms = state.getMentalState();
Expand All @@ -48,16 +46,16 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
Term t2 = FormulaQualifier.qualifyTerm(reaDef, KBType.ORGANIZATION.getType());

Term test = Term.createTerm(t1 + ", \\+ " + t2);
engine.unify(ms, test, state.getBindings());
ms.unify(test, state.getBindings());

SolveInfo result = engine.solve(ms, test);
SolveInfo result = ms.solve(test);

logger.finest("Attempting to enact: " + result.isSuccess());
if (result.isSuccess()) {
state.addBindings(result);

Term qualified = FormulaQualifier.qualifyTerm(reaDef, KBType.ORGANIZATION.getType());
engine.unify(ms, qualified, state.getBindings());
ms.unify(qualified, state.getBindings());

if (!qualified.isGround()) {
throw new AORTAException("Cannot execute action: term '" + qualified + "' is not ground.");
Expand All @@ -72,7 +70,7 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
}
state.getAgent().getArtifactAgent().enact(roleName);
ActionExecution tr = new ActionExecution();
tr.remove(newState, engine, FormulaQualifier.qualifyStruct((Struct) option, KBType.OPTION));
tr.remove(newState, FormulaQualifier.qualifyStruct((Struct) option, KBType.OPTION));

Tracer.queue(state.getAgent().getName(), "ARTIFACT.enact(" + qualified + ")");
} catch (CartagoException ex) {
Expand All @@ -81,11 +79,11 @@ protected AgentState executeAction(QueryEngine engine, Term option, AgentState s
}

ActionExecution tr = new ActionExecution();
tr.add(newState, engine, (Struct) qualified);
tr.remove(newState, engine, FormulaQualifier.qualifyStruct((Struct) option, KBType.OPTION));
tr.add(newState, (Struct) qualified);
tr.remove(newState, FormulaQualifier.qualifyStruct((Struct) option, KBType.OPTION));

Struct send = ml.send(Term.TRUE, new Struct("tell"), qualified);
tr.add(newState, engine, FormulaQualifier.qualifyStruct(send, KBType.OPTION));
tr.add(newState, FormulaQualifier.qualifyStruct(send, KBType.OPTION));

logger.fine("[" + state.getAgent().getName() + "] Executing action: enact(" + qualified + ")");
Tracer.queue(state.getAgent().getName(), "enact(" + qualified + ")");
Expand Down
Loading

0 comments on commit 0bbb309

Please sign in to comment.