Exemplo n.º 1
0
 public Do Run( string input, AlchemyController controller, ICommunicator communicator )
 {
     string elementname = input.TrimStart( '*' );
     if( controller.ElementExists( elementname ) )
         controller.ForeachNonterminalElement( ( e ) => controller.ReportChangedRule( new Rule( new[] { elementname, e.Name } ) ) );
     return Do.AnotherRule;
 }
Exemplo n.º 2
0
        public void AlchemyControllerRecommendsEmptyRuleIfDone()
        {
            var rs = new RuleSet();
            var controller = new AlchemyController( rs );
            controller.RegisterNewElement( "fire" );
            var rule = controller.RecommendNewRule();
            controller.ReportChangedRule( rule );

            var finalRule = controller.RecommendNewRule();

            Assert.AreEqual( Rule.EmptyRule, finalRule );
        }
Exemplo n.º 3
0
        public void RuleSetOnlyNotifiesWhenStrictlyNeccessary()
        {
            var changedprops = new List<string>();
            var persister = new XmlPersister( new RuleSetXmlSerializer(), new StreamFactory( "data.xml" ), 2000 );
            var rs = persister.RecreateRuleSet();
            rs.PropertyChanged += ( s, e ) => changedprops.Add( e.PropertyName );

            var controller = new AlchemyController( rs );

            var rule = controller.RecommendNewRule();
            rule.Result = new[] { new Element( "alpha" ) };
            controller.ReportChangedRule( rule );

            Assert.AreEqual( 1, changedprops.Count( p => p.Equals( "FoundElements" ) ) );
            Assert.AreEqual( 1, changedprops.Count( p => p.Equals( "Rules" ) ) );
        }
Exemplo n.º 4
0
        public void RuleSetNotifiesOnPropertyChanged()
        {
            var changedprops = new List<string>();
            var r = new RuleSet();
            r.PropertyChanged += ( s, e ) => changedprops.Add( e.PropertyName );

            var controller = new AlchemyController( r );
            controller.RegisterNewElement( "fire" );
            var rule = controller.RecommendNewRule();
            rule.Result = new[] { new Element( "water" ) };

            controller.ReportChangedRule( rule );

            Assert.AreEqual( 2, changedprops.Count( p => p.Equals( "FoundElements" ) ) );
            Assert.AreEqual( 2, r.FoundElements.Count() );
            Assert.AreEqual( 1, changedprops.Count( p => p.Equals( "Rules" ) ) );
            Assert.AreEqual( 1, r.Rules.Count() );
        }
        public Do Run( string input, AlchemyController controller, ICommunicator communicator )
        {
            var splitinput = input.TrimStart( ' ', '#' ).Split( ':' );
            if( splitinput.Length < 2 )
                return Do.AnotherRule;

            var ingredients = splitinput[0].Split( ',' );

            foreach( var ingredient in ingredients )
            {
                if( !controller.ElementExists( ingredient ) )
                {
                    communicator.Display( "Bad data, element named '" + ingredient + "' isnt in list of known elements. If this isnt a typo, please add element using '>element', or, if possible, add rule that creates element" );
                    return Do.AnotherRule;
                }
            }

            var rule = new Rule( ingredients );
            rule.SetResult( splitinput[1] );
            controller.ReportChangedRule( rule, true );

            return Do.AnotherRule;
        }
Exemplo n.º 6
0
        public void AlchemyControllerWontReccomendRuleWithTerminalElements()
        {
            var rs = new RuleSet();
            var a = new AlchemyController( rs );

            a.RegisterNewElement( "a" );
            a.RegisterNewElement( "b" );
            a.RegisterNewElement( "c" );
            a.FinalizeElement( "a" );

            var rules = new List<Rule>();
            for( int i = 0; i < 100; i++ )
            {
                var r1 = a.RecommendNewRule();
                if( r1 == Rule.EmptyRule )
                    break;
                rules.Add( r1 );
                a.ReportChangedRule( r1 );
            }

            Assert.AreEqual( 3, rules.Count );
            Assert.IsFalse( rules.Any( r => r.Ingredients.Any( i => i.Name.Equals( a ) ) ) );
        }
Exemplo n.º 7
0
        public void RuleOverrideTriggersPropchangedIfRuleChanged()
        {
            var rs = new RuleSet()
            {
                FoundElements = new[] { new Element( "fire" ), new Element( "water" ) },
                Rules = new[] { new Rule( new[] { "fire", "fire" }, "fire" ) }
            };
            var c = new AlchemyController( rs );
            int count = 0;
            rs.PropertyChanged += ( a, e ) => { count++; };

            c.ReportChangedRule( new Rule( new[] { "fire", "fire" }, "water" ), true );

            Assert.AreEqual( 1, count );
        }
Exemplo n.º 8
0
        public void ControllerRecommendsCombinationsButNotDuplicates()
        {
            var rs = new RuleSet();
            var c = new AlchemyController( rs );
            c.RegisterNewElement( "a" );
            c.RegisterNewElement( "mb" );
            c.RegisterNewElement( "aoeu" );
            c.ReportChangedRule( c.RecommendNewRule() );

            var rules = new List<Rule>();
            for( int i = 0; i < 100; i++ )
            {
                var r1 = c.RecommendNewRule();
                if( r1 == Rule.EmptyRule )
                    break;
                rules.Add( r1 );
                c.ReportChangedRule( r1 );
            }

            Assert.AreEqual( 5, rules.Count );
        }
Exemplo n.º 9
0
        public void ControllerRecommendsAllPossibleCombinationsOfElements()
        {
            const string e1 = "1";
            const string e2 = "2";
            var rs = new RuleSet();
            var c = new AlchemyController( rs );
            c.RegisterNewElement( e1 );
            c.RegisterNewElement( e2 );
            var rules = new List<Rule>();
            for( int i = 0; i < 100; i++ )
            {
                var r1 = c.RecommendNewRule();
                if( r1 == Rule.EmptyRule )
                    break;
                rules.Add( r1 );
                c.ReportChangedRule( r1 );
            }

            Assert.AreEqual( 3, rules.Count );
            Assert.IsTrue( AssertForCombo( e1, e1, rules ) );
            Assert.IsTrue( AssertForCombo( e1, e2, rules ) );
            Assert.IsTrue( AssertForCombo( e2, e2, rules ) );
        }
Exemplo n.º 10
0
        public void CanReportTestedRuleBackToController()
        {
            var rs = new RuleSet();
            var controller = new AlchemyController( rs );
            controller.RegisterNewElement( "fire" );
            var rule = controller.RecommendNewRule();
            rule.Result = new[] { new Element( "water" ) };

            controller.ReportChangedRule( rule );

            Assert.AreEqual( 1, rs.Rules.Count() );
            Assert.AreEqual( 2, rs.FoundElements.Count() );
        }