Skip to content

Commit

Permalink
Started compound
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Sep 4, 2019
1 parent 4b312d4 commit d53cad2
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import es.weso.rdf.nodes.RDFNode
import es.weso.shex.{SemAct, ShapeExpr}
import es.weso.shex.validator.Table.CTable
import es.weso.utils.SeqUtils.filterOptions
import ConstraintRef._

case class CandidateLine(values: List[(Arc,ConstraintRef)]) {
def mkBag: Bag[ConstraintRef] = Bag.toBag(values.map(_._2))
Expand All @@ -24,7 +25,10 @@ object CandidateLine {

implicit lazy val showCandidateLine = new Show[CandidateLine] {
def show(cl: CandidateLine): String = {
s"CandidateLine: ${cl.values.map{ case (arc,cref) => (arc.show, cref.show)}.mkString(",")}"
def compare(pair1:(Arc,ConstraintRef), pair2:(Arc,ConstraintRef)): Boolean =
Ordering[ConstraintRef].compare(pair1._2, pair2._2) <= 0

s"CandidateLine: ${cl.values.sortWith(compare).map{ case (arc,cref) => (arc.show, cref.show)}.mkString(",")}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ case class ConstraintRef(n: Int) extends AnyVal {
}

object ConstraintRef {

implicit lazy val orderingConstraintRef = new Ordering[ConstraintRef] {

def compare(c1: ConstraintRef, c2: ConstraintRef): Int = {
Ordering[Int].compare(c1.n, c2.n)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import es.weso.shex._
import es.weso.shex.compact.Parser.TripleExprMap
import cats.implicits._


/* Candidates table */
object Table extends LazyLogging {

Expand Down Expand Up @@ -189,8 +188,9 @@ object Table extends LazyLogging {
val (cref,expr) = current
s"${cref.show}->${expr.show}" :: s
}
cs.foldLeft(List[String]())(combine).mkString(",")
cs.foldLeft(List[String]())(combine).mkString("\n")
}
val paths = table.paths.toList
s"""Constraints:\n${showConstraints(table.constraints)}\nPaths: ${table.paths.toString}\n---endTable\n""".stripMargin
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,14 @@ case class Validator(schema: Schema,
c.crefs.nonEmpty
}

private[validator] def showCandidateLines(cs: List[CandidateLine]): String = {
cs.length match {
case 0 => "No candidate lines"
case 1 => s"One candidate line\n${cs.head.show}"
case _ => cs.map(_.show).mkString("\n")
}
}

private[validator] def checkCandidates(
attempt: Attempt,
bagChecker: BagChecker_,
Expand All @@ -722,9 +730,10 @@ case class Validator(schema: Schema,
StringError(
s"""|None of the candidates matched. Attempt: ${attempt.show}
|Bag: ${bagChecker.show}
|Candidate lines:${as.map(_.show).mkString(",")}
|Candidate lines:${showCandidateLines(as)}
|""".stripMargin
)})

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import org.apache.jena.query._
import es.weso.rdf.nodes._
import es.weso.rdf.nodes.RDFNode
import es.weso.rdf.triples.RDFTriple

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.util.{Either, Left, Right, Try}
import org.apache.jena.rdf.model.Property
import org.apache.jena.rdf.model.Statement
Expand All @@ -24,7 +23,6 @@ import com.typesafe.scalalogging.LazyLogging
import es.weso.rdf.jena.JenaMapper._
import es.weso.utils.EitherUtils

// TODO: Refactor to change String type by IRI
case class Endpoint(endpointIRI: IRI)
extends RDFReader
with RDFReasoner
Expand Down Expand Up @@ -190,7 +188,7 @@ case class Endpoint(endpointIRI: IRI)
jena2rdfnode(st.getObject))
}

def property2iri(p: Property): IRI = {
private def property2iri(p: Property): IRI = {
IRI(p.getURI)
}

Expand Down Expand Up @@ -245,7 +243,7 @@ case class Endpoint(endpointIRI: IRI)
val ls: List[Map[String, RDFNode]] = result.asScala.toList.map(qs => {
val qsm = new QuerySolutionMap()
qsm.addAll(qs)
qsm.asMap.asScala.mapValues(node => jenaNode2RDFNodeUnsafe(node)).toMap
qsm.asMap.asScala.view.mapValues(node => jenaNode2RDFNodeUnsafe(node)).toMap
})
ls
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package es.weso.rdf.jena
import java.io.ByteArrayOutputStream
import cats.implicits._
import com.typesafe.scalalogging.LazyLogging
import es.weso.rdf._
import es.weso.rdf.jena.JenaMapper._
import es.weso.rdf.jena.SPARQLQueries._
import es.weso.rdf.nodes.{RDFNode, _}
import es.weso.rdf.path.SHACLPath
import es.weso.rdf.triples.RDFTriple
import es.weso.utils.EitherUtils
import io.circe.Json
import io.circe.parser.parse
import org.apache.jena.query._
import org.apache.jena.rdf.model.{Model, Property, Statement, RDFNode => JenaRDFNode}
import org.slf4j._
import scala.jdk.CollectionConverters._
import scala.util.{Either, Left, Right, Try}
import cats._
import cats.implicits._

case class Compound(endpoints: List[Endpoint],
rdfModels: List[RDFAsJenaModel])
extends RDFReader
with RDFReasoner
with LazyLogging {

type Rdf = Compound

val id = s"Compound"

def availableParseFormats: List[String] = List()
def availableSerializeFormats: List[String] = List()

override def getPrefixMap: PrefixMap = {
// TODO: Can we get more info about prefix maps from an endpoint?
PrefixMap(Map())
}

val log = LoggerFactory.getLogger("Endpoint")

override def fromString(cs: CharSequence,
format: String,
base: Option[IRI]): Either[String, Compound] = {
throw new Exception("Cannot parse into a compound")
}

override def serialize(format: String, base: Option[IRI]): Either[String,String] = {
Left(s"Endpoint cannot be serialized to $format")
}

override def iris(): Either[String,Set[IRI]] = ???
override def subjects(): Either[String,Set[RDFNode]] = ???
override def predicates(): Either[String, Set[IRI]] = ???
override def iriObjects(): Either[String,Set[IRI]] = ???
override def getSHACLInstances(c: RDFNode): Either[String,Seq[RDFNode]] = ???

override def hasSHACLClass(n: RDFNode, c: RDFNode): Either[String, Boolean] = ???

override def nodesWithPath(path: SHACLPath): Either[String, Set[(RDFNode, RDFNode)]] = ???

override def subjectsWithPath(path: SHACLPath, obj: RDFNode): Either[String, Set[RDFNode]] = ???

override def objectsWithPath(subj: RDFNode, path: SHACLPath): Either[String,Set[RDFNode]] = ???

override def checkDatatype(node: RDFNode, datatype: IRI): Either[String,Boolean] =
JenaMapper.wellTypedDatatype(node, datatype)

override def rdfTriples(): Either[String,Set[RDFTriple]] = ???

def triplesWithSubject(node: RDFNode): Either[String,Set[RDFTriple]] = ???

def triplesWithPredicate(p: IRI): Either[String,Set[RDFTriple]] = {
???
}

def triplesWithObject(node: RDFNode): Either[String,Set[RDFTriple]] = {
val xs = for {
ts1 <- endpoints.map(_.triplesWithObject(node))
ts2 <- rdfModels.map(_.triplesWithObject(node))
} yield combine(ts1,ts2)
flat(xs)
}

private def flat[A](ls: List[Either[String,Set[A]]]): Either[String,Set[A]] = {
ls.sequence.map(_.flatten).map(_.toSet)
}

private def combine[A](es1: Either[String,Set[A]], es2: Either[String,Set[A]]): Either[String,Set[A]]=
for {
s1 <- es1
s2 <- es2
} yield s1 ++ s2

def triplesWithPredicateObject(p: IRI, o: RDFNode): Either[String, Set[RDFTriple]] = {
val xs = for {
ts1 <- endpoints.map(_.triplesWithPredicateObject(p, o))
ts2 <- rdfModels.map(_.triplesWithPredicateObject(p, o))
} yield for {
s1 <- ts1
s2 <- ts2
} yield s1 ++ s2
xs.toList.sequence.map(_.flatten).map(_.toSet)
}

override def applyInference(inference: String): Either[String, Rdf] = {
inference.toUpperCase match {
case "NONE" => Right(this)
case other => Left(s"Unsupported inference $other for compound model")
}
}

override def availableInferenceEngines: List[String] = List("NONE")

override def querySelect(queryStr: String): Either[String, List[Map[String,RDFNode]]] = ???

override def queryAsJson(queryStr: String): Either[String, Json] = ???

override def getNumberOfStatements(): Either[String,Int] = ???

override def isIsomorphicWith(other: RDFReader): Either[String,Boolean] =
Left(s"Unimplemented isIsomorphicWith between endpoints")

override def asRDFBuilder: Either[String,RDFBuilder] =
Left(s"Unimplemented isIsomorphicWith between endpoints")

override def rdfReaderName: String = s"Compound"

override def sourceIRI: Option[IRI] = None
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package es.weso.rdf.jena

import es.weso.rdf.nodes._
import es.weso.rdf.nodes.RDFNode
import es.weso.rdf.triples.RDFTriple

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import scala.util.Try
import es.weso.rdf._
import org.apache.jena.rdf.model.{Model, Property, Resource, Statement, RDFNode => JenaRDFNode}
Expand Down Expand Up @@ -52,9 +50,6 @@ case class RDFAsJenaModel(model: Model,
val m = ModelFactory.createDefaultModel
val str_reader = new StringReader(cs.toString)
val baseURI = base.getOrElse(IRI(""))
// println(s"baseURI: $baseURI")
// The following 4 statements are equivalent to :
// RDFDataMgr.read(m, str_reader, baseURI, shortnameToLang(format))
val g: Graph = m.getGraph
val dest : StreamRDF = StreamRDFLib.graph(g)
val ctx : Context = null
Expand All @@ -76,10 +71,6 @@ case class RDFAsJenaModel(model: Model,
}
}

/* private def getRDFLang(formatName: String): Either[String,Lang] = {
} */


override def serialize(formatName: String, base: Option[IRI]): Either[String, String] = for {
format <- getRDFFormat(formatName)
str <- Try {
Expand Down Expand Up @@ -298,7 +289,7 @@ case class RDFAsJenaModel(model: Model,
val ls: List[Map[String, RDFNode]] = result.asScala.toList.map(qs => {
val qsm = new QuerySolutionMap()
qsm.addAll(qs)
qsm.asMap.asScala.mapValues(node => jenaNode2RDFNodeUnsafe(node)).toMap
qsm.asMap.asScala.view.mapValues(node => jenaNode2RDFNodeUnsafe(node)).toMap
})
ls
}
Expand Down

0 comments on commit d53cad2

Please sign in to comment.