Skip to content

Commit

Permalink
Simplified evaluation function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcnally committed Sep 27, 2017
1 parent d231476 commit fa9188e
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package io.mochalog.sarl.beliefs.social;

import io.mochalog.sarl.beliefs.query.BeliefQuery;
import io.mochalog.sarl.beliefs.social.analysis.SocialExperimentBallot;
import io.mochalog.sarl.beliefs.social.analysis.SocialPoll;
import io.mochalog.sarl.beliefs.social.analysis.SocialPollBallot;
import io.mochalog.sarl.beliefs.social.analysis.SocialPollImpl;
import io.mochalog.sarl.beliefs.social.analysis.ExperimentEvaluator;
import io.mochalog.sarl.beliefs.social.analysis.poll.SocialPoll;
import io.mochalog.sarl.beliefs.social.analysis.poll.SocialPollBallot;
import io.mochalog.sarl.beliefs.social.analysis.poll.SocialPollImpl;
import io.mochalog.sarl.beliefs.util.EventSpaceUtils;

import io.sarl.lang.core.Address;
Expand All @@ -41,7 +41,6 @@
import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;

/**
* Skill allowing for basic belief-reasoning interactions with
Expand Down Expand Up @@ -216,8 +215,7 @@ private void allBelieveThatEvaluator(boolean isTrue, SocialPollBallot poll,

@Override
public SocialPoll conductPoll(EventSpace space, Scope<Address> scope, BeliefQuery query,
long timeout, Procedure2<? super SocialPollBallot, ? super BeliefDisclosure> evaluator,
Procedure1<? super Boolean> onResult)
long timeout, ExperimentEvaluator<SocialPollBallot> evaluator, Procedure1<? super Boolean> onResult)
{
SocialPollImpl.Executor executor = new SocialPollImpl.Executor();
return executor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static abstract class Executor<E extends Executor<E, S>, S extends Abstra
private Set<BeliefQuery> surveys;
// Scope of participants to ask surveys of
private Scope<Address> surveyScope;
// Evaluation function to apply to the experiment
private ExperimentEvaluator<? super S> evaluator;

// Number of scheduling threads to queue
private static final int NUM_EXPERIMENT_SCHEDULING_THREADS = 20;
Expand Down Expand Up @@ -163,6 +165,19 @@ public E setSurveyScope(Scope<Address> scope)
return self();
}

@Override
public ExperimentEvaluator<? super S> getEvaluator()
{
return evaluator;
}

@Override
public E setEvaluator(ExperimentEvaluator<? super S> evaluator)
{
this.evaluator = evaluator;
return self();
}

@Override
public long getExperimentTimeout()
{
Expand All @@ -186,35 +201,42 @@ public E endExperimentAfter(long timeout)
@Override
public S execute() throws ExecutionFailedException
{
S experiment = build();
// Attempt to start experiment in the given event space
if (experiment == null || !EventSpaceUtils.registerInEventSpace(experiment, space, principal))
{
throw new ExecutionFailedException("Social experiment could not be " +
"conducted in space (" + space.getSpaceID() + "). Access restricted.");
}

// Signal that experiment has started
((AbstractSocialExperiment) experiment).inProgress = true;
// Ask each survey query in experiment space
experiment.surveyParticipants(surveys, surveyScope);

// Schedule an experiment timeout (after time elapsed, kill
// the experiment and produce a negative result
if (experimentTimeout != DEFAULT_TIMEOUT)
// Valid experiments require an evaluation function
// and a space to be performed in
if (space != null && evaluator != null)
{
scheduler.schedule(() -> onExperimentTimeout(experiment), experimentTimeout,
TimeUnit.MILLISECONDS);
S experiment = build(space, evaluator);
// Attempt to start experiment in the given event space
if (experiment != null && EventSpaceUtils.registerInEventSpace(experiment, space, principal))
{
// Signal that experiment has started
((AbstractSocialExperiment) experiment).inProgress = true;
// Ask each survey query in experiment space
experiment.surveyParticipants(surveys, surveyScope);

// Schedule an experiment timeout (after time elapsed, kill
// the experiment and produce a negative result
if (experimentTimeout != DEFAULT_TIMEOUT)
{
scheduler.schedule(() -> onExperimentTimeout(experiment), experimentTimeout,
TimeUnit.MILLISECONDS);
}

return experiment;
}
}

return experiment;
throw new ExecutionFailedException("Social experiment could not be " +
"conducted in space (" + space.getSpaceID() + "). Access restricted.");
}

/**
* Build a new experiment to be executed by the Executor.
* @param space Space to conduct experiment in
* @param evaluator Evaluation function to use
* @return Social experiment instance
*/
protected abstract S build();
protected abstract S build(EventSpace space, ExperimentEvaluator<? super S> evaluator);

/**
* Get current executor instance for correct chaining
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

package io.mochalog.sarl.beliefs.social.analysis;

import io.mochalog.sarl.beliefs.social.BeliefDisclosure;

/**
* Interface for access to ballot of a poll being
* conducted in an event space. Allows for computation
* of decision result.
*
*/
public interface SocialPollBallot extends SocialPoll, SocialExperimentBallot
@FunctionalInterface
public interface ExperimentEvaluator<S extends SocialExperiment>
{
/**
* Compute the result of the poll and subsequently
* stop accepting submissions.
* @param result Result of the experiment
*/
public void finalisePollResult(boolean result);
public void evaluate(S experiment, BeliefDisclosure response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public interface SocialExperimentExecutor<E extends SocialExperimentExecutor<E,
*/
public Scope<Address> getSurveyScope();

/**
* Get evaluation function used to evaluate responses.
* @return Evaluation function
*/
public ExperimentEvaluator<? super S> getEvaluator();

/**
* Set the evaluation function the experiment will use to evaluate responses.
* @param evaluator Evaluation function
* @return Executor instance
*/
public E setEvaluator(ExperimentEvaluator<? super S> evaluator);

/**
* Provide a timeout after which the executed experiment
* will be finalised.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.social.analysis.contest;

import io.mochalog.sarl.beliefs.social.analysis.SocialExperiment;

/**
*
*/
public interface AgentContest extends SocialExperiment
{
}
Loading

0 comments on commit fa9188e

Please sign in to comment.