Search in sources :

Example 1 with LiteralLabel

use of com.hp.hpl.jena.graph.impl.LiteralLabel in project stanbol by apache.

the class RdfIndexingSource method processValue.

/**
     * Processes a {@link Node} and adds the according value to the parsed
     * Representation.
     * @param value The node to convert to an value for the Representation
     * @param source the representation (MUST NOT be <code>null</code>
     * @param field the field (MUST NOT be <code>null</code>)
     */
private void processValue(Node value, Representation source, String field) {
    if (value == null) {
        log.warn("Encountered NULL value for field {} and entity {}", field, source.getId());
    } else if (value.isURI()) {
        //add a reference
        source.addReference(field, value.getURI());
    } else if (value.isLiteral()) {
        //add a value or a text depending on the dataType
        LiteralLabel ll = value.getLiteral();
        //            log.debug("LL: lexical {} | value {} | dataType {} | language {}",
        //                new Object[]{ll.getLexicalForm(),ll.getValue(),ll.getDatatype(),ll.language()});
        //if the dataType == null , than we can expect a plain literal
        RDFDatatype dataType = ll.getDatatype();
        if (dataType != null) {
            //add a value
            Object literalValue;
            try {
                literalValue = ll.getValue();
                if (literalValue instanceof BaseDatatype.TypedValue) {
                    //used for unknown data types
                    // -> in such cases just use the lexical type
                    String lexicalValue = ((BaseDatatype.TypedValue) literalValue).lexicalValue;
                    if (lexicalValue != null && !lexicalValue.isEmpty()) {
                        source.add(field, lexicalValue);
                    }
                } else if (literalValue instanceof XSDDateTime) {
                    //Entityhub uses the time
                    source.add(field, ((XSDDateTime) literalValue).asCalendar().getTime());
                } else if (literalValue instanceof XSDDuration) {
                    String duration = literalValue.toString();
                    if (duration != null && !duration.isEmpty()) {
                        source.add(field, literalValue.toString());
                    }
                } else if (!ll.getLexicalForm().isEmpty()) {
                    source.add(field, literalValue);
                }
            //else ignore literals that are empty
            } catch (DatatypeFormatException e) {
                log.warn(" Unable to convert {} to {} -> use lecicalForm", ll.getLexicalForm(), ll.getDatatype());
                literalValue = ll.getLexicalForm();
            }
        } else {
            //add a text
            String lexicalForm = ll.getLexicalForm();
            if (lexicalForm != null && !lexicalForm.isEmpty()) {
                String language = ll.language();
                if (language != null && language.length() < 1) {
                    language = null;
                }
                source.addNaturalText(field, lexicalForm, language);
            }
        //else ignore empty literals
        }
    // "" is parsed if there is no language
    } else if (value.isBlank()) {
        if (bnodePrefix != null) {
            //STANBOL-765: convert Bnodes to URIs
            StringBuilder sb = new StringBuilder(bnodePrefix);
            sb.append(value.getBlankNodeId().getLabelString());
            source.addReference(field, sb.toString());
        } else {
            logIgnoredBnode(log, source, field, value);
        }
    } else {
        log.warn("ignoreing value {} for field {} and RDFTerm {} because it is of an unsupported type!", new Object[] { value, field, source.getId() });
    }
//end different value node type
}
Also used : DatatypeFormatException(com.hp.hpl.jena.datatypes.DatatypeFormatException) XSDDateTime(com.hp.hpl.jena.datatypes.xsd.XSDDateTime) XSDDuration(com.hp.hpl.jena.datatypes.xsd.XSDDuration) RDFDatatype(com.hp.hpl.jena.datatypes.RDFDatatype) LiteralLabel(com.hp.hpl.jena.graph.impl.LiteralLabel)

Example 2 with LiteralLabel

use of com.hp.hpl.jena.graph.impl.LiteralLabel in project stanbol by apache.

