wikipunk commited on
Commit
a3de7e3
1 Parent(s): 852bfb5

init dataset load script

Browse files
Files changed (2) hide show
  1. README.md +22 -0
  2. d3fend.py +61 -0
README.md ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # D3FEND
2
+
3
+ Branch: https://github.com/d3fend/d3fend-ontology/tree/release/0.13.0-BETA-1
4
+
5
+ Commit: 3dcc495879bb62cee5c4109e9b784dd4a2de3c9d
6
+
7
+ CWE extension:
8
+ https://github.com/d3fend/d3fend-ontology/tree/release/0.13.0-BETA-1/extensions/cwe
9
+
10
+ After building d3fend-full.owl I imported the ontology into Protege
11
+ version 5.6.1 with the Pellet reasoner plug-in. First I use the Debug
12
+ Ontology plugin to check the ontology for consistency and
13
+ coherency. If it all checks out, I move onto exporting the inferences.
14
+
15
+ In Protege navigate to File>Export Inferred Axioms as ontology and
16
+ make sure to check all of the checkboxes including asserted axioms and
17
+ annotations. See this blog post for more
18
+ information: https://www.michaeldebellis.com/post/export-inferred-axioms.
19
+
20
+ Once you have the materialized ontology you can filter it with
21
+ d3fend.sparql.
22
+
d3fend.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ from datasets import SplitGenerator, DatasetInfo, GeneratorBasedBuilder
4
+ from rdflib import Graph, URIRef, Literal, BNode
5
+ from rdflib.namespace import RDF, RDFS, OWL, XSD, DCTERMS, SKOS, DCAM, Namespace
6
+ from datasets.features import Features, Value
7
+
8
+ D3F = Namespace('http://d3fend.mitre.org/ontologies/d3fend.owl#')
9
+
10
+ class D3FENDDatasetBuilder(GeneratorBasedBuilder):
11
+ VERSION = "1.0.0"
12
+
13
+ def _info(self):
14
+ return DatasetInfo(
15
+ description="D3FEND is a framework which encodes a countermeasure knowledge base as a knowledge graph. The graph contains the types and relations that define key concepts in the cybersecurity countermeasure domain and the relations necessary to link those concepts to each other. Each of these concepts and relations are linked to references in the cybersecurity literature.",
16
+ homepage="https://d3fend.mitre.org/",
17
+ license="MIT",
18
+ features=Features({
19
+ 'subject': Value('string'),
20
+ 'predicate': Value('string'),
21
+ 'object': Value('string')
22
+ })
23
+ )
24
+
25
+ def _split_generators(self, dl_manager):
26
+ # Download and extract the dataset
27
+ path = dl_manager.download_and_extract(["d3fend.nt.gz"])
28
+
29
+ return [SplitGenerator(name=datasets.Split.TRAIN,
30
+ gen_kwargs={'filepath': path})]
31
+
32
+ def _generate_examples(self, filepath):
33
+ id_ = 0
34
+ graph = Graph(bind_namespaces="core")
35
+ graph.bind("d3f", D3F)
36
+ graph.bind("dcterms", DCTERMS)
37
+ graph.bind("skos", SKOS)
38
+ graph.bind("dcam", DCAM)
39
+ graph.parse(filepath)
40
+ # Yield individual triples from the graph as N3
41
+ for (s, p, o) in graph.triples((None, None, None)):
42
+ yield id_, {
43
+ 'subject': s.n3(),
44
+ 'predicate': p.n3(),
45
+ 'object': o.n3()
46
+ }
47
+ id_ += 1
48
+
49
+ from rdflib.util import from_n3
50
+
51
+ def triple(features):
52
+ try:
53
+ subject_node = from_n3(features['subject'])
54
+ predicate_node = from_n3(features['predicate'])
55
+ object_node = from_n3(features['object'])
56
+ return (subject_node, predicate_node, object_node)
57
+ except Exception as e:
58
+ print(f"Error transforming features {features}: {e}")
59
+ return (None, None, None)
60
+
61
+ from datasets import load_dataset