FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log inLog in 


json doc

Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog
View previous topic :: View next topic  
Author Message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 8 Feb 2017 11:05    Post subject: json doc Reply with quote

without any experience with the json example which i have downloaded, i tried to load some
data into a format i can put into a json doc using the following code:

domains
xav_PairLst    = xav_pairs*.
xav_pairs      = xav_Pair*.
xav_Pair        = av(col_name,data_type).
data_type      = string;real;xav_pairs;xav_PairLst.

and in program...


                   XAV_Pairs1 =
                                         [av("EntityUID","808b259b-a996-4649-9cdb-21e4cd5fe654"),
                                         av("Addresses",[av("InstanceName", "Home Address"),
                                                                  av("Zip", "30135"),
                                                                  av("State", "GA"),
                                                                  av("Country","USA"),
                                                               av("Address1", "6513 Osceola Way")
                                       ]),
                                          av("Description","Demographic Profile"),
                                          av("OwnerUID","115f1838-6cc4-40d5-bcf2-5fb19fd27fe0"),
                                          av("SpecificTypeID", "d8347260-f601-4ba7-a1d8-7da53fc965b4"),

                                          av("Identifications",[[av("InstanceName","Test Account"),
                                                                     av("IdentificationKey","Membership"),
                                                                         av("IdentificationValue", "1232445-234")
                                                                      ],[av("InstanceName", "Individual"),
                                                                       av("IdentificationKey", "Individual"),
                                                                          av("IdentificationValue", "0001")
                                                                      ]]),
                                          av("ReleaseStatusID",0),
                                          av("ConceptInstanceUID","c7033682-0a4d-4e49-b5d1-574369ca4865"),
                                          av("EffectiveDate","Feb 2, 2017 10:04:26 AM"),

                                          av("Name",[av("InstanceName","EntityName"),
                                                            av("FirstName","Michael"),
                                                            av("FullName","Michael W. Saxon"),
                                                            av("LastName","Saxon"),
                                                            av("MiddleName","W.")
                                                           ])
                                        ] ,
    XAV_Pairs5 =
                        [av("EntityUID","808b259b-a996-4649-9cdb-21e4cd5fe654"),
                      av("Description","Employee Profile"),
                      av("OwnerUID","08830664-0647-48ab-b0cf-0949a6b14713"),
                      av("SpecificTypeID","b750a104-dfe9-43f4-ae1d-73241a58a4bc"),
                      av("ReleaseStatusID",0),
                         av("EmployeeID","004"),
                      av("ConceptInstanceUID","a7d03848-1282-4b44-95c1-5c52ce9512b8"),
                      av("EffectiveDate","Feb 2, 2017 10:04:26 AM")
                  ],
        Rest5 = [av("attributes",
                       [av("EntityUID", "808b259b-a996-4649-9cdb-21e4cd5fe654"),
                     av("Description","Patient Profile"),
                    av("OwnerUID","691621e7-e25d-4bbf-b22a-b8eabf93de7e"),
                    av("SpecificTypeID","4ea1b8cf-3b38-4e08-bd57-4995c938616e"),
                    av("ReleaseStatusID", 0),
                    av("PatientID","sdlgjdlgldjf"),
                     av("ConceptInstanceUID","ada105e0-33dc-4722-aff9-7d9f2e61b49c"),
                      av("EffectiveDate","Feb 2, 2017 10:04:26 AM")
                 ])],

            Rest = [av("attributes",XAV_Pairs5)|Rest5],

  _Doc = [av("abstractions", [ av("attributes",[XAV_Pairs1|Rest])])].

the above line is supposed to make a result that packs those av lists together.
the first av at the top has a string instead of another av pair. i was hoping the domain would translate it but i am missing something. i tried both string and text(string) with no luck.

i will look hard at the rpc example because this is for loading a doc from a service using jdoc
but i know av(col_name,data_value) does use data_value for both text and numbers so i attempted to do the same with this.
i get either error c504 : The expression has type '::string', which is incompatible with the type 'main::data_type'
or
The expression has type 'A$*', which is incompatible with the type 'main::data_type' when using
text(string)


