Skip to content

Commit cb97109

Browse files
committed
dn resource scenarios 1-4 now working. Changes to _rm/, _typesystem,/resource.py to implement resources creating artifact, linking, assigning properties
1 parent 055db6a commit cb97109

File tree

9 files changed

+126
-94
lines changed

9 files changed

+126
-94
lines changed

elmclient/_project.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ def resolve_property_name_to_uri(self, name, shapeuri=None, exception_if_not_fou
302302
# the context is the attribute definition - needed to be specified ultimately by the user when enumeration value names aren't unique
303303
def resolve_enum_name_to_uri(self, name, propertyuri=None, exception_if_not_found=True):
304304
logger.info( f"resolve_enum_name_to_uri {name=} {propertyuri=}" )
305+
# print( f"resolve_enum_name_to_uri {name=} {propertyuri=}" )
305306
result = self.get_enum_id(name,propertyuri)
306307
logger.info( f"resolve_enum_name_to_uri {name=} {propertyuri=} {result=}" )
308+
# print( f"resolve_enum_name_to_uri {name=} {propertyuri=} {result=}" )
307309
return result
308310

309311
# for OSLC query, given a type URI, return its name

elmclient/_queryparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,13 @@ def term(self, s):
270270
else:
271271
t1 = type(value)
272272
logger.info( f"t1 {value} {t1=}" )
273+
# print( f"t1 {value} {t1=}" )
273274
if isinstance(value, str) and not value.startswith('"') and not value.startswith("'") and ':' not in value and not re.match(r"\d",value):
274275
# this is a valueidentifier - try to resolve it as an enum in the context of identifier
275276
if self.resolverobject.resolve_enum_name_to_uri is not None:
277+
# print( f"{self.resolverobject.resolve_enum_name_to_uri(value, identifier)=}" )
278+
# print( f"{self.resolverobject=}" )
279+
# print( f"{self.resolverobject.resolve_enum_name_to_uri=}" )
276280
result = self.resolverobject.resolve_enum_name_to_uri(value, identifier)
277281
if result is None:
278282
raise Exception(f"Single ref {value} not resolved in context {identifier}")
@@ -281,6 +285,7 @@ def term(self, s):
281285
else:
282286
s[2] = '"'+result+'"'
283287
logger.info( f"Term returning {s}" )
288+
# print( f"Term returning {s}" )
284289
return s
285290

286291
def simpleidentifier(self,s):

elmclient/_rm.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,37 +1079,36 @@ def _load_type_from_resource_shape(self, el, supershape=None):
10791079
if property_title_x is None:
10801080
burp
10811081
property_title = property_title_x.text
1082-
print( f"Loading prop {property_title}" )
1082+
# print( f"Loading prop {property_title}" )
10831083
# propuri = rdfxml.xml_find_element( el, 'oslc:propertyDefinition').get( "{%s}resource" % rdfxml.RDF_DEFAULT_PREFIX["rdf"])
10841084
propuri = rdfxml.xmlrdf_get_resource_uri( el, 'oslc:propertyDefinition')
1085-
print( f"{propuri=}" )
1085+
# print( f"{propuri=}" )
10861086
# prefer range over valueType (doesn't nmatter for rm but does for qm which has both in a property definition)
10871087
proptype = rdfxml.xmlrdf_get_resource_uri( el,'oslc:range' )
10881088
if proptype is None:
10891089
proptype = rdfxml.xmlrdf_get_resource_uri(el,'oslc:valueType' )
1090-
print( f"{proptype=}" )
1090+
# print( f"{proptype=}" )
10911091
# lookup the codec, if there is one
10921092
propcodec = valueTypeToCodec.get( proptype )
1093-
print( f"{propcodec=}" )
1093+
# print( f"{propcodec=}" )
10941094

10951095
# work out if multivalued
10961096
mvtext_x = rdfxml.xmlrdf_get_resource_uri( el, "oslc:occurs" )
10971097
isMultiValued = mvtext_x=="http://open-services.net/ns/core#Zero-or-many"
10981098
# print( f"{property_title} {isMultiValued=} {mvtext_x=}" )
10991099

1100-
self.register_property( property_title, propuri, isMultiValued=isMultiValued, shape_uri=uri, typeCodec=propcodec )
1101-
11021100
if self.is_known_property_uri( propuri ):
11031101
logger.debug( f"ALREADY KNOWN" )
1104-
print( f"ALREADY KNOWN" )
1102+
# print( f"ALREADY KNOWN" )
11051103
continue
11061104

1105+
self.register_property( property_title, propuri, isMultiValued=isMultiValued, shape_uri=uri, typeCodec=propcodec )
11071106

11081107
# get the property definition
11091108
# all links have a Reference oslc:representation
11101109
# true links have <oslc:range rdf:resource="http://open-services.net/ns/core#Resource"/>
11111110
if rdfxml.xml_find_element( el, "oslc:representation[@rdf:resource='http://open-services.net/ns/core#Reference']") is not None and ( rdfxml.xml_find_element( el, "oslc:range[@rdf:resource='http://open-services.net/ns/core#Resource']" ) is not None or rdfxml.xml_find_element( el, "oslc:valueType[@rdf:resource='http://open-services.net/ns/core#Resource']" ) is not None):
1112-
print( f"Ref {propuri=}" )
1111+
# print( f"Ref {propuri=}" )
11131112
if propuri is not None:
11141113
# print( f"Ref1 {propuri=}" )
11151114
if propcodec is None: # don't override if the codec is already set!
@@ -1131,7 +1130,7 @@ def _load_type_from_resource_shape(self, el, supershape=None):
11311130
self.register_linktype( property_title, propuri, label, inverselabel=inverselabel, rdfuri=rdfuri, typeCodec=propcodec, isMultiValued=isMultiValued )
11321131
if True:
11331132
logger.info( f"Defining property {name}.{property_title} {propuri=} {propcodec=} +++++++++++++++++++++++++++++++++++++++" )
1134-
print( f"Defining property {name}.{property_title} {propuri=} {uri=} {propcodec=} +++++++++++++++++++++++++++++++++++++++" )
1133+
# print( f"Defining property {name}.{property_title} {propuri=} {uri=} {propcodec=} +++++++++++++++++++++++++++++++++++++++" )
11351134
# self.register_property(property_title,propuri, shape_uri=uri)
11361135

11371136
# if property_title=="Residual POHS":
@@ -1777,7 +1776,7 @@ def deliver( self, *, targetstream_u=None, policy=None ):
17771776

17781777
for apolicy in policies:
17791778
policyxml += f'<rm_config:policy rdf:resource="{apolicy}" />'
1780-
print( f"{policyxml=}" )
1779+
# print( f"{policyxml=}" )
17811780

17821781
body=f"""<rdf:RDF
17831782
xmlns:dcterms="http://purl.org/dc/terms/"

elmclient/_typesystem.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,31 @@
55

66

77
import logging
8+
import keyword
89

910
from . import rdfxml
1011
from . import utils
1112

13+
1214
logger = logging.getLogger(__name__)
1315

1416
#################################################################################################
1517

18+
def makeSafeAttributeName( name, propuri ):
19+
# print( f"Make safe name fror {name } so it can be used as a Python object attribute" )
20+
res = ""
21+
for c in name:
22+
if not c.isalpha():
23+
c = "_"
24+
res += c
25+
if keyword.iskeyword( res ) or keyword.issoftkeyword( res ):
26+
# print( f"unsafe {name}" )
27+
res = makeSafeAttributeName( rdfxml.uri_to_prefixed_tag( propuri ), propuri )
28+
# print( f"Make safe name fror {name } {res}" )
29+
return res
30+
31+
#################################################################################################
32+
1633
class No_Type_System_Mixin():
1734
def __init__(self,*args,**kwargs):
1835
self.has_typesystem=False
@@ -179,7 +196,7 @@ def is_known_linktype_uri( self, uri ):
179196

180197
def register_shape( self, shape_name, shape_uri, *, rdfuri=None, shape_formats=None ):
181198
logger.info( f"register_shape {shape_name=} {shape_uri=}" )
182-
print( f"register_shape {shape_name=} {shape_uri=}" )
199+
# print( f"register_shape {shape_name=} {shape_uri=}" )
183200
shape_uri = self.normalise_uri( shape_uri)
184201
if shape_uri in self.shapes:
185202
raise Exception( f"Shape {shape_uri} already defined!" )
@@ -222,19 +239,24 @@ def is_known_property_uri( self, property_uri, *, raiseifnotfound=True ):
222239
# def register_property( self, property_name, property_uri, *, property_value_type=None, shape_uri=None, altname = None, do_not_overwrite=True, property_definition_uri=None, isMultiValued=False, typeCodec=None ):
223240
def register_property( self, property_name, property_uri, *, shape_uri=None, property_value_type=None, altname = None, do_not_overwrite=True, property_definition_uri=None, isMultiValued=False, typeCodec=None ):
224241
logger.info( f"register_property {property_name=} {property_uri=} {isMultiValued=} {typeCodec=}" )
225-
print( f"register_property {property_name=} {property_uri=} {isMultiValued=} {typeCodec=}" )
242+
# print( f"register_property {property_name=} {property_uri=} {isMultiValued=} {typeCodec=}" )
226243
if property_uri in self.properties:
227244
# print( f"Already defined {self.properties[property_uri]=}" )
228245
# burp
229246
pass
230247
property_uri = self.normalise_uri( property_uri )
248+
safeName = makeSafeAttributeName( property_name, property_uri )
231249
# shape_uri = self.normalise_uri( shape_uri )
250+
232251
if not do_not_overwrite or property_uri not in self.properties:
233252
# self.properties[property_uri] = {'name': property_name, 'shape': shape_uri, 'enums': [], 'value_type': property_value_type, 'altname':altname, 'isMultiValued':isMultiValued, 'typeCodec': typeCodec }
234-
self.properties[property_uri] = {'name': property_name, 'enums': [], 'value_type': property_value_type, 'altname':altname, 'isMultiValued':isMultiValued, 'typeCodec': typeCodec }
253+
self.properties[property_uri] = {'name': property_name, 'safeName': safeName, 'enums': [], 'value_type': property_value_type, 'altname':altname, 'isMultiValued':isMultiValued, 'typeCodec': typeCodec }
254+
235255
if altname and property_definition_uri and ( not do_not_overwrite or property_definition_uri not in self.properties):
236256
self.properties[property_definition_uri] = {'name': altname, 'enums': [], 'value_type': property_value_type, 'altname':None, 'isMultiValued':isMultiValued, 'typeCodec': typeCodec }
237257
self.properties[rdfxml.uri_to_default_prefixed_tag(property_definition_uri)] = {'name': altname, 'enums': [], 'value_type': property_value_type, 'altname':None, 'isMultiValued':isMultiValued, 'typeCode': typeCodec }
258+
259+
# make sure the property is recorded on the shape
238260
if shape_uri is not None and property_uri not in self.shapes[shape_uri]['properties']:
239261
self.shapes[shape_uri]['properties'].append(property_uri)
240262

@@ -253,10 +275,13 @@ def register_property_codec( self, property_name, property_uri, codec ):
253275
def register_linktype( self, linktype_name, linktype_uri, label, *, inverselabel=None, rdfuri=None, isMultiValued=False, typeCodec=None ):
254276
logger.info( f"register_linktype {linktype_name=} {linktype_uri=} {label=} {inverselabel=} {rdfuri=} {typeCodec=}" )
255277
linktype_uri = self.normalise_uri( linktype_uri )
278+
279+
safeName = makeSafeAttributeName( linktype_name, linktype_uri )
280+
256281
# shape_uri = self.normalise_uri( shape_uri )
257282
if linktype_uri not in self.linktypes:
258283
# self.linktypes[linktype_uri] = {'name': label, 'inverselabel': inverselabel, 'shape': shape_uri, 'rdfuri': rdfuri }
259-
self.linktypes[linktype_uri] = {'name': linktype_name, 'label': label, 'inverselabel': inverselabel, 'rdfuri': rdfuri, 'typeCodec': typeCodec }
284+
self.linktypes[linktype_uri] = {'name': linktype_name, 'safeName': safeName, 'label': label, 'inverselabel': inverselabel, 'rdfuri': rdfuri, 'typeCodec': typeCodec }
260285
# if shape_uri is not None:
261286
# self.shapes[shape_uri]['linktypes'].append(linktype_uri)
262287

@@ -325,6 +350,7 @@ def is_known_enum_uri( self, enum_uri ):
325350

326351
def register_enum( self, enum_name, enum_uri, property_uri, *, id=None ):
327352
logger.info( f"register_enum {enum_name=} {enum_uri=} {property_uri=} {id=}" )
353+
# print( f"register_enum {enum_name=} {enum_uri=} {property_uri=} {id=}" )
328354
# add the enum to the property
329355
enum_uri = self.normalise_uri( enum_uri )
330356
property_uri = self.normalise_uri( property_uri )
@@ -349,16 +375,20 @@ def get_enum_name( self, enum_uri ):
349375

350376
def get_enum_id( self, enum_name, property_uri ):
351377
logger.info( f"get_enum_id {enum_name=} {property_uri=}" )
378+
# print( f"get_enum_id {enum_name=} {property_uri=}" )
352379
property_uri = self.normalise_uri( property_uri )
353380
result = None
354381
logger.info( f"{self.properties[property_uri]=}" )
382+
# print( f"{self.properties[property_uri]=}" )
355383
logger.info( f"{self.properties[property_uri]['enums']=}" )
384+
# print( f"{self.properties[property_uri]['enums']=}" )
356385
for enum_uri in self.properties[property_uri]['enums']:
357386
if self.enums[enum_uri]['name']==enum_name:
358387
result = self.enums[enum_uri]['id'] or enum_uri
359388
# result = enum_uri # this makes ccm queries for e.g. rc:cm:type=Defect not work - ccm doens't like getting a URI - # unfortunately I can't remember why I added this line :-(
360389
break
361390
logger.info( f"get_enum_id {enum_name=} {property_uri=} {result=}" )
391+
# print( f"get_enum_id {enum_name=} {property_uri=} {result=}" )
362392
return result
363393

364394
# generic uri/name

elmclient/examples/dn_resource_scenario2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache
6060
# 1=clear cache initially then continue with cache enabled
6161
# 2=clear cache and disable caching
62-
caching = 0
62+
caching = 2
6363

6464
##################################################################################
6565
if __name__=="__main__":

elmclient/examples/dn_resource_scenario3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache
6060
# 1=clear cache initially then continue with cache enabled
6161
# 2=clear cache and disable caching
62-
caching = 0
62+
caching = 2
6363

6464
##################################################################################
6565
if __name__=="__main__":
@@ -95,14 +95,14 @@
9595
# create a stakeholder requirement
9696
reqt1 = c.createCoreResource( "Stakeholder Requirement" )
9797

98-
print( f"New artifact ID is {reqt1.Identifier=}" )
98+
print( f"New artifact ID is {reqt1.Identifier}" )
9999

100-
print( f"{reqt1=}" )
100+
print( f"Your new Stakeholder Requirement is:{reqt1}" )
101101

102102
# reqt1.Primary_Text = "Hello, World"
103103
reqt1.Primary_Text = '<div xmlns="http://www.w3.org/1999/xhtml">Hello, World</div>'
104104

105105
reqt1.put()
106106

107-
print( f"{reqt1=}" )
107+
print( f"With the Primary_Text set your Stakeholder Requirement is:{reqt1}" )
108108

elmclient/examples/dn_resource_scenario4.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache
6161
# 1=clear cache initially then continue with cache enabled
6262
# 2=clear cache and disable caching
63-
caching = 0
63+
caching = 2
6464

6565
##################################################################################
6666
if __name__=="__main__":
@@ -92,47 +92,50 @@
9292
print( f"{config_u=}" )
9393
c.set_local_config(config_u)
9494

95-
# find an example with a pre-created link
96-
# coresys=c.resourceQuery( "Identifier=4736",returnBindings=False )
95+
## find an example with a pre-created link so you can see the format
96+
# coresys=c.resourceQuery( "Identifier=4770",returnBindings=False )
9797
# print( f"{coresys=}" )
9898

9999
#####################################################################################################
100100
# create a stakeholder requirement
101101
reqt1 = c.createCoreResource( "Stakeholder Requirement" )
102102

103-
print( f"Stakeholder Requirement artifact ID is {reqt1.Identifier=}" )
103+
print( f"Stakeholder Requirement artifact ID is {reqt1.Identifier}" )
104104

105-
# print( f"{reqt1=}" )
105+
# print( f"{reqt1}" )
106106

107107
reqt1.Primary_Text = '<div xmlns="http://www.w3.org/1999/xhtml">I am what a Stakeholder wants!</div>'
108108

109109
reqt1.put()
110110

111-
print( f"With the updated primaryText your Stakeholder Requirement is:\n{reqt1=}" )
111+
print( f"With the updated primaryText your Stakeholder Requirement is:\n{reqt1}" )
112112

113113
#####################################################################################################
114114
# create a system requirement
115115
reqt2 = c.createCoreResource( "System Requirement" )
116116

117-
print( f"System Requirement artifact ID is {reqt2.Identifier=}" )
117+
print( f"System Requirement artifact ID is {reqt2.Identifier}" )
118118

119-
# print( f"{reqt2=}" )
119+
# print( f"{reqt2}" )
120120

121121
reqt2.Primary_Text = '<div xmlns="http://www.w3.org/1999/xhtml">I am what a Systems Engineer wants!</div>'
122122

123123
reqt2.put()
124124

125-
print( f"With the updated primaryText your System Requirement is:\n{reqt2=}" )
125+
print( f"With the updated primaryText your System Requirement is:\n{reqt2}" )
126126

127127
#####################################################################################################
128128
# create Satisfies link from the System Requirement to the Stakeholder Requirement
129129
reqt2.Questions="There is a question"
130+
reqt2.Test_Criteria="There are some criteria"
131+
130132
reqt2.put()
131-
print( f"With the Questions set your System Requirement is: {reqt2=}" )
132-
burp
133+
134+
print( f"With the Questions set your System Requirement is: {reqt2}" )
135+
133136
# create the link to the reqt1 id
134137
reqt2.Satisfies=reqt1.Identifier
135138

136139
reqt2.put()
137140

138-
print( f"With the Satisfies link your System Requirement is: {reqt2=}" )
141+
print( f"With the Satisfies link your System Requirement is: {reqt2}" )

elmclient/examples/dn_resource_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
print( f"{mores[0]=}" )
9797
mores[0].Title = mores[0].Title+"1"
9898
print( f"{mores[0]=}" )
99-
mores[0].addCoreArtifactLink( "Satisfies", 2854 )
99+
# mores[0].addCoreArtifactLink( "Satisfies", 2854 )
100100
print( f"After add link {mores[0]=}" )
101101
mores[0].to_etree()
102102
mores[0].put()

0 commit comments

Comments
 (0)