Simple Plugin - The Explanation

Developer tutorials for Prototype 8.0

Simple Plugin - The Explanation

Postby Leslie » Thu Oct 02, 2014 6:14 pm

CSharp Code

using System;
using Syn.Engine;
using Syn.Engine.Data;

namespace MyPlugin
{
public class MyPlugin : SynPlugin
{
public override void Initialize()
{
try
{
SynIcon pageIcon = new SynIcon();
pageIcon.Title = "My Plugin";
pageIcon.Description = "My Plugin Page Description";
pageIcon.Page = new Page();
pageIcon.Plugin = this;
pageIcon.DisplayImage = Properties.Resources.page_image;

this.Engine.SettingsPanel.AddIcon(pageIcon);
this.Engine.AddAutoParser(new HelloPluginCommand());
this.Engine.AddGear(new HelloPluginGear());

this.SettingsFile.GetElementsByTagName("Some_Setting").Item(0).InnerText = bool.TrueString;

this.Engine.ServiceCaller.Respond("My Plugin is now ready");
}
catch (Exception ex)
{
SynLogger.Log(this, "Initialize", ex);
}
}

public class HelloPluginCommand : SynData
{
public HelloPluginCommand()
{
this.Data.LoadPatterns("MyPlugin");
}
}

public class HelloPluginGear : SynGear
{
public void HelloPlugin(HelloPluginCommand command)
{
command.Data.Caller.Respond("Hello Developer!");
}
}
}
}


Visual Basic Code

Imports Syn.Engine
Imports Syn.Engine.Data

Public Class MyPlugin
Inherits SynPlugin

Public Overrides Sub Initialize()
Try
Dim pageIcon As New SynIcon
pageIcon.Plugin = Me
pageIcon.Page = New Page
pageIcon.Title = "My Plugin"
pageIcon.Description = "My Plugin Page Description"
pageIcon.DisplayImage = My.Resources.page_image

Me.Engine.SettingsPanel.AddIcon(pageIcon)
Me.Engine.AddAutoParser(New HelloPluginCommand)
Me.Engine.AddGear(New HelloPluginGear)

Me.SettingsFile.GetElementsByTagName("Some_Setting").Item(0).InnerText = Boolean.TrueString

Me.Engine.ServiceCaller.Respond("My Plugin is now ready")
Catch ex As Exception
SynLogger.Log(Me, "Initialize", ex)
End Try
End Sub

Public Class HelloPluginCommand : Inherits SynData
Sub New()
Me.Data.LoadPatterns("MyPlugin")
End Sub
End Class

Public Class HelloPluginGear : Inherits SynGear
Public Sub HelloPlugin(command As HelloPluginCommand)
command.Data.Caller.Respond("Hello Developer!")
End Sub
End Class

End Class


Lets get to business. To create a plugin for SynEngine you will first have to create a empty class library and import Syn.Engine.Dll into it.

In the constructor try not accessing any file or document or even Engine functionalities - because most of the functionalities work properly only when all the Plugins are loaded into the Engine. If you want to access some functionalities of the Engine when the plugin is loaded try overriding the Initialize method

In the Initialize function we create a page ( a user control ) and add it to the Settings Panel - which is later displayed as an icon in the Settings Panel. Later we add a Gear and a Data. Then we respond to the Service Caller ( which is the caller that runs background services ) and finally we access and modify the tag "Some_Setting" in the XML document of the Plugin. It can be seen that the document is directly accessed without specifying any path or access roots. This XML document should have the same name as the plugin's class name and must be placed in the "Settings" directory.

We have placed the entire block in a try catch block and we try to log any error during the Initialization . The default Logger of the Engine will log all the details in the "SynEngine.Log" file which is stored in the root directory.

SynData

A Data is like a parameter to a function. A Data can be generated by a parser and the parser uses complex matching techniques to generate a Data or to be more precise a Data is matched against a string and a pattern and if the match returns a value then the Data along with the match value is inserted into the Caller's Context.

So in the above example - we create a term called "HelloPluginCommand" and we then load its pattern from the Plugin's XML File - Which is placed in the Settings directory

Now lets see how the pattern is:

XML Code

<main>
<syndata name="HelloPluginCommand">
<pattern>b(hello plugin)b</pattern>
</syndata>
</main>



As you can see - we created a Tag named "SynData" and gave it the exact name as the Data class we defined inside our plugin which is "HelloPluginCommand". The pattern we gave it is a regex pattern of dotnet and it matches the word "hello" . 

Do remember that regex patterns should be in small letters because the default message that will be received and parsed by Parsers and AutoParsers are in small letters. 

SynGear




Now a SynGear is a container of functions- all Public functions inside a SynGear are analyzed by the Engine and stored in memory for execution when all its parameters are available.

In the above example we create a SynGear and name it HelloPluginGear and inside it we create a public function named "HelloPlugin". The name is irrelevant because the Engine never considers it for any parsing or execution function. In the function we set a parameter named command which is indeed our HelloPluginCommand.

Now you see that we have a function right ? and this function is taking a parameter of the type HelloPluginCommand. Now how does this function execute ?.

Lets get into details.

So when the User - sends a message "Hello Plugin" - the AutoParser will parse your Data based on the patterns loaded for it. Now once we a match for this Data is loaded into the Caller's Context - after its loaded into the Caller's context all functions known to the Engine via Gears will be checked and if anyone of them accepts this Data as its parameter then the function called with this argument.

Next within the function "HelloPlugin" we have set a response to the so called Caller- A Caller is nothing but someone who actually sends the information/message "Hello Plugin" to the Engine for evaluation.

And the response we have set is "Hello Plugin'. This response will be sent to the Caller immediately.


It must be noted that in the Initialize function we added the HelloPluginGear and HelloPluginCommand to the Engine. You can see that we added the HelloPluginCommand to be AutoParsed. This is because there is an AutoParser within the Engine that can parse Data that are based strictly on known string patterns.
User avatar
Leslie
Lead Software Architect
 
Posts: 310
Joined: Fri Sep 14, 2012 12:20 pm

Return to Developer Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest
cron