-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2nq.py
executable file
·2139 lines (1722 loc) · 94.5 KB
/
csv2nq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import argparse
import csv
import os
import json
from os.path import exists
import datetime
# Local imports
from nq import nqwriter
# The domain model can indicate if it supports recently introduced features by
# adding entries in DomainFeature.csv. Each feature has a reserved URI, and the
# definitive list is specified here.
#
# To indicate domain model support for one of these features, DomainFeature.csv
# must have:
# - an entry with the URI matching the relevant URI specified here
# - the 'supported' field for that entry must be set to 'TRUE'
#
# The 'supported' field provides a convenient way for domain modellers to switch
# some features off after they have been added.
# Domain model expects triples to be expanded
HAS_POPULATION_MODEL = "feature#PopulationModel"
# Strings appended to the average case to get min and max members of each population triplet
MIN_SUFFIX = "_Min"
MAX_SUFFIX = "_Max"
# Flags rather than naming conventions denote secondary and normal operational process threats
HAS_THREAT_TYPE_FLAGS = "feature#ThreatTypeFlags"
# Flags denote whether threats or control strategies should be used in current or future
# risk calculations.
HAS_RISK_TYPE_FLAGS = "feature#RiskTypeFlags"
# Threats can have mixed causes (both TWAS and MS), which means SSM doesn't need to raise the
# likelihood of each MS to at least the level equivalent to the TW level of its TWAS.
HAS_MIXED_THREAT_CAUSES = "feature#MixedThreatCauses"
# Asset and relationship types are flagged if they are only used for system model construction
# inference.
HAS_CONSTRUCTION_STATE = "feature#ConstructionStateFlags"
# There is no feature for CSGs having optional controls because as fas as SSM is concerned
# that is an optional feature.
# The second line of a CSV file often contains default values and if so will include domain#000000
DUMMY_URI = "domain#000000"
#
# Domain model graph URI, version info, label and description, plus feature list.
#
def output_domain_model(nqw, unfiltered, heading):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Find out what features are supported
if(exists("DomainFeature.csv")):
with open("DomainFeature.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
comment_index = header.index("comment")
supported_index = header.index("supported")
population_support = False
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the subsequent row
if(row[uri_index] == HAS_POPULATION_MODEL):
population_support = True
if(not raw.expanded):
print("Domain model specifies population support, but this was suppressed by the csv2nq command line")
supported = row[supported_index].lower() == "true"
# Write out the line if the feature is supported
if(supported):
feature_list.append(row[uri_index])
if(raw.expanded and HAS_POPULATION_MODEL not in feature_list):
print("Population support was selected via the csv2nq command line, but is not supported by this domain model")
# Then convert the domain model information
with open("DomainModel.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
domainGraph_index = header.index("domainGraph")
reasonerClass_index = header.index("reasonerClass")
# Extract the information we need from the subsequent row
row = next(reader)
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: row = next(reader)
uri = "<{}>".format(row[uri_index])
# Replace the last part of the provided domainGraph URI with the commandline argument if provided
domainGraph = row[domainGraph_index]
if args["name"]:
uri_frags = domainGraph.split("/")
uri_frags[-1] = args["name"]
domainGraph = "/".join(uri_frags)
if (not raw.expanded and HAS_POPULATION_MODEL in feature_list):
domainGraph = "<{}-unexpanded>".format(domainGraph)
label = nqw.encode_string(row[label_index] + "-UNEXPANDED")
feature_list.remove(HAS_POPULATION_MODEL)
else:
domainGraph = "<{}>".format(domainGraph)
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
versionInfo = args["version"]
if (unfiltered):
versionInfo += "-unfiltered"
versionInfo = nqw.encode_string(versionInfo)
reasonerClass = nqw.encode_string(row[reasonerClass_index])
# Set NQ writer to the (pre-encoded) domain graph URI
nqw.set_graph(domainGraph)
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_owl_uri("owl#imports"), nqw.encode_ssm_uri("core"))
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_owl_uri("owl#Ontology"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#domainGraph"), domainGraph)
nqw.write_quad(uri, nqw.encode_owl_uri("owl#versionInfo"), versionInfo)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#reasonerClass"), reasonerClass)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
# Finally, add a list of the features that are supported
for featureRef in feature_list:
feature = nqw.encode_ssm_uri(featureRef.replace("feature#", "domain#Feature-"))
nqw.write_quad(feature, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#ModelFeature"))
# Output a spacer at the end of this section
nqw.write_comment("")
print("Domain model feature list: ", feature_list)
#
# Scale conversion: used for likelihood, impact, risk and population scales, plus two scales
# not yet used by SSM describing the cost and performance overheads of controls.
def output_scale(nqw, saveHighest, infilename, entity, heading):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
savedValue = -1
savedUri = ""
with open(infilename, newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
levelValue_index = header.index("levelValue")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
levelValue = nqw.encode_integer(row[levelValue_index])
# Save the end of scale for later use
newValue = int(row[levelValue_index])
if(saveHighest):
if(savedValue < newValue):
savedValue = newValue
savedUri = row[uri_index]
else:
if(newValue == 0):
savedUri = row[uri_index]
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#{}".format(entity)))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#levelValue"), levelValue)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Output a spacer at the end of this section
nqw.write_comment("")
return savedUri
#
# End of each scale, saved for later when converting that scale (actually, only MIN_IMPACT is used).
MAX_TW = ""
MIN_LIKELIHOOD = ""
MIN_IMPACT = ""
MIN_RISK = ""
MIN_POP = ""
MIN_COST = ""
MIN_PERF = ""
#
# Assets and relationships.
#
def output_domain_assets(nqw, unfiltered, heading, entities):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the assets
with open("DomainAsset.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
isAssertable_index = header.index("isAssertable")
isVisible_index = header.index("isVisible")
if(HAS_CONSTRUCTION_STATE in feature_list):
constructionState_index = header.index("constructionState")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
isAssertable = nqw.encode_boolean(row[isAssertable_index])
isVisible = nqw.encode_boolean(row[isVisible_index])
if(HAS_CONSTRUCTION_STATE in feature_list):
isConstructionState = nqw.encode_boolean(row[constructionState_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_owl_uri("owl#Class"))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isAssertable"), isAssertable)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isVisible"), isVisible)
if(HAS_CONSTRUCTION_STATE in feature_list):
if(row[constructionState_index].lower() == "true" and not unfiltered):
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isConstructionState"), isConstructionState)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Save the asset for later: key = URI, value = entity reference embedded in other URI
entities[row[uri_index]] = row[uri_index][len("domain#"):]
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the asset parents
with open("DomainAssetParents.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
subClassOf_index = header.index("subClassOf")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
subClassOf = nqw.encode_ssm_uri(row[subClassOf_index])
# Output line we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#subClassOf"), subClassOf)
# Output a spacer at the end of this section
nqw.write_comment("")
def output_relationships(nqw, unfiltered, heading, entities):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the relationships
with open("ObjectProperty.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
isAssertable_index = header.index("isAssertable")
isVisible_index = header.index("isVisible")
hidden_index = header.index("hidden")
if(HAS_CONSTRUCTION_STATE in feature_list):
constructionState_index = header.index("constructionState")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
hidden = nqw.encode_boolean(row[hidden_index])
isAssertable = nqw.encode_boolean(row[isAssertable_index])
isVisible = nqw.encode_boolean(row[isVisible_index])
if(HAS_CONSTRUCTION_STATE in feature_list):
isConstructionState = nqw.encode_boolean(row[constructionState_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_owl_uri("owl#ObjectProperty"))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isAssertable"), isAssertable)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isVisible"), isVisible)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hidden"), hidden)
if(HAS_CONSTRUCTION_STATE in feature_list):
if(row[constructionState_index].lower() == "true" and not unfiltered):
nqw.write_quad(uri, nqw.encode_ssm_uri("core#isConstructionState"), isConstructionState)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Save the relationship for later: key = URI, value = reference embedded in other URI
entities[row[uri_index]] = row[uri_index][len("domain#"):]
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the relationship type parents
with open("ObjectPropertyParents.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
subPropertyOf_index = header.index("subPropertyOf")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
subPropertyOf = nqw.encode_ssm_uri(row[subPropertyOf_index])
# Output line we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#subPropertyOf"), subPropertyOf)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the relationship domains
with open("ObjectPropertyDomains.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
domain_index = header.index("domain")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
theDomain = nqw.encode_ssm_uri(row[domain_index])
# Output line we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#domain"), theDomain)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the relationship ranges
with open("ObjectPropertyRanges.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
range_index = header.index("range")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
theRange = nqw.encode_ssm_uri(row[range_index])
# Output line we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#range"), theRange)
# Output a spacer at the end of this section
nqw.write_comment("")
#
# Roles, controls, misbehaviours and trustworthiness attributes: all except roles need to be
# expanded into triplets if population models are to be supported.
#
def output_roles(nqw, heading, entities):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the roles
with open("Role.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#Role"))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Save for later
entities[row[uri_index]] = row[uri_index][len("domain#Role_"):]
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the asset types that can take each role
with open("RoleLocations.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
metaLocatedAt_index = header.index("metaLocatedAt")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
metaLocatedAt = nqw.encode_ssm_uri(row[metaLocatedAt_index])
# Output line we need to the NQ file
nqw.write_quad(uri, nqw.encode_ssm_uri("core#metaLocatedAt"), metaLocatedAt)
# Output a spacer at the end of this section
nqw.write_comment("")
def output_cmr_entity(nqw, unfiltered, entityType, heading, infilename, locfilename, entities):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the properties
with open(infilename, newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
isVisible_index = header.index("isVisible")
if(entityType == "Control"):
cost_index = header.index("unitCost")
perf_index = header.index("performanceImpact")
typ = nqw.encode_ssm_uri("core#" + entityType)
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
(min_uri, av_uri, max_uri) = nqw.encode_ssm_uri(add_minmax(row[uri_index]))
(min_label, av_label, max_label) = nqw.encode_string(add_minmax(row[label_index]))
comment = nqw.encode_string(row[comment_index])
if unfiltered:
av_isVisible = nqw.encode_boolean("True")
minmax_isVisible = nqw.encode_boolean("True")
else:
av_isVisible = nqw.encode_boolean(row[isVisible_index].lower())
minmax_isVisible = nqw.encode_boolean("False")
if(entityType == "Control"):
unitCost = nqw.encode_ssm_uri(row[cost_index])
performanceImpact = nqw.encode_ssm_uri(row[perf_index])
# Output the average version
nqw.write_quad(av_uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), typ)
nqw.write_quad(av_uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(av_uri, nqw.encode_rdfs_uri("rdf-schema#label"), av_label)
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#isVisible"), av_isVisible)
if(entityType == "Control"):
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#unitCost"), unitCost)
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#performanceImpact"), performanceImpact)
if(HAS_POPULATION_MODEL in feature_list):
# Output the min and max versions
nqw.write_quad(min_uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), typ)
nqw.write_quad(min_uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(min_uri, nqw.encode_rdfs_uri("rdf-schema#label"), min_label)
nqw.write_quad(min_uri, nqw.encode_ssm_uri("core#isVisible"), minmax_isVisible)
if(entityType == "Control"):
nqw.write_quad(min_uri, nqw.encode_ssm_uri("core#unitCost"), unitCost)
nqw.write_quad(min_uri, nqw.encode_ssm_uri("core#performanceImpact"), performanceImpact)
nqw.write_quad(max_uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), typ)
nqw.write_quad(max_uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(max_uri, nqw.encode_rdfs_uri("rdf-schema#label"), max_label)
nqw.write_quad(max_uri, nqw.encode_ssm_uri("core#isVisible"), minmax_isVisible)
if(entityType == "Control"):
nqw.write_quad(max_uri, nqw.encode_ssm_uri("core#unitCost"), unitCost)
nqw.write_quad(max_uri, nqw.encode_ssm_uri("core#performanceImpact"), performanceImpact)
# link the three versions
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#hasMin"), min_uri)
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#hasMax"), max_uri)
nqw.write_quad(min_uri, nqw.encode_ssm_uri("core#minOf"), av_uri)
nqw.write_quad(max_uri, nqw.encode_ssm_uri("core#maxOf"), av_uri)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Save the entity for later: key = URI, value = entity reference embedded in other URI
entities[row[uri_index]] = row[uri_index][len("domain#"):]
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the asset types that can have each property
with open(locfilename, newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
metaLocatedAt_index = header.index("metaLocatedAt")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
(min_uri, av_uri, max_uri) = nqw.encode_ssm_uri(add_minmax(row[uri_index]))
metaLocatedAt = nqw.encode_ssm_uri(row[metaLocatedAt_index])
# Output line we need to the NQ file
nqw.write_quad(av_uri, nqw.encode_ssm_uri("core#metaLocatedAt"), metaLocatedAt)
if(HAS_POPULATION_MODEL in feature_list):
nqw.write_quad(min_uri, nqw.encode_ssm_uri("core#metaLocatedAt"), metaLocatedAt)
nqw.write_quad(max_uri, nqw.encode_ssm_uri("core#metaLocatedAt"), metaLocatedAt)
# Output a spacer at the end of this section
nqw.write_comment("")
#
# Trustworthiness attribute erosion, threat causation suppression.
#
def output_twis(nqw, heading, twa_misbehaviour):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the TWIS entries
with open("TWIS.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
affected_by_index = header.index("affectedBy")
affects_index = header.index("affects")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
affected_by = nqw.encode_ssm_uri(row[affected_by_index])
affects = nqw.encode_ssm_uri(row[affects_index])
# Save the mapping from TWA to Misbehaviour
twa = row[affects_index]
misbehaviour = row[affected_by_index]
twa_misbehaviour[twa] = misbehaviour
# Output lines we need to the NQ file
# Average case
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#TrustworthinessImpactSet"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affectedBy"), affected_by)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affects"), affects)
# Min/max cases : not sure if SSM still needs these but keep them until this can be confirmed
# Note that this relies upon the TWIS URI being of the form: domain#TWIS-affects-affected_by
if(HAS_POPULATION_MODEL in feature_list):
affected_by = row[affected_by_index][7:] # remove initial "domain#"
affects = row[affects_index][7:]
min_affected_by = affected_by + "_Min"
max_affected_by = affected_by + "_Max"
min_affects = affects + "_Min"
max_affects = affects + "_Max"
uri = nqw.encode_ssm_uri("domain#TWIS-" + min_affects + "-" + max_affected_by)
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#TrustworthinessImpactSet"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affects"), nqw.encode_ssm_uri("domain#" + min_affects))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affectedBy"), nqw.encode_ssm_uri("domain#" + max_affected_by))
uri = nqw.encode_ssm_uri("domain#TWIS-" + max_affects + "-" + min_affected_by)
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#TrustworthinessImpactSet"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affects"), nqw.encode_ssm_uri("domain#" + max_affects))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#affectedBy"), nqw.encode_ssm_uri("domain#" + min_affected_by))
# Output a spacer at the end of this section
nqw.write_comment("")
# Output a spacer at the end of this section
nqw.write_comment("")
def output_mis(nqw, heading):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Set the input filename and other parameters
infilename = "MIS.csv"
# Output the properties
with open(infilename, newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
inhibited_index = header.index("inhibited")
inhibited_by_index = header.index("inhibitedBy")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
inhibited = nqw.encode_ssm_uri(row[inhibited_index])
inhibited_by = nqw.encode_ssm_uri(row[inhibited_by_index])
# Output lines we need to the NQ file
# Average case - we don't need the other two because this is new do the validator can do the expansion
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#MisbehaviourInhibitionSet"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#inhibited"), inhibited)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#inhibitedBy"), inhibited_by)
# Output a spacer at the end of this section
nqw.write_comment("")
#
# Patterns: do not need to be expanded as population triplets.
#
def output_root_patterns(nqw, heading, roles, assets, relationships, nodes, links):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the root pattern
with open("RootPattern.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
# Note that there is a comment field used in the MS Access DB editor, but it is not exported to NQ
comment_index = header.index("comment")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#RootPattern"))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the root pattern nodes
with open("RootPatternNodes.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
hasNode_index = header.index("hasNode")
keyNode_index = header.index("keyNode")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
hasNode = nqw.encode_ssm_uri(row[hasNode_index])
keyNode = row[keyNode_index].lower()
# Output lines we need to the NQ file
if(keyNode == "true"):
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasKeyNode"), hasNode)
elif(keyNode == "false"):
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasRootNode"), hasNode)
else:
raise ValueError("Matching pattern {} has bad keyNode value {}".format(uri,keyNode))
# Save the node
if row[hasNode_index] not in nodes:
nodes[row[hasNode_index]] = create_node(row[hasNode_index], roles, assets)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the root pattern links
with open("RootPatternLinks.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
hasLink_index = header.index("hasLink")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
hasLink = nqw.encode_ssm_uri(row[hasLink_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasLink"), hasLink)
# Save the link
if row[hasLink_index] not in links:
links[row[hasLink_index]] = create_link(row[hasLink_index], roles, relationships)
# Output a spacer at the end of this section
nqw.write_comment("")
def output_matching_patterns(nqw, heading, roles, assets, relationships, nodes, links):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the matching pattern
with open("MatchingPattern.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
hasRootPattern_index = header.index("hasRootPattern")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
label = nqw.encode_string(row[label_index])
comment = nqw.encode_string(row[comment_index])
hasRootPattern = nqw.encode_ssm_uri(row[hasRootPattern_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#MatchingPattern"))
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#label"), label)
nqw.write_quad(uri, nqw.encode_rdfs_uri("rdf-schema#comment"), comment)
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasRootPattern"), hasRootPattern)
# Output a spacer at the end of this resource
nqw.write_comment("")
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the matching pattern nodes
with open("MatchingPatternNodes.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
has_node_index = header.index("hasNode")
mandatory_node_index = header.index("mandatoryNode")
prohibited_node_index = header.index("prohibitedNode")
sufficient_node_index = header.index("sufficientNode")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
has_node = nqw.encode_ssm_uri(row[has_node_index])
mandatory_node = row[mandatory_node_index].lower()
prohibited_node = row[prohibited_node_index].lower()
sufficient_node = row[sufficient_node_index].lower()
# Output lines we need to the NQ file
if mandatory_node == "true":
if sufficient_node == "true":
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasSufficientNode"), has_node)
else:
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasNecessaryNode"), has_node)
elif prohibited_node == "true":
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasProhibitedNode"), has_node)
else:
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasOptionalNode"), has_node)
# Save the node
if row[has_node_index] not in nodes:
nodes[row[has_node_index]] = create_node(row[has_node_index], roles, assets)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the matching pattern links
with open("MatchingPatternLinks.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
hasLink_index = header.index("hasLink")
prohibited_index = header.index("prohibited")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
hasLink = nqw.encode_ssm_uri(row[hasLink_index])
prohibited = row[prohibited_index].lower()
# Output lines we need to the NQ file
if(prohibited == "true"):
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasProhibitedLink"), hasLink)
else:
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasLink"), hasLink)
# Save the link
if row[hasLink_index] not in links:
links[row[hasLink_index]] = create_link(row[hasLink_index], roles, relationships)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the matching pattern relations to DNGs
with open("MatchingPatternDNG.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
hasDistinctNodeGroup_index = header.index("hasDistinctNodeGroup")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
hasDistinctNodeGroup = nqw.encode_ssm_uri(row[hasDistinctNodeGroup_index])
# Output lines we need to the NQ file
nqw.write_quad(hasDistinctNodeGroup, nqw.encode_rdfns_uri("22-rdf-syntax-ns#type"), nqw.encode_ssm_uri("core#DistinctNodeGroup"))
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasDistinctNodeGroup"), hasDistinctNodeGroup)
# Output a spacer at the end of this section
nqw.write_comment("")
# Output the DNG Nodes
with open("DistinctNodeGroupNodes.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
hasNode_index = header.index("hasNode")
for row in reader:
# Skip the first line which contains default values for csvformat
if DUMMY_URI in row: continue
# Extract the information we need from the next row
uri = nqw.encode_ssm_uri(row[uri_index])
hasNode = nqw.encode_ssm_uri(row[hasNode_index])
# Output lines we need to the NQ file
nqw.write_quad(uri, nqw.encode_ssm_uri("core#hasNode"), hasNode)
# Output a spacer at the end of this section
nqw.write_comment("")
def output_construction_patterns(nqw, heading, roles, assets, relationships, nodes, links):
# Output a heading for this section
nqw.write_comment("")
nqw.write_comment(heading)
nqw.write_comment("")
# Output the matching pattern
with open("ConstructionPattern.csv", newline="") as csvfile:
# Create the CSV reader object
reader = csv.reader(csvfile)
# Check that the table is as expected: if fields are missing this will raise an exception
header = next(reader)
uri_index = header.index("URI")
label_index = header.index("label")
comment_index = header.index("comment")
hasMatchingPattern_index = header.index("hasMatchingPattern")
hasPriority_index = header.index("hasPriority")
iterate_index = header.index("iterate")
maxIterations_index = header.index("maxIterations")