com.hp.hpl.jena.graph.NodeFactory Java Examples

The following examples show how to use com.hp.hpl.jena.graph.NodeFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IsLiteralTestEvaluator.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    TestingSelector testingSelector = (TestingSelector) nodeSelector;
    FunctionTest functionTest = (FunctionTest) testingSelector.getTest();

    if(functionTest.getArgSelectors().get(0) instanceof PropertySelector) {
        PropertySelector arg = (PropertySelector) functionTest.getArgSelectors().get(0);
        PropertySelector delegate = (PropertySelector) testingSelector.getDelegate();

        Var target = Var.alloc(VarIDGenerator.createID());
        elementGroup.addTriplePattern(new Triple(var.asNode(), NodeFactory.createURI(delegate.getProperty().toString()), target));

        Var selector = Var.alloc(VarIDGenerator.createID());
        elementGroup.addTriplePattern(new Triple(target.asNode(), NodeFactory.createURI(arg.getProperty().toString()), selector.asNode()));

        elementGroup.addElementFilter(new ElementFilter(new E_IsLiteral(new ExprVar(selector))));

        return selector;
    } else {
        throw new IllegalStateException("Argument of function isLiteral has to be a PropertySelector");
    }
}
 
Example #2
Source File: IsATestEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    TestingSelector testingSelector = (TestingSelector) nodeSelector;
    NodeTest nodeTest = testingSelector.getTest();
    Var delVar = LDPathEvaluator.evaluate(testingSelector.getDelegate(), elementGroup, var, evaluatorConfiguration);

    IsATest isATest = (IsATest) nodeTest;
    elementGroup.addTriplePattern(new Triple(delVar.asNode(), RDF.type.asNode(), NodeFactory.createURI(isATest.getPathExpression(new SesameValueBackend()).replace("<", "").replace(">", "").replaceFirst("is-a ", ""))));

    return delVar;
}
 
Example #3
Source File: IsATestEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Expr evaluate(NodeTest nodeTest, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    IsATest isATest = (IsATest) nodeTest;
    Var tmpVar = Var.alloc(Var.alloc(VarIDGenerator.createID()));
    elementGroup.addTriplePattern(new Triple(var.asNode(), RDF.type.asNode(), tmpVar.asNode()));
    return new E_Equals(new ExprVar(tmpVar.asNode()), new NodeValueNode(NodeFactory.createURI(isATest.getPathExpression(new SesameValueBackend()).replace("<", "").replace(">", "").replaceFirst("is-a ", ""))));
}
 
Example #4
Source File: PathEqualityTestEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Expr evaluate(NodeTest nodeTest, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    PathEqualityTest pathEqualityTest = (PathEqualityTest) nodeTest;
    Var tmpVar =  LDPathEvaluator.evaluate(pathEqualityTest.getPath(), elementGroup, var, evaluatorConfiguration);
    if(pathEqualityTest.getNode() instanceof org.openrdf.model.impl.LiteralImpl) {
        return new E_Equals(new ExprVar(tmpVar.asNode()), new NodeValueNode(NodeFactory.createLiteral(((LiteralImpl) pathEqualityTest.getNode()).getLabel().toString())));
    } else {
        return new E_Equals(new ExprVar(tmpVar.asNode()), new NodeValueNode(NodeFactory.createURI(pathEqualityTest.getNode().toString())));
    }
}
 
Example #5
Source File: PropertySelectorEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    PropertySelector propertySelector = (PropertySelector) nodeSelector;
    if (propertySelector instanceof WildcardSelector) {
        throw new IllegalStateException(propertySelector.getClass() + " is not supported.");
    }

    Var id = Var.alloc(VarIDGenerator.createID());
    elementGroup.addTriplePattern(new Triple(var.asNode(), NodeFactory.createURI(propertySelector.getProperty().toString()), id.asNode()));

    return id;
}
 
Example #6
Source File: ReversePropertySelectorEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    ReversePropertySelector reversePropertySelector = (ReversePropertySelector) nodeSelector;
    Var id = Var.alloc(VarIDGenerator.createID());
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(new TriplePath(var.asNode(), new P_Inverse(new P_Link(NodeFactory.createURI(reversePropertySelector.getProperty().toString()))), id.asNode()));
    ElementGroup group = new ElementGroup();
    group.addElement(epb);
    elementGroup.addElement(group);
    return id;
}
 
