Search in sources :

Example 1 with BlankNode

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

the class ClerezzaRDFUtils method urifyBlankNodes.

public static void urifyBlankNodes(Graph model) {
    HashMap<BlankNode, IRI> blankNodeMap = new HashMap<BlankNode, IRI>();
    Graph remove = new SimpleGraph();
    Graph add = new SimpleGraph();
    for (Triple t : model) {
        BlankNodeOrIRI subj = t.getSubject();
        RDFTerm obj = t.getObject();
        IRI pred = t.getPredicate();
        boolean match = false;
        if (subj instanceof BlankNode) {
            match = true;
            IRI ru = blankNodeMap.get(subj);
            if (ru == null) {
                ru = createRandomUri();
                blankNodeMap.put((BlankNode) subj, ru);
            }
            subj = ru;
        }
        if (obj instanceof BlankNode) {
            match = true;
            IRI ru = blankNodeMap.get(obj);
            if (ru == null) {
                ru = createRandomUri();
                blankNodeMap.put((BlankNode) obj, ru);
            }
            obj = ru;
        }
        if (match) {
            remove.add(t);
            add.add(new TripleImpl(subj, pred, obj));
        }
    }
    model.removeAll(remove);
    model.addAll(add);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) HashMap(java.util.HashMap) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 2 with BlankNode

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

the class BlankNodeAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.BlankNodeAtom tmp = (org.apache.stanbol.rules.manager.atoms.BlankNodeAtom) ruleAtom;
    UriRefOrVariable subject;
    UriRefOrVariable predicate;
    ResourceOrVariable object;
    IObjectAtom argument2UriResource = tmp.getArgument2();
    IObjectAtom argument1UriResource = tmp.getArgument1();
    ClerezzaSparqlObject subjectCSO = (ClerezzaSparqlObject) adapter.adaptTo(argument2UriResource, ConstructQuery.class);
    ClerezzaSparqlObject predicateCSO = (ClerezzaSparqlObject) adapter.adaptTo(argument1UriResource, ConstructQuery.class);
    subject = new UriRefOrVariable((IRI) subjectCSO.getClerezzaObject());
    predicate = new UriRefOrVariable((IRI) predicateCSO.getClerezzaObject());
    object = new ResourceOrVariable(new BlankNode());
    return (T) new ClerezzaSparqlObject(new SimpleTriplePattern(subject, predicate, object));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ResourceOrVariable(org.apache.clerezza.rdf.core.sparql.query.ResourceOrVariable) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) UriRefOrVariable(org.apache.clerezza.rdf.core.sparql.query.UriRefOrVariable) IObjectAtom(org.apache.stanbol.rules.manager.atoms.IObjectAtom) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) ClerezzaSparqlObject(org.apache.stanbol.rules.adapters.clerezza.ClerezzaSparqlObject) SimpleTriplePattern(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleTriplePattern)

Example 3 with BlankNode

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

the class ResourceMapping method apply.

@Override
public boolean apply(Graph graph, BlankNodeOrIRI subject, Metadata metadata) {
    boolean added = false;
    BlankNodeOrIRI s = new BlankNode();
    mappingLogger.log(subject, ontProperty, null, s);
    if (!required.isEmpty()) {
        Graph g = new SimpleGraph();
        for (Mapping m : required) {
            if (!m.apply(g, s, metadata)) {
                return false;
            }
        }
        graph.addAll(g);
        added = true;
    }
    for (Mapping m : optional) {
        if (m.apply(graph, s, metadata)) {
            added = true;
        }
    }
    if (added) {
        for (Mapping m : additional) {
            m.apply(graph, s, metadata);
        }
        graph.add(new TripleImpl(subject, ontProperty, s));
    }
    return added;
}
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) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 4 with BlankNode

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

the class TestMetaxaCore method printTriples.

/**
     * This prints out the Stanbol Enhancer triples that would be created for the metadata
     * contained in the given model.
     *
     * @param m a {@link Model}
     *
     * @return an {@code int} with the number of added triples
     */
private int printTriples(Model m) {
    int tripleCounter = 0;
    HashMap<BlankNode, BlankNode> blankNodeMap = new HashMap<BlankNode, BlankNode>();
    ClosableIterator<Statement> it = m.iterator();
    while (it.hasNext()) {
        Statement oneStmt = it.next();
        BlankNodeOrIRI subject = (BlankNodeOrIRI) MetaxaEngine.asClerezzaResource(oneStmt.getSubject(), blankNodeMap);
        IRI predicate = (IRI) MetaxaEngine.asClerezzaResource(oneStmt.getPredicate(), blankNodeMap);
        RDFTerm object = MetaxaEngine.asClerezzaResource(oneStmt.getObject(), blankNodeMap);
        if (null != subject && null != predicate && null != object) {
            Triple t = new TripleImpl(subject, predicate, object);
            LOG.debug("adding " + t);
            tripleCounter++;
        } else {
            LOG.debug("skipped " + oneStmt.toString());
        }
    }
    it.close();
    return tripleCounter;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) Statement(org.ontoware.rdf2go.model.Statement) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNode(org.ontoware.rdf2go.model.node.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 5 with BlankNode

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

the class ClerezzaTripleCallback method getBlankNode.

private BlankNode getBlankNode(String s) {
    if (bNodeMap.containsKey(s)) {
        return bNodeMap.get(s);
    } else {
        final BlankNode result = new BlankNode();
        bNodeMap.put(s, result);
        return result;
    }
}
Also used : BlankNode(org.apache.clerezza.commons.rdf.BlankNode)

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