by the way, thanks for the lead of the rpc/json service example, it looks good. if it eliminates
the need for the above approach l will scrap it and just dive in to that. i was trying to define a
list structure that would make reaching down into the doc easily.

AEH

_________________
aeh
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 9 Feb 2017 13:13    Post subject: Reply with quote

Your data_type domain is not what you think it is.

You have defined 4 functors, not something that is the union of 4 domains.

That kind of union does not exist in Visual Prolog.

You should write something like this instead:

domains
    data_type =
         string(string String);
         real(real Real);
         xav_pairs(xav_pairs Xav_pairs);
         xav_PairLst(xav_PairLst Xav_PairLst).

However from json DOM, I believe you have discovered the PFC JSON support. And I belive it will be much faster for you to use that than to rely on your own code (we have already made the work).

_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 15 Feb 2017 23:06    Post subject: json dom Reply with quote

i would like you to build me a handler for json-DOM-visual prolog. i will have to replace the sql statements in my interpreter with references to my 5 data structures.
eg:profiles,rates,stats,codes,accum.
so DOC=profiles(entity,uid) could return a document with that data. creating a data access layer would make the database transparent to the user and let them focus on the content of those 5 structures. the av pairs lst i tried to define was an attempt to map a document into lists of key value pairs for those structures. now it seems like i need to just use the 7 data types of json as u suggested.

can you estimate time and cost for a handler that would be comparable to the java-json-dom capability ?

thanks

AEH[/img]

_________________
aeh
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 16 Feb 2017 12:23    Post subject: Reply with quote

I do not completely understand what you write.

However it is quite simple to build a JSON structure from and ODBC query.

For example like here (this code is not completely vip 7.5 compatible, but it should not be a big problem to update it):

class predicates
    select : jsonProc_name.
clauses
    select(OdbcRpcContext, ArgMap) = Result :-
        Age = ArgMap:get_real("age"),
        Stmt = OdbcRpcContext:odbcConnection:execDirect("select Name, Age from PersonStr where Age > ?", [real(Age)]),
        Result = fetch_json(Stmt).

class predicates
    fetch_json : (odbcStatement Stmt) -> json::jsonValue Result.
clauses
    fetch_json(Stmt) = a(Result) :-
        NumColumns = Stmt:numResultCols(),
        Result =
            [ o(R) ||
                Stmt:fetch_nd(),
                R = jsonObject::new(),
                foreach Column = std::fromTo(1, NumColumns) do
                    Name = Stmt:getColumnName(Column),
                    Value = Stmt:getColumnValue(Column),
                    JSON = fromValue(Value),
                    R:set(Name, JSON)
                end foreach
            ],
        Stmt:free().

class predicates
    fromValue : (value Value) -> json::jsonValue JsonValue.
clauses
    %!!
    fromValue(none) = json::n.
    fromValue(boolean(false)) = json::f.
    fromValue(boolean(true)) = json::t.
    fromValue(unsigned64(Value)) = json::r(Value).
    fromValue(integer64(Value)) = json::r(Value).
    fromValue(unsigned(Value)) = json::r(Value).
    fromValue(integer(Value)) = json::r(Value).
    fromValue(real(Value)) = json::r(Value).
    fromValue(char(Value)) = json::s(string::charToString(Value)).
    fromValue(string(Value)) = json::s(Value).
    fromValue(string8(Value)) = json::s(string8::fromUtf8(Value)).
    fromValue(binary(Value)) = json::s(cryptography::base64_encode(Value)).
    fromValue(gmtTimeValue(Value)) = json::s(string::present(Value)).
    fromValue(localTimeValue(Value)) = json::s(string::present(Value)).
    fromValue(Value) = _ :-
        binaryNonAtomic(_) = Value,
        cannotConvert(Value).
    fromValue(Value) = _ :-
        object(_) = Value,
        cannotConvert(Value).
    fromValue(Value) = _ :-
        pointer(_) = Value,
        cannotConvert(Value).
    fromValue(Value) = _ :-
        handle(_) = Value,
        cannotConvert(Value).