the class StartsWithAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    Node arg1Node = null;
    Node arg2Node = null;
    org.apache.stanbol.rules.manager.atoms.EndsWithAtom tmp = (org.apache.stanbol.rules.manager.atoms.EndsWithAtom) ruleAtom;
    ExpressionAtom argument = tmp.getArgument();
    ExpressionAtom term = tmp.getTerm();
    ClauseEntry clauseEntry1 = adapter.adaptTo(argument, Rule.class);
    ClauseEntry clauseEntry2 = adapter.adaptTo(term, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    if (clauseEntry1 instanceof HigherOrderClauseEntry) {
        arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries());
    } else if (clauseEntry1 instanceof NodeClauseEntry) {
        arg1Node = ((NodeClauseEntry) clauseEntry1).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (clauseEntry2 instanceof HigherOrderClauseEntry) {
        arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries());
    } else if (clauseEntry2 instanceof NodeClauseEntry) {
        Node tmpNode = ((NodeClauseEntry) clauseEntry2).getNode();
        LiteralLabel literalLabel = tmpNode.getLiteral();
        String lexicalForm = literalLabel.getLexicalForm();
        StringBuilder sb = new StringBuilder();
        sb.append("^");
        sb.append(lexicalForm);
        String regex = sb.toString();
        arg2Node = Node_RuleVariable.createLiteral(regex);
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(arg1Node);
    nodes.add(arg2Node);
    return (T) new Functor("regex", nodes, new BuiltinRegistry());
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) BuiltinRegistry(com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry) LiteralLabel(com.hp.hpl.jena.graph.impl.LiteralLabel) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)

Example 3 with LiteralLabel

use of com.hp.hpl.jena.graph.impl.LiteralLabel in project stanbol by apache.

the class EndsWithAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    Node arg1Node = null;
    Node arg2Node = null;
    org.apache.stanbol.rules.manager.atoms.EndsWithAtom tmp = (org.apache.stanbol.rules.manager.atoms.EndsWithAtom) ruleAtom;
    ExpressionAtom argument = tmp.getArgument();
    ExpressionAtom term = tmp.getTerm();
    ClauseEntry clauseEntry1 = adapter.adaptTo(argument, Rule.class);
    ClauseEntry clauseEntry2 = adapter.adaptTo(term, Rule.class);
    List<ClauseEntry> clauseEntries = new ArrayList<ClauseEntry>();
    if (clauseEntry1 instanceof HigherOrderClauseEntry) {
        arg1Node = ((HigherOrderClauseEntry) clauseEntry1).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry1).getClauseEntries());
    } else if (clauseEntry1 instanceof NodeClauseEntry) {
        arg1Node = ((NodeClauseEntry) clauseEntry1).getNode();
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (clauseEntry2 instanceof HigherOrderClauseEntry) {
        arg2Node = ((HigherOrderClauseEntry) clauseEntry2).getBindableNode();
        clauseEntries.addAll(((HigherOrderClauseEntry) clauseEntry2).getClauseEntries());
    } else if (clauseEntry2 instanceof NodeClauseEntry) {
        Node tmpNode = ((NodeClauseEntry) clauseEntry2).getNode();
        LiteralLabel literalLabel = tmpNode.getLiteral();
        String lexicalForm = literalLabel.getLexicalForm();
        StringBuilder sb = new StringBuilder();
        sb.append(lexicalForm);
        sb.append("$");
        String regex = sb.toString();
        arg2Node = Node_RuleVariable.createLiteral(regex);
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    java.util.List<Node> nodes = new ArrayList<Node>();
    nodes.add(arg1Node);
    nodes.add(arg2Node);
    return (T) new Functor("regex", nodes, BuiltinRegistry.theRegistry);
}
Also used : Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) Functor(com.hp.hpl.jena.reasoner.rulesys.Functor) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) ClauseEntry(com.hp.hpl.jena.reasoner.rulesys.ClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) HigherOrderClauseEntry(org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry) NodeClauseEntry(org.apache.stanbol.rules.adapters.jena.NodeClauseEntry) LiteralLabel(com.hp.hpl.jena.graph.impl.LiteralLabel)

Aggregations

LiteralLabel (com.hp.hpl.jena.graph.impl.LiteralLabel)3 Node (com.hp.hpl.jena.graph.Node)2 ClauseEntry (com.hp.hpl.jena.reasoner.rulesys.ClauseEntry)2 Functor (com.hp.hpl.jena.reasoner.rulesys.Functor)2 ArrayList (java.util.ArrayList)2 HigherOrderClauseEntry (org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry)2 NodeClauseEntry (org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)2 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)2 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)2 DatatypeFormatException (com.hp.hpl.jena.datatypes.DatatypeFormatException)1 RDFDatatype (com.hp.hpl.jena.datatypes.RDFDatatype)1 XSDDateTime (com.hp.hpl.jena.datatypes.xsd.XSDDateTime)1 XSDDuration (com.hp.hpl.jena.datatypes.xsd.XSDDuration)1 BuiltinRegistry (com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry)1