发布新帖

查找

文章
· 四月 5, 2022 阅读大约需 7 分钟

Using Globals as a graph database to store and retrieve graph structure data

image

Hi Community,

This post is a introduction of my openexchange iris-globals-graphDB application.
In this article I will demonstrate how to save and retrieve Graph Data into InterSystems Globals with the help of Python Flask Web Framework and PYVIS Interactive network visualizations Library

Recommendation

 

Step1 : Establish Connection with IRIS Globals by using python native SDK

 #create and establish connection
  if not self.iris_connection:
         self.iris_connection = irisnative.createConnection("localhost", 1972, "USER", "superuser", "SYS")
                                     
  # Create an iris object
  self.iris_native = irisnative.createIris(self.iris_connection)
  return self.iris_native

 

Step2 : Save data to globals by using  iris_native.set( ) function     

#import nodes data from csv file
isdefined = self.iris_native.isDefined("^g1nodes")
if isdefined == 0:
    with open("/opt/irisapp/misc/g1nodes.csv", newline='') as csvfile:

 reader = csv.DictReader(csvfile)
 for row in reader:
    self.iris_native.set(row["name"], "^g1nodes", row["id"])

 #import edges data from csv file
 isdefined = self.iris_native.isDefined("^g1edges")
 if isdefined == 0:
    with open("/opt/irisapp/misc/g1edges.csv", newline='') as csvfile:
 reader = csv.DictReader(csvfile)
 counter = 0                
 for row in reader:
    counter = counter + 1
    #Save data to globals
    self.iris_native.set(row["source"]+'-'+row["target"], "^g1edges", counter)  

 

Step3: Pass nodes and edges data to PYVIS from globals by using iris_native.get() function

 #Get nodes data for basic graph    
  def get_g1nodes(self):
        iris = self.get_iris_native()
        leverl1_subscript_iter = iris.iterator("^g1nodes")
        result = []
        # Iterate over all nodes forwards
        for level1_subscript, level1_value in leverl1_subscript_iter:
            #Get data from globals
            val = iris.get("^g1nodes",level1_subscript)
            element = {"id": level1_subscript, "label": val, "shape":"circle"}
            result.append(element)            
        return result

    #Get edges data for basic graph  
    def get_g1edges(self):
        iris = self.get_iris_native()
        leverl1_subscript_iter = iris.iterator("^g1edges")
        result = []
        # Iterate over all nodes forwards
        for level1_subscript, level1_value in leverl1_subscript_iter:
            #Get data from globals
            val = iris.get("^g1edges",level1_subscript)
            element = {"from": int(val.rpartition('-')[0]), "to": int(val.rpartition('-')[2])}
            result.append(element)            
        return result

 

Step4: Use PYVIS Javascript to generate graph data

<script type="text/javascript">
    // initialize global variables.
    var edges;
    var nodes;
    var network;
    var container;
    var options, data;
  
    // This method is responsible for drawing the graph, returns the drawn network
    function drawGraph() {
        var container = document.getElementById('mynetwork');
        let node = JSON.parse('{{ nodes | tojson }}');
        let edge = JSON.parse('{{ edges | tojson }}');
     
        // parsing and collecting nodes and edges from the python
        nodes = new vis.DataSet(node);
        edges = new vis.DataSet(edge);

        // adding nodes and edges to the graph
        data = {nodes: nodes, edges: edges};

        var options = {
            "configure": {
                "enabled": true,
                "filter": [
                "physics","nodes"
            ]
            },
            "nodes": {
                "color": {
                  "border": "rgba(233,180,56,1)",
                  "background": "rgba(252,175,41,1)",
                  "highlight": {
                    "border": "rgba(38,137,233,1)",
                    "background": "rgba(40,138,255,1)"
                  },
                  "hover": {
                    "border": "rgba(42,127,233,1)",
                    "background": "rgba(42,126,255,1)"
                 }
                },

                "font": {
                  "color": "rgba(255,255,255,1)"
                }
              },
            "edges": {
                "color": {
                    "inherit": true
                },
                "smooth": {
                    "enabled": false,
                    "type": "continuous"
                }
            },
            "interaction": {
                "dragNodes": true,
                "hideEdgesOnDrag": false,
                "hideNodesOnDrag": false,
                "navigationButtons": true,
                "hover": true
            },

            "physics": {
                "barnesHut": {
                    "avoidOverlap": 0,
                    "centralGravity": 0.3,
                    "damping": 0.09,
                    "gravitationalConstant": -80000,
                    "springConstant": 0.001,
                    "springLength": 250
                },

                "enabled": true,
                "stabilization": {
                    "enabled": true,
                    "fit": true,
                    "iterations": 1000,
                    "onlyDynamicEdges": false,
                    "updateInterval": 50
                }
            }
        }
        // if this network requires displaying the configure window,
        // put it in its div
        options.configure["container"] = document.getElementById("config");
        network = new vis.Network(container, data, options);
        return network;
    }
    drawGraph();
</script>

 

Step5: Calling above codes from app.py main file

#Mian route. (index)
@app.route("/")
def index():
    #Establish connection and import data to globals
    irisglobal = IRISGLOBAL()
    irisglobal.import_g1_nodes_edges()
    irisglobal.import_g2_nodes_edges()

    #getting nodes data from globals
    nodes = irisglobal.get_g1nodes()
    #getting edges data from globals
    edges = irisglobal.get_g1edges()

    #To display graph with configuration
    pyvis = True
    return render_template('index.html', nodes = nodes,edges=edges,pyvis=pyvis)    


Thanks 

2 Comments
讨论 (2)1
登录或注册以继续
文章
· 三月 29, 2022 阅读大约需 1 分钟

プログラムでCSVファイルを1行ずつ読み込む方法

これは、InterSystems FAQサイトの記事です。
 

%SQL.Util.Proceduresクラスの CSV() プロシジャを使用することにより、実現できます。
下記が使用例のコードとなります。(test.csvというファイルが c:\temp にあるという前提)

 Set rowtype="Name VARCHAR(50),UID VARCHAR(50), PHONE VARCHAR(50)"
 Set filename="c:\temp\test.csv"
 Set result=##class(%SQL.Statement).%ExecDirect(,"call %SQL_Util.CSV(,?,?)",.rowtype,filename)
 Set rset =result.%NextResult()
 
 // 全件表示したい場合は、do rset.%Display()
 While rset.%Next() {
     Write "Name:",rset.%GetData(1)," UID:",rset.%GetData(2)," PHONE:",rset.%GetData(3),!
     }

 Set rset="",result=""
 Quit

 

上記を実行することにより結果セットとして各行およびフィールドにアクセスできます。
実行例:

USER>do ^CSV
Name    UID     PHONE
山田,太郎 0001    080-1111-1111
bbb     0003    090-2222-2222
ccc     0009    "050-3333-3333"
ddd     0010    0120-17-1972
4 Rows(s) Affected

 

 %SQL.Util.Proceduresクラスのクラスリファレンスも併せてご確認ください。
クラスリファレンス:%SQL.Util.Procedures.CSV()

3 Comments
讨论 (3)1
登录或注册以继续
文章
· 三月 27, 2022 阅读大约需 2 分钟

.Net Gateway(オブジェクトゲートウェイ)で外部DLLを使用する方法

これはInterSystems FAQ サイトの記事です。

  • まず予め Visual Studioを使用して、呼び出したい内容のクラスライブラリを作成します。
  • そのプロジェクトをコンパイルし、DLLを作成します。
  • 管理ポータルでオブジェクトゲートウェイを作成します。   システム管理 > 構成 > 接続性 > オブジェクトゲートウェイ ゲートウェイ名 : testGateway サーバ名 / IPアドレス : 127.0.0.1 ポート : 55000 (後はデフォルト)  
  • testGateway を開始します。


※管理ポータルから、もしくはコマンドから開始できます。 

do ##class(%Net.Remote.Service).StartGateway(gateway.Name)
  •   以下のいずれかの方法でDLLを呼び出すプロキシクラスを作成します。

       a. コマンドで作成する場合 

     set dllPath="C:\temp\TestDll.dll
     do ##class(%Net.Remote.Gateway).%ExpressImport(dllPath, 55000, "127.0.0.1")

       b. スタジオで作成する場合


 [ツール] - [アドイン] - [.Net ゲートウェイウィザード] ⑥ 以下のような手順でコードを実行します。

例: 

  Set gateway=##class(%Net.Remote.Gateway).%New()
   set host="localhost"
   set port=55000
   set namespace="USER"
   set timeout=2
   Set classPath=##class(%ListOfDataTypes).%New()
   Set samplePath="C:\temp\TestDll.dll"
   Do classPath.Insert(samplePath)
  Set status=gateway.%Connect(host,port,namespace,timeout,classPath)
   write !,"status: ",status
   set cl=##class(TestDll.MyDllClass).%New(gateway)
   do cl.test(gateway)

 

最後の行の do cl.test(..) は、VisualStudioで作成したクラスライブラリの関数になります。

 

呼び出し引数は、作成したプロキシクラスに従ってください。

讨论 (0)1
登录或注册以继续
文章
· 三月 24, 2022 阅读大约需 3 分钟

GlobalToJSON-XL-Academic

This package offers a utility to export an XLarge Global into a JSON object file and to show
or import it again. In a previous example, this all was processed in memory. But if this is a
large Global you may either experience <MAXSTRING> or an <STORE>  error
if the generated JSON structure exceeds available memory.
 


Academic refers to the structure created.

  • each node of the Global including the top node is represented as a JSON object
  • {"node":<node name>,"val":<value stored>,"sub":[<JSON array of subscript objects>]}
  • value and subscript are optional but one of them always exists for a valid node
  • the JSON object for the lowest level subscript has only value but no further subscript.

So this is basically a 1:1 image of your global and it's exported to a file (default: gbl.json)
In addition to the export, a show method displays the generated file.
The tricky part is the import from file. It is a customize JSON parser as all others just
operate in memory. this fails with a reasonable-sized Global
(eg. ^oddDEF with ~ 1.7 million nodes takes ~ 78MB JSON file.)

Prerequisites

Make sure you have git and Docker desktop installed.

Installation

Clone/git pull the repo into any local directory

git clone https://github.com/rcemper/GlobalToJSON-XLA.git    

Run the IRIS container with your project:

docker-compose up -d --build

How to Test it

This is the pre-loaded Global ^dc.MultiD for testing.
 

There are 3 methods available

  • ClassMethod export(gref As %String = "^%", file = "gbl.json") As %String file = 0 >>> display to terminal
  • ClassMethod show(file = "gbl.json") As %String
  • ClassMethod import( file = "gbl.json", test = 0) As %String test = 1 >>> load into a PPG

Open IRIS terminal

$ docker-compose exec iris iris session iris

USER>write ##class(dc.GblToJSON.XLA).export("^dc.MultiD")
File gbl.json created

USER>write ##class(dc.GblToJSON.XLA).export("^dc.MultiD",0)
{"node":"^dc.MultiD"
,"val":5
,"sub":[
{"node":1
,"val":"$lb(\"Braam,Ted Q.\",51353)"
,"sub":[
{"node":"mJSON"
,"val":"{}"
}
---  truncated ---

USER>>write ##class(dc.GblToJSON.XLA).show()
{"node":"^dc.MultiD"
,"val":5
,"sub":[
{"node":1
,"val":"$lb(\"Braam,Ted Q.\",51353)"
---  truncated ---  

validated JSON object

Now we want to verify the load function as a test into a PPG

USER>write ##class(dc.GblToJSON.XLA).import(,1)
Global ^||dc.MultiD loaded

USER>zwrite ^||dc.MultiD
^||dc.MultiD=5
^||dc.MultiD(1)=$lb("Braam,Ted Q.",51353)
^||dc.MultiD(1,"mJSON")="{}"
^||dc.MultiD(2)=$lb("Klingman,Uma C.",62459)
^||dc.MultiD(2,2,"Multi","a")=1
^||dc.MultiD(2,2,"Multi","rob",1)="rcc"
^||dc.MultiD(2,2,"Multi","rob",2)=2222
^||dc.MultiD(2,"Multi","a")=1
^||dc.MultiD(2,"Multi","rob",1)="rcc"
^||dc.MultiD(2,"Multi","rob",2)=2222
^||dc.MultiD(2,"mJSON")="{""A"":""ahahah"",""Rob"":""VIP"",""Rob2"":1111,""Rob3"":true}"
^||dc.MultiD(3)=$lb("Goldman,Kenny H.",45831)
^||dc.MultiD(3,"mJSON")="{}"
^||dc.MultiD(4)=$lb("","")
^||dc.MultiD(4,"mJSON")="{""rcc"":122}"
^||dc.MultiD(5)=$lb("","")
^||dc.MultiD(5,"mJSON")="{}"
 
USER>

q.a.d.

Code Quality

CodeQuality

Do not wonder about some strange code constructs.
They were required as CodeQuality neither understands the NEW command, nor the scope of %variables  !   sad

Video

Online Demo Terminal
Online Demo SMP

Previous article in DC

GitHub

1 Comment
讨论 (1)1
登录或注册以继续
公告
· 三月 23, 2022

Spend a day in a Future City with Global Masters!

Hi Developers!

Let's take a wild ride through the futuristic cities of tomorrow, and the technological advancements they boast! 📡

We invite you to have a journey to the Future City where you can dream of the future, check your knowledge about our Developer Community, and also imagine what the InterSystems Technology will be like. 

So get your devices ready—gear up that self-driving car 🚀, 3D-print some food 🍿, and collect some bonus points! 💰

  

讨论 (0)1
登录或注册以继续