class predicates
    cannotConvert : (value Value) erroneous.
clauses
    cannotConvert(Value) :-
        exception::raise_errorf("Cannot convert % to a JSON value", Value).


_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 16 Feb 2017 18:26    Post subject: json Reply with quote

let me take a shot with that. excellent. thank you.
_________________
aeh
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 21 Feb 2017 15:36    Post subject: json to and from doc Reply with quote

i see that code is putting all types into json types but i havent got it to compile.

what i need though is the mechanism for taking a nosql document and be able to get and put the
values of that document by position and name.

an example would be to get the address or any other attribute i might need. so the request and response need to be read or written by the entries of a document. i tried to use av because it was already in my project to populate forms but i really need to follow a standard like the java json
dom uses to get the values in string,value arrays and objects. so, addresses is an entry in profile which is one of my 5 data structures from which i know what to ask for either by position or name
from the following document....


{

"requestUUID": "05aa84a8-9e70-4b80-a7da-c95e700ffcb5",
"responseUUID": "b0c83dc0-3d63-417c-a23a-88cf8d5eda51",
"abstractions": [{
"attributes": {
"EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",
"Addresses": [{
"InstanceName": "Home Address",
"Zip": "30135",
"State": "GA",
"Country": "USA",
"Address1": "6513 Osceola Way",
"City": "Douglasville"
}
],
"Description": "Demographic Profile",
"OwnerUID": "115f1838-6cc4-40d5-bcf2-5fb19fd27fe0",
"SpecificTypeID": "d8347260-f601-4ba7-a1d8-7da53fc965b4",
"Identifications": [{
"InstanceName": "Test Account",
"IdentificationKey": "Membership",
"IdentificationValue": "1232445-234"
}, {
"InstanceName": "Individual",
"IdentificationKey": "Individual",
"IdentificationValue": "0001"
}
],
"ReleaseStatusID": 0,
"ConceptInstanceUID": "c7033682-0a4d-4e49-b5d1-574369ca4865",
"EffectiveDate": "Feb 2, 2017 10:04:26 AM",
"Name": {
"InstanceName": "EntityName",
"FirstName": "Michael",
"FullName": "Michael W. Saxon",
"LastName": "Saxon",
"MiddleName": "W."
}
}
}, {
"attributes": {
"EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",
"Description": "Employee Profile",
"OwnerUID": "08830664-0647-48ab-b0cf-0949a6b14713",
"SpecificTypeID": "b750a104-dfe9-43f4-ae1d-73241a58a4bc",
"ReleaseStatusID": 0,
"EmployeeID": "004",
"ConceptInstanceUID": "a7d03848-1282-4b44-95c1-5c52ce9512b8",
"EffectiveDate": "Feb 2, 2017 10:04:26 AM"
}
}, {
"attributes": {
"EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",
"Description": "Patient Profile",
"OwnerUID": "691621e7-e25d-4bbf-b22a-b8eabf93de7e",
"PatientID": "sdlgjdlgldjf",
"SpecificTypeID": "4ea1b8cf-3b38-4e08-bd57-4995c938616e",
"ReleaseStatusID": 0,
"ConceptInstanceUID": "ada105e0-33dc-4722-aff9-7d9f2e61b49c",
"EffectiveDate": "Feb 2, 2017 10:04:26 AM"
}
}
]
}


i have seen the java json dom pull any value from that doc by name or position and i need
the same thing in prolog. does this request make sense yet? if possible, can you estimate
the time and cost to build this?

i have included the java dom example.

_________________
aeh
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 22 Feb 2017 10:07    Post subject: Reply with quote

I am not quite sure what you are looking for (which is not already there).

Perhaps you just need to realize how it works, maybe this may help:

