Search code examples
clips

Clips.NET encoding problem. String cyrillic facts are not readable


I am trying to use CLIPS.NET to integrate my CLIPS expert system with .Net App. The problem is that some of my facts that I use includes some Cyrillic strings.

When I got result back to .Net app from Clips. All Cyrillic strings are scrambled (see the picture below).

csharp clips

Code Sample (fullName here has Cyrillic symbols inside):

String findAllActiveMembers = "(find-all-facts ((?f  Member )) (= ?f:isActive 1))";
            MultifieldValue members = (MultifieldValue)clips.Eval(findAllActiveMembers);
            foreach (FactAddressValue member in members) {
                SymbolValue tag = (SymbolValue)member.GetSlotValue("tagName");
                StringValue fullName = (StringValue)member.GetSlotValue("fullName");
                this.factList.Items.Add(tag.Value + " " + fullName.Value);
            }

What can I do to get them correctly? Clips IDE 6.4 works fine and shows these strings correctly

Thank you!


Solution

  • It's a bug in the code that's converting the C UTF-8 strings to .NET strings. To fix it, you'll need to make a change to the SingleFieldToPrimitiveValue method in the CLIPSNET_Values.cpp file. Replace these lines:

    rv = gcnew StringValue(gcnew String(theCString));
       .
       .
       .
    rv = gcnew SymbolValue(gcnew String(theCString));
       .
       .
       .
    rv = gcnew InstanceNameValue(gcnew String(theCString));
    

    With these:

    rv = gcnew StringValue(gcnew String(theCString,0,strlen(theCString),UTF8Encoding::UTF8));
       .
       .
       .
    rv = gcnew SymbolValue(gcnew String(theCString,0,strlen(theCString),UTF8Encoding::UTF8));
       .
       .
       .
    rv = gcnew InstanceNameValue(gcnew String(theCString,0,strlen(theCString),UTF8Encoding::UTF8));
    

    You can also download the file here: https://sourceforge.net/p/clipsrules/code/HEAD/tree/branches/64x/windows/MVS_2017/CLIPSCLRWrapper/Source/Integration/CLIPSNET_Values.cpp