File: examples.rst

package info (click to toggle)
rudof 0.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,968 kB
  • sloc: python: 1,404; makefile: 32; sh: 1
file content (179 lines) | stat: -rw-r--r-- 5,303 bytes parent folder | download | duplicates (2)
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
Examples
========

This page contains practical examples of using ``pyrudof``.


SHACL Validation
----------------

.. code-block:: python

    from pyrudof import *

    rudof = Rudof(RudofConfig())

    rudof.read_shacl_str("""
        prefix : <http://example.org/>
        prefix sh: <http://www.w3.org/ns/shacl#>
        prefix xsd: <http://www.w3.org/2001/XMLSchema#>

        :Person a sh:NodeShape;
            sh:targetNode :ok, :ko ;
            sh:property [
                sh:path :name ;
                sh:minCount 1;
                sh:maxCount 1;
                sh:datatype xsd:string ;
            ] .
    """)

    rudof.read_data_str("""
        prefix : <http://example.org/>
        :ok :name "alice" .
        :ko :name 1 .
    """)

    result = rudof.validate_shacl(ShaclValidationMode(), ShapesGraphSource())
    print(result.show_as_table())


ShEx Validation
---------------

.. code-block:: python

    from pyrudof import *

    rudof = Rudof(RudofConfig())

    rudof.read_shex_str("""
        prefix : <http://example.org/>
        prefix xsd: <http://www.w3.org/2001/XMLSchema#>

        :Person {
            :name xsd:string ;
            :age xsd:integer ?
        }
    """)

    rudof.read_data_str("""
        prefix : <http://example.org/>
        :alice :name "Alice" ; :age 30 .
        :bob :name "Bob" .
    """)

    shapemap = rudof.read_shapemap_str(":alice@:Person, :bob@:Person")
    result = rudof.validate_shex(shapemap)
    print(result)


DCTAP to ShEx Conversion
-------------------------

.. code-block:: python

    from pyrudof import Rudof, RudofConfig, ShExFormatter

    rudof = Rudof(RudofConfig())

    rudof.read_dctap_str("""
    shapeId,propertyId,Mandatory,Repeatable,valueDatatype,valueShape
    Person,name,true,false,xsd:string,
    ,birthdate,false,false,xsd:date,
    ,enrolledIn,false,true,,Course
    Course,name,true,false,xsd:string,
    ,student,false,true,,Person
    """)

    rudof.dctap2shex()
    result = rudof.serialize_current_shex(ShExFormatter())
    print(result)


SPARQL CONSTRUCT Query on Wikidata
-----------------------------------

.. code-block:: python

    from pyrudof import Rudof, RudofConfig, QueryResultFormat

    rudof = Rudof(RudofConfig())
    rudof.use_endpoint("https://query.wikidata.org/sparql")

    result = rudof.run_query_construct_str("""
        PREFIX wd:  <http://www.wikidata.org/entity/>
        PREFIX wdt: <http://www.wikidata.org/prop/direct/>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX :    <http://example.org/>

        CONSTRUCT {
            ?p a :Person ;
               :name ?person .
        } WHERE {
            ?p wdt:P31 wd:Q5 ;
               rdfs:label ?person .
            FILTER (lang(?person) = "en")
        }
        LIMIT 5
    """, QueryResultFormat.Turtle)

    print(result)


Synthetic Data Generation
-------------------------

.. code-block:: python

    import pyrudof

    config = pyrudof.GeneratorConfig()
    config.set_entity_count(100)
    config.set_output_path("/tmp/synthetic_data.ttl")
    config.set_output_format(pyrudof.OutputFormat.Turtle)
    config.set_seed(42)
    config.set_cardinality_strategy(pyrudof.CardinalityStrategy.Balanced)

    generator = pyrudof.DataGenerator(config)
    generator.run("schema.shex")


ShEx to UML Diagram
-------------------

.. code-block:: python

    from pyrudof import Rudof, RudofConfig, UmlGenerationMode

    rudof = Rudof(RudofConfig())

    rudof.read_shex_str("""
        prefix : <http://example.org/>
        prefix xsd: <http://www.w3.org/2001/XMLSchema#>

        :Person {
            :name xsd:string ;
            :knows @:Person *
        }
    """)

    uml = rudof.shex2uml(UmlGenerationMode())
    print(uml)


More Examples
-------------

Additional examples can be found in the repository:

- `generate_example.py <https://github.com/rudof-project/rudof/blob/master/python/examples/generate_example.py>`_ — Basic generation examples
- `advanced_generate_example.py <https://github.com/rudof-project/rudof/blob/master/python/examples/advanced_generate_example.py>`_ — Advanced generation patterns
- `config_file_example.py <https://github.com/rudof-project/rudof/blob/master/python/examples/config_file_example.py>`_ — Configuration file usage
- `dctap2shex.py <https://github.com/rudof-project/rudof/blob/master/python/examples/dctap2shex.py>`_ — DCTAP to ShEx conversion
- `construct_query_wikidata.py <https://github.com/rudof-project/rudof/blob/master/python/examples/construct_query_wikidata.py>`_ — SPARQL queries on Wikidata
- `generate_from_schema.py <https://github.com/rudof-project/rudof/blob/master/python/examples/generate_from_schema.py>`_ — Generate data from a schema file
- `shex2uml.py <https://github.com/rudof-project/rudof/blob/master/python/examples/shex2uml.py>`_ — Generate UML diagrams from ShEx
- `shacl_validate.py <https://github.com/rudof-project/rudof/blob/master/python/examples/shacl_validate.py>`_ — SHACL validation examples
- `shex_validate.py <https://github.com/rudof-project/rudof/blob/master/python/examples/shex_validate.py>`_ — ShEx validation examples
- `compare_schemas.py <https://github.com/rudof-project/rudof/blob/master/python/examples/compare_schemas.py>`_ — Schema comparison