Search in sources :

Example 6 with BlankNode

use of org.apache.clerezza.commons.rdf.BlankNode in project stanbol by apache.

the class ClerezzaRDFParser method parse.

@Override
public RDFDataset parse(Object input) throws JsonLdError {
    count = 0;
    Map<BlankNode, String> bNodeMap = new HashMap<BlankNode, String>(1024);
    final RDFDataset result = new RDFDataset();
    if (input instanceof Graph) {
        for (Triple t : ((Graph) input)) {
            handleStatement(result, t, bNodeMap);
        }
    }
    //help gc
    bNodeMap.clear();
    return result;
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) HashMap(java.util.HashMap) BlankNode(org.apache.clerezza.commons.rdf.BlankNode)

Example 7 with BlankNode

use of org.apache.clerezza.commons.rdf.BlankNode in project stanbol by apache.

the class IndexedGraphTest method createGraph.

private static void createGraph(Collection<Triple> tc, int triples, Long seed) {
    Random rnd = new Random();
    if (seed != null) {
        rnd.setSeed(seed);
    }
    LiteralFactory lf = LiteralFactory.getInstance();
    //randoms are in the range [0..3]
    //literal
    double l = 1.0;
    //int
    double i = l / 3;
    //double
    double d = l * 2 / 3;
    //bNode
    double b = 2.0;
    //create new bNode
    double nb = b - (l * 2 / 3);
    double random;
    BlankNodeOrIRI subject = null;
    IRI predicate = null;
    List<IRI> predicateList = new ArrayList<IRI>();
    predicateList.add(RDF.first);
    predicateList.add(RDF.rest);
    predicateList.add(RDF.type);
    predicateList.add(RDFS.label);
    predicateList.add(RDFS.comment);
    predicateList.add(RDFS.range);
    predicateList.add(RDFS.domain);
    predicateList.add(FOAF.name);
    predicateList.add(FOAF.nick);
    predicateList.add(FOAF.homepage);
    predicateList.add(FOAF.age);
    predicateList.add(FOAF.depiction);
    String URI_PREFIX = "http://www.test.org/bigGraph/ref";
    Language DE = new Language("de");
    Language EN = new Language("en");
    Iterator<IRI> predicates = predicateList.iterator();
    List<BlankNode> bNodes = new ArrayList<BlankNode>();
    bNodes.add(new BlankNode());
    for (int count = 0; tc.size() < triples; count++) {
        random = rnd.nextDouble() * 3;
        if (random >= 2.5 || count == 0) {
            if (random <= 2.75) {
                subject = new IRI(URI_PREFIX + count);
            } else {
                int rndIndex = (int) ((random - 2.75) * bNodes.size() / (3.0 - 2.75));
                subject = bNodes.get(rndIndex);
            }
        }
        if (random > 2.0 || count == 0) {
            if (!predicates.hasNext()) {
                Collections.shuffle(predicateList, rnd);
                predicates = predicateList.iterator();
            }
            predicate = predicates.next();
        }
        if (random <= l) {
            //literal
            if (random <= i) {
                tc.add(new TripleImpl(subject, predicate, lf.createTypedLiteral(count)));
            } else if (random <= d) {
                tc.add(new TripleImpl(subject, predicate, lf.createTypedLiteral(random)));
            } else {
                Literal text;
                if (random <= i) {
                    text = new PlainLiteralImpl("Literal for " + count);
                } else if (random <= d) {
                    text = new PlainLiteralImpl("An English literal for " + count, EN);
                } else {
                    text = new PlainLiteralImpl("Ein Deutsches Literal für " + count, DE);
                }
                tc.add(new TripleImpl(subject, predicate, text));
            }
        } else if (random <= b) {
            //bnode
            BlankNode bnode;
            if (random <= nb) {
                bnode = new BlankNode();
                bNodes.add(bnode);
            } else {
                //>nb <b
                int rndIndex = (int) ((random - nb) * bNodes.size() / (b - nb));
                bnode = bNodes.get(rndIndex);
            }
            tc.add(new TripleImpl(subject, predicate, bnode));
        } else {
            //IRI
            tc.add(new TripleImpl(subject, predicate, new IRI(URI_PREFIX + count * random)));
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) ArrayList(java.util.ArrayList) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Random(java.util.Random) Language(org.apache.clerezza.commons.rdf.Language) Literal(org.apache.clerezza.commons.rdf.Literal) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 8 with BlankNode

use of org.apache.clerezza.commons.rdf.BlankNode in project stanbol by apache.

the class IndexedGraphTest method bNodeConsitency.

@Test
public void bNodeConsitency() {
    Graph mGraph = getEmptyGraph();
    final BlankNode bNode = new BlankNode() {

        @Override
        public int hashCode() {
            return -1;
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof BlankNode;
        }
    };
    final BlankNode bNodeClone = new BlankNode() {

        @Override
        public int hashCode() {
            return -1;
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof BlankNode;
        }
    };
    mGraph.add(new TripleImpl(bNode, uriRef1, uriRef2));
    mGraph.add(new TripleImpl(bNodeClone, uriRef2, uriRef3));
    BlankNodeOrIRI bNodeBack = mGraph.filter(null, uriRef1, uriRef2).next().getSubject();
    Assert.assertEquals("The bnode we get back is not equals to the one we added", bNode, bNodeBack);
    BlankNodeOrIRI bNodeBack2 = mGraph.filter(null, uriRef2, uriRef3).next().getSubject();
    Assert.assertEquals("The returnned bnodes are no longer equals", bNodeBack, bNodeBack2);
    Assert.assertTrue("Not finding a triple when searching with equal bNode", mGraph.filter(bNodeBack, uriRef2, null).hasNext());
}
Also used : SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Test(org.junit.Test) GraphTest(org.apache.clerezza.rdf.core.test.GraphTest)

Example 9 with BlankNode

use of org.apache.clerezza.commons.rdf.BlankNode in project stanbol by apache.

the class UserResource method createRole.

/**
     * Creates a new role wit the the specified role name
     *
     * @param newUserName
     * @return user node in system graph
     */
private GraphNode createRole(String newRoleName, String comment) {
    BlankNode subject = new BlankNode();
    GraphNode roleNode = new GraphNode(subject, systemGraph);
    roleNode.addProperty(RDF.type, PERMISSION.Role);
    roleNode.addProperty(DC.title, new PlainLiteralImpl(newRoleName));
    roleNode.addProperty(RDFS.comment, new PlainLiteralImpl(comment));
    return roleNode;
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) GraphNode(org.apache.clerezza.rdf.utils.GraphNode)

Example 10 with BlankNode

use of org.apache.clerezza.commons.rdf.BlankNode in project stanbol by apache.

the class UserResource method addPermission.

// public final static String permissionsBase = "urn:x-localhost/role/";
private GraphNode addPermission(GraphNode subjectNode, String permissionString) {
    if (hasPermission(subjectNode, permissionString)) {
        return subjectNode;
    }
    GraphNode permissionNode = new GraphNode(new BlankNode(), systemGraph);
    permissionNode.addProperty(RDF.type, PERMISSION.Permission);
    // permissionNode.addProperty(DC.title, new PlainLiteralImpl(permissionName));
    subjectNode.addProperty(PERMISSION.hasPermission, permissionNode.getNode());
    permissionNode.addProperty(PERMISSION.javaPermissionEntry, new PlainLiteralImpl(permissionString));
    return subjectNode;
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) GraphNode(org.apache.clerezza.rdf.utils.GraphNode)

Aggregations

BlankNode (org.apache.clerezza.commons.rdf.BlankNode)18 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)11 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)10 Graph (org.apache.clerezza.commons.rdf.Graph)6 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)6 IRI (org.apache.clerezza.commons.rdf.IRI)5 Triple (org.apache.clerezza.commons.rdf.Triple)5 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)5 HashMap (java.util.HashMap)4 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)4 GraphNode (org.apache.clerezza.rdf.utils.GraphNode)3 Statement (org.ontoware.rdf2go.model.Statement)2 BlankNode (org.ontoware.rdf2go.model.node.BlankNode)2 RDFDataset (com.github.jsonldjava.core.RDFDataset)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1