Skip to content

Commit 72bffb0

Browse files
committed
feat: TBox generation
1 parent 919fa4a commit 72bffb0

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

src/awl/jsonld_context.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@
2727
"value": "awl:HasValue",
2828
},
2929
},
30-
"args": "awl:HasArgument",
30+
"FunctionDef": {
31+
"@id": "awl:FunctionDef",
32+
"@context": {"@base": "https://example.org/", "name": "@id"},
33+
},
34+
"args": {
35+
"@id": "awl:HasArgumentList",
36+
"@context": {
37+
"args": {
38+
"@id": "awl:HasPart",
39+
"@context": {
40+
"_type:": None,
41+
# "arg": "awl:HasKey",
42+
"value": "awl:HasValue",
43+
"annotation": "awl:HasType",
44+
},
45+
}
46+
},
47+
},
3148
"keywords": {
3249
"@id": "awl:HasKeywordArgument",
33-
"@context": {"value": "awl:HasValue"},
50+
"@context": {
51+
"value": "awl:HasValue",
52+
"annotation": "awl:HasAnnotation",
53+
},
3454
},
3555
"arg": "awl:HasKey",
56+
"returns": "awl:HasReturnType",
3657
}
3758
]
3859
}

tests/test_usecases.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import rdflib
2+
3+
from awl.core import AstSerialization
4+
5+
6+
def test_tbox_generation():
7+
src_code = """
8+
def MyProcess(input: MyInput) -> MyOutput:
9+
pass
10+
"""
11+
ast_serialization = AstSerialization()
12+
ast_serialization.parse(src_code)
13+
print(ast_serialization.dumps())
14+
15+
# add to graph
16+
g = rdflib.Graph()
17+
g.parse(data=ast_serialization.to_jsonld(), format="json-ld")
18+
print(g.serialize(format="turtle"))
19+
20+
# query all awl:FunctionDef
21+
# construct owl restrictions for each arg type annotation
22+
# construct owl restrictions for return type annotation
23+
query = """
24+
PREFIX awl: <https://w3id.org/awl/schema/>
25+
PREFIX ex: <https://example.org/>
26+
PREFIX owl: <http://www.w3.org/2002/07/owl#>
27+
CONSTRUCT {
28+
?arg_type a owl:Class .
29+
?return_type a owl:Class .
30+
?f a owl:Class ;
31+
owl:equivalentClass [
32+
a owl:Restriction ;
33+
owl:onProperty awl:HasInput ;
34+
owl:someValuesFrom ?arg_type ;
35+
] ;
36+
owl:equivalentClass [
37+
a owl:Restriction ;
38+
owl:onProperty awl:HasOutput ;
39+
owl:someValuesFrom ?return_type ;
40+
] .
41+
}
42+
WHERE {
43+
?f a awl:FunctionDef ;
44+
awl:HasArgumentList/awl:HasPart/awl:HasType ?arg_type ;
45+
awl:HasReturnType ?return_type .
46+
}
47+
"""
48+
49+
# print result as ttl
50+
qres = g.query(query)
51+
# import as a graph
52+
g = rdflib.Graph()
53+
g.parse(data=qres.serialize(format="json-ld"), format="json-ld")
54+
55+
# add a dummy owl ontology
56+
g.parse(
57+
data="""
58+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
59+
@prefix ex: <https://example.org/> .
60+
@prefix awl: <https://w3id.org/awl/schema/> .
61+
ex:MyProcessInventory a owl:Ontology .
62+
""",
63+
format="ttl",
64+
)
65+
66+
print(g.serialize(format="turtle"))
67+
68+
69+
if __name__ == "__main__":
70+
test_tbox_generation()

0 commit comments

Comments
 (0)