clauses
    run() :-
        % Parse the "document" in to a json::jsonValue
        DocValue = json::fromString(doc),
        out("doc", DocValue),
        % The result is a JSON object so we extract the object
        json::o(DocObject) == DocValue,
        % Get the "abstractions" from the object
        AbstractionsValue = DocObject:get("abstractions"),
        out("AbstractionsValue", AbstractionsValue),
        %  The result is a JSON Array so we extract the list of json::jsonValue's
        json::a(AbstractionValueList) == AbstractionsValue,
        % For the exampel we use the second json::jsonValue in the list
        [_, AbstractionValue2 | _] == AbstractionValueList,
        out("AbstractionValue2", AbstractionValue2),
        % It is a JSON object so we extract the object
        json::o(AbstractionObject2) == AbstractionValue2,
        % Get the "attributes" from the object
        AttributesValue2 = AbstractionObject2:get("attributes"),
        out("AttributesValue2", AttributesValue2),
        % The attributes is a JSON object, so we extract the object
        json::o(AttributesObject2) == AttributesValue2,
        % Get the "Description" from the object
        DescroptionValue2 = AttributesObject2:get("Description"),
        out("DescroptionValue2", DescroptionValue2),
        % The description is a JSON string, so we extract the string
        json::s(DescriptionString2) == DescroptionValue2,
        stdio::writef("The description is: %\n", DescriptionString2).

class predicates
    out : (string Label, json::jsonValue Value).
clauses
    out(Label, Value) :-
        O = stdio::outputStream,
        O:write(Label, "\n"),
        jsonPrettyPrint::ppValue(O, Value),
        O:write("\n\n").

constants
    doc =
        @<{>
        @<   "requestUUID": "05aa84a8-9e70-4b80-a7da-c95e700ffcb5",>
        @<   "responseUUID": "b0c83dc0-3d63-417c-a23a-88cf8d5eda51",>
        @<   "abstractions": [{>
        @<         "attributes": {>
        @<            "EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",>
        @<            "Addresses": [{>
        @<                  "InstanceName": "Home Address",>
        @<                  "Zip": "30135",>
        @<                  "State": "GA",>
        @<                        "Country": "USA",>
        @<                  "Address1": "6513 Osceola Way",>
        @<                  "City": "Douglasville">
        @<               }>
        @<            ],>
        @<            "Description": "Demographic Profile",>
        @<            "OwnerUID": "115f1838-6cc4-40d5-bcf2-5fb19fd27fe0",>
        @<            "SpecificTypeID": "d8347260-f601-4ba7-a1d8-7da53fc965b4",>
        @<            "Identifications": [{>
        @<                  "InstanceName": "Test Account",>
        @<                  "IdentificationKey": "Membership",>
        @<                  "IdentificationValue": "1232445-234">
        @<               }, {>
        @<                  "InstanceName": "Individual",>
        @<                  "IdentificationKey": "Individual",>
        @<                  "IdentificationValue": "0001">
        @<               }>
        @<            ],>
        @<            "ReleaseStatusID": 0,>
        @<            "ConceptInstanceUID": "c7033682-0a4d-4e49-b5d1-574369ca4865",>
        @<            "EffectiveDate": "Feb 2, 2017 10:04:26 AM",>
        @<            "Name": {>
        @<               "InstanceName": "EntityName",>
        @<               "FirstName": "Michael",>
        @<               "FullName": "Michael W. Saxon",>
        @<               "LastName": "Saxon",>
        @<               "MiddleName": "W.">
        @<            }>
        @<         }>
        @<      }, {>
        @<         "attributes": {>
        @<            "EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",>
        @<            "Description": "Employee Profile",>
        @<            "OwnerUID": "08830664-0647-48ab-b0cf-0949a6b14713",>
        @<            "SpecificTypeID": "b750a104-dfe9-43f4-ae1d-73241a58a4bc",>
        @<            "ReleaseStatusID": 0,>
        @<            "EmployeeID": "004",>
        @<            "ConceptInstanceUID": "a7d03848-1282-4b44-95c1-5c52ce9512b8",>
        @<            "EffectiveDate": "Feb 2, 2017 10:04:26 AM">
        @<         }>
        @<      }, {>
        @<         "attributes": {>
        @<            "EntityUID": "808b259b-a996-4649-9cdb-21e4cd5fe654",>
        @<            "Description": "Patient Profile",>
        @<            "OwnerUID": "691621e7-e25d-4bbf-b22a-b8eabf93de7e",>
        @<            "PatientID": "sdlgjdlgldjf",>
        @<            "SpecificTypeID": "4ea1b8cf-3b38-4e08-bd57-4995c938616e",>
        @<            "ReleaseStatusID": 0,>
        @<            "ConceptInstanceUID": "ada105e0-33dc-4722-aff9-7d9f2e61b49c",>
        @<            "EffectiveDate": "Feb 2, 2017 10:04:26 AM">
        @<         }>
        @<      }>
        @<   ]>
        @<}>.

