Skip to content

Commit

Permalink
Migrated belief queries to SARL events
Browse files Browse the repository at this point in the history
Mochalog Query object now wrapped in SARL BeliefQuery
event, to facilitate passing around of queries between
agents in a given space.
  • Loading branch information
mattmcnally committed Aug 28, 2017
1 parent 883385f commit d4a4b1a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
25 changes: 13 additions & 12 deletions src/main/java/io/mochalog/sarl/beliefs/SelfBeliefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.mochalog.sarl.beliefs;

import io.mochalog.sarl.beliefs.query.BeliefQuery;

import io.mochalog.bridge.prolog.PrologContext;
import io.mochalog.bridge.prolog.SandboxedPrologContext;
import io.mochalog.bridge.prolog.query.Query;
import io.mochalog.bridge.prolog.query.QuerySolution;
import io.mochalog.bridge.prolog.query.QuerySolutionList;

Expand Down Expand Up @@ -51,14 +52,14 @@ public class SelfBeliefs extends Skill implements Beliefs
private class EventBoundBeliefQuery<T extends Event> extends Behavior
{
// Query to perform
private Query query;
private BeliefQuery query;

/**
* Constructor.
* @param agent Owner agent
* @param query Query to perform
*/
public EventBoundBeliefQuery(Agent agent, Query query)
public EventBoundBeliefQuery(Agent agent, BeliefQuery query)
{
super(agent);
setQuery(query);
Expand All @@ -80,7 +81,7 @@ public void onEventGuard(final T event, Collection<Runnable> handlers)
* Set the Prolog query to perform on event
* @param query Query to perform
*/
public void setQuery(Query query)
public void setQuery(BeliefQuery query)
{
this.query = query;
}
Expand Down Expand Up @@ -162,9 +163,9 @@ public boolean believes(String query, Object... args)
}

@Override
public boolean believes(Query query)
public boolean believes(BeliefQuery query)
{
return knowledgeBase.prove(query);
return knowledgeBase.prove(query.unboxedQuery);
}

@Override
Expand All @@ -174,9 +175,9 @@ public QuerySolution ask(String query, Object... args)
}

@Override
public QuerySolution ask(Query query)
public QuerySolution ask(BeliefQuery query)
{
return knowledgeBase.askForSolution(query);
return knowledgeBase.askForSolution(query.unboxedQuery);
}

@Override
Expand All @@ -186,19 +187,19 @@ public QuerySolutionList askAll(String query, Object... args)
}

@Override
public QuerySolutionList askAll(Query query)
public QuerySolutionList askAll(BeliefQuery query)
{
return knowledgeBase.askForAllSolutions(query);
return knowledgeBase.askForAllSolutions(query.unboxedQuery);
}

@Override
public <T extends Event> void askOn(String query, Object... args)
{
askOn(Query.format(query, args));
askOn(new BeliefQuery(query, args));
}

@Override
public <T extends Event> void askOn(Query query)
public <T extends Event> void askOn(BeliefQuery query)
{
// Bind the given event type to a belief query behavior
Behavior eventBoundBeliefQuery = new EventBoundBeliefQuery<T>(getOwner(), query);
Expand Down
11 changes: 6 additions & 5 deletions src/main/sarl/io/mochalog/sarl/beliefs/Beliefs.sarl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package io.mochalog.sarl.beliefs

import io.mochalog.bridge.prolog.query.Query
import io.mochalog.sarl.beliefs.query.BeliefQuery

import io.mochalog.bridge.prolog.query.QuerySolution
import io.mochalog.bridge.prolog.query.QuerySolutionList
import io.mochalog.bridge.prolog.query.exception.NoSuchSolutionException
Expand Down Expand Up @@ -80,7 +81,7 @@ capacity Beliefs
* @param query Query to ask
* @return True if query satisfied, false otherwise.
*/
def believes(query : Query) : boolean
def believes(query : BeliefQuery) : boolean

/**
* Ask for first belief which satisfies query
Expand All @@ -98,7 +99,7 @@ capacity Beliefs
* @return Agent belief
* @throws NoSuchSolutionException Query unsatisfied by agent knowledge base
*/
def ask(query : Query) : QuerySolution
def ask(query : BeliefQuery) : QuerySolution
throws NoSuchSolutionException

/**
Expand All @@ -114,7 +115,7 @@ capacity Beliefs
* @param query Query to ask
* @return Agent belief list
*/
def askAll(query : Query) : QuerySolutionList
def askAll(query : BeliefQuery) : QuerySolutionList

/**
* Compel agent to query its beliefs in response
Expand All @@ -131,5 +132,5 @@ capacity Beliefs
* @param query Query to ask
* @param <T> Event type to respond to
*/
def askOn(query : Query) with T extends Event
def askOn(query : BeliefQuery) with T extends Event
}
52 changes: 52 additions & 0 deletions src/main/sarl/io/mochalog/sarl/beliefs/query/BeliefQuery.sarl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2017 The Mochalog-SARL-Beliefs Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.mochalog.sarl.beliefs.query

import io.mochalog.bridge.prolog.query.Query

/**
* Event-compatible wrapper of the Mochalog Query
* object, allowing queries to be conveniently passed
* amongst agents
*/
public event BeliefQuery
{
// Internal Prolog query to be asked of a given
// knowledge base
public val unboxedQuery : Query

/**
* Constructor.
* @param text Unformatted query text
* @param args Arguments to format query with
*/
public new(text : String, args : Object*)
{
this(
Query.format(text, args)
)
}

/**
* Constructor.
* @param query Query to ask
*/
public new(query : Query)
{
unboxedQuery = query
}
}

0 comments on commit d4a4b1a

Please sign in to comment.