Example #7
Source File: RecursivePathSelectorEvaluator.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public Var evaluate(NodeSelector nodeSelector, ElementGroup elementGroup, Var var, LDPathEvaluatorConfiguration evaluatorConfiguration) {
    RecursivePathSelector recursivePathSelector = (RecursivePathSelector) nodeSelector;

    Var id = Var.alloc(VarIDGenerator.createID());
    ElementPathBlock epb = new ElementPathBlock();
    String pathExpression = recursivePathSelector.getPathExpression(new SesameValueBackend());
    /**
     * initial pathExpression contains:
     *      (<http://www.w3.org/ns/oa#hasBody> / <http://www.example.com/schema#recursiveBodyValue>)*
     *
     * Because P_ZeroOrMore and so one, creates the same expression we have to strip the redundant chars.
     */
    String strippedPathExpression = pathExpression.substring(2, pathExpression.length() -3 );

    TriplePath triplePath = null;
    if (pathExpression.contains("*")) {
        triplePath = new TriplePath(var.asNode(), new P_ZeroOrMore1(new P_Link(NodeFactory.createURI(strippedPathExpression))), id.asNode());
    } else if (pathExpression.contains("+")) {
        triplePath = new TriplePath(var.asNode(), new P_OneOrMore1(new P_Link(NodeFactory.createURI(strippedPathExpression))), id.asNode());
    } else {
        throw new IllegalStateException("Only ZeroOrMorePath(*), OneOrMorePath(+) path selectors are currently supported.");
    }

    epb.addTriple(triplePath);
    ElementGroup group = new ElementGroup();
    group.addElement(epb);
    elementGroup.addElement(group);
    return id;
}
 
Example #8
Source File: EvalQuery.java    From anno4j with Apache License 2.0 5 votes vote down vote up
public static <T extends ResourceObject> Query evaluate(QueryServiceConfiguration queryServiceDTO, URI rootType) throws ParseException {

        Query query = QueryFactory.make();
        query.setQuerySelectType();

        ElementGroup elementGroup = new ElementGroup();

        Var objectVar = Var.alloc("root");

        // Creating and adding the first triple - could be something like: "?objectVar rdf:type oa:Annotation
        Triple t1 = new Triple(objectVar, RDF.type.asNode(), NodeFactory.createURI(rootType.toString()));
        elementGroup.addTriplePattern(t1);

        // Evaluating the criteria
        for (Criteria c : queryServiceDTO.getCriteria()) {
            SesameValueBackend backend = new SesameValueBackend();

            LdPathParser parser = new LdPathParser(backend, queryServiceDTO.getConfiguration(), new StringReader(c.getLdpath()));
            Var var = LDPathEvaluator.evaluate(parser.parseSelector(queryServiceDTO.getPrefixes()), elementGroup, objectVar, queryServiceDTO.getEvaluatorConfiguration());

            if (c.getConstraint() != null) {
                String resolvedConstraint = resolveConstraintPrefix(c.getConstraint(), queryServiceDTO, parser);
                EvalComparison.evaluate(elementGroup, c, var, resolvedConstraint);
            }
        }

        // Adding all generated patterns to the query object
        query.setQueryPattern(elementGroup);

        // Choose what we want so select - SELECT ?annotation in this case
        query.addResultVar(objectVar);

        // Setting the default prefixes, like rdf: or dc:
        query.getPrefixMapping().setNsPrefixes(queryServiceDTO.getPrefixes());

        return query;
    }
 
Example #9
Source File: LearningSparql_SELECT_ITCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
/**
 * Curiously the Jena memory mode fails while SolRDF returns expected results
 */
@Test
@Ignore
public void from() throws Exception {
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.1#"), "ex069.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.2#"), "ex122.ttl"));
	
	selectTest(misteryGuest("ex123.rq"));		
}
 
Example #10
Source File: LearningSparql_SELECT_ITCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void fromNamed() throws Exception {
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.1#"), "ex069.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.2#"), "ex122.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.3#"), "ex125.ttl"));
	
	selectTest(misteryGuest("ex126.rq"));		
}
 
Example #11
Source File: FromAndFromNamedClauses_ITCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
/**
 * Loads data. 
 * 
 * @throws Exception hopefully never.
 */
@Before
public void loadData() throws Exception {
	load(misteryGuest(null, NodeFactory.createURI("http://grapha.com"), "one_triple_1.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://graphb.com"), "one_triple_2.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://graphc.com"), "one_triple_3.ttl"));
	load(misteryGuest(null, NodeFactory.createURI("http://graphd.com"), "one_triple_4.ttl"));
}
 
Example #12
Source File: LearningSparql_CONSTRUCT_DESCRIBE_ITCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test
public void copyingDataFromAnotherGraph() throws Exception {
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.1#"), "ex012.ttl"));		
	load(misteryGuest(null, NodeFactory.createURI("http://example.org.1#"), "ex125.ttl"));
	
	constructTest(misteryGuest("ex180.rq"));	
}
 
Example #13
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void load(
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	final PipedRDFIterator<Triple> iterator = new PipedRDFIterator<Triple>();
	final StreamRDF inputStream = new PipedTriplesStream(iterator);
	
	executor.submit(new Runnable() {
		@Override
		public void run() {
			try {
				RDFDataMgr.parse(
						inputStream, 
						stream.getStream(), 
						RDFLanguages.contentTypeToLang(stream.getContentType()));
			} catch (final IOException exception) {
				throw new SolrException(ErrorCode.SERVER_ERROR, exception);
			}					
		}
	});
		
	// Graph Store Protocol indicates the target graph URI separately.
	// So the incoming Content-type here is one that maps "Triples Loader" but
	// the indexed tuple could be a Quad.
	final String graphUri = request.getParams().get(Names.GRAPH_URI_ATTRIBUTE_NAME);
	
	final DatasetGraph dataset = new LocalDatasetGraph(request, response, null, null);
	final Graph defaultGraph = graphUri == null 
			? dataset.getDefaultGraph() 
			: dataset.getGraph(NodeFactory.createURI(graphUri));
	while (iterator.hasNext()) {
		defaultGraph.add(iterator.next());
	}		
}
 
Example #14
Source File: NTriples.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link Node} representation of a literal.
 * 
 * @param literal the resource (as string in NT format).
 * @return a {@link Node} representation of a literal.
 */		
public static Node asLiteral(final String literal) {
	if (literal.startsWith(START_LITERAL_CHAR)) {
		int endIndexOfValue = endIndexOfValue(literal);

		if (endIndexOfValue != -1) {
			final String literalValue = unescape(literal.substring(1, endIndexOfValue));

			final int startIndexOfLanguage = literal.indexOf(LANGUAGE_MARKER, endIndexOfValue);
			final int startIndexOfDatatype = literal.indexOf(DATATYPE_MARKER, endIndexOfValue);

			if (startIndexOfLanguage != -1) {
				return NodeFactory.createLiteral(
						literalValue, 
						literal.substring(startIndexOfLanguage + LANGUAGE_MARKER.length()), 
						null);
			} else if (startIndexOfDatatype != -1) {
				return NodeFactory.createLiteral(
						literalValue, 
						null, 
						NodeFactory.getType(literal.substring(startIndexOfDatatype + DATATYPE_MARKER.length())));
			} else {
				return NodeFactory.createLiteral(literalValue);
			}
		}
	}

	throw new IllegalArgumentException(literal);
}
 
Example #15
Source File: NTriples.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Parses the given input as URI.
 * 
 * @param uriAsString the URI string value.
 * @return the {@link Node} URI representation of the given value.
 */
private static Node internalAsURI(final String uriAsString) {
	final String uri = unescape(uriAsString.substring(1, uriAsString.length() - 1));
	return NodeFactory.createURI(uri);		
}
 
Example #16
Source File: NTriples.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Parses the given input as a blank node.
 * 
 * @param blankNodeAsString the blank node string value.
 * @return the {@link Node} Blank node representation of the given value.
 */
private static Node internalAsBlankNode(final String blankNodeAsString) {
	return NodeFactory.createAnon(AnonId.create(blankNodeAsString.substring(2)));	
}