(The syntax coloring of the doc string is not correct here in the forum).

_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 22 Feb 2017 10:17    Post subject: Reply with quote

Notice that there are short-cuts on jsonObject's that look-up and extract in a single operation:

clauses
    run() :-
        % Parse the "document" in to a json::jsonValue
        DocValue = json::fromString(doc),
        out("doc", DocValue),
        % The result is a JSON object so we extract the object
        json::o(DocObject) == DocValue,
        % Get the "abstractions" from the object
        AbstractionValueList = DocObject:get_array("abstractions"),
        % For the exampel we use the second json::jsonValue in the list
        [_, AbstractionValue2 | _] == AbstractionValueList,
        out("AbstractionValue2", AbstractionValue2),
        % It is a JSON object so we extract the object
        json::o(AbstractionObject2) == AbstractionValue2,
        % Get the "attributes" from the object
        AttributesObject2 = AbstractionObject2:get_object("attributes"),
        % Get the "Description" from the object
        DescriptionString2 = AttributesObject2:get_string("Description"),
        stdio::writef("The description is: %\n", DescriptionString2).

I.e. get_array, get_object, get_string , etc.

_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 22 Feb 2017 21:56    Post subject: json to and from doc Reply with quote

that was in fact the problem. thank you so much for taking your time to explain it. i am on the move again!!!
_________________
aeh
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 23 Feb 2017 10:12    Post subject: Reply with quote

Excellent Very Happy
_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 23 Feb 2017 23:03    Post subject: parse dom Reply with quote

i am rolling now. the only other point is has anyone parsed a syntax statement like the java dom does?

eg:

requestSyntax = "json.abstractions[0].attributes.Identifications[IdentificationKey=Individual].IdentificationValue";


thanks. you have given me a means to write my piece of a real dynamic programming platform.
i will keep you posted.

_________________
aeh


Last edited by augustinharrison on 24 Feb 2017 19:55; edited 1 time in total
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 24 Feb 2017 9:47    Post subject: Reply with quote

We have not made such a system. In our way of using JSON is it not very relevant. We don't use JSON as a way of dealing with data (we use normal Visual Prolog for that) we only use JSON as a serialization format, in line with XML, plain Visual Prolog term read/write , etc.

I can suggest that you create a term based language for it. Something like:

domains
    jsonPath = jsonSelector*.

domains
    jsonSelector =
        a(string Name); % attribute
        i(positive Index); % index
        s(string Attribute, string Value). % select first match

Your example:

requestSyntax = "json.abstractions[0].attributes.Identifications[IdentificationKey=Individual].IdentificationValue";

Will correspond to:

    Path = [a("abstractions"), i(0), a("attributes"), a("Identifications"), s("IdentificationKey", "Individual"), a("IdentificationValue")],
    RequestSyntax = trySelect(Json, Path),

Once you have that it will also be "quite easy" to create a parser for the "string" version of the language using the parser generator, and let that transform into your functor language.

So you can write this instead:

     RequestSyntax = trySelect(Json, "abstractions[0].attributes.Identifications[IdentificationKey=Individual].IdentificationValue"),


_________________
Regards Thomas Linder Puls
Prolog Development Center
Back to top
View user's profile Send private message
augustinharrison




Joined: 28 Jul 2010
Posts: 27

PostPosted: 24 Feb 2017 19:34    Post subject: perfect Reply with quote

once again, that is exactly what i needed. thank you so much.

AEH

_________________
aeh
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum