Example #1
0
        bool Test()
        {
            //
            // TODO: Add code to start application here
            //
            sml.Kernel kernel = sml.Kernel.CreateKernelInNewThread("SoarKernelSML");

            // Make sure the kernel was ok
            if (kernel.HadError())
            {
                throw new Exception("Error initializing kernel: " + kernel.GetLastErrorDescription());
            }

            sml.Agent agent = kernel.CreateAgent("First");

            // We test the kernel for an error after creating an agent as the agent
            // object may not be properly constructed if the create call failed so
            // we store errors in the kernel in this case.  Once this create is done we can work directly with the agent.
            if (kernel.HadError())
            {
                throw new Exception("Error creating agent: " + kernel.GetLastErrorDescription());
            }

            bool ok = agent.LoadProductions("..\\Tests\\testcsharpsml.soar");

            if (!ok)
            {
                System.Console.Out.WriteLine("Load failed");
                return(false);
            }

            Agent  pAgent  = agent;             // So it's easier copying existing C++ code here
            Kernel pKernel = kernel;

            Identifier pInputLink = agent.GetInputLink();

            if (pInputLink == null)
            {
                throw new Exception("Error getting the input link");
            }

            // Some random adds
            Identifier    pID   = pAgent.CreateIdWME(pInputLink, "plane");
            StringElement pWME1 = pAgent.CreateStringWME(pID, "type", "Boeing747");
            IntElement    pWME2 = pAgent.CreateIntWME(pID, "speed", 200);

            // Then add some tic tac toe stuff which should trigger output
            Identifier    pSquare = pAgent.CreateIdWME(pInputLink, "square");
            StringElement pEmpty  = pAgent.CreateStringWME(pSquare, "content", "RANDOM");
            IntElement    pRow    = pAgent.CreateIntWME(pSquare, "row", 1);
            IntElement    pCol    = pAgent.CreateIntWME(pSquare, "col", 2);

            ok = pAgent.Commit();

            // Quick test of init-soar
            pAgent.InitSoar();

            // Update the square's value to be empty.  This ensures that the update
            // call is doing something.  Otherwise, when we run we won't get a match.
            pAgent.Update(pEmpty, "EMPTY");
            ok = pAgent.Commit();

            String myTestData = "my data";

            sml.Agent.RunEventCallback           runCall    = new sml.Agent.RunEventCallback(MyRunEventCallback);
            sml.Agent.ProductionEventCallback    prodCall   = new sml.Agent.ProductionEventCallback(MyProductionEventCallback);
            sml.Agent.PrintEventCallback         printCall  = new sml.Agent.PrintEventCallback(MyPrintEventCallback);
            sml.Agent.OutputEventCallback        outputCall = new sml.Agent.OutputEventCallback(MyOutputEventCallback);
            sml.Agent.XMLEventCallback           xmlCall    = new sml.Agent.XMLEventCallback(MyXMLEventCallback);
            sml.Agent.OutputNotificationCallback noteCall   = new sml.Agent.OutputNotificationCallback(MyOutputNotificationCallback);
            sml.Kernel.AgentEventCallback        agentCall  = new sml.Kernel.AgentEventCallback(MyAgentEventCallback);
            sml.Kernel.SystemEventCallback       systemCall = new sml.Kernel.SystemEventCallback(MySystemEventCallback);
            sml.Kernel.UpdateEventCallback       updateCall = new sml.Kernel.UpdateEventCallback(MyUpdateEventCallback);
            sml.Kernel.StringEventCallback       strCall    = new sml.Kernel.StringEventCallback(MyStringEventCallback);
            sml.Kernel.RhsFunction           rhsCall        = new sml.Kernel.RhsFunction(MyTestRhsFunction);
            sml.Kernel.ClientMessageCallback clientCall     = new sml.Kernel.ClientMessageCallback(MyTestClientMessageCallback);

            int runCallbackID    = agent.RegisterForRunEvent(sml.smlRunEventId.smlEVENT_AFTER_DECISION_CYCLE, runCall, myTestData);
            int prodCallbackID   = agent.RegisterForProductionEvent(sml.smlProductionEventId.smlEVENT_AFTER_PRODUCTION_FIRED, prodCall, myTestData);
            int printCallbackID  = agent.RegisterForPrintEvent(sml.smlPrintEventId.smlEVENT_PRINT, printCall, myTestData);
            int outputCallbackID = agent.AddOutputHandler("move", outputCall, myTestData);
            int noteCallbackID   = agent.RegisterForOutputNotification(noteCall, myTestData);
            int xmlCallbackID    = agent.RegisterForXMLEvent(sml.smlXMLEventId.smlEVENT_XML_TRACE_OUTPUT, xmlCall, myTestData);
            int agentCallbackID  = kernel.RegisterForAgentEvent(sml.smlAgentEventId.smlEVENT_BEFORE_AGENT_REINITIALIZED, agentCall, myTestData);
            int systemCallbackID = kernel.RegisterForSystemEvent(sml.smlSystemEventId.smlEVENT_SYSTEM_START, systemCall, myTestData);
            int updateCallbackID = kernel.RegisterForUpdateEvent(sml.smlUpdateEventId.smlEVENT_AFTER_ALL_OUTPUT_PHASES, updateCall, myTestData);
            int stringCallbackID = kernel.RegisterForStringEvent(sml.smlStringEventId.smlEVENT_EDIT_PRODUCTION, strCall, myTestData);
            int rhsCallbackID    = kernel.AddRhsFunction("test-rhs", rhsCall, myTestData);
            int clientCallbackID = kernel.RegisterForClientMessageEvent("test-client", clientCall, myTestData);

            // Running the agent will trigger most of the events we're listening for so
            // we can check that they're working correctly.
            agent.RunSelf(3);
            //kernel.RunAllAgents(3) ;

            // Trigger an agent event
            agent.InitSoar();

            ok = agent.UnregisterForRunEvent(runCallbackID);
            ok = ok && agent.UnregisterForProductionEvent(prodCallbackID);
            ok = ok && agent.UnregisterForPrintEvent(printCallbackID);
            ok = ok && agent.RemoveOutputHandler(outputCallbackID);
            ok = ok && agent.UnregisterForOutputNotification(noteCallbackID);
            ok = ok && agent.UnregisterForXMLEvent(xmlCallbackID);
            ok = ok && kernel.UnregisterForAgentEvent(agentCallbackID);
            ok = ok && kernel.UnregisterForSystemEvent(systemCallbackID);
            ok = ok && kernel.UnregisterForUpdateEvent(updateCallbackID);
            ok = ok && kernel.UnregisterForStringEvent(stringCallbackID);
            ok = ok && kernel.RemoveRhsFunction(rhsCallbackID);
            ok = ok && kernel.UnregisterForClientMessageEvent(clientCallbackID);

            if (!ok)
            {
                System.Console.Out.WriteLine("Failed to unregister an event");
                return(false);
            }

            // Close down the kernel (or for a remote connection disconnect from the kernel w/o closing it down).
            kernel.Shutdown();

            return(ok);
        }
        bool Test()
        {
            //
            // TODO: Add code to start application here
            //
            sml.Kernel kernel = sml.Kernel.CreateKernelInNewThread("SoarKernelSML") ;

            // Make sure the kernel was ok
            if (kernel.HadError())
                throw new Exception("Error initializing kernel: " + kernel.GetLastErrorDescription()) ;

            sml.Agent agent = kernel.CreateAgent("First") ;

            // We test the kernel for an error after creating an agent as the agent
            // object may not be properly constructed if the create call failed so
            // we store errors in the kernel in this case.  Once this create is done we can work directly with the agent.
            if (kernel.HadError())
                throw new Exception("Error creating agent: " + kernel.GetLastErrorDescription()) ;

            bool ok = agent.LoadProductions("..\\Tests\\testcsharpsml.soar") ;
            if (!ok)
            {
                System.Console.Out.WriteLine("Load failed") ;
                return false ;
            }

            Agent pAgent = agent ;	// So it's easier copying existing C++ code here
            Kernel pKernel = kernel ;

            Identifier pInputLink = agent.GetInputLink() ;

            if (pInputLink == null)
                throw new Exception("Error getting the input link") ;

            // Some random adds
            Identifier pID = pAgent.CreateIdWME(pInputLink, "plane") ;
            StringElement pWME1 = pAgent.CreateStringWME(pID, "type", "Boeing747") ;
            IntElement pWME2    = pAgent.CreateIntWME(pID, "speed", 200) ;

            // Then add some tic tac toe stuff which should trigger output
            Identifier pSquare = pAgent.CreateIdWME(pInputLink, "square") ;
            StringElement pEmpty = pAgent.CreateStringWME(pSquare, "content", "RANDOM") ;
            IntElement pRow = pAgent.CreateIntWME(pSquare, "row", 1) ;
            IntElement pCol = pAgent.CreateIntWME(pSquare, "col", 2) ;

            ok = pAgent.Commit() ;

            // Quick test of init-soar
            pAgent.InitSoar() ;

            // Update the square's value to be empty.  This ensures that the update
            // call is doing something.  Otherwise, when we run we won't get a match.
            pAgent.Update(pEmpty, "EMPTY") ;
            ok = pAgent.Commit() ;

            String myTestData = "my data" ;
            sml.Agent.RunEventCallback runCall = new sml.Agent.RunEventCallback(MyRunEventCallback) ;
            sml.Agent.ProductionEventCallback prodCall = new sml.Agent.ProductionEventCallback(MyProductionEventCallback) ;
            sml.Agent.PrintEventCallback printCall = new sml.Agent.PrintEventCallback(MyPrintEventCallback) ;
            sml.Agent.OutputEventCallback outputCall = new sml.Agent.OutputEventCallback(MyOutputEventCallback) ;
            sml.Agent.XMLEventCallback xmlCall		 = new sml.Agent.XMLEventCallback(MyXMLEventCallback) ;
            sml.Agent.OutputNotificationCallback noteCall = new sml.Agent.OutputNotificationCallback(MyOutputNotificationCallback) ;
            sml.Kernel.AgentEventCallback agentCall = new sml.Kernel.AgentEventCallback(MyAgentEventCallback) ;
            sml.Kernel.SystemEventCallback systemCall = new sml.Kernel.SystemEventCallback(MySystemEventCallback) ;
            sml.Kernel.UpdateEventCallback updateCall = new sml.Kernel.UpdateEventCallback(MyUpdateEventCallback) ;
            sml.Kernel.StringEventCallback strCall    = new sml.Kernel.StringEventCallback(MyStringEventCallback) ;
            sml.Kernel.RhsFunction rhsCall			  = new sml.Kernel.RhsFunction(MyTestRhsFunction) ;
            sml.Kernel.ClientMessageCallback clientCall	= new sml.Kernel.ClientMessageCallback(MyTestClientMessageCallback) ;

            int runCallbackID	= agent.RegisterForRunEvent(sml.smlRunEventId.smlEVENT_AFTER_DECISION_CYCLE, runCall, myTestData) ;
            int prodCallbackID	= agent.RegisterForProductionEvent(sml.smlProductionEventId.smlEVENT_AFTER_PRODUCTION_FIRED, prodCall, myTestData) ;
            int printCallbackID	= agent.RegisterForPrintEvent(sml.smlPrintEventId.smlEVENT_PRINT, printCall, myTestData) ;
            int outputCallbackID= agent.AddOutputHandler("move", outputCall, myTestData) ;
            int noteCallbackID  = agent.RegisterForOutputNotification(noteCall, myTestData) ;
            int xmlCallbackID   = agent.RegisterForXMLEvent(sml.smlXMLEventId.smlEVENT_XML_TRACE_OUTPUT, xmlCall, myTestData) ;
            int agentCallbackID	= kernel.RegisterForAgentEvent(sml.smlAgentEventId.smlEVENT_BEFORE_AGENT_REINITIALIZED, agentCall, myTestData) ;
            int systemCallbackID = kernel.RegisterForSystemEvent(sml.smlSystemEventId.smlEVENT_SYSTEM_START, systemCall, myTestData);
            int updateCallbackID= kernel.RegisterForUpdateEvent(sml.smlUpdateEventId.smlEVENT_AFTER_ALL_OUTPUT_PHASES, updateCall, myTestData) ;
            int stringCallbackID= kernel.RegisterForStringEvent(sml.smlStringEventId.smlEVENT_EDIT_PRODUCTION, strCall, myTestData) ;
            int rhsCallbackID	= kernel.AddRhsFunction("test-rhs", rhsCall, myTestData) ;
            int clientCallbackID= kernel.RegisterForClientMessageEvent("test-client", clientCall, myTestData) ;

            // Running the agent will trigger most of the events we're listening for so
            // we can check that they're working correctly.
            agent.RunSelf(3) ;
            //kernel.RunAllAgents(3) ;

            // Trigger an agent event
            agent.InitSoar() ;

            ok = agent.UnregisterForRunEvent(runCallbackID) ;
            ok = ok && agent.UnregisterForProductionEvent(prodCallbackID) ;
            ok = ok && agent.UnregisterForPrintEvent(printCallbackID) ;
            ok = ok && agent.RemoveOutputHandler(outputCallbackID) ;
            ok = ok && agent.UnregisterForOutputNotification(noteCallbackID) ;
            ok = ok && agent.UnregisterForXMLEvent(xmlCallbackID) ;
            ok = ok && kernel.UnregisterForAgentEvent(agentCallbackID) ;
            ok = ok && kernel.UnregisterForSystemEvent(systemCallbackID) ;
            ok = ok && kernel.UnregisterForUpdateEvent(updateCallbackID) ;
            ok = ok && kernel.UnregisterForStringEvent(stringCallbackID) ;
            ok = ok && kernel.RemoveRhsFunction(rhsCallbackID) ;
            ok = ok && kernel.UnregisterForClientMessageEvent(clientCallbackID) ;

            if (!ok)
            {
                System.Console.Out.WriteLine("Failed to unregister an event") ;
                return false ;
            }

            // Close down the kernel (or for a remote connection disconnect from the kernel w/o closing it down).
            kernel.Shutdown() ;

            return ok ;
        }