Skip to content
Snippets Groups Projects
Commit f74ff69d authored by Danielle Maree McKenney's avatar Danielle Maree McKenney
Browse files

Add constraint validation example with dummy data

parent f7d7d003
No related branches found
No related tags found
No related merge requests found
.idea/ .idea/
.DS_Store .DS_Store
target/ target/
*.iml *.iml
\ No newline at end of file .project
.classpath
.factorypath
.settings/
bin/
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Make sure java 17 is installed. Make sure java 17 is installed.
Make sure that Maven 3.8.3 is installed on your computer with `mvn --version`. If it is not, follow the instructions here: https://maven.apache.org/install.html. Make sure that Maven 3.8.3 is installed on your computer with `mvn --version`. If it is not, follow the instructions here: https://maven.apache.org/install.html.
Test that things are up and running and can work on your machine. Run mvn package in the kg-shape-project directory, and you should see a build success message. Test that things are up and running and can work on your machine. Run `mvn package` in the kg-shape-project directory, and you should see a build success message.
Then from the same directory, in the command line run: Then from the same directory, in the command line run:
`java -cp target/kg-shape-project-1.0-SNAPSHOT.jar com.kgshape.app.App` `java -cp target/kg-shape-project-1.0-SNAPSHOT.jar com.kgshape.app.App`
......
package com.kgshape.app;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.graph.Graph;
import org.apache.jena.rdf.model.*;
import org.apache.jena.shex.*;
import org.apache.jena.shex.sys.ShexLib;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class Constraints {
public static void main( String[] args ) {
// Create schema (these are the actual constraints that we've defined)
String shapeString = """
PREFIX : <http://example.org/>
PREFIX schema: <http://example.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:User IRI {\s
schema:name xsd:string ;
schema:lastName xsd:string ;
schema:birthDate xsd:date? ;
schema:age xsd:long? ;
}""";
ShexSchema shapes = Shex.schemaFromString(shapeString);
// Create the shape map; this will define which parts of our graph should be checked against which parts of our schema
String shapeMapString = "<http://example.org/alice> @<http://example.org/User>, <http://example.org/bob> @<http://example.org/User>";
String baseUrl= "http://example.org/";
ShexMap shapeMap = Shex.shapeMapFromString(shapeMapString, baseUrl);
// Create some dummy data; in our real implementation we will have a model from a sparql query
Model model = ModelFactory.createDefaultModel();
String personURI = "http://example.org/alice";
Resource alice = model.createResource(personURI);
Property nameProperty = model.createProperty("http://example.org/", "name");
Property lastNameProperty = model.createProperty("http://example.org/", "lastName");
Property dateProperty = model.createProperty("http://example.org/", "date");
Property ageProperty = model.createProperty("http://example.org/", "age");
Property extraProperty = model.createProperty("http://example.org/", "extra");
String name = "Alice";
alice.addProperty(nameProperty, name);
alice.addProperty(lastNameProperty, "Sumin");
alice.addProperty(dateProperty, "1980-03-10");
alice.addLiteral(ageProperty, 100);
String bobURI = "http://example.org/bob";
Resource bob = model.createResource(bobURI);
String bobName = "Bob";
bob.addProperty(nameProperty, bobName);
bob.addProperty(lastNameProperty, "Sumin2");
bob.addLiteral(ageProperty, 10);
// todo this type checking for date doesn't work.
bob.addLiteral(dateProperty, "notADate");
// Note: having an extra property that isn't defined in the shape does not break anything
bob.addProperty(extraProperty, "x");
Resource userGraph = model.createResource(baseUrl+"User");
Graph g =DatasetGraphFactory.create().getGraph(userGraph.asNode());
g.add(alice.getProperty(nameProperty).asTriple());
g.add(alice.getProperty(lastNameProperty).asTriple());
g.add(alice.getProperty(dateProperty).asTriple());
g.add(alice.getProperty(ageProperty).asTriple());
g.add(bob.getProperty(nameProperty).asTriple());
g.add(bob.getProperty(lastNameProperty).asTriple());
g.add(bob.getProperty(dateProperty).asTriple());
g.add(bob.getProperty(ageProperty).asTriple());
ShexReport report = ShexValidator.get().validate(g, shapes, shapeMap);
// Print report
ShexLib.printReport(report);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment