GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Infinite loop while framing data · Issue #338 · jsonld-java/jsonld-java

Infinite loop while framing data #338

New issue
Copy link
New issue
Copy link
Open
Open
Infinite loop while framing data#338
Copy link
Assignees
garpinc

Description

@garpinc
garpinc
opened on May 4, 2023
Issue body actions

With the following test case and the serialized data model-ser.zip (you need to unzip and change the path to the file). When running test case we run into an infinite loop. I think something needs to be done to handle this problem because you never know what data is going to be loaded.

    @Test
    public void testJsonLdCycle() throws Exception {
        final JsonLdOptions opts = new JsonLdOptions();

        //opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE));
        opts.setUseNativeTypes(true);
        opts.setCompactArrays(true);
        //opts.setOmitGraph(getWriterConfig().get(OMIT_GRAPH));
        opts.setEmbed(Embed.ALWAYS);

        String testFrame = "{\"@context\" : [ \"http://cambridgesemantics.com/ontologies/GraphmartStatus/LayerStatus\", \"http://cambridgesemantics.com/ontologies/GraphmartStatus/LayerChildStatus\", \"http://www.w3.org/2002/07/owl/Thing\", \"http://cambridgesemantics.com/ontologies/GraphmartStatus/GraphmartStatus\" ], \"@type\" : [ \"http://cambridgesemantics.com/ontologies/GraphmartStatus#GraphmartStatus\" ]}";

        final SesameRDFParser serialiser = new SesameRDFParser();

        try (ObjectInputStream in = new ObjectInputStream(new UserDirFileInputStream("/test/resources/coredata/model.ser"))) {
            Model model = (LinkedHashModel) in.readObject();

            Object output = JsonLdProcessor.fromRDF(model, opts, serialiser);
            JsonLdProcessor.frame(output, JsonUtils.fromString(testFrame), opts);
        }

    }

    public class SesameRDFParser implements com.github.jsonldjava.core.RDFParser {

        public void setPrefix(RDFDataset result, String fullUri, String prefix) {
            result.setNamespace(fullUri, prefix);
        }

        public void handleStatement(RDFDataset result, org.openrdf.model.Statement nextStatement) {
            // TODO: from a basic look at the code it seems some of these could be
            // null
            // null values for IRIs will probably break things further down the line
            // and i'm not sure yet if this should be something handled later on, or
            // something that should be checked here
            final String subject = getResourceValue(nextStatement.getSubject());
            final String predicate = getResourceValue(nextStatement.getPredicate());
            final Value object = nextStatement.getObject();
            final String graphName = getResourceValue(nextStatement.getContext());

            if (object instanceof Literal) {
                final Literal literal = (Literal) object;
                final String value = literal.getLabel();
                final String language = literal.getLanguage();

                String datatype = getResourceValue(literal.getDatatype());

                // In RDF-1.1, Language Literals internally have the datatype
                // rdf:langString
                if (language != null && datatype == null) {
                    datatype = org.openrdf.model.vocabulary.RDF.LANGSTRING.stringValue();
                }

                // In RDF-1.1, RDF-1.0 Plain Literals are now Typed Literals with
                // type xsd:String
                if (language == null && datatype == null) {
                    datatype = XMLSchema.STRING.stringValue();
                }

                result.addQuad(subject, predicate, value, datatype, language, graphName);

            } else {
                result.addQuad(subject, predicate, getResourceValue((Resource) object), graphName);
            }
        }

        private String getResourceValue(Resource subject) {
            if (subject == null) {
                return null;
            } else if (subject instanceof URI) {
                return subject.stringValue();
            } else if (subject instanceof BNode) {
                return "_:" + subject.stringValue();
            }

            throw new IllegalStateException("Did not recognise resource type: " + subject.getClass().getName());
        }

        @Override
        public RDFDataset parse(Object input) throws JsonLdError {
            final RDFDataset result = new RDFDataset();
            if (input instanceof Statement) {
                handleStatement(result, (Statement) input);
            } else if (input instanceof Graph) {
                if (input instanceof Model) {
                    final Set<Namespace> namespaces = ((Model) input).getNamespaces();
                    for (final Namespace nextNs : namespaces) {
                        result.setNamespace(nextNs.getName(), nextNs.getPrefix());
                    }
                }

                for (final org.openrdf.model.Statement nextStatement : (Graph) input) {
                    handleStatement(result, nextStatement);
                }
            }
            return result;
        }

    }

Activity

  1. garpinc commented on May 5, 2023

    @garpinc
    garpinc
    on May 5, 2023
    Author
    More actions

    Been trying to debug it to find issue but I'm lost and need help. @dr0i can you help please?

  2. dr0i
    self-assigned this
    on May 5, 2023
  3. garpinc commented on May 5, 2023

    @garpinc
    garpinc
    on May 5, 2023
    Author
    More actions

    I realize that the test case needed SesameRDFParser to be complete. I've included it.

  4. dr0i commented on Jul 4, 2023

    @dr0i
    dr0i
    on Jul 4, 2023
    Member
    More actions

    Hello @garpinc, I just had a bit of a closer look. Is it possible to provide code and data that would be more easily integrated into jsonld-java, say deserialized data wich then can be loaded into JsonLdProcessor.fromRDF(...) ? At best a branch of course, if possible.

  5. dr0i
    assigned
    garpinc
    and unassigned
    dr0i
    on Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @garpinc
    garpinc

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.