Skip to content

Commit

Permalink
FuzzyModule: Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Jan 6, 2019
1 parent 2a6621b commit 64feed1
Show file tree
Hide file tree
Showing 25 changed files with 4,121 additions and 292 deletions.
1,201 changes: 1,201 additions & 0 deletions build/yuka.js

Large diffs are not rendered by default.

347 changes: 180 additions & 167 deletions build/yuka.min.js

Large diffs are not rendered by default.

1,189 changes: 1,188 additions & 1 deletion build/yuka.module.js

Large diffs are not rendered by default.

84 changes: 0 additions & 84 deletions examples/fuzzy/index.html

This file was deleted.

11 changes: 4 additions & 7 deletions src/fuzzy/FuzzyModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class FuzzyModule {
const flvs = this.flvs;
const rules = this.rules;

this.initConsequences();
this._initConsequences();

for ( let i = 0, l = rules.length; i < l; i ++ ) {

Expand Down Expand Up @@ -151,15 +151,12 @@ class FuzzyModule {

}

/**
* Initializes the consequents of all rules.
*
* @return {FuzzyModule} A reference to this fuzzy module.
*/
initConsequences() {
_initConsequences() {

const rules = this.rules;

// initializes the consequents of all rules.

for ( let i = 0, l = rules.length; i < l; i ++ ) {

const rule = rules[ i ];
Expand Down
16 changes: 8 additions & 8 deletions src/fuzzy/operators/FuzzyFAIRLY.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class FuzzyFAIRLY extends FuzzyTerm {
/**
* Constructs a new fuzzy FAIRLY hedge with the given values.
*
* @param {FuzzySet} fuzzySet - The fuzzy set this hedge is working on.
* @param {FuzzyTerm} fuzzyTerm - The fuzzy term this hedge is working on.
*/
constructor( fuzzySet = null ) {
constructor( fuzzyTerm = null ) {

super();

/**
* The fuzzy set this hedge is working on.
* @type FuzzySet
* The fuzzy term this hedge is working on.
* @type FuzzyTerm
* @default null
*/
this.fuzzySet = fuzzySet;
this.fuzzyTerm = fuzzyTerm;

}

Expand All @@ -36,7 +36,7 @@ class FuzzyFAIRLY extends FuzzyTerm {
*/
clearDegreeOfMembership() {

this.fuzzySet.clearDegreeOfMembership();
this.fuzzyTerm.clearDegreeOfMembership();

return this;

Expand All @@ -49,7 +49,7 @@ class FuzzyFAIRLY extends FuzzyTerm {
*/
getDegreeOfMembership() {

const dom = this.fuzzySet.getDegreeOfMembership();
const dom = this.fuzzyTerm.getDegreeOfMembership();

return Math.sqrt( dom );

Expand All @@ -62,7 +62,7 @@ class FuzzyFAIRLY extends FuzzyTerm {
*/
updateDegreeOfMembership( value ) {

this.fuzzySet.updateDegreeOfMembership( Math.sqrt( value ) );
this.fuzzyTerm.updateDegreeOfMembership( Math.sqrt( value ) );

return this;

Expand Down
16 changes: 8 additions & 8 deletions src/fuzzy/operators/FuzzyVERY.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class FuzzyVERY extends FuzzyTerm {
/**
* Constructs a new fuzzy VERY hedge with the given values.
*
* @param {FuzzySet} fuzzySet - The fuzzy set this hedge is working on.
* @param {FuzzyTerm} fuzzyTerm - The fuzzy term this hedge is working on.
*/
constructor( fuzzySet = null ) {
constructor( fuzzyTerm = null ) {

super();

/**
* The fuzzy set this hedge is working on.
* @type FuzzySet
* The fuzzy term this hedge is working on.
* @type FuzzyTerm
* @default null
*/
this.fuzzySet = fuzzySet;
this.fuzzyTerm = fuzzyTerm;

}

Expand All @@ -36,7 +36,7 @@ class FuzzyVERY extends FuzzyTerm {
*/
clearDegreeOfMembership() {

this.fuzzySet.clearDegreeOfMembership();
this.fuzzyTerm.clearDegreeOfMembership();

return this;

Expand All @@ -49,7 +49,7 @@ class FuzzyVERY extends FuzzyTerm {
*/
getDegreeOfMembership() {

const dom = this.fuzzySet.getDegreeOfMembership();
const dom = this.fuzzyTerm.getDegreeOfMembership();

return dom * dom;

Expand All @@ -62,7 +62,7 @@ class FuzzyVERY extends FuzzyTerm {
*/
updateDegreeOfMembership( value ) {

this.fuzzySet.updateDegreeOfMembership( value * value );
this.fuzzyTerm.updateDegreeOfMembership( value * value );

return this;

Expand Down
2 changes: 1 addition & 1 deletion src/fuzzy/sets/LeftShoulderFuzzySet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LeftShoulderFuzzySet extends FuzzySet {
* @param {Number} midpoint - Represents the peak value of this fuzzy set.
* @param {Number} right - Represents the right border of this fuzzy set.
*/
constructor( left, midpoint, right ) {
constructor( left = 0, midpoint = 0, right = 0 ) {

// the representative value is the midpoint of the plateau of the shoulder

Expand Down
2 changes: 1 addition & 1 deletion src/fuzzy/sets/RightShoulderFuzzySet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RightShoulderFuzzySet extends FuzzySet {
* @param {Number} midpoint - Represents the peak value of this fuzzy set.
* @param {Number} right - Represents the right border of this fuzzy set.
*/
constructor( left, midpoint, right ) {
constructor( left = 0, midpoint = 0, right = 0 ) {

// the representative value is the midpoint of the plateau of the shoulder

Expand Down
2 changes: 1 addition & 1 deletion src/fuzzy/sets/SingletonFuzzySet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SingletonFuzzySet extends FuzzySet {
* @param {Number} midpoint - Represents the peak value of this fuzzy set.
* @param {Number} right - Represents the right border of this fuzzy set.
*/
constructor( left, midpoint, right ) {
constructor( left = 0, midpoint = 0, right = 0 ) {

super( midpoint );

Expand Down
28 changes: 14 additions & 14 deletions src/yuka.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ export { Time } from './core/Time.js';
export { Telegram } from './core/Telegram.js';
export { State } from './fsm/State.js';
export { StateMachine } from './fsm/StateMachine.js';
// export { FuzzyAND } from './fuzzy/operators/FuzzyAND.js';
// export { FuzzyFAIRLY } from './fuzzy/operators/FuzzyFAIRLY.js';
// export { FuzzyOR } from './fuzzy/operators/FuzzyOR.js';
// export { FuzzyVERY } from './fuzzy/operators/FuzzyVERY.js';
// export { LeftShoulderFuzzySet } from './fuzzy/sets/LeftShoulderFuzzySet.js';
// export { RightShoulderFuzzySet } from './fuzzy/sets/RightShoulderFuzzySet.js';
// export { SingletonFuzzySet } from './fuzzy/sets/SingletonFuzzySet.js';
// export { TriangularFuzzySet } from './fuzzy/sets/TriangularFuzzySet.js';
// export { FuzzyCompositeTerm } from './fuzzy/FuzzyCompositeTerm.js';
// export { FuzzyModule } from './fuzzy/FuzzyModule.js';
// export { FuzzyRule } from './fuzzy/FuzzyRule.js';
// export { FuzzySet } from './fuzzy/FuzzySet.js';
// export { FuzzyTerm } from './fuzzy/FuzzyTerm.js';
// export { FuzzyVariable } from './fuzzy/FuzzyVariable.js';
export { FuzzyAND } from './fuzzy/operators/FuzzyAND.js';
export { FuzzyFAIRLY } from './fuzzy/operators/FuzzyFAIRLY.js';
export { FuzzyOR } from './fuzzy/operators/FuzzyOR.js';
export { FuzzyVERY } from './fuzzy/operators/FuzzyVERY.js';
export { LeftShoulderFuzzySet } from './fuzzy/sets/LeftShoulderFuzzySet.js';
export { RightShoulderFuzzySet } from './fuzzy/sets/RightShoulderFuzzySet.js';
export { SingletonFuzzySet } from './fuzzy/sets/SingletonFuzzySet.js';
export { TriangularFuzzySet } from './fuzzy/sets/TriangularFuzzySet.js';
export { FuzzyCompositeTerm } from './fuzzy/FuzzyCompositeTerm.js';
export { FuzzyModule } from './fuzzy/FuzzyModule.js';
export { FuzzyRule } from './fuzzy/FuzzyRule.js';
export { FuzzySet } from './fuzzy/FuzzySet.js';
export { FuzzyTerm } from './fuzzy/FuzzyTerm.js';
export { FuzzyVariable } from './fuzzy/FuzzyVariable.js';
export { CompositeGoal } from './goal/CompositeGoal.js';
export { Goal } from './goal/Goal.js';
export { GoalEvaluator } from './goal/GoalEvaluator.js';
Expand Down
112 changes: 112 additions & 0 deletions test/unit_test/fuzzy/FuzzyCompositeTerm.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* @author Mugen87 / https://github.com/Mugen87
*/

const expect = require( 'chai' ).expect;
const YUKA = require( '../../../build/yuka.js' );

const FuzzyCompositeTerm = YUKA.FuzzyCompositeTerm;
const FuzzyTerm = YUKA.FuzzyTerm;

describe( 'FuzzyCompositeTerm', function () {

describe( '#constructor()', function () {

it( 'should create an object with correct default values', function () {

const compositeTerm = new FuzzyCompositeTerm();

expect( compositeTerm ).to.have.a.property( 'terms' ).that.is.an( 'array' );

} );

it( 'should apply the parameters to the new object', function () {

const atomicTerm1 = new FuzzyTerm();
const atomicTerm2 = new FuzzyTerm();

const compositeTerm = new FuzzyCompositeTerm( [ atomicTerm1, atomicTerm2 ] );

expect( compositeTerm.terms ).to.include( atomicTerm1, atomicTerm2 );

} );

it( 'should inherit from FuzzyTerm', function () {

const compositeTerm = new FuzzyCompositeTerm();

expect( compositeTerm ).is.an.instanceof( FuzzyTerm );

} );

} );

describe( '#clearDegreeOfMembership()', function () {

it( 'should execute .clearDegreeOfMembership() for all terms', function () {

const atomicTerm1 = new CustomFuzzyTerm();
atomicTerm1.degreeOfMembership = 0.5;
const atomicTerm2 = new CustomFuzzyTerm();
atomicTerm2.degreeOfMembership = 0.25;

const compositeTerm = new FuzzyCompositeTerm( [ atomicTerm1, atomicTerm2 ] );
compositeTerm.clearDegreeOfMembership();

expect( atomicTerm1.degreeOfMembership ).to.equal( 0 );
expect( atomicTerm2.degreeOfMembership ).to.equal( 0 );

} );

} );

describe( '#clearDegreeOfMembership()', function () {

it( 'should execute .updateDegreeOfMembership() for all terms', function () {

const atomicTerm1 = new CustomFuzzyTerm();
const atomicTerm2 = new CustomFuzzyTerm();

const compositeTerm = new FuzzyCompositeTerm( [ atomicTerm1, atomicTerm2 ] );
compositeTerm.updateDegreeOfMembership( 0.75 );

expect( atomicTerm1.degreeOfMembership ).to.equal( 0.75 );
expect( atomicTerm2.degreeOfMembership ).to.equal( 0.75 );

} );

} );

} );

//

class CustomFuzzyTerm extends FuzzyTerm {

constructor() {

super();

this.degreeOfMembership = 0.5;

}

clearDegreeOfMembership() {

this.degreeOfMembership = 0;

}

getDegreeOfMembership() {

return this.degreeOfMembership;

}

updateDegreeOfMembership( value ) {

this.degreeOfMembership = value;

}

}

0 comments on commit 64feed1

Please sign in to comment.