The Instructor Interface and the Teach method

Using the Instructor interface is a way a to load a Validation Syntax on QInjection.
You have two kinds of Instructor, the LPP Instructor and the HPP Instructor, called also INodesInstructor and IElementsInstructor.

These are the interfaces:
    public interface IElementsInstructor
    {
        IEnumerable<Element> Elements { get; }

        IEnumerable<Tuple<Restriction, object>> Restrictions(Element element);
    }

    public interface INodesInstructor
    {
        IEnumerable<Node> Nodes { get; }

        IEnumerable<Branch> Branches(Node node);

        IEnumerable<Tuple<Restriction, object>> Restrictions(Node node);
    }


INodesInstructor configures Nodes, Branches between them and Restrictions to apply on them.
IElementsInstructor configures Elements and Restrictions to apply on them.

This is an example of Instructor For Nodes:
    internal class InstructorNodesSimple : INodesInstructor
    {
        private List<Node> _nodesmap;
        private Dictionary<Node, List<Branch>> _branchesmap;
        private Dictionary<Node, List<Tuple<Restriction, object>>> _restrictionsmap;

        public InstructorNodesSimple()
        {
            _nodesmap = new List<Node>();
            _nodesmap.Add(Node.BinaryExpression);
            _nodesmap.Add(Node.PrefixUnaryExpression);
            _nodesmap.Add(Node.PostfixUnaryExpression);
            _nodesmap.Add(Node.ConditionalExpression);
            _nodesmap.Add(Node.IdentifierName);
            _nodesmap.Add(Node.LiteralExpression);
            _nodesmap.Add(Node.MemberAccessExpression);
            _nodesmap.Add(Node.InvocationExpression);
            _nodesmap.Add(Node.ParenthesizedExpression);

            _branchesmap = new Dictionary<Node, List<Branch>>();
            foreach (var n in _nodesmap)
            {
                _branchesmap[n] = new List<Branch>();
                _branchesmap[n].Add(Branch.Body);
                _branchesmap[n].Add(Branch.Expression);
                _branchesmap[n].Add(Branch.LeftMember);
                _branchesmap[n].Add(Branch.RightMember);
                _branchesmap[n].Add(Branch.Condition);
                _branchesmap[n].Add(Branch.WhenTrue);
                _branchesmap[n].Add(Branch.WhenFalse);
            }

            _nodesmap.Add(Node.Argument);
            _branchesmap[Node.Argument] = new List<Branch>();
            _branchesmap[Node.Argument].Add(Branch.Argument);

            _restrictionsmap = new Dictionary<Node, List<Tuple<Restriction, object>>>();

            _restrictionsmap[Node.Argument] = new List<Tuple<Restriction, object>>();
            _restrictionsmap[Node.Argument].Add(new Tuple<Restriction, object>(Restriction.Reference, Reference.Reference));

            _restrictionsmap[Node.PrefixUnaryExpression] = new List<Tuple<Restriction, object>>();
            _restrictionsmap[Node.PrefixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.LogicalNot));
            _restrictionsmap[Node.PrefixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.PlusPlus));
            _restrictionsmap[Node.PrefixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.MinusMinus));
            _restrictionsmap[Node.PrefixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.BitwiseComplement));

            _restrictionsmap[Node.PostfixUnaryExpression] = new List<Tuple<Restriction, object>>();
            _restrictionsmap[Node.PostfixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.LogicalNot));
            _restrictionsmap[Node.PostfixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.PlusPlus));
            _restrictionsmap[Node.PostfixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.MinusMinus));
            _restrictionsmap[Node.PostfixUnaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, Operator.BitwiseComplement));

            _restrictionsmap[Node.BinaryExpression] = new List<Tuple<Restriction, object>>();
            foreach (Operator op in Enum.GetValues(typeof(Operator)))
            {
                _restrictionsmap[Node.BinaryExpression].Add(new Tuple<Restriction, object>(Restriction.Operator, op));
            }

            _restrictionsmap[Node.InvocationExpression] = new List<Tuple<Restriction, object>>();
            _restrictionsmap[Node.InvocationExpression].Add(new Tuple<Restriction, object>(Restriction.Name, "Check"));
            _restrictionsmap[Node.InvocationExpression].Add(new Tuple<Restriction, object>(Restriction.Arity, 2));
            _restrictionsmap[Node.InvocationExpression].Add(new Tuple<Restriction, object>(Restriction.NameRegex, @"^[\w]{1,5}"));

            _restrictionsmap[Node.MemberAccessExpression] = new List<Tuple<Restriction, object>>();
            _restrictionsmap[Node.MemberAccessExpression].Add(new Tuple<Restriction, object>(Restriction.Name, "item"));
            _restrictionsmap[Node.MemberAccessExpression].Add(new Tuple<Restriction, object>(Restriction.NameRegex, @"^[\w]{1,5}"));
        }
        public IEnumerable<Node> Nodes
        {
            get { return _nodesmap; }
        }

        public IEnumerable<Branch> Branches(Node node)
        {
            if (_branchesmap.ContainsKey(node))
                return _branchesmap[node];
            else
                return new List<Branch>();
        }

        public IEnumerable<Tuple<Restriction, object>> Restrictions(Node node)
        {
            if (_restrictionsmap.ContainsKey(node))
                return _restrictionsmap[node];
            else
                return new List<Tuple<Restriction, object>>();
        }
    }

If you want to use it then you have to Teach to QInjection with a code like the following one:
	InstructorNodesSimple instructor = new InstructorNodesSimple();

	QInjector injector = new QInjector();

	injector.Teach(instructor);

This is what we call CPP or Configuration Profile Pattern.


Last edited Jan 12, 2014 at 1:21 PM by SamNium, version 1