C# to APL to C# Round trip with Unicode string

General discussions related to using the C# Script Engine in APL+Win.

Moderators: Tech Support, phpbb_admin

C# to APL to C# Round trip with Unicode string

Postby Ajay Askoolum » July 15th, 2020, 11:46 am

I have a result that looks like this:
Code: Select all
      yy„Œcse 'GetValue' 'System.Text.Encoding.ASCII.GetString((byte[])Gatwick.STAsBinary().Value)'
      yy
§§…???[??????I@
      ½yy
21
The shape matches the length of the same value in C#. When I paste the value of yy with Enable Unicode Clipboard enabled, it changes
Code: Select all
½"™™…???[??????I@" This is Paste of the value of yy from the session ... it & its shape changed [Enable Unicode Clipboard is on]
In C# the same string looks like
"\u0001\u0001\0\0\0\u001a???\a[??\0??\u007f??I@"
When I pass back yy to C#, it is NOT recognised; it is deemed to have been truncated.

When I pass the C# string from APL, it works. If I pass yy it doesn't.
Code: Select all
      ⎕cse 'ExecStmt' 'SqlGeography Gatwick2 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")), 4326);' "\u0001\u0001\0\0\0\u001a???\a[??\0??\u007f??I@"
0
      ⎕cse 'ExecStmt' 'SqlGeography Gatwick2 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")), 4326);' yy
¯1
      ⎕cse 'GetLastError'
Message: 24209: Unexpected end of input. Check that the input data is complete and has not been truncated.


1. Is there a way to map yy to how it looks in C#?
2. Is there a way for C# to pass back the string to APL in such a way that there is no loss?
Ajay Askoolum
 
Posts: 885
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: C# to APL to C# Round trip with Unicode string

Postby Ajay Askoolum » July 15th, 2020, 1:21 pm

I think, the round trip would remain a problem because APL+Win is bound to []av (ie. lacks full Unicode support).

For now,my workaround is to convert the byte[] array to a hex string before it reaches APL and to reverse the process when it reaches C# ... unless a better solution exists?
Ajay Askoolum
 
Posts: 885
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: C# to APL to C# Round trip with Unicode string

Postby joe_blaze » July 15th, 2020, 3:55 pm

Hi Ajay:

You might review the documentation of the CSE unicode property in the CSE manual included with the installation of the CSE in APL+Win.
joe_blaze
 
Posts: 393
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: C# to APL to C# Round trip with Unicode string

Postby Ajay Askoolum » July 16th, 2020, 1:50 am

Hi Joe, Thanks for the RTFM reminder; in one way or another, I'm forever getting bitten by that one!
I made some changes, summarised by this function
Code: Select all
    ∇ Trial
[1]   ⍝ @ Ajay Askoolum
[2]
[3]   yyFromCSharp←"\u0001\u0001\0\0\0\u001a???\a[??\0??\u007f??I@" ⍝ pasted with Enable Unicode Clipboard on
[4]   ⎕cse 'unicode' 0
[5]   ⎕cse 'ExecStmt' 'SqlGeography ajayUC0 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' yyFromCShar
      p
[6]
[7]   yyuc0←⎕cse 'GetValue' 'System.Text.Encoding.Unicode.GetString(Gatwick.STAsBinary().Value)'
[8]   yyuc0
[9]   ⎕cse 'ExecStmt' 'SqlGeography ajayUC0 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' yyuc0
[10]
[11]  ⎕cse 'unicode' 1
[12]  ⎕cse 'ExecStmt' 'SqlGeography ajayUC1 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' yyFromCShar
      p
[13]
[14]  yyuc1←⎕cse 'GetValue' 'System.Text.Encoding.Unicode.GetString(Gatwick.STAsBinary().Value)'
[15]
[16]  yyuc1
[17]  ⎕cse 'GetValue' 'yyuc1'
[18]  ⎕cse 'ExecStmt' 'SqlGeography ajayuc1 SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' (⎕cse 'GetVal
      ue' 'yyuc1')
[19]  →0
[20]
[21]  NOTES:
[22]  Line[5] & Line[11] are identical but for variable names AND Line[5] has unicode 0 & Line[11] has unicode 1 ... Line[5] works, Line[11] fails
[23]
[24]  Line[7] & Line[18] ae identical but for variable names  AND Line[7] has unicode 0 & Line [18] adjusts for unicode 1 ... both fail
    ∇
My session output with []trace is
Code: Select all
      Trial
Trial[4] 1
Trial[5] 0
Trial[7] a????+????
Trial[8] a????+????
Trial[9] ¯1
Trial[11] 0
CSE ERROR: An invalid argument was entered
Trial[12] ⎕cse 'ExecStmt' 'SqlGeography ajayUC1 = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' yyFromC
      Sharp
          ^
      →⎕lc+1
Trial[12] →13
Trial[14] <<<UCS Characters>>>
Trial[16] <<<UCS Characters>>>
CSE ERROR: M
Trial[17] ⎕cse 'GetValue' 'yyuc1'
          ^
      →⎕lc+1
CSE ERROR: M
Trial[18] ⎕cse 'ExecStmt' 'SqlGeography ajayuc1 SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(System.Text.Encoding.ASCII.GetBytes("{0}")),4326);' (⎕cse 'Ge
      tValue' 'yyuc1')
I am puzzled by the atypical error CSE ERROR: M. Regardless, I have not overcome the problem and cannot see why Line[12] fails and even more puzzled by the error on Line[17]. I also tried Line[18] with the double quotes wrapping the argument removed with no success. (This is not indicative because both versions of Line[18] are inheriting the error on Line[17])

What am I missing?
Ajay Askoolum
 
Posts: 885
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: C# to APL to C# Round trip with Unicode string

Postby joe_blaze » July 16th, 2020, 1:40 pm

Hi Ajay:

To properly analyze your issue, please simplify it to fundamentals. I do not have SqlGeometry and related tools set up here.

Here is one common error when using value substitution:
⎕cself←'C'⎕cse 'Init' 'System'
⎕cse 'ExecStmt' 'using System;'
0
⎕cse 'ExecStmt' 'using System.Text;'
0
⎕cse 'unicode'
0
⎕cse 'ExecStmt' 'string t = {0};' '"abcd"'
0
⎕cse 'ExecStmt' 'string t = {0};' 'abcd'
¯1
⎕cse 'GetLastError'
Message: (1,12): error CS0103: The name 'abcd' does not exist in the current con
text
StackTrace: at Roslyn.Scripting.CommonScriptEngine.CompilationError(Diagnosti
cBag localDiagnostics, DiagnosticBag diagnostics)

at Roslyn.Scripting.CommonScriptEngine.Compile(String code, String path, Diag
nosticBag diagnostics, Session session, Type delegateType, Type returnType
, CancellationToken cancellationToken, Boolean isInteractive, Boolean isEx
ecute, CommonCompilation& compilation, Delegate& factory)

at Roslyn.Scripting.CommonScriptEngine.Execute[T](String code, String path, D
iagnosticBag diagnostics, Session session, Boolean isInteractive)

at Roslyn.Scripting.Session.Execute(String code)

at APLNext.CSScriptEngine.ScriptHost2.Execute(String script, Boolean isExecSt
mt, Object[] replacementValues) in C:\repos\CSEComponents\SignalR\CSEComms
\APLNext.CSScriptEngine\ScriptHost2.cs:line 316
InnerExceptionMessage:
InnerExceptionStackTrace:
joe_blaze
 
Posts: 393
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: C# to APL to C# Round trip with Unicode string

Postby joe_blaze » July 16th, 2020, 1:44 pm

A value substitution marker, e.g. {0}, within a C# statement to be executed by 'ExecStmt' is considered by the CSE to refer to an APL value outside of the text of that C# statement.

Also check the CSE manual section on 'Value Substitution of APL+Win value' and examples #177 198 206
joe_blaze
 
Posts: 393
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Re: C# to APL to C# Round trip with Unicode string

Postby Ajay Askoolum » July 17th, 2020, 4:48 am

This proved to be too much of a hassle.

I am returning the value as a hex string (one C# expression) and a subroutine in C# to reverse the process when I send it back via CSE.

This gives me added benefits: with a slight modification (simply add the prefix 0x to the return string from two pathways), the values I am returning fit Microsoft SQL Server VARBINARY and GEOGRAPHY data types.

I am turning the return string "\u0001\u0001\0\0\0\u001a???\a[??\0??\u007f??I@" to either 0x0101000000716048C8E88452C04EB35460CC0D4440 (VARBINARY) or 0xE6100000010C4EB35460CC0D4440716048C8E88452C0 (GEOGRAPHY) (or both).

The hex representation of the string is 0101000000716048C8E88452C04EB35460CC0D4440 (no 0x prefix) and that needs to be re-converted when used as an argument from APL+Win.

Strings like 0x0101000000716048C8E88452C04EB35460CC0D4440 can be stored in native files, or APL arrays, or component files (with or without the prefix). (or in SQL Server when the prefix is present).

By way of an explanation of my present objective, I am exploring spatial calculations from APL+Win using https://www.nuget.org/packages/Microsof ... ver.Types/ As this is a .NET assembly (no Interop), I need to use the C# Script Engine.

The complication of nightmare proportions is that APL+Win has its own data types, the NET assembly has its own data types, and C# has its own data types. From APL+Win the round trip of APL+Win to C# via []CSE to the .NET assembly requires very careful and intricate management (they do NOT coerce using explicit casting/conversion) Hence the threads in the forum - with the expectation that someone might have solved the issues already.


This is a session (it might provide more clarification)
Code: Select all
    ∇ APL2000
[1]   ⍝ @ Ajay Askoolum
[2]   CreatePoint ¯74.07670790737194 40.10779956947104 ⍝ This is APL2000's location
[3]   ⎕cse 'ExecStmt' 'var binary = APL2000.Serialize().Value;'
[4]   ⎕cse 'ExecStmt' 'string hexString =  "0x" + BitConverter.ToString(binary).Replace("-", "");'
[5]   ⍝ ⎕cse 'ExecStmt' 'SqlGeography ajay = (SqlGeography) hexString;'
[6]   ⍝ GEOGRAPHY
[7]   ⎕cse 'GetValue' 'hexString' ⍝ Direct assignment in SSMS & can write it to  SQL Server Column Type geography;
[8]   ⎕cse 'ExecStmt' 'var binary2 = (byte[])APL2000.STAsBinary().Value;'
[9]   ⍝ WKB or VARBINARY
[10]  ⎕cse 'ExecStmt' 'string hexString =  "0x" + BitConverter.ToString(binary2).Replace("-", "");'
[11]  ⎕cse 'GetValue' 'hexString' ⍝ Write to SQL Server column type VARBINARY(MAX)
[12]
[13]  ⍝ See ToSQLSServer for more detailed notes
    ∇

      APL2000
0
0
0
0xE6100000010C4EB35460CC0D4440716048C8E88452C0
0
0
0x0101000000716048C8E88452C04EB35460CC0D4440
      ⍝ The 0's are ⎕cse return codes - need to sink them.
Just to complete the picture, compare the results with those in the attached screenshot of a SQL Server session.
Attachments
apl.png
apl.png (266.12 KiB) Viewed 133 times
Ajay Askoolum
 
Posts: 885
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Re: C# to APL to C# Round trip with Unicode string

Postby Ajay Askoolum » July 19th, 2020, 5:05 pm

I attach a PDF providing a glimpse into spatial geography with APL+Win.

If you are sceptical about the scope of spatial geography, remember:

- every lamp post in the world is a point,
- every country is a polygon, as are every city, lake, sea, port, airport,
- if you have a mobile phone, you are creating a linestring every time you move beyond some 15 feet

Have you wondered how online sellers are able to determine the nearest store to your present location?

If you have an interest in this field, a strong opinion on the subject matter, a spatial problem for which you seek a solution or any comments, please express your views in this thread; I am anxious to read your feedback.
Attachments
Overview Spatial Geography from APL+Win.pdf
(422.61 KiB) Downloaded 3 times
Ajay Askoolum
 
Posts: 885
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom


Return to APL+Win & The C# Script Engine

Who is online

Users browsing this forum: No registered users and 1 guest

cron