|
| 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