Sitelet https://github.com/weso/shaclex/commit/de0a3185603441fa23bcd3a7323e69abfad54cd6
Skip to content

Commit de0a318

Browse files
committed
Updated shaclex
1 parent 6b9b569 commit de0a318

7 files changed

Lines changed: 38 additions & 48 deletions

File tree

‎build.sbt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ lazy val supportedScalaVersions = List(scala212, scala213)
44

55
// Local dependencies
66
lazy val utilsVersion = "0.1.69"
7-
lazy val srdfVersion = "0.1.76"
7+
lazy val srdfVersion = "0.1.77"
88
lazy val shexVersion = "0.1.71"
99
lazy val shaclVersion = "0.1.65"
1010
lazy val shapeMapsVersion = "0.1.61"

‎modules/schema/src/main/scala/es/weso/schema/Result.scala‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ case class Result(
4646

4747
def addTrigger(trigger: ValidationTrigger): Result = this.copy(trigger = Some(trigger))
4848

49-
def show(base: Option[IRI]): String = {
49+
sealed abstract trait DetailsOption extends Product with Serializable
50+
case object Details extends DetailsOption
51+
case object NoDetails extends DetailsOption
52+
53+
def show(base: Option[IRI], details: DetailsOption): String = {
5054
val sb = new StringBuilder
5155
if (isValid) {
5256
if (shapeMaps.size == 0) {
@@ -106,7 +110,8 @@ case class Result(
106110
}
107111

108112
def serialize(format: String, base: Option[IRI] = None, builder: RDFBuilder): IO[String] = format.toUpperCase match {
109-
case Result.TEXT => IO(show(base))
113+
case Result.COMPACT => IO(show(base, NoDetails))
114+
case Result.DETAILS => IO(show(base, Details))
110115
case Result.JSON => toJsonString2spaces(builder)
111116
case _ => IO.raiseError(new RuntimeException(s"Unsupported format to serialize result: $format, $this"))
112117
}
@@ -146,9 +151,11 @@ object Result extends LazyLogging {
146151
override def show(r: Result): String = r.show(None)
147152
}
148153

149-
lazy val TEXT = "TEXT"
154+
lazy val TEXT = "COMPACT"
150155
lazy val JSON = "JSON"
151-
lazy val availableResultFormats = List(TEXT, JSON).map(_.toUpperCase)
156+
lazy val DETAILS = "DETAILS"
157+
158+
lazy val availableResultFormats = List(TEXT, JSON, DETAILS).map(_.toUpperCase)
152159
lazy val defaultResultFormat = availableResultFormats.head
153160

154161
// TODO: implement this

‎modules/schema/src/main/scala/es/weso/schema/ShExSchema.scala‎

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import es.weso.slang.{SLang2Clingo, ShEx2SLang}
1717

1818
import scala.util._
1919
import es.weso.shex.ResolvedSchema
20+
import scala.util.control.NoStackTrace
2021

2122
case class ShExSchema(schema: SchemaShEx)
2223
extends es.weso.schema.Schema
@@ -114,28 +115,10 @@ case class ShExSchema(schema: SchemaShEx)
114115
pm <- rdf.getPrefixMap
115116
res <- r match {
116117
case Left(error) =>
117-
IO(Result(
118-
false,
119-
"Error validating",
120-
Seq(),
121-
EmptyReport,
122-
Seq(ErrorInfo(error.getMessage())),
123-
None,
124-
pm,
125-
schema.prefixMap
126-
))
118+
IO(Result(false,"Error validating",Seq(),EmptyReport,Seq(ErrorInfo(error.getMessage())),None,pm,schema.prefixMap))
127119
case Right(result) => for {
128120
resultShapeMap <- result.toResultShapeMap
129-
} yield Result(
130-
true,
131-
"Validated",
132-
Seq(resultShapeMap),
133-
EmptyReport,
134-
Seq(),
135-
None,
136-
pm,
137-
schema.prefixMap
138-
)
121+
} yield Result(true,"Validated",Seq(resultShapeMap),EmptyReport,Seq(),None,pm,schema.prefixMap)
139122
}
140123
} yield res
141124

@@ -148,29 +131,28 @@ case class ShExSchema(schema: SchemaShEx)
148131
}
149132

150133
private def handleErr[A](e: Either[String,A]): IO[A] = e.fold(
151-
s => IO.raiseError(new RuntimeException(s)),
134+
s => err(s),
152135
IO.pure(_)
153136
)
154137

155138
override def fromRDF(rdf: RDFReader): IO[es.weso.schema.Schema] = for {
156139
eitherSchema <- RDF2ShEx.rdf2Schema(rdf)
157-
schema <- IO.fromEither(eitherSchema.leftMap(s => new RuntimeException(s"Error obtaining schema from RDF: $s\nRDF:\n${rdf.rdfReaderName}")))
158-
/* e.map(s => {
159-
val schema: es.weso.schema.Schema = ShExSchema(s)
160-
schema
161-
}).value >>= handleErr */
140+
schema <- IO.fromEither(eitherSchema.leftMap(s => ShExSchemaError(s"Error obtaining schema from RDF: $s\nRDF:\n${rdf.rdfReaderName}")))
162141
} yield ShExSchema(schema)
163142

164143
override def serialize(format: String, base: Option[IRI]): IO[String] = {
165144
val fmt = format.toUpperCase
166145
RDFAsJenaModel.empty.flatMap(_.use(builder => for {
167146
str <- fmt.toUpperCase match {
168147
case _ if (formatsUpperCase.contains(fmt)) => SchemaShEx.serialize(schema, fmt, base, builder)
169-
case _ => IO.raiseError(new RuntimeException(s"Can't serialize to format $format. Supported formats=$formats"))
148+
case _ => err(s"Can't serialize to format $format. Supported formats=$formats")
170149
}
171150
} yield str))
172151
}
173152

153+
case class ShExSchemaError(msg:String) extends RuntimeException(msg) with NoStackTrace
154+
private def err[A](msg: String): IO[A] = IO.raiseError(ShExSchemaError(msg))
155+
174156
override def empty: es.weso.schema.Schema = ShExSchema(schema = SchemaShEx.empty)
175157

176158
override def shapes: List[String] = {

‎modules/schema/src/main/scala/es/weso/schema/ShaclTQ.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ case class ShaclTQ(shapesGraph: Model) extends Schema {
9090
IO { report.hasProperty(SH.conforms,JenaDatatypes.TRUE) }
9191

9292
private def countViolations(report: Resource): IO[Int] =
93-
IO { report.getModel.listResourcesWithProperty(RDF.`type`,SH.Violation).toList.size }
93+
IO { report.getModel.listResourcesWithProperty(RDF.`type`,SH.ValidationResult).toList.size }
9494

9595

9696
private def report2reader(model: Model): IO[RDFReader] = for {

‎src/main/scala/es/weso/shaclex/Main.scala‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import cats.implicits._
44
import org.rogach.scallop._
55
import org.rogach.scallop.exceptions._
66
import com.typesafe.scalalogging._
7-
// import es.weso.rdf.PrefixMap
87
import es.weso.rdf.jena.Endpoint
98
import es.weso.rdf.nodes.IRI
10-
// import es.weso.schemaInfer.SchemaInfer
119
import es.weso.shaclex.repl.Repl
12-
// import es.weso.shapeMaps.NodeSelector
1310
import fs2._
1411

1512
import scala.io.Source
16-
// import es.weso.server._
1713
import es.weso.schema._
1814
import es.weso.rdf.jena.RDFAsJenaModel
1915
import scala.concurrent.duration._
@@ -27,6 +23,7 @@ import cats._
2723
// import scala.concurrent.ExecutionContext
2824
import fs2.Pipe
2925
import es.weso.rdf.RDFBuilder
26+
import es.weso.rdf.InferenceEngine
3027
// import cats.implicits._
3128

3229
object Main extends IOApp with LazyLogging {
@@ -115,6 +112,7 @@ object Main extends IOApp with LazyLogging {
115112
schema <- getSchema(opts, baseFolder, rdf)
116113
triggerName = opts.trigger.toOption.getOrElse(ValidationTrigger.default.name)
117114
shapeMapStr <- getShapeMapStr(opts, baseFolder)
115+
// _ <- IO { pprint.log(shapeMapStr) }
118116
pm <- rdf.getPrefixMap
119117
trigger <- fromEither(ValidationTrigger.findTrigger(triggerName, shapeMapStr, relativeBaseStr,
120118
opts.node.toOption, opts.shapeLabel.toOption,
@@ -230,16 +228,14 @@ object Main extends IOApp with LazyLogging {
230228
val path = baseFolder.resolve(opts.shapeMap())
231229
for {
232230
// TODO: Allow different shapeMap formats
233-
content <- getContents(path.toFile.getName())
231+
content <- getContents(path)
234232
} yield content.toString
235233
} else "".pure[IO]
236234
}
237235

238236

239237
// TODO: Move to a common tools package
240-
private def getContents(fileName: String): IO[CharSequence] = {
241-
val path = Paths.get(fileName)
242-
// implicit val cs = IO.contextShift(ExecutionContext.global)
238+
private def getContents(path: Path): IO[CharSequence] = {
243239
val decoder: Pipe[IO,Byte,String] = fs2.text.utf8Decode
244240
Stream.resource(Blocker[IO]).flatMap(blocker =>
245241
fs2.io.file.readAll[IO](path, blocker,4096).through(decoder)
@@ -266,8 +262,11 @@ object Main extends IOApp with LazyLogging {
266262
} else {
267263
IO(RDFAsJenaModel.fromURI(opts.dataurl(), opts.dataFormat(), relativeBase))
268264
}
269-
newRdf = if (opts.inference.isDefined) rdf.evalMap(rdf => rdf.applyInference(opts.inference()))
270-
else rdf
265+
newRdf = if (opts.inference.isDefined) for {
266+
inference <- Resource.liftF(fromEither(InferenceEngine.fromString(opts.inference())))
267+
r <- rdf.evalMap(rdf => rdf.applyInference(inference))
268+
} yield r
269+
else rdf
271270
} yield newRdf
272271
} else if (opts.endpoint.isDefined) {
273272
IO(Resource.liftF(Endpoint.fromString(opts.endpoint())))

‎src/main/scala/es/weso/shaclex/MainOpts.scala‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package es.weso.shaclex
22
import org.rogach.scallop._
33
import es.weso.schema._
4+
import es.weso.rdf._
45
import es.weso.rdf.jena.RDFAsJenaModel
56
import es.weso.schema.ValidationTrigger
67
import es.weso.shapeMaps.ShapeMap
@@ -23,6 +24,7 @@ class MainOpts(arguments: List[String],
2324
private lazy val shapeMapFormats = ShapeMap.formats
2425
private lazy val defaultValidationReportFormat = "TURTLE"
2526
private lazy val validationReportFormats = RDFAsJenaModel.availableFormats.map(_.toUpperCase).distinct
27+
private lazy val inferenceValues: List[String] = InferenceEngine.availableInferenceEngineNames
2628

2729
banner("""| shaclex: SHACL/ShEx processor
2830
| Options:
@@ -261,11 +263,11 @@ class MainOpts(arguments: List[String],
261263
required = false,
262264
descr = "Label (IRI) of Constraint.scala in Schema")
263265

264-
val inference: ScallopOption[String] = opt[String](
265-
"inference",
266-
default = None,
266+
val inference: ScallopOption[String] = opt[String]("inference",
267+
default = Some("NONE"),
267268
required = false,
268-
descr = "Apply some inference before. Available values: RDFS")
269+
descr = s"Apply some inference before. Available values: ${inferenceValues.mkString(",")}"
270+
)
269271

270272
val shapeInfer: ScallopOption[Boolean] = toggle("shapeInfer",
271273
prefix="no-",

‎version.sbt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version in ThisBuild := "0.1.70"
1+
version in ThisBuild := "0.1.73"

0 commit comments

Comments
 (0)