Examples of JsonLd


Examples of org.apache.stanbol.commons.jsonld.JsonLd

            !deParameterizedIdentifier.equalsIgnoreCase(APPLICATION_JSON)) {
            logger.info("serialize() the format '" + deParameterizedIdentifier + "' is not supported by this implementation");
            return;
        }

        JsonLd jsonLd = new JsonLd();
        // If there is no namespace prefix map set, we use the namespaces
        // known from the NamespaceEnum
        if (this.namespacePrefixMap.isEmpty()) {
            for (NamespaceEnum ns : NamespaceEnum.values()) {
                logger.debug("Adding JSON-LD namespace " + ns.getPrefix() + ":" + ns.getNamespace());
                this.namespacePrefixMap.put(ns.getNamespace(), ns.getPrefix());
            }
        }
        jsonLd.setNamespacePrefixMap(this.namespacePrefixMap);
        jsonLd.setUseTypeCoercion(this.useTypeCoercion);

        Map<NonLiteral, String> subjects = createSubjectsMap(tc);
        for (NonLiteral subject : subjects.keySet()) {
            JsonLdResource resource = new JsonLdResource();
           
            String strSubject = subject.toString();
            if (subject instanceof UriRef) {
                UriRef uri = (UriRef) subject;
                strSubject = uri.getUnicodeString();
            }
            resource.setSubject(strSubject);

            Iterator<Triple> triplesFromSubject = tc.filter(subject, null, null);
            while (triplesFromSubject.hasNext()) {
                Triple currentTriple = triplesFromSubject.next();
                if (currentTriple.getPredicate().getUnicodeString().equals(RDF_NS_TYPE)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("serialize() adding rdf:type: \"a\":" + currentTriple.getObject());
                    }
                    resource.addType(((UriRef) currentTriple.getObject()).getUnicodeString());
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("serializer() adding predicate " + currentTriple.getPredicate().toString() + " with object " + currentTriple.getObject().toString());
                    }

                    String property = currentTriple.getPredicate().getUnicodeString();
                    JsonLdProperty jldProperty = resource.getProperty(property);
                    if (jldProperty == null) {
                        jldProperty = new JsonLdProperty(property);
                    }
                   
                    String strValue = currentTriple.getObject().toString();
                    JsonLdPropertyValue jldValue = new JsonLdPropertyValue();

                    if (currentTriple.getObject() instanceof PlainLiteral) {
                        PlainLiteral plain = (PlainLiteral) currentTriple.getObject();
                        if (plain.getLanguage() != null) {
                            jldValue.setLanguage(plain.getLanguage().toString());
                        }
                        strValue = plain.getLexicalForm();
                    }
                    else if (currentTriple.getObject() instanceof TypedLiteral) {
                        TypedLiteral typedObject = (TypedLiteral) currentTriple.getObject();
                        String type = typedObject.getDataType().getUnicodeString();
                        jldValue.setType(type);
                        strValue = typedObject.getLexicalForm();
                    }
                    else if (currentTriple.getObject() instanceof UriRef) {
                        UriRef uriRef = (UriRef) currentTriple.getObject();
                        jldValue.setType(JsonLdCommon.ID);
                        strValue = uriRef.getUnicodeString();
                    }
                   
                    jldValue.setValue(convertValueType(strValue));
                    jldProperty.addValue(jldValue);
                    resource.putProperty(jldProperty);
                }
            }

            jsonLd.put(resource.getSubject(), resource);
        }

        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(serializedGraph,"utf-8"));
            writer.write(jsonLd.toString(this.indentation));
            writer.flush();
        } catch (IOException ioe) {
            logger.error(ioe.getMessage());
            throw new RuntimeException(ioe.getMessage());
        }
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

        if (!deParameterizedIdentifier.equalsIgnoreCase(SUPPORTED_FORMAT)) {
            logger.info("serialize() the format '" + deParameterizedIdentifier + "' is not supported by this implementation");
            return;
        }

        JsonLd jsonLd = new JsonLd();
        // If there is no namespace prefix map set, we use the namespaces
        // known from the NamespaceEnum
        if (this.namespacePrefixMap.isEmpty()) {
            for (NamespaceEnum ns : NamespaceEnum.values()) {
                logger.debug("Adding JSON-LD namespace " + ns.getPrefix() + ":" + ns.getNamespace());
                this.namespacePrefixMap.put(ns.getNamespace(), ns.getPrefix());
            }
        }
        jsonLd.setNamespacePrefixMap(this.namespacePrefixMap);
        jsonLd.setUseTypeCoercion(this.useTypeCoercion);

        Map<NonLiteral, String> subjects = createSubjectsMap(tc);
        for (NonLiteral subject : subjects.keySet()) {
            JsonLdResource resource = new JsonLdResource();
           
            String strSubject = subject.toString();
            if (subject instanceof UriRef) {
                UriRef uri = (UriRef) subject;
                strSubject = uri.getUnicodeString();
            }
            resource.setSubject(strSubject);

            Iterator<Triple> triplesFromSubject = tc.filter(subject, null, null);
            while (triplesFromSubject.hasNext()) {
                Triple currentTriple = triplesFromSubject.next();
                if (currentTriple.getPredicate().getUnicodeString().equals(RDF_NS_TYPE)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("serialize() adding rdf:type: \"a\":" + currentTriple.getObject());
                    }
                    resource.addType(((UriRef) currentTriple.getObject()).getUnicodeString());
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("serializer() adding predicate " + currentTriple.getPredicate().toString() + " with object " + currentTriple.getObject().toString());
                    }

                    String property = currentTriple.getPredicate().getUnicodeString();
                    JsonLdProperty jldProperty = resource.getProperty(property);
                    if (jldProperty == null) {
                        jldProperty = new JsonLdProperty(property);
                    }
                   
                    String strValue = currentTriple.getObject().toString();
                    JsonLdPropertyValue jldValue = new JsonLdPropertyValue();

                    if (currentTriple.getObject() instanceof PlainLiteral) {
                        PlainLiteral plain = (PlainLiteral) currentTriple.getObject();
                        if (plain.getLanguage() != null) {
                            jldValue.setLanguage(plain.getLanguage().toString());
                        }
                        strValue = plain.getLexicalForm();
                    }
                    else if (currentTriple.getObject() instanceof TypedLiteral) {
                        TypedLiteral typedObject = (TypedLiteral) currentTriple.getObject();
                        String type = typedObject.getDataType().getUnicodeString();
                        jldValue.setType(type);
                        strValue = typedObject.getLexicalForm();
                    }
                    else if (currentTriple.getObject() instanceof UriRef) {
                        UriRef uriRef = (UriRef) currentTriple.getObject();
                        jldValue.setType(JsonLdCommon.ID);
                        strValue = uriRef.getUnicodeString();
                    }
                   
                    jldValue.setValue(convertValueType(strValue));
                    jldProperty.addValue(jldValue);
                    resource.putProperty(jldProperty);
                }
            }

            jsonLd.put(resource.getSubject(), resource);
        }

        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(serializedGraph,"utf-8"));
            writer.write(jsonLd.toString(this.indentation));
            writer.flush();
        } catch (IOException ioe) {
            logger.error(ioe.getMessage());
            throw new RuntimeException(ioe.getMessage());
        }
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

       
        return fact;
    }

    public JsonLd factToJsonLd() {
        JsonLd jsonLd = new JsonLd();
       
        JsonLdResource subject = new JsonLdResource();
        subject.setProfile(this.factSchemaURN);
        for (String role : this.roleMap.keySet()) {
            subject.putProperty(role, this.roleMap.get(role));
        }
        jsonLd.put(subject);
       
        return jsonLd;
    }
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

    }
    rows.add(result);
  }

  public String toJSON() {
    JsonLd root = new JsonLd();
    if (rows != null && !rows.isEmpty()) {
        int rowCount = 0;
      for (FactResult result : rows) {
          rowCount++;
          JsonLdResource subject = new JsonLdResource();
          subject.setSubject("R" + rowCount);
        for (int i = 0; i < header.size(); i++) {
          subject.putProperty(header.get(i), result.getValues().get(i));
        }
        root.put(subject);
      }
    }

    return root.toString();
  }
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

        ResponseBuilder rb = Response.status(Status.BAD_REQUEST).entity("No query sent.");
        CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
      return rb.build();
    }

    JsonLd jsonLdQuery = null;
    try {
      jsonLdQuery = JsonLdParser.parse(queryString);
    } catch (Exception e) {
      logger.info("Could not parse query", e);
      ResponseBuilder rb = Response.status(Status.BAD_REQUEST).entity(
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public Response postFacts(String jsonLdFacts, @Context HttpHeaders requestHeaders) {
        JsonLd jsonLd = null;
        try {
            jsonLd = JsonLdParser.parse(jsonLdFacts);
        } catch (Exception e) {
            /* ignore here */
        }

        if (jsonLd == null) {
          ResponseBuilder rb = Response.status(Status.BAD_REQUEST).entity("Could not parse provided JSON-LD structure.")
                    .type(MediaType.TEXT_PLAIN);
          CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
            return rb.build();
        }

        int factId = -1;
        if (jsonLd.getResourceSubjects().size() < 2) {
            // post a single fact
            Fact fact = Fact.factFromJsonLd(jsonLd);
            if (fact != null) {
                logger.info("Request for posting new fact for {}", fact.getFactSchemaURN());
                try {
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

                "Could not find fact with ID " + factId + " for fact schema " + factSchemaURN);
            CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
            return rb.build();
        }
        else {
            JsonLd factAsJsonLd = fact.factToJsonLd();
            ResponseBuilder rb = Response.status(Status.OK).entity(factAsJsonLd.toString())
                    .type(MediaType.APPLICATION_JSON);
            CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
            return rb.build();
        }
    }
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

                "Could not find fact with ID " + factId + " for fact schema " + factSchemaURN);
            CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
            return rb.build();
        }
        else {
            JsonLd factAsJsonLd = fact.factToJsonLd();
           
            StringBuilder sb = new StringBuilder();
            sb.append("<html><body>");
            sb.append("<pre>");
            sb.append(factAsJsonLd.toString(2));
            sb.append("</pre>");
            sb.append("</body></html>");
           
            ResponseBuilder rb = Response.status(Status.OK).entity(sb.toString()).type(MediaType.TEXT_HTML);
            CorsHelper.addCORSOrigin(servletContext, rb, requestHeaders);
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

        }
    }
   
    @Test
    public void testFactsFromJsonLdMultiFactsMultiSchema() {
        JsonLd jsonLd = new JsonLd();
        jsonLd.addNamespacePrefix("http://iks-project.eu/ont/", "iks");
        jsonLd.addNamespacePrefix("http://upb.de/persons/", "upb");
       
        JsonLdResource fact1Resource = new JsonLdResource();
        fact1Resource.setSubject("fact1");
        fact1Resource.setProfile("iks:employeeOf");
        fact1Resource.putProperty("person", new JsonLdIRI("upb:bnagel"));
        fact1Resource.putProperty("organization", new JsonLdIRI("http://uni-paderborn.de"));
        jsonLd.put(fact1Resource);

        JsonLdResource fact2Resource = new JsonLdResource();
        fact2Resource.setSubject("fact2");
        fact2Resource.setProfile("iks:friendOf");
        fact2Resource.putProperty("person", new JsonLdIRI("upb:bnagel"));
        fact2Resource.putProperty("friend", new JsonLdIRI("upb:fchrist"));
        jsonLd.put(fact2Resource);
       
        Set<Fact> facts = Fact.factsFromJsonLd(jsonLd);
        assertNotNull(facts);
        assertEquals(2, facts.size());
        boolean fact1OK = false;
View Full Code Here

Examples of org.apache.stanbol.commons.jsonld.JsonLd

public class QueryTest {

    @Test
    public void testToQueryFromJsonLd() throws Exception {
        JsonLd jldq = new JsonLd();
        jldq.addNamespacePrefix("http://iks-project.eu/ont/", "iks");
       
        JsonLdResource subject = new JsonLdResource();
        List<String> select = new ArrayList<String>();
        select.add("person");
        subject.putProperty("select", select);
        subject.putProperty("from", "iks:employeeOf");

        Map<String, Object> orga = new HashMap<String, Object>();
        orga.put("organization", new JsonLdIRI("http://upb.de"));

        Map<String, Object> eq = new HashMap<String, Object>();
        eq.put("=", orga);
       
        List<Object> where = new ArrayList<Object>();
        where.add(eq);
       
        subject.putProperty("where", where);
       
        jldq.put(subject);
       
        Query query = Query.toQueryFromJsonLd(jldq);
        assertNotNull(query);
       
        assertNotNull(query.getFromSchemaURN());
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.