Determine datatype of tag property

I’m trying to determine the datatype of a custom property on a tag between an array or a dataset. Some sample script is below. I can’t seem to figure out how to determine the ignition datatypes… (ta and tb are tags with a custom property called “States”. ta is an array datatype, tb is dataset)

Is there something I should be importing into the scripting?

sample script:
+++++++++++++++++++++++++++++++++++++++++

aa = {“a” : 1, “b” : 2}
ab = [‘a’,‘b’]

ta = system.tag.readBlocking(["[edge]SampleData/StateTestArray.States"])[0].value
tb = system.tag.readBlocking(["[edge]SampleData/StateTestDataset.States"])[0].value

print “aa :”, type(aa)
if type(aa) is dict : print ‘aa type is : dict’
print “—”

print “ab :”, type(ab)
if type(ab) is list : print ‘ab type is : list’
print “—”

#####################

print “ta:”, type(ta)
#if type(ta) is array.array : print ‘found type : array’
print “—”

print “tb:”,type(tb)
#if type(tb) is com.inductiveautomation.ignition.common.BasicDataset : print ‘found type : dataset’
print “—”

+++++++++++++++++++++++++++++++
output is:

aa : <type ‘dict’>
aa type is : dict

ab : <type ‘list’>
ab type is : list

ta: <type ‘array.array’>

tb: <type ‘com.inductiveautomation.ignition.common.BasicDataset’>

Like this:

from org.python.core import PyArray
from com.inductiveautomation.ignition.common import BasicDataset

##ARRAY
arrayProp = system.tag.readBlocking(["[default]tag.ta"])[0].value
if isinstance(arrayProp, PyArray):
	print 'isArray'
	
##DATASET
datasetProp = system.tag.readBlocking(["[default]tag.tb"])[0].value
if isinstance(datasetProp, BasicDataset):
	print 'isDataset'
1 Like

Jython doesn’t competely describe all java types, particularly arrays and lists, since it runs all types through its own system before apply pure java to the outliers. (The type() function is jython, not java.) In java, you normally use an object’s .getClass() method to inspect what it really is, and that normally works when jython’s type() function is insufficient. Unfortunately, jython’s access wrapper suppresses this method on java arrays, presumably as part of support for iterating over them like normal python sequence types.

It so happens I was playing with this problem yesterday, and worked out how to get the java class of arrays. For your edification (paste in the script console’s multiline buffer and execute):

from java.lang import Object
import jarray

getClassM = Object().class.getMethod('getClass', [])

def probe(someObject):
	t = type(someObject)
	try:
		c0 = someObject.getClass()
	except:
		c0 = None
	c1 = getClassM.invoke(someObject, [])
	try:
		if c1.isArray():
			return t, c0, c1, c1.getComponentType()
	except:
		pass
	return t, c0, c1

byteArray = jarray.zeros(4, 'b')
print repr(byteArray)
print probe(byteArray)

intArray = jarray.zeros(4, 'i')
print repr(intArray)
print probe(intArray)

ds = system.dataset.toDataSet(['Name', 'Value'], [['Something', 50]])
print repr(ds)
print probe(ds)

print 0.4, probe(0.4)
print 125, probe(125)
print 12345678901234L, probe(12345678901234L)
print {}, probe({})
print [], probe([])

Poke around in the public javadoc for java and Ignition for more information.

{ Also, please edit your post so it is more readable. Highly all the code you posted and click the “preformatted text” button at the top of the editor. This one: </>. }

1 Like

Impresive :face_with_monocle:

1 Like