Skip to content

Commit

Permalink
add minCount 1st stage
Browse files Browse the repository at this point in the history
Signed-off-by: Heshan Jayasinghe <shanujse@gmail.com>

Signed-off-by: Heshan Jayasinghe <shanujse@gmail.com>
  • Loading branch information
heshanjse committed Jun 20, 2017
1 parent d6e122a commit 05d880f
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 68 deletions.
Expand Up @@ -10,7 +10,7 @@
*/
public class MinCountPropertyShape extends PathPropertyShape{

Integer minCount;
public Integer minCount;

public MinCountPropertyShape(Resource next, SailRepositoryConnection connection) {
super(next,connection);
Expand Down
2 changes: 1 addition & 1 deletion core/shacl/src/main/java/org/eclipse/rdf4j/AST/Path.java
Expand Up @@ -9,7 +9,7 @@
* Created by heshanjayasinghe on 6/10/17.
*/
public class Path implements Resource {
Resource path;
public Resource path;
Resource id;
SailRepositoryConnection connection;

Expand Down
Expand Up @@ -9,7 +9,7 @@
* Created by heshanjayasinghe on 6/11/17.
*/
public class PathPropertyShape extends PropertyShape {
Path path;
public Path path;

public PathPropertyShape(Resource id, SailRepositoryConnection connection) {
super(id, connection);
Expand Down
Expand Up @@ -20,7 +20,7 @@ public PropertyShape(Resource id,SailRepositoryConnection connection) {
}


static class Factory{
public static class Factory{
List<PropertyShape> ret;
List< PropertyShape > getProprtyShapes(Resource propertyShapeId, SailRepositoryConnection connection){
ret = new ArrayList<>();
Expand Down
47 changes: 29 additions & 18 deletions core/shacl/src/main/java/org/eclipse/rdf4j/AST/Shape.java
@@ -1,51 +1,62 @@
package org.eclipse.rdf4j.AST;

import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.vocabulary.SH;

import java.util.ArrayList;
import java.util.List;

/**
* Created by heshanjayasinghe on 6/10/17.
*/
public class Shape {
Resource id;
SailRepositoryConnection connection;
List<PropertyShape> propertyShapes;
TargetClass targetClass;


public Shape(Resource id, SailRepositoryConnection connection) {
this.id = id;
this.connection = connection;
propertyShapes = new PropertyShape.Factory().getProprtyShapes(id,connection);
ValueFactory vf = connection.getValueFactory();
if(connection.hasStatement(id, vf.createIRI(SH.BASE_URI, "targetClass"), null, true)) {
targetClass = new TargetClass(id, connection);
}

}

static class Factory{
Shape shape;
Shape getShapes(Resource id, SailRepositoryConnection connection){
if(!hasShape()){
shape = new Shape(id ,connection);
public static class Factory{

private List<Shape> shapes;
public List<Shape> getShapes(SailRepositoryConnection connection) {
shapes = new ArrayList<>();

//SHACL.SHAPE --> RDFS.RESOURCE
RepositoryResult<Statement> statements = connection.getStatements(null, RDF.TYPE, SHACL.SHAPE);
while (statements.hasNext()) {
Resource shapeId = statements.next().getSubject();
if (hasTargetClass(shapeId, connection)) {
shapes.add(new TargetClass(shapeId, connection));
} else {
shapes.add(new Shape(shapeId, connection));
}
return shapes;
}
return shape;
return shapes;
}

private boolean hasShape() {
if (shape == null)
return false;
return true;
private boolean hasTargetClass(Resource shapeId, SailRepositoryConnection connection) {
for (Shape shape:shapes) {
if(shape instanceof TargetClass)
return true;
}
return false;
}
}


}



}
56 changes: 42 additions & 14 deletions core/shacl/src/main/java/org/eclipse/rdf4j/Main.java
@@ -1,27 +1,55 @@
package org.eclipse.rdf4j;

import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.sail.NotifyingSailConnection;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.validation.ShaclSail;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {


public static void main(String[] args) {
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
shaclSail.initialize();

try (NotifyingSailConnection connection = shaclSail.getConnection()) {
connection.begin();
connection.addStatement(RDFS.CLASS, RDF.TYPE, RDFS.RESOURCE);
connection.commit();
}
try (NotifyingSailConnection connection = shaclSail.getConnection()) {
connection.begin();
connection.removeStatements(RDFS.CLASS, RDF.TYPE, RDFS.RESOURCE);
connection.commit();
SailRepository shacl = new SailRepository(new MemoryStore());
shacl.initialize();

try (SailRepositoryConnection connection = shacl.getConnection()) {
RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
rdfParser.setRDFHandler(new StatementCollector(){
@Override
public void handleStatement(Statement st) {
connection.add(st);
}
});

String filename = "shacl.ttl";
InputStream input = ShaclSail.class.getResourceAsStream("/" + filename);
rdfParser.parse(input, "");
RepositoryResult<Statement> result = connection.getStatements(null, null, null);
while (result.hasNext()) {
Statement st = result.next();
System.out.println("db contains: " + st);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

ShaclSail shaclSail = new ShaclSail(new MemoryStore(),shacl);
shaclSail.initialize();
System.out.println("done");

}
}
21 changes: 21 additions & 0 deletions core/shacl/src/main/java/org/eclipse/rdf4j/Main2.java
@@ -0,0 +1,21 @@
package org.eclipse.rdf4j;

import org.eclipse.rdf4j.AST.MinCountPropertyShape;
import org.eclipse.rdf4j.AST.Path;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;

/**
* Created by heshanjayasinghe on 6/20/17.
*/
public class Main2 {
static ValueFactory vf = SimpleValueFactory.getInstance();

public static void main(String[] args) {
MinCountPropertyShape minCountPropertyShape = new MinCountPropertyShape(null,null);
minCountPropertyShape.minCount = 1;
Path path = new Path(null,null);
path.path = vf.createIRI("http://example.org/ssn");
minCountPropertyShape.path =path;
}
}
Expand Up @@ -5,8 +5,10 @@
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.TreeModel;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.sail.*;
import org.eclipse.rdf4j.sail.helpers.NotifyingSailWrapper;
import org.eclipse.rdf4j.sail.memory.MemoryStore;

import java.util.List;

Expand All @@ -18,14 +20,23 @@ public class ShaclSail extends NotifyingSailWrapper {
List<Shape> shapes;
private Model newStatements;
private boolean statementsRemoved;
private SailRepository shacl;

public ShaclSail(NotifyingSail memoryStore) {
super(memoryStore);
}

@Override
public void setBaseSail(Sail baseSail) {
super.setBaseSail(baseSail);
public ShaclSail(MemoryStore memoryStore,SailRepository shacl) {
super(memoryStore);
this.shacl = shacl;
}

public void initialize()throws SailException{
super.initialize();

try(SailRepositoryConnection connection = shacl.getConnection()){
shapes = new Shape.Factory().getShapes(connection);
}
}

@Override
Expand Down
Expand Up @@ -34,4 +34,10 @@ protected Model createModel(){
return new TreeModel();
};

/*public void validate(){
Plan plan = createPlan(sail.shapes,this);
plan.validate(this);
}*/

}
46 changes: 17 additions & 29 deletions core/shacl/src/main/resources/shacl.ttl
@@ -1,32 +1,20 @@
@prefix ex: <http://example.org/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
ex:Picasso a ex:Artist ;
foaf:firstName "Pablo" ;
foaf:surname "Picasso";
ex:creatorOf ex:guernica ;
ex:homeAddress _:node1 .
@base <http://example.com/ns> .
@prefix ex: <http://example.com/ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

_:node1 ex:street "31 Art Gallery" ;
ex:city "Madrid" ;
ex:country "Spain" .
<http://example.com/ns>
rdf:type owl:Ontology ;
owl:imports <http://example.com/person-ontology> .

ex:guernica a ex:Painting ;
rdfs:label "Guernica";
ex:technique "oil on canvas".
ex:PersonShape
a sh:Shape ;
sh:targetClass ex:Person ;
sh:property [
sh:path ex:ssn ;
sh:minCount 1 ;

ex:VanGogh a ex:Artist ;
foaf:firstName "Vincent" ;
foaf:surname "van Gogh";
ex:creatorOf ex:starryNight, ex:sunflowers, ex:potatoEaters .

ex:starryNight a ex:Painting ;
ex:technique "oil on canvas";
rdfs:label "Starry Night" .

ex:sunflowers a ex:Painting ;
ex:technique "oil on canvas";
rdfs:label "Sunflowers" .

ex:potatoEaters a ex:Painting ;
ex:technique "oil on canvas";
rdfs:label "The Potato Eaters" .
] .

0 comments on commit 05d880f

Please sign in to comment.