17
« on: December 18, 2016, 11:00:49 pm »
If you want to interface with Hal's brain from an external project and can utilize COM/ActiveX here are some code samples in VB6 and VB.NET. C# would be very similar to the VB.net version.
Here's the full code how to make a command line version of Hal in VB.NET:
Option Strict Off
Option Explicit On
Module modHalConsole
'Constants that point to path of script to run and database to open
Const HalScript As String = "C:\Program Files\Zabaware\Ultra Hal Assistant 6\Hal6.uhp"
Const HalDB As String = "C:\Program Files\Zabaware\Ultra Hal Assistant 6\HalBrain.db"
'Objects that will contain reference to HalBrain and WordNet activex dll
Dim HalBrain As Object
Dim WN As Object
'Variants to hold contents of Hal's short term memory
Dim GainControl, PrevUserSent, PrevSent, Insults, Hate, Swear, Compliment, LastResponseTime, CustomMem, LastTopicList As Object
Sub Main()
Dim HalScriptControl As New MSScriptControl.ScriptControl
Dim HalCommands As Object
Dim ScriptOut As String
Dim PassVars(14) As Object
'Create instance of HalBrain and WordNet ActiveX DLL
HalBrain = CreateObject("UltraHalAsst6.Brain")
WN = CreateObject("UltraHalAsst6.WordNet")
'Initialize Hal's memory
ResetMem()
'Read script file and add to script control, open database connection
Dim ff As Short
Dim TheScript As String
ff = FreeFile()
FileOpen(ff, HalScript, OpenMode.Input)
TheScript = InputString(ff, LOF(ff))
FileClose(ff)
HalScriptControl.AllowUI = True
HalScriptControl.Language = "VBScript"
HalScriptControl.Timeout = 10000
HalScriptControl.UseSafeSubset = False
HalScriptControl.Reset()
HalScriptControl.AddCode(TheScript)
HalScriptControl.AddObject("HalBrain", HalBrain, True) 'Give script control access to HalBrain object
HalScriptControl.AddObject("WN", WN, True) 'Give script control access to WN object
HalBrain.OpenDatabase(HalDB) 'Open database
Do
'Load up object array with variant function paramaters script is expecting
PassVars(0) = Console.ReadLine()
PassVars(1) = "Judge"
PassVars(2) = "Steve"
PassVars(3) = 0 'Learning
PassVars(4) = HalDB
PassVars(5) = Hate
PassVars(6) = Swear
PassVars(7) = Insults
PassVars(8) = Compliment
PassVars(9) = PrevSent
PassVars(10) = LastResponseTime
PassVars(11) = PrevUserSent
PassVars(12) = CustomMem
PassVars(13) = GainControl
PassVars(14) = LastTopicList
'Run Script
ScriptOut = HalScriptControl.Run("UltraHal", PassVars)
'If script was successfull it will contain <STOREVARS> in which case we remember the variables passed and extract the response
If InStr(ScriptOut, "<STOREVARS>") Then
ScriptOut = HalBrain.RecallVars(ScriptOut, HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList)
End If
Console.WriteLine(ScriptOut)
Loop
End Sub
Sub ResetMem()
Hate = 0
Swear = 0
Insults = 0
Compliment = 0
PrevSent = ""
LastResponseTime = ""
PrevUserSent = ""
CustomMem = ""
GainControl = ""
LastTopicList = ""
End Sub
End Module