Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I need help #29

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ repositories {

dependencies {
implementation("ca.uhn.hapi.fhir", "hapi-fhir-base", "4.0.0")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.utilities", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu2", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu2016may", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu3", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.r4", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.r5", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.convertors", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.validation", "5.3.9")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.utilities", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu2", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu2016may", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.dstu3", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.r4", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.r5", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.convertors", "5.4.4-SNAPSHOT")
implementation("ca.uhn.hapi.fhir", "org.hl7.fhir.validation", "5.4.4-SNAPSHOT")
implementation("ch.qos.logback", "logback-classic", "1.2.3")
implementation("com.google.code.gson", "gson", "2.8.5")
implementation("commons-codec", "commons-codec", "1.9")
Expand Down Expand Up @@ -106,24 +106,15 @@ dependencies {
implementation("com.google.guava", "guava", "23.6-jre")
implementation("org.json", "json", "20171018")
implementation("com.damnhandy", "handy-uri-templates", "2.1.6")
implementation("es.weso", "schema_2.12", "0.0.60")
implementation("es.weso", "shacl_2.12", "0.0.60")
implementation("es.weso", "shex_2.12", "0.0.60")
implementation("es.weso", "manifest_2.12", "0.0.60")
implementation("es.weso", "srdfjena_2.12", "0.0.60")
implementation("es.weso", "srdf_2.12", "0.0.60")
implementation("es.weso", "utils_2.12", "0.0.60")
implementation("es.weso", "converter_2.12", "0.0.60")
implementation("es.weso", "rbe_2.12", "0.0.60")
implementation("es.weso", "typing_2.12", "0.0.60")
implementation("es.weso", "validating_2.12", "0.0.60")
implementation("es.weso", "server_2.12", "0.0.60")
implementation("org.scalactic", "scalactic_2.12", "3.0.1")
implementation("org.scalatest", "scalatest_2.12", "3.0.1")
implementation("com.typesafe.scala-logging", "scala-logging_2.12", "3.5.0")
implementation("org.rogach", "scallop_2.12", "2.0.6")
implementation("org.typelevel", "cats-core_2.12", "0.9.0")
implementation("org.typelevel", "cats-kernel_2.12", "0.9.0")
implementation("es.weso", "schema_2.12", "0.1.98-SNAPSHOT")
implementation("es.weso", "shacl_2.12", "0.1.75")
implementation("es.weso", "shex_2.12", "0.1.91")
implementation("es.weso", "srdfjena_2.12", "0.1.101")
implementation("es.weso", "srdf_2.12", "0.1.101")
implementation("es.weso", "utils_2.12", "0.1.94")
implementation("es.weso", "rbe_2.12", "0.1.91")
implementation("es.weso", "typing_2.12", "0.1.94")
implementation("es.weso", "validating_2.12", "0.1.94")
implementation("org.antlr", "antlr4-runtime", "4.6")
implementation("io.circe", "circe-core_2.11", "0.7.0-M2")
implementation("com.atlassian.commonmark", "commonmark", "0.12.1")
Expand Down
85 changes: 30 additions & 55 deletions src/main/java/org/hl7/fhir/rdf/ShExValidator.java
Original file line number Diff line number Diff line change
@@ -1,84 +1,59 @@
package org.hl7.fhir.rdf;
//
//import org.apache.jena.riot.RDFDataMgr;
//
//import java.nio.file.Files;
//import java.nio.file.Paths;
//import java.util.logging.Logger;

//import com.hp.hpl.jena.rdf.model.Model;

import cats.effect.IO;
import es.weso.rdf.PrefixMap;
import es.weso.rdf.RDFBuilder;
import es.weso.rdf.RDFReader;
import es.weso.rdf.jena.RDFAsJenaModel;
import es.weso.rdf.jena.RDFAsJenaModel$;
import es.weso.rdf.locations.Location;
import es.weso.rdf.nodes.IRI;
import es.weso.rdf.triples.RDFTriple;
import es.weso.schema.Result;
import es.weso.schema.Schema;
import es.weso.schema.ShExSchema$;
import org.apache.jena.rdf.model.Model;
import es.weso.rdf.nodes.RDFNode;
import scala.Option;
import scala.collection.immutable.Map;
import scala.collection.immutable.Map$;
import scala.collection.immutable.Set;
import scala.Predef$;
import cats.effect.IO;

import java.nio.file.Files;
import java.nio.file.Paths;
import static cats.effect.unsafe.implicits.*;

public class ShExValidator {

// private Logger log = Logger.getLogger(ShExValidator.class.getName());
private final Schema schema;

public ShExValidator(String schemaFile) throws Exception {
// load shex from the path
// log.info("Reading ShEx file " + schemaFile);
schema = readSchema(schemaFile);
}

public Schema readSchema(String schemaFile) throws Exception {
// Create a none, see: http://stackoverflow.com/questions/1997433/how-to-use-scala-none-from-java-code
Option<String> none = Option.apply((String) null); // Create a none
Option<String> none = Option.apply(null); // Create a none
String contents = new String(Files.readAllBytes(Paths.get(schemaFile)));
return ShExSchema$.MODULE$.fromString(contents, "SHEXC", none).get();
return ShExSchema$.MODULE$.fromString(contents, "SHEXC", none).unsafeRunSync(global());
}

public void validate(Model dataModel) {
Option<String> none = Option.apply(null); // Create a none
RDFReader rdf = new RDFAsJenaModel(dataModel);
Result result = schema.validate(rdf, "TARGETDECLS", none, none, rdf.getPrefixMap(), schema.pm());
if (result.isValid()) {
// log.info("Result is valid");
// System.out.println("Valid. Result: " + result.show());
} else {
System.out.println("Not valid");
}
Option<String> noneStr = Option.empty();
Option<IRI> noneIri = Option.empty();
Option<RDFBuilder> noneBuilder = Option.empty();
new java.util.HashMap<RDFNode, Set<Location>>();
Map<RDFNode, Set<Location>> nodesLocations = Map$.MODULE$.empty() ;
Map<RDFTriple, Set<Location>> tripleLocations = Map$.MODULE$.empty() ;
IO<Result> r = null ;
PrefixMap pm = PrefixMap.empty();
IO<PrefixMap> ioPm = IO.pure(pm);
RDFAsJenaModel rdf = RDFAsJenaModel.fromModel(dataModel, noneIri, noneIri, nodesLocations, tripleLocations).unsafeRunSync(global());
IO<Result> ioResult = schema.validate(rdf, "TARGETDECLS", "", noneStr, noneStr, pm, schema.pm(), noneBuilder);
Result result = ioResult.unsafeRunSync(global());
if (!result.isValid()) System.out.println("Not valid");
}


//
//
//
// public void validate(Model dataModel, Schema schema, PrefixMap pm) throws Exception {
// RDFReader rdf = new RDFAsJenaModel(dataModel);
// ShExMatcher matcher = new ShExMatcher(schema,rdf);
//// ShExResult result = matcher.validate();
//// if (result.isValid()) {
//// log.info("Result is valid");
//// System.out.println("Valid. Result: " + result.show(1,pm));
//// } else {
//// System.out.println("Not valid");
//// }
// }
//
// public void validate(String dataFile, String schemaFile) throws Exception {
//// log.info("Reading data file " + dataFile);
//// Model dataModel = RDFDataMgr.loadModel(dataFile);
//// log.info("Model read. Size = " + dataModel.size());
//
//
////
//// Schema schema = pair._1();
//// PrefixMap pm = pair._2();
//////
////// log.info("Schema read" + schema.show());
////
//// validate(dataModel,schema,pm);
// }
//
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.hl7.fhir.convertors.VersionConvertorAdvisor50;
import org.hl7.fhir.convertors.VersionConvertor_10_50;
import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_50;
import org.hl7.fhir.dstu2.formats.IParser.OutputStyle;
import org.hl7.fhir.dstu2.model.Resource;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.model.Bundle;
import org.hl7.fhir.r5.model.*;
import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r5.model.CodeSystem;
import org.hl7.fhir.r5.model.CompartmentDefinition;
import org.hl7.fhir.r5.model.ValueSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DSTU2ValidationConvertor implements VersionConvertorAdvisor50 {
public class DSTU2ValidationConvertor extends BaseAdvisor_10_50 {

private Bundle source;
private VersionConvertor_10_50 vc;
Expand All @@ -31,9 +28,9 @@ public void convert(String bundleSource, String bundleTarget) throws Exception {
throw new Exception(e);
}
}

@Override
public boolean ignoreEntry(BundleEntryComponent src) {
public boolean ignoreEntry(@Nullable BundleEntryComponent src, @NotNull FhirPublication fhirPublication) {
return src.getResource() instanceof CompartmentDefinition || src.getResource() instanceof CodeSystem;
}

Expand All @@ -53,35 +50,6 @@ public static void main(String[] args) throws Exception {
dstu2.convert(src + "dataelements.xml", dst + "dataelements-r2.xml");
}

@Override
public Resource convertR2(org.hl7.fhir.r5.model.Resource resource) throws FHIRException {
if (resource instanceof ValueSet) {
ValueSet vs = (ValueSet) resource;
if (vs.hasCompose() && vs.getCompose().getExclude().isEmpty() && vs.getCompose().getInclude().size() == 1 && !vs.getCompose().getInclude().get(0).hasValueSet()) {
String url = vs.getCompose().getInclude().get(0).getSystem();
CodeSystem cs = findCodeSystem(url);
if (cs != null) {
org.hl7.fhir.dstu2.model.ValueSet vst = (org.hl7.fhir.dstu2.model.ValueSet) VersionConvertor_10_50.convertResource(vs);
vst.setCompose(null);
vst.setCodeSystem(vc.convertCodeSystem(cs));
return vst;
}
}
}
return null;
}

@Override
public org.hl7.fhir.dstu2016may.model.Resource convertR2016May(org.hl7.fhir.r5.model.Resource resource) throws FHIRException {
return null;
}

@Override
public org.hl7.fhir.dstu3.model.Resource convertR3(org.hl7.fhir.r5.model.Resource resource) throws FHIRException {
return null;
}


private CodeSystem findCodeSystem(String url) {
for (BundleEntryComponent b : source.getEntry()) {
if (b.getResource() instanceof CodeSystem) {
Expand All @@ -103,11 +71,4 @@ public void handleCodeSystem(CodeSystem tgtcs, ValueSet vs) {
public CodeSystem getCodeSystem(ValueSet src) {
return null;
}

@Override
public org.hl7.fhir.r4.model.Resource convertR4(org.hl7.fhir.r5.model.Resource resource) throws FHIRException {
// TODO Auto-generated method stub
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void addConvertedIg4(NPMPackageGenerator npm, ImplementationGuide ig) th
}

private void addConvertedIg3(NPMPackageGenerator npm, ImplementationGuide ig) throws IOException, FHIRException {
org.hl7.fhir.dstu3.model.Resource res = VersionConvertor_30_50.convertResource(ig, false);
org.hl7.fhir.dstu3.model.Resource res = VersionConvertor_30_50.convertResource(ig);
npm.addFile(Category.RESOURCE, "ImplementationGuide-"+ig.getId()+".json", new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(res));
}

Expand Down Expand Up @@ -247,7 +247,7 @@ private void addConvertedIg4X(NPMPackageGenerator npm, ImplementationGuide ig) t
}

private void addConvertedIg3X(NPMPackageGenerator npm, ImplementationGuide ig) throws IOException, FHIRException {
org.hl7.fhir.dstu3.model.Resource res = VersionConvertor_30_50.convertResource(ig, false);
org.hl7.fhir.dstu3.model.Resource res = VersionConvertor_30_50.convertResource(ig);
npm.addFile(Category.RESOURCE, "ImplementationGuide-"+ig.getId()+".xml", new org.hl7.fhir.dstu3.formats.XmlParser().composeBytes(res));
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/hl7/fhir/tools/publisher/ExampleInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.hl7.fhir.utilities.CSFileInputStream;
import org.hl7.fhir.utilities.Logger;
import org.hl7.fhir.utilities.Logger.LogMessageType;
import org.hl7.fhir.utilities.SIDUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.validation.ValidationMessage;
Expand Down Expand Up @@ -528,7 +529,7 @@ public void setByRdf(boolean byRdf) {


@Override
public Element fetch(Object appContext, String url) throws IOException, FHIRException {
public Element fetch(IResourceValidator validator, Object appContext, String url) throws IOException, FHIRException {
String[] parts = url.split("\\/");
if (parts.length == 2 && definitions.hasResource(parts[0])) {
ResourceDefn r = definitions.getResourceByName(parts[0]);
Expand Down Expand Up @@ -580,7 +581,7 @@ public Element fetch(Object appContext, String url) throws IOException, FHIRExce


@Override
public ReferenceValidationPolicy validationPolicy(Object appContext, String path, String url) {
public ReferenceValidationPolicy validationPolicy(IResourceValidator validator, Object appContext, String path, String url) {
String[] parts = url.split("\\/");
if (VALIDATE_CONFORMANCE_REFERENCES) {
if (Utilities.existsInList(url, "ValueSet/LOINCDepressionAnswersList", "ValueSet/LOINCDifficultyAnswersList", "CodeSystem/npi-taxonomy", "ValueSet/1.2.3.4.5", "StructureDefinition/daf-patient", "ValueSet/zika-affected-area"))
Expand All @@ -595,7 +596,7 @@ public ReferenceValidationPolicy validationPolicy(Object appContext, String path


@Override
public boolean resolveURL(Object appContext, String path, String url, String type) throws IOException, FHIRException {
public boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type) throws IOException, FHIRException {
if (path.endsWith(".fullUrl"))
return true;
if (url.startsWith("http://hl7.org/fhir")) {
Expand All @@ -605,13 +606,13 @@ public boolean resolveURL(Object appContext, String path, String url, String typ
if (parts.length >= 5 && definitions.hasResource(parts[4])) {
if ("DataElement".equals(parts[4]))
return true;
Element res = fetch(appContext, url.substring(20));
Element res = fetch(validator, appContext, url.substring(20));
return true; // disable this test. Try again for R4. res != null || Utilities.existsInList(parts[4], "NamingSystem", "CapabilityStatement", "CompartmentDefinition", "ConceptMap");
} else if (context.fetchCodeSystem(url) != null)
return true;
else if (definitions.getMapTypes().containsKey(url))
return true;
else if (url.startsWith("http://hl7.org/fhir/sid") || Utilities.existsInList(url, "http://hl7.org/fhir/ConsentPolicy/opt-in", "http://hl7.org/fhir/ConsentPolicy/opt-out", "http://hl7.org/fhir/api",
else if (SIDUtilities.isKnownSID(url) || Utilities.existsInList(url, "http://hl7.org/fhir/ConsentPolicy/opt-in", "http://hl7.org/fhir/ConsentPolicy/opt-out", "http://hl7.org/fhir/api",
"http://hl7.org/fhir/terminology-server", "http://hl7.org/fhir/knowledge-repository", "http://hl7.org/fhir/measure-processor"))
return true;
else
Expand All @@ -629,21 +630,21 @@ public IValidatorResourceFetcher setLocale(Locale locale) {


@Override
public byte[] fetchRaw(String source) throws MalformedURLException, IOException {
public byte[] fetchRaw(IResourceValidator validator, String source) throws MalformedURLException, IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
return TextFile.streamToBytes(c.getInputStream());
}


@Override
public CanonicalResource fetchCanonicalResource(String url) {
public CanonicalResource fetchCanonicalResource(IResourceValidator validator, String url) {
return null;
}


@Override
public boolean fetchesCanonicalResource(String url) {
public boolean fetchesCanonicalResource(IResourceValidator validator, String url) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.apache.commons.lang3.NotImplementedException;
import org.fhir.ucum.UcumException;
import org.hl7.fhir.convertors.SpecDifferenceEvaluator;
import org.hl7.fhir.convertors.TypeLinkProvider;
import org.hl7.fhir.convertors.VersionConvertor_40_50;
import org.hl7.fhir.convertors.loaders.R4ToR5Loader;
import org.hl7.fhir.convertors.SpecDifferenceEvaluator.TypeLinkProvider;
import org.hl7.fhir.convertors.txClient.TerminologyClientR5;
import org.hl7.fhir.definitions.Config;
import org.hl7.fhir.definitions.generators.specification.BaseGenerator;
Expand Down