Skip to content

Commit

Permalink
fix: cyclic linked graphs end in infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jschnasse committed Mar 30, 2016
1 parent db2490b commit ac3f228
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/de/hbz/lobid/helper/JsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ License, or (at your option) any later version.

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class JsonConverter {
private String mainSubjectOfTheResource;

private EtikettMakerInterface etikette;
private Map<Statement, Statement> visited = new HashMap<>();

/**
* @param e An EtikettMaker provides access to labels
Expand Down Expand Up @@ -261,13 +263,27 @@ private void createObject(String uri, Map<String, Object> newObject) {
if (s.getObject() instanceof org.openrdf.model.Literal) {
newObject.put(e.name, s.getObject().stringValue());
} else {
if (statementVisited(s)) {
continue;
}
if (!mainSubjectOfTheResource.equals(s.getObject().stringValue())) {
createObject(newObject, s, e);
} else {
newObject.put(e.name, s.getObject().stringValue());
}
}

}

}

private boolean statementVisited(Statement s) {
boolean result = visited.containsKey(s);
if (result) {
logger.debug("Already visited " + s);
}
visited.put(s, s);
return result;
}

private Set<Statement> find(String uri) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/TestRdfToJsonConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private static boolean testFiles(String fnameNtriples, String fnameJson,
String uri) {
Map<String, Object> expected = null;
Map<String, Object> actual = null;
TestRdfToJsonConversion.logger.info("New test, begin converting files");
TestRdfToJsonConversion.logger
.info("Convert " + fnameNtriples + " to " + fnameJson);
try (InputStream in = new FileInputStream(new File(fnameNtriples));
InputStream out = new File(fnameJson).exists()
? new FileInputStream(new File(fnameJson)) : makeFile(fnameJson)) {
Expand Down
45 changes: 45 additions & 0 deletions src/test/resources/input/nt/00507/HT005077920.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<http://lobid.org/resource/ZDB47030-2/about> <http://xmlns.com/foaf/0.1/primaryTopic> <http://lobid.org/resource/ZDB47030-2> .
<http://lobid.org/resource/ZDB47030-2/about> <http://purl.org/dc/terms/modified> "20060320"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/ZDB47030-2/about> <http://purl.org/dc/terms/created> "19991118"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://d-nb.info/gnd/4067488-5> <http://d-nb.info/standards/elementset/gnd#preferredNameForTheSubjectHeading> "Zeitschrift"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://d-nb.info/gnd/4067488-5> <http://d-nb.info/standards/elementset/gnd#preferredName> "Zeitschrift"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/ontology/bibo/shortTitle> "HITEE"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Industrielle Technik"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/elements/1.1/publisher> "Routledge"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ontology/bibo/Journal> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/subject> <http://d-nb.info/gnd/4020517-4> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/medium> <http://rdvocab.info/termList/RDAproductionMethod/1010> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Regionalgeschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/ontology/bibo/edition> "Germantown, NY : Periodical Service Co."^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/ontology/bibo/issn> "07341512"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectChain> "Technik | Geschichte | Zeitschrift"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/subject> <http://d-nb.info/gnd/4059205-4> .
<http://lobid.org/resource/HT005077920> <http://www.w3.org/2002/07/owl#sameAs> <http://ld.zdb-services.de/resource/47030-2> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#zdbID> "47030-2"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/issued> "1983 - "^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Zeitgeschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Periodikum"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/language> <http://id.loc.gov/vocabulary/iso639-2/eng> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Ortsgeschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/vocab/frbr/core#exemplar> <http://lobid.org/item/HT005077920:DE-361:14%20CA000%20H870> .
<http://lobid.org/resource/HT005077920> <http://purl.org/ontology/bibo/shortTitle> "HITEEB"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://www.w3.org/2002/07/owl#sameAs> <http://lobid.org/resource/ZDB47030-2> .
<http://lobid.org/resource/HT005077920> <http://rdvocab.info/Elements/publicationStatement> "Abingdon; Routledge; 1983"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/vocab/frbr/core#exemplar> <http://lobid.org/item/HT005077920:DE-294-11:Z%201023> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Landesgeschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#hbzID> "HT005077920"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://www.w3.org/2007/05/powder-s#describedby> <http://lobid.org/resource/HT005077920/about> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/subject> <http://d-nb.info/gnd/4067488-5> .
<http://lobid.org/resource/HT005077920> <http://rdvocab.info/Elements/placeOfPublication> "Abingdon"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/ontology/bibo/shortTitle> "Hist Technol"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/dc/terms/BibliographicResource> .
<http://lobid.org/resource/HT005077920> <http://purl.org/lobid/lv#subjectLabel> "Zeitschriften"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://xmlns.com/foaf/0.1/isPrimaryTopicOf> <http://193.30.112.134/F/?func=find-c&ccl_term=IDN%3DHT005077920> .
<http://lobid.org/resource/HT005077920> <http://rdvocab.info/Elements/otherTitleInformation> "HT ; an international journal"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/HT005077920> <http://purl.org/dc/terms/title> "History and technology"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://lobid.org/resource/ZDB47030-2> <http://www.w3.org/2007/05/powder-s#describedby> <http://lobid.org/resource/ZDB47030-2/about> .
<http://lobid.org/resource/ZDB47030-2> <http://www.w3.org/2002/07/owl#sameAs> <http://lobid.org/resource/HT005077920> .
<http://d-nb.info/gnd/4020517-4> <http://d-nb.info/standards/elementset/gnd#preferredNameForTheSubjectHeading> "Geschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://d-nb.info/gnd/4020517-4> <http://d-nb.info/standards/elementset/gnd#preferredName> "Geschichte"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://d-nb.info/gnd/4059205-4> <http://d-nb.info/standards/elementset/gnd#preferredNameForTheSubjectHeading> "Technik"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://d-nb.info/gnd/4059205-4> <http://d-nb.info/standards/elementset/gnd#preferredName> "Technik"^^<http://www.w3.org/2001/XMLSchema#string> .
62 changes: 62 additions & 0 deletions src/test/resources/output/json/00507/HT005077920.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"@context" : "http://lobid.org/context/lobid-resources.json",
"describedby" : [ {
"id" : "http://lobid.org/resource/HT005077920/about"
} ],
"edition" : [ "Germantown, NY : Periodical Service Co." ],
"exemplar" : [ {
"id" : "http://lobid.org/item/HT005077920:DE-294-11:Z%201023"
}, {
"id" : "http://lobid.org/item/HT005077920:DE-361:14%20CA000%20H870"
} ],
"hbzId" : [ "HT005077920" ],
"id" : "http://lobid.org/resource/HT005077920",
"isPrimaryTopicOf" : [ {
"id" : "http://193.30.112.134/F/?func=find-c&ccl_term=IDN%3DHT005077920"
} ],
"issn" : [ "07341512" ],
"issued" : [ "1983 - " ],
"language" : [ {
"id" : "http://id.loc.gov/vocabulary/iso639-2/eng"
} ],
"medium" : [ {
"id" : "http://rdvocab.info/termList/RDAproductionMethod/1010"
} ],
"otherTitleInformation" : [ "HT ; an international journal" ],
"placeOfPublication" : [ "Abingdon" ],
"publicationStatement" : [ "Abingdon; Routledge; 1983" ],
"publisher" : [ "Routledge" ],
"sameAs" : [ {
"id" : "http://ld.zdb-services.de/resource/47030-2"
}, {
"describedby" : [ {
"dateCreated" : "19991118",
"dateModified" : "20060320",
"id" : "http://lobid.org/resource/ZDB47030-2/about",
"primaryTopic" : [ {
"id" : "http://lobid.org/resource/ZDB47030-2",
"sameAs" : "http://lobid.org/resource/HT005077920"
} ]
} ],
"id" : "http://lobid.org/resource/ZDB47030-2"
} ],
"shortTitle" : [ "Hist Technol", "HITEEB", "HITEE" ],
"subject" : [ {
"id" : "http://d-nb.info/gnd/4020517-4",
"preferredName" : "Geschichte",
"preferredNameForTheSubjectHeading" : "Geschichte"
}, {
"id" : "http://d-nb.info/gnd/4059205-4",
"preferredName" : "Technik",
"preferredNameForTheSubjectHeading" : "Technik"
}, {
"id" : "http://d-nb.info/gnd/4067488-5",
"preferredName" : "Zeitschrift",
"preferredNameForTheSubjectHeading" : "Zeitschrift"
} ],
"subjectChain" : [ "Technik | Geschichte | Zeitschrift" ],
"subjectLabel" : [ "Industrielle Technik", "Zeitschriften", "Landesgeschichte", "Ortsgeschichte", "Periodikum", "Zeitgeschichte", "Regionalgeschichte" ],
"title" : [ "History and technology" ],
"type" : [ "http://purl.org/ontology/bibo/Journal", "http://purl.org/dc/terms/BibliographicResource" ],
"zdbId" : [ "47030-2" ]
}

0 comments on commit ac3f228

Please sign